178 lines
4.6 KiB
Go
178 lines
4.6 KiB
Go
package encounter
|
|
|
|
import (
|
|
"os"
|
|
"time"
|
|
|
|
"service/internal/interfaces/satusehat"
|
|
)
|
|
|
|
func MapRequestToFHIR(req EncounterRequest) satusehat.FHIRPayload {
|
|
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": classDisplay,
|
|
})
|
|
}
|
|
|
|
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 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": ind,
|
|
})
|
|
}
|
|
payload.Set("participant", participants)
|
|
}
|
|
|
|
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{}{}
|
|
if req.PeriodStart != nil && !req.PeriodStart.IsZero() {
|
|
period["start"] = req.PeriodStart.Format(time.RFC3339)
|
|
}
|
|
if req.PeriodEnd != nil && !req.PeriodEnd.IsZero() {
|
|
period["end"] = req.PeriodEnd.Format(time.RFC3339)
|
|
}
|
|
if len(period) > 0 {
|
|
payload.Set("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
|
|
}
|