Perbaikan Create Patient

This commit is contained in:
2026-01-09 10:34:17 +07:00
parent 1b4ef62f5b
commit 86850af48c

View File

@@ -362,6 +362,12 @@ func (h *PatientHandler) CreatePatient(c *gin.Context) {
return 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") dbConn, err := h.db.GetSQLXDB("db_antrean")
if err != nil { if err != nil {
h.logAndRespondError(c, "Database connection failed", err, http.StatusInternalServerError) 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) ctx, cancel := context.WithTimeout(c.Request.Context(), 60*time.Second)
defer cancel() 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) createdPatient, err := h.executeCreateTransaction(ctx, dbConn, req)
if err != nil { if err != nil {
h.logAndRespondError(c, "Failed to create patient", err, http.StatusInternalServerError) h.logAndRespondError(c, "Failed to create patient", err, http.StatusInternalServerError)
@@ -379,7 +444,7 @@ func (h *PatientHandler) CreatePatient(c *gin.Context) {
h.invalidateRelatedCache() h.invalidateRelatedCache()
c.JSON(http.StatusCreated, gin.H{ c.JSON(http.StatusCreated, gin.H{
"message": "Patient berhasil dibuat / relations ditambahkan", "message": "Patient berhasil dibuat",
"data": createdPatient, "data": createdPatient,
}) })
} }