penambahan All case Satu sehat
This commit is contained in:
@@ -1,141 +1,177 @@
|
||||
package encounter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"service/internal/interfaces/satusehat"
|
||||
)
|
||||
|
||||
// MapRequestToFHIR mengubah EncounterRequest (DTO internal) menjadi objek Payload FHIR.
|
||||
// Penggunaan FHIRPayload (.Set dan .Append) akan memudahkan penulisan dan maintenance JSON yang kompleks.
|
||||
func MapRequestToFHIR(req EncounterRequest) satusehat.FHIRPayload {
|
||||
payload := satusehat.NewFHIRPayload("Encounter").
|
||||
Set("status", req.Status).
|
||||
Set("class", map[string]interface{}{
|
||||
orgID := os.Getenv("SATUSEHAT_ORG_ID")
|
||||
if orgID == "" {
|
||||
orgID = req.OrganizationID
|
||||
}
|
||||
|
||||
payload := satusehat.NewFHIRPayload("Encounter")
|
||||
|
||||
if orgID != "" && req.EncounterID != "" {
|
||||
payload.Set("identifier", []map[string]interface{}{
|
||||
{
|
||||
"system": "http://sys-ids.kemkes.go.id/encounter/" + orgID,
|
||||
"value": req.EncounterID,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
if req.Status != "" {
|
||||
payload.Set("status", req.Status)
|
||||
}
|
||||
|
||||
if req.Class != "" {
|
||||
classDisplay := "ambulatory"
|
||||
switch req.Class {
|
||||
case "EMER":
|
||||
classDisplay = "emergency"
|
||||
case "IMP":
|
||||
classDisplay = "inpatient encounter"
|
||||
}
|
||||
|
||||
payload.Set("class", map[string]interface{}{
|
||||
"system": "http://terminology.hl7.org/CodeSystem/v3-ActCode",
|
||||
"code": req.Class,
|
||||
"display": getClassDisplay(req.Class),
|
||||
}).
|
||||
Set("subject", map[string]interface{}{
|
||||
"reference": "Patient/" + req.PatientID,
|
||||
"display": req.PatientName,
|
||||
"display": classDisplay,
|
||||
})
|
||||
}
|
||||
|
||||
if req.OrganizationID != "" {
|
||||
payload.Set("serviceProvider", map[string]interface{}{
|
||||
"reference": "Organization/" + req.OrganizationID,
|
||||
})
|
||||
|
||||
if req.EncounterID != "" {
|
||||
payload.Append("identifier", map[string]interface{}{
|
||||
"system": "http://sys-ids.kemkes.go.id/encounter/" + req.OrganizationID,
|
||||
"value": req.EncounterID,
|
||||
})
|
||||
if req.Subject != nil {
|
||||
subj := map[string]interface{}{"reference": req.Subject.Reference}
|
||||
if req.Subject.Display != "" {
|
||||
subj["display"] = req.Subject.Display
|
||||
}
|
||||
payload.Set("subject", subj)
|
||||
}
|
||||
|
||||
if req.EpisodeOfCareID != "" {
|
||||
payload.Append("episodeOfCare", map[string]interface{}{
|
||||
"reference": "EpisodeOfCare/" + req.EpisodeOfCareID,
|
||||
})
|
||||
}
|
||||
|
||||
if req.PractitionerID != "" {
|
||||
payload.Append("participant", map[string]interface{}{
|
||||
"type": []map[string]interface{}{
|
||||
{
|
||||
"coding": []map[string]interface{}{
|
||||
{
|
||||
"system": "http://terminology.hl7.org/CodeSystem/v3-ParticipationType",
|
||||
"code": "ATND",
|
||||
"display": "attender",
|
||||
if len(req.Participant) > 0 {
|
||||
var participants []map[string]interface{}
|
||||
for _, p := range req.Participant {
|
||||
ind := map[string]interface{}{"reference": p.Reference}
|
||||
if p.Display != "" {
|
||||
ind["display"] = p.Display
|
||||
}
|
||||
participants = append(participants, map[string]interface{}{
|
||||
"type": []map[string]interface{}{
|
||||
{
|
||||
"coding": []map[string]interface{}{
|
||||
{"system": "http://terminology.hl7.org/CodeSystem/v3-ParticipationType", "code": "ATND", "display": "attender"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"individual": map[string]interface{}{
|
||||
"reference": "Practitioner/" + req.PractitionerID,
|
||||
"display": req.PractitionerName,
|
||||
},
|
||||
})
|
||||
"individual": ind,
|
||||
})
|
||||
}
|
||||
payload.Set("participant", participants)
|
||||
}
|
||||
|
||||
if req.LocationID != "" {
|
||||
payload.Append("location", map[string]interface{}{
|
||||
"location": map[string]interface{}{
|
||||
"reference": "Location/" + req.LocationID,
|
||||
"display": req.LocationName,
|
||||
},
|
||||
})
|
||||
if len(req.Location) > 0 {
|
||||
var locations []map[string]interface{}
|
||||
for _, l := range req.Location {
|
||||
if l.Location != nil {
|
||||
locRef := map[string]interface{}{"reference": l.Location.Reference}
|
||||
if l.Location.Display != "" {
|
||||
locRef["display"] = l.Location.Display
|
||||
}
|
||||
locEntry := map[string]interface{}{"location": locRef}
|
||||
period := map[string]interface{}{}
|
||||
if l.PeriodStart != nil && !l.PeriodStart.IsZero() {
|
||||
period["start"] = l.PeriodStart.Format(time.RFC3339)
|
||||
}
|
||||
if l.PeriodEnd != nil && !l.PeriodEnd.IsZero() {
|
||||
period["end"] = l.PeriodEnd.Format(time.RFC3339)
|
||||
}
|
||||
if len(period) > 0 {
|
||||
locEntry["period"] = period
|
||||
}
|
||||
locations = append(locations, locEntry)
|
||||
}
|
||||
}
|
||||
payload.Set("location", locations)
|
||||
}
|
||||
|
||||
period := map[string]interface{}{
|
||||
"start": req.PeriodStart.Format(time.RFC3339),
|
||||
period := map[string]interface{}{}
|
||||
if req.PeriodStart != nil && !req.PeriodStart.IsZero() {
|
||||
period["start"] = req.PeriodStart.Format(time.RFC3339)
|
||||
}
|
||||
if req.PeriodEnd != nil {
|
||||
if req.PeriodEnd != nil && !req.PeriodEnd.IsZero() {
|
||||
period["end"] = req.PeriodEnd.Format(time.RFC3339)
|
||||
}
|
||||
payload.Set("period", period)
|
||||
if len(period) > 0 {
|
||||
payload.Set("period", period)
|
||||
}
|
||||
|
||||
payload.Append("statusHistory", map[string]interface{}{
|
||||
"status": req.Status,
|
||||
"period": period,
|
||||
})
|
||||
if len(req.Diagnosis) > 0 {
|
||||
var diagnoses []map[string]interface{}
|
||||
for _, d := range req.Diagnosis {
|
||||
diag := map[string]interface{}{}
|
||||
if d.Condition != nil {
|
||||
cond := map[string]interface{}{"reference": d.Condition.Reference}
|
||||
if d.Condition.Display != "" {
|
||||
cond["display"] = d.Condition.Display
|
||||
}
|
||||
diag["condition"] = cond
|
||||
}
|
||||
if d.Use != nil {
|
||||
sys := d.Use.System
|
||||
if sys == "" {
|
||||
sys = "http://terminology.hl7.org/CodeSystem/diagnosis-role"
|
||||
}
|
||||
useCoding := map[string]interface{}{"system": sys, "code": d.Use.Code}
|
||||
if d.Use.Display != "" {
|
||||
useCoding["display"] = d.Use.Display
|
||||
}
|
||||
diag["use"] = map[string]interface{}{"coding": []map[string]interface{}{useCoding}}
|
||||
}
|
||||
if d.Rank > 0 {
|
||||
diag["rank"] = d.Rank
|
||||
}
|
||||
diagnoses = append(diagnoses, diag)
|
||||
}
|
||||
payload.Set("diagnosis", diagnoses)
|
||||
}
|
||||
|
||||
if len(req.StatusHistory) > 0 {
|
||||
var statusHistories []map[string]interface{}
|
||||
for _, sh := range req.StatusHistory {
|
||||
shEntry := map[string]interface{}{"status": sh.Status}
|
||||
shp := map[string]interface{}{}
|
||||
if sh.PeriodStart != nil && !sh.PeriodStart.IsZero() {
|
||||
shp["start"] = sh.PeriodStart.Format(time.RFC3339)
|
||||
}
|
||||
if sh.PeriodEnd != nil && !sh.PeriodEnd.IsZero() {
|
||||
shp["end"] = sh.PeriodEnd.Format(time.RFC3339)
|
||||
}
|
||||
if len(shp) > 0 {
|
||||
shEntry["period"] = shp
|
||||
}
|
||||
statusHistories = append(statusHistories, shEntry)
|
||||
}
|
||||
payload.Set("statusHistory", statusHistories)
|
||||
} else if req.Status != "" && req.PeriodStart != nil && !req.PeriodStart.IsZero() {
|
||||
payload.Set("statusHistory", []map[string]interface{}{
|
||||
{
|
||||
"status": req.Status,
|
||||
"period": map[string]interface{}{
|
||||
"start": req.PeriodStart.Format(time.RFC3339),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
if orgID != "" {
|
||||
payload.Set("serviceProvider", map[string]interface{}{
|
||||
"reference": "Organization/" + orgID,
|
||||
})
|
||||
}
|
||||
|
||||
return payload
|
||||
}
|
||||
|
||||
// MapPendaftaranToRequest mengubah data database t_pendaftaran menjadi EncounterRequest
|
||||
func MapPendaftaranToRequest(dbData PendaftaranDB, organizationID string, patientIHS string, practitionerIHS string) EncounterRequest {
|
||||
// Penentuan status encounter
|
||||
status := "arrived"
|
||||
if dbData.Batal.Valid && dbData.Batal.String == "Y" {
|
||||
status = "cancelled"
|
||||
} else if dbData.KeluarPoly.Valid {
|
||||
status = "finished"
|
||||
} else if dbData.MasukPoly.Valid {
|
||||
status = "in-progress"
|
||||
}
|
||||
|
||||
// Waktu mulai
|
||||
periodStart := time.Now()
|
||||
if dbData.MasukPoly.Valid {
|
||||
periodStart = dbData.MasukPoly.Time
|
||||
} else if dbData.JamReg.Valid {
|
||||
periodStart = dbData.JamReg.Time
|
||||
}
|
||||
|
||||
var periodEnd *time.Time
|
||||
if dbData.KeluarPoly.Valid {
|
||||
periodEnd = &dbData.KeluarPoly.Time
|
||||
}
|
||||
|
||||
return EncounterRequest{
|
||||
EncounterID: fmt.Sprintf("%d", dbData.IdxDaftar),
|
||||
OrganizationID: organizationID,
|
||||
PatientID: patientIHS, // Hasil pemetaan/pencarian IHS
|
||||
PatientName: "Pasien " + dbData.NoMR.String, // Fallback Name
|
||||
PractitionerID: practitionerIHS, // Hasil pemetaan/pencarian IHS
|
||||
PractitionerName: dbData.DokterNameHFIS.String,
|
||||
LocationID: fmt.Sprintf("%d", dbData.KdPoly.Int64), // Catatan: Anda harus me-mapping ini ke IHS Location ID
|
||||
LocationName: dbData.PoliNameHFIS.String,
|
||||
Status: status,
|
||||
Class: "AMB", // Default: Rawat Jalan (Ambulatory)
|
||||
PeriodStart: periodStart,
|
||||
PeriodEnd: periodEnd,
|
||||
}
|
||||
}
|
||||
|
||||
func getClassDisplay(code string) string {
|
||||
switch code {
|
||||
case "AMB":
|
||||
return "ambulatory"
|
||||
case "IMP":
|
||||
return "inpatient encounter"
|
||||
case "EMER":
|
||||
return "emergency"
|
||||
default:
|
||||
return "ambulatory"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user