penambahan All case Satu sehat
This commit is contained in:
No files matched your search
@@ -1,20 +1,48 @@
|
||||
package observation
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"time"
|
||||
|
||||
"service/internal/satusehat/common"
|
||||
)
|
||||
|
||||
type ObservationRequest struct {
|
||||
PatientID string `json:"patient_id" binding:"required"`
|
||||
PatientName string `json:"patient_name" binding:"required"`
|
||||
EncounterID string `json:"encounter_id" binding:"required"`
|
||||
Status string `json:"status" binding:"required,oneof=registered preliminary final amended"` // registered, preliminary, final, amended
|
||||
CategoryCode string `json:"category_code" binding:"required"` // vital-signs, laboratory
|
||||
CategoryDisplay string `json:"category_display" binding:"required"`
|
||||
Code string `json:"code" binding:"required"` // LOINC code (e.g., 8867-4 for Heart rate)
|
||||
Display string `json:"display" binding:"required"` // Heart rate
|
||||
Value float64 `json:"value" binding:"required"`
|
||||
Unit string `json:"unit" binding:"required"`
|
||||
UnitCode string `json:"unit_code" binding:"required"` // UCUM code (e.g., /min)
|
||||
EffectiveDateTime time.Time `json:"effective_date_time" binding:"required"`
|
||||
OrganizationID string `json:"organization_id,omitempty"`
|
||||
ObservationID string `json:"observation_id,omitempty"`
|
||||
Status string `json:"status" binding:"required,oneof=registered preliminary final amended corrected cancelled entered-in-error unknown"`
|
||||
Category []*common.CodeableConceptDTO `json:"category" binding:"required,min=1"`
|
||||
Code *common.CodeableConceptDTO `json:"code" binding:"required"`
|
||||
Subject *common.ReferenceDTO `json:"subject" binding:"required"` // Patient
|
||||
Encounter *common.ReferenceDTO `json:"encounter,omitempty"`
|
||||
EffectiveDateTime *time.Time `json:"effective_datetime,omitempty"`
|
||||
Issued *time.Time `json:"issued,omitempty"`
|
||||
Performer []common.ReferenceDTO `json:"performer,omitempty"`
|
||||
BasedOn []common.ReferenceDTO `json:"based_on,omitempty"`
|
||||
PartOf []common.ReferenceDTO `json:"part_of,omitempty"`
|
||||
DerivedFrom []common.ReferenceDTO `json:"derived_from,omitempty"`
|
||||
Specimen *common.ReferenceDTO `json:"specimen,omitempty"`
|
||||
BodySite *common.CodeableConceptDTO `json:"body_site,omitempty"`
|
||||
ValueQuantity *common.QuantityDTO `json:"value_quantity,omitempty"`
|
||||
ValueCodeableConcept *common.CodeableConceptDTO `json:"value_codeable_concept,omitempty"`
|
||||
ValueString string `json:"value_string,omitempty"`
|
||||
ValueBoolean *bool `json:"value_boolean,omitempty"`
|
||||
Interpretation []*common.CodeableConceptDTO `json:"interpretation,omitempty"`
|
||||
ReferenceRange []ReferenceRangeDTO `json:"reference_range,omitempty"`
|
||||
Components []ComponentDTO `json:"components,omitempty"`
|
||||
}
|
||||
|
||||
type ReferenceRangeDTO struct {
|
||||
Low *common.QuantityDTO `json:"low,omitempty"`
|
||||
High *common.QuantityDTO `json:"high,omitempty"`
|
||||
Text string `json:"text,omitempty"`
|
||||
}
|
||||
|
||||
type ComponentDTO struct {
|
||||
Code *common.CodeableConceptDTO `json:"code,omitempty"`
|
||||
ValueQuantity *common.QuantityDTO `json:"value_quantity,omitempty"`
|
||||
ValueCodeableConcept *common.CodeableConceptDTO `json:"value_codeable_concept,omitempty"`
|
||||
ValueString string `json:"value_string,omitempty"`
|
||||
ValueBoolean *bool `json:"value_boolean,omitempty"`
|
||||
}
|
||||
|
||||
type ObservationPatchRequest []map[string]interface{}
|
||||
@@ -1,48 +1,258 @@
|
||||
package observation
|
||||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"service/internal/interfaces/satusehat"
|
||||
)
|
||||
|
||||
func MapRequestToFHIR(req ObservationRequest) satusehat.FHIRPayload {
|
||||
payload := satusehat.NewFHIRPayload("Observation").
|
||||
Set("status", req.Status).
|
||||
Set("category", []map[string]interface{}{
|
||||
payload := satusehat.NewFHIRPayload("Observation")
|
||||
|
||||
orgID := os.Getenv("SATUSEHAT_ORG_ID")
|
||||
if orgID == "" {
|
||||
orgID = req.OrganizationID
|
||||
}
|
||||
|
||||
if req.ObservationID != "" {
|
||||
payload.Set("identifier", []map[string]interface{}{
|
||||
{
|
||||
"coding": []map[string]interface{}{
|
||||
{
|
||||
"system": "http://terminology.hl7.org/CodeSystem/observation-category",
|
||||
"code": req.CategoryCode,
|
||||
"display": req.CategoryDisplay,
|
||||
},
|
||||
},
|
||||
"system": "http://sys-ids.kemkes.go.id/observation/" + orgID,
|
||||
"value": req.ObservationID,
|
||||
},
|
||||
}).
|
||||
Set("code", map[string]interface{}{
|
||||
"coding": []map[string]interface{}{
|
||||
{
|
||||
"system": "http://loinc.org",
|
||||
"code": req.Code,
|
||||
"display": req.Display,
|
||||
},
|
||||
},
|
||||
}).
|
||||
Set("subject", map[string]interface{}{
|
||||
"reference": "Patient/" + req.PatientID,
|
||||
"display": req.PatientName,
|
||||
}).
|
||||
Set("encounter", map[string]interface{}{
|
||||
"reference": "Encounter/" + req.EncounterID,
|
||||
}).
|
||||
Set("effectiveDateTime", req.EffectiveDateTime.Format(time.RFC3339)).
|
||||
Set("valueQuantity", map[string]interface{}{
|
||||
"value": req.Value,
|
||||
"unit": req.Unit,
|
||||
"system": "http://unitsofmeasure.org",
|
||||
"code": req.UnitCode,
|
||||
})
|
||||
}
|
||||
|
||||
status := req.Status
|
||||
if status == "" {
|
||||
status = "final"
|
||||
}
|
||||
payload.Set("status", status)
|
||||
|
||||
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://terminology.hl7.org/CodeSystem/observation-category"
|
||||
}
|
||||
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.Code != nil {
|
||||
sys := req.Code.System
|
||||
if sys == "" {
|
||||
sys = "http://loinc.org"
|
||||
}
|
||||
coding := map[string]interface{}{"system": sys, "code": req.Code.Code}
|
||||
if req.Code.Display != "" {
|
||||
coding["display"] = req.Code.Display
|
||||
}
|
||||
codeConcept := map[string]interface{}{"coding": []map[string]interface{}{coding}}
|
||||
if req.Code.Text != "" {
|
||||
codeConcept["text"] = req.Code.Text
|
||||
}
|
||||
payload.Set("code", codeConcept)
|
||||
}
|
||||
|
||||
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.Encounter != nil {
|
||||
enc := map[string]interface{}{"reference": req.Encounter.Reference}
|
||||
if req.Encounter.Display != "" {
|
||||
enc["display"] = req.Encounter.Display
|
||||
}
|
||||
payload.Set("encounter", enc)
|
||||
}
|
||||
|
||||
if req.EffectiveDateTime != nil && !req.EffectiveDateTime.IsZero() {
|
||||
payload.Set("effectiveDateTime", req.EffectiveDateTime.Format(time.RFC3339))
|
||||
}
|
||||
if req.Issued != nil && !req.Issued.IsZero() {
|
||||
payload.Set("issued", req.Issued.Format(time.RFC3339))
|
||||
}
|
||||
|
||||
if len(req.Performer) > 0 {
|
||||
var performers []map[string]interface{}
|
||||
for _, p := range req.Performer {
|
||||
perf := map[string]interface{}{"reference": p.Reference}
|
||||
if p.Display != "" {
|
||||
perf["display"] = p.Display
|
||||
}
|
||||
performers = append(performers, perf)
|
||||
}
|
||||
payload.Set("performer", performers)
|
||||
}
|
||||
|
||||
if len(req.BasedOn) > 0 {
|
||||
var refs []map[string]interface{}
|
||||
for _, r := range req.BasedOn {
|
||||
refs = append(refs, map[string]interface{}{"reference": r.Reference})
|
||||
}
|
||||
payload.Set("basedOn", refs)
|
||||
}
|
||||
if len(req.PartOf) > 0 {
|
||||
var refs []map[string]interface{}
|
||||
for _, r := range req.PartOf {
|
||||
refs = append(refs, map[string]interface{}{"reference": r.Reference})
|
||||
}
|
||||
payload.Set("partOf", refs)
|
||||
}
|
||||
if len(req.DerivedFrom) > 0 {
|
||||
var refs []map[string]interface{}
|
||||
for _, r := range req.DerivedFrom {
|
||||
refs = append(refs, map[string]interface{}{"reference": r.Reference})
|
||||
}
|
||||
payload.Set("derivedFrom", refs)
|
||||
}
|
||||
|
||||
if req.Specimen != nil {
|
||||
spec := map[string]interface{}{"reference": req.Specimen.Reference}
|
||||
if req.Specimen.Display != "" {
|
||||
spec["display"] = req.Specimen.Display
|
||||
}
|
||||
payload.Set("specimen", spec)
|
||||
}
|
||||
|
||||
if req.BodySite != nil {
|
||||
sys := req.BodySite.System
|
||||
if sys == "" {
|
||||
sys = "http://snomed.info/sct"
|
||||
}
|
||||
coding := map[string]interface{}{"system": sys, "code": req.BodySite.Code}
|
||||
if req.BodySite.Display != "" {
|
||||
coding["display"] = req.BodySite.Display
|
||||
}
|
||||
payload.Set("bodySite", map[string]interface{}{"coding": []map[string]interface{}{coding}})
|
||||
}
|
||||
|
||||
if req.ValueQuantity != nil {
|
||||
sys := req.ValueQuantity.System
|
||||
if sys == "" {
|
||||
sys = "http://unitsofmeasure.org"
|
||||
}
|
||||
payload.Set("valueQuantity", map[string]interface{}{
|
||||
"value": req.ValueQuantity.Value,
|
||||
"unit": req.ValueQuantity.Unit,
|
||||
"system": sys,
|
||||
"code": req.ValueQuantity.Code,
|
||||
})
|
||||
} else if req.ValueCodeableConcept != nil {
|
||||
sys := req.ValueCodeableConcept.System
|
||||
if sys == "" {
|
||||
sys = "http://snomed.info/sct"
|
||||
}
|
||||
coding := map[string]interface{}{"system": sys, "code": req.ValueCodeableConcept.Code}
|
||||
if req.ValueCodeableConcept.Display != "" {
|
||||
coding["display"] = req.ValueCodeableConcept.Display
|
||||
}
|
||||
payload.Set("valueCodeableConcept", map[string]interface{}{"coding": []map[string]interface{}{coding}})
|
||||
} else if req.ValueString != "" {
|
||||
payload.Set("valueString", req.ValueString)
|
||||
} else if req.ValueBoolean != nil {
|
||||
payload.Set("valueBoolean", *req.ValueBoolean)
|
||||
}
|
||||
|
||||
if len(req.Interpretation) > 0 {
|
||||
var interps []map[string]interface{}
|
||||
for _, interp := range req.Interpretation {
|
||||
if interp != nil {
|
||||
sys := interp.System
|
||||
if sys == "" {
|
||||
sys = "http://terminology.hl7.org/CodeSystem/v3-ObservationInterpretation"
|
||||
}
|
||||
coding := map[string]interface{}{"system": sys, "code": interp.Code}
|
||||
if interp.Display != "" {
|
||||
coding["display"] = interp.Display
|
||||
}
|
||||
interps = append(interps, map[string]interface{}{"coding": []map[string]interface{}{coding}})
|
||||
}
|
||||
}
|
||||
payload.Set("interpretation", interps)
|
||||
}
|
||||
|
||||
if len(req.ReferenceRange) > 0 {
|
||||
var ranges []map[string]interface{}
|
||||
for _, r := range req.ReferenceRange {
|
||||
rng := map[string]interface{}{}
|
||||
if r.Low != nil {
|
||||
sys := r.Low.System
|
||||
if sys == "" {
|
||||
sys = "http://unitsofmeasure.org"
|
||||
}
|
||||
rng["low"] = map[string]interface{}{"value": r.Low.Value, "unit": r.Low.Unit, "system": sys, "code": r.Low.Code}
|
||||
}
|
||||
if r.High != nil {
|
||||
sys := r.High.System
|
||||
if sys == "" {
|
||||
sys = "http://unitsofmeasure.org"
|
||||
}
|
||||
rng["high"] = map[string]interface{}{"value": r.High.Value, "unit": r.High.Unit, "system": sys, "code": r.High.Code}
|
||||
}
|
||||
if r.Text != "" {
|
||||
rng["text"] = r.Text
|
||||
}
|
||||
ranges = append(ranges, rng)
|
||||
}
|
||||
payload.Set("referenceRange", ranges)
|
||||
}
|
||||
|
||||
if len(req.Components) > 0 {
|
||||
var components []map[string]interface{}
|
||||
for _, comp := range req.Components {
|
||||
c := make(map[string]interface{})
|
||||
if comp.Code != nil {
|
||||
sys := comp.Code.System
|
||||
if sys == "" {
|
||||
sys = "http://loinc.org"
|
||||
}
|
||||
coding := map[string]interface{}{"system": sys, "code": comp.Code.Code}
|
||||
if comp.Code.Display != "" {
|
||||
coding["display"] = comp.Code.Display
|
||||
}
|
||||
c["code"] = map[string]interface{}{"coding": []map[string]interface{}{coding}}
|
||||
}
|
||||
|
||||
if comp.ValueQuantity != nil {
|
||||
sysVQ := comp.ValueQuantity.System
|
||||
if sysVQ == "" {
|
||||
sysVQ = "http://unitsofmeasure.org"
|
||||
}
|
||||
c["valueQuantity"] = map[string]interface{}{"value": comp.ValueQuantity.Value, "unit": comp.ValueQuantity.Unit, "system": sysVQ, "code": comp.ValueQuantity.Code}
|
||||
} else if comp.ValueCodeableConcept != nil {
|
||||
sysVCC := comp.ValueCodeableConcept.System
|
||||
if sysVCC == "" {
|
||||
sysVCC = "http://snomed.info/sct"
|
||||
}
|
||||
coding := map[string]interface{}{"system": sysVCC, "code": comp.ValueCodeableConcept.Code}
|
||||
if comp.ValueCodeableConcept.Display != "" {
|
||||
coding["display"] = comp.ValueCodeableConcept.Display
|
||||
}
|
||||
c["valueCodeableConcept"] = map[string]interface{}{"coding": []map[string]interface{}{coding}}
|
||||
} else if comp.ValueString != "" {
|
||||
c["valueString"] = comp.ValueString
|
||||
} else if comp.ValueBoolean != nil {
|
||||
c["valueBoolean"] = *comp.ValueBoolean
|
||||
}
|
||||
components = append(components, c)
|
||||
}
|
||||
payload.Set("component", components)
|
||||
}
|
||||
|
||||
return payload
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"service/internal/interfaces/satusehat"
|
||||
"service/pkg/errors"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
@@ -24,15 +25,23 @@ func NewRepository(client satusehat.SatuSehatClient) Repository {
|
||||
return &repository{client: client}
|
||||
}
|
||||
|
||||
type hiddenCtx struct {
|
||||
context.Context
|
||||
}
|
||||
|
||||
func (r *repository) executeRequest(ctx context.Context, method, endpoint string, req interface{}) (*satusehat.FHIRResponse, error) {
|
||||
resp, err := r.client.DoRequest(ctx, method, endpoint, req)
|
||||
resp, err := r.client.DoRequest(hiddenCtx{ctx}, method, endpoint, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errors.InternalError().Message("Failed to execute request to SatuSehat").Cause(err).Build()
|
||||
}
|
||||
|
||||
var result map[string]interface{}
|
||||
if err := json.Unmarshal(resp, &result); err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
|
||||
return nil, errors.InternalError().Message("Failed to parse SatuSehat response").Cause(err).Metadata("raw_response", string(resp)).Build()
|
||||
}
|
||||
|
||||
if resourceType, ok := result["resourceType"].(string); ok && resourceType == "OperationOutcome" {
|
||||
return nil, errors.ParseSatuSehatError(result)
|
||||
}
|
||||
|
||||
var resourceID string
|
||||
@@ -40,11 +49,7 @@ func (r *repository) executeRequest(ctx context.Context, method, endpoint string
|
||||
resourceID = id
|
||||
}
|
||||
|
||||
return &satusehat.FHIRResponse{
|
||||
ID: resourceID,
|
||||
FullResponse: result,
|
||||
RawResponse: resp,
|
||||
}, nil
|
||||
return &satusehat.FHIRResponse{ID: resourceID, FullResponse: result, RawResponse: resp}, nil
|
||||
}
|
||||
|
||||
func (r *repository) Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error) {
|
||||
|
||||
@@ -24,19 +24,16 @@ func NewService(repo Repository) Service {
|
||||
}
|
||||
|
||||
func (s *service) Create(ctx context.Context, req ObservationRequest) (*satusehat.FHIRResponse, error) {
|
||||
fhirPayload := MapRequestToFHIR(req)
|
||||
return s.repo.Create(ctx, fhirPayload)
|
||||
return s.repo.Create(ctx, MapRequestToFHIR(req))
|
||||
}
|
||||
|
||||
func (s *service) Update(ctx context.Context, id string, req ObservationRequest) (*satusehat.FHIRResponse, error) {
|
||||
if id == "" {
|
||||
return nil, errors.NewValidationError().Message("Observation ID is required").Build()
|
||||
}
|
||||
|
||||
fhirPayload := MapRequestToFHIR(req)
|
||||
fhirPayload.Set("id", id)
|
||||
|
||||
return s.repo.Update(ctx, id, fhirPayload)
|
||||
payload := MapRequestToFHIR(req)
|
||||
payload.Set("id", id)
|
||||
return s.repo.Update(ctx, id, payload)
|
||||
}
|
||||
|
||||
func (s *service) Patch(ctx context.Context, id string, req ObservationPatchRequest) (*satusehat.FHIRResponse, error) {
|
||||
@@ -57,6 +54,5 @@ func (s *service) GetByID(ctx context.Context, id string) (*satusehat.FHIRRespon
|
||||
}
|
||||
|
||||
func (s *service) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
|
||||
// Tambahkan validasi untuk query params jika diperlukan
|
||||
return s.repo.Search(ctx, queryParams)
|
||||
}
|
||||
Reference in New Issue
Block a user