146 lines
3.5 KiB
Go
146 lines
3.5 KiB
Go
package composition
|
|
|
|
import (
|
|
"os"
|
|
"time"
|
|
|
|
"service/internal/interfaces/satusehat"
|
|
)
|
|
|
|
func MapRequestToFHIR(req CompositionRequest) satusehat.FHIRPayload {
|
|
payload := satusehat.NewFHIRPayload("Composition")
|
|
|
|
orgID := os.Getenv("SATUSEHAT_ORG_ID")
|
|
if orgID == "" {
|
|
orgID = req.OrganizationID
|
|
}
|
|
|
|
if orgID != "" && req.CompositionID != "" {
|
|
payload.Set("identifier", map[string]interface{}{
|
|
"system": "http://sys-ids.kemkes.go.id/composition/" + orgID,
|
|
"value": req.CompositionID,
|
|
})
|
|
}
|
|
|
|
status := req.Status
|
|
if req.Status != "" {
|
|
status = req.Status
|
|
} else {
|
|
status = "final"
|
|
}
|
|
payload.Set("status", status)
|
|
|
|
if req.Type != nil {
|
|
sys := req.Type.System
|
|
if sys == "" {
|
|
sys = "http://loinc.org"
|
|
}
|
|
coding := map[string]interface{}{"system": sys, "code": req.Type.Code}
|
|
if req.Type.Display != "" {
|
|
coding["display"] = req.Type.Display
|
|
}
|
|
typeConcept := map[string]interface{}{"coding": []map[string]interface{}{coding}}
|
|
if req.Type.Text != "" {
|
|
typeConcept["text"] = req.Type.Text
|
|
}
|
|
payload.Set("type", typeConcept)
|
|
}
|
|
|
|
if len(req.Category) > 0 {
|
|
var categories []map[string]interface{}
|
|
for _, c := range req.Category {
|
|
if c != nil {
|
|
sys := c.System
|
|
if sys == "" {
|
|
sys = "http://loinc.org"
|
|
}
|
|
coding := map[string]interface{}{"system": sys, "code": c.Code}
|
|
if c.Display != "" {
|
|
coding["display"] = c.Display
|
|
}
|
|
categories = append(categories, map[string]interface{}{"coding": []map[string]interface{}{coding}})
|
|
}
|
|
}
|
|
payload.Set("category", categories)
|
|
}
|
|
|
|
if req.PatientID != "" {
|
|
subj := map[string]interface{}{"reference": "Patient/" + req.PatientID}
|
|
if req.PatientDisplay != "" {
|
|
subj["display"] = req.PatientDisplay
|
|
}
|
|
payload.Set("subject", subj)
|
|
}
|
|
if req.EncounterID != "" {
|
|
enc := map[string]interface{}{"reference": "Encounter/" + req.EncounterID}
|
|
if req.EncounterDisplay != "" {
|
|
enc["display"] = req.EncounterDisplay
|
|
}
|
|
payload.Set("encounter", enc)
|
|
}
|
|
if req.Date != nil && !req.Date.IsZero() {
|
|
payload.Set("date", req.Date.Format(time.RFC3339))
|
|
}
|
|
if req.Title != "" {
|
|
payload.Set("title", req.Title)
|
|
}
|
|
|
|
if len(req.Author) > 0 {
|
|
var authors []map[string]interface{}
|
|
for _, a := range req.Author {
|
|
author := map[string]interface{}{"reference": a.Reference}
|
|
if a.Display != "" {
|
|
author["display"] = a.Display
|
|
}
|
|
authors = append(authors, author)
|
|
}
|
|
payload.Set("author", authors)
|
|
}
|
|
|
|
if len(req.Sections) > 0 {
|
|
var sections []map[string]interface{}
|
|
for _, s := range req.Sections {
|
|
sec := map[string]interface{}{}
|
|
if s.Title != "" {
|
|
sec["title"] = s.Title
|
|
}
|
|
if s.Code != nil {
|
|
sys := s.Code.System
|
|
if sys == "" {
|
|
sys = "http://loinc.org"
|
|
}
|
|
coding := map[string]interface{}{"system": sys, "code": s.Code.Code}
|
|
if s.Code.Display != "" {
|
|
coding["display"] = s.Code.Display
|
|
}
|
|
sec["code"] = map[string]interface{}{"coding": []map[string]interface{}{coding}}
|
|
}
|
|
if s.Text != nil {
|
|
status := s.Text.Status
|
|
if status == "" {
|
|
status = "generated"
|
|
}
|
|
sec["text"] = map[string]interface{}{
|
|
"status": status,
|
|
"div": s.Text.Div,
|
|
}
|
|
}
|
|
if len(s.Entries) > 0 {
|
|
var entries []map[string]interface{}
|
|
for _, e := range s.Entries {
|
|
entry := map[string]interface{}{"reference": e.Reference}
|
|
if e.Display != "" {
|
|
entry["display"] = e.Display
|
|
}
|
|
entries = append(entries, entry)
|
|
}
|
|
sec["entry"] = entries
|
|
}
|
|
sections = append(sections, sec)
|
|
}
|
|
payload.Set("section", sections)
|
|
}
|
|
|
|
return payload
|
|
}
|