From 86850af48c888f07ef752b788742fe97c29a1552 Mon Sep 17 00:00:00 2001 From: Annisa Rachmadiyanti Date: Fri, 9 Jan 2026 10:34:17 +0700 Subject: [PATCH] Perbaikan Create Patient --- internal/handlers/patient/ms_patient.go | 67 ++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/internal/handlers/patient/ms_patient.go b/internal/handlers/patient/ms_patient.go index a11ef6d..d1b7f9f 100644 --- a/internal/handlers/patient/ms_patient.go +++ b/internal/handlers/patient/ms_patient.go @@ -362,6 +362,12 @@ func (h *PatientHandler) CreatePatient(c *gin.Context) { return } + if req.MedicalRecordNumber == nil || strings.TrimSpace(*req.MedicalRecordNumber) == "" { + h.respondError(c, "medical_record_number required", fmt.Errorf("missing medical_record_number"), http.StatusBadRequest) + return + } + medRec := strings.TrimSpace(*req.MedicalRecordNumber) + dbConn, err := h.db.GetSQLXDB("db_antrean") if err != nil { h.logAndRespondError(c, "Database connection failed", err, http.StatusInternalServerError) @@ -371,6 +377,65 @@ func (h *PatientHandler) CreatePatient(c *gin.Context) { ctx, cancel := context.WithTimeout(c.Request.Context(), 60*time.Second) defer cancel() + // Check if patient with same medical_record_number already exists + existing, err := h.getPatientByMedicalRecordNumber(medRec) + if err == nil && existing.ID != 0 { + // Patient exists -> attach relations only inside a transaction + tx, err := dbConn.BeginTxx(ctx, nil) + if err != nil { + h.logAndRespondError(c, "Failed to begin transaction for existing patient relations", err, http.StatusInternalServerError) + return + } + + if req.Visits != nil { + if _, err := h.insertVisits(ctx, tx, req.Visits, existing.ID); err != nil { + _ = tx.Rollback() + h.logAndRespondError(c, "Failed to insert visits for existing patient", err, http.StatusInternalServerError) + return + } + } + if req.Attachments != nil { + if _, err := h.insertAttachments(ctx, tx, req.Attachments, existing.ID); err != nil { + _ = tx.Rollback() + h.logAndRespondError(c, "Failed to insert attachments for existing patient", err, http.StatusInternalServerError) + return + } + } + if req.Payments != nil { + if _, err := h.insertPaymentTypes(ctx, tx, req.Payments, existing.ID); err != nil { + _ = tx.Rollback() + h.logAndRespondError(c, "Failed to insert payment types for existing patient", err, http.StatusInternalServerError) + return + } + } + + if err := tx.Commit(); err != nil { + h.logAndRespondError(c, "Failed to commit transaction for existing patient relations", err, http.StatusInternalServerError) + return + } + + // Fetch fresh patient including newly inserted relations + updated, err := h.getPatientByMedicalRecordNumber(medRec) + if err != nil { + h.logAndRespondError(c, "Failed to fetch updated patient after inserting relations", err, http.StatusInternalServerError) + return + } + + h.invalidateRelatedCache() + c.JSON(http.StatusCreated, gin.H{ + "message": "Relations ditambahkan ke pasien yang ada", + "data": updated, + }) + return + } + + // If error is other than not found, return error + if err != nil && err != sql.ErrNoRows { + h.logAndRespondError(c, "Failed to check existing patient", err, http.StatusInternalServerError) + return + } + + // Patient not found -> create new patient with relations createdPatient, err := h.executeCreateTransaction(ctx, dbConn, req) if err != nil { h.logAndRespondError(c, "Failed to create patient", err, http.StatusInternalServerError) @@ -379,7 +444,7 @@ func (h *PatientHandler) CreatePatient(c *gin.Context) { h.invalidateRelatedCache() c.JSON(http.StatusCreated, gin.H{ - "message": "Patient berhasil dibuat / relations ditambahkan", + "message": "Patient berhasil dibuat", "data": createdPatient, }) }