penambahan All case Satu sehat
This commit is contained in:
No files matched your search
@@ -1,16 +1,36 @@
|
||||
package specimen
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"time"
|
||||
|
||||
"service/internal/satusehat/common"
|
||||
)
|
||||
|
||||
type SpecimenRequest struct {
|
||||
PatientID string `json:"patient_id" binding:"required"`
|
||||
PatientName string `json:"patient_name" binding:"required"`
|
||||
Status string `json:"status" binding:"required,oneof=available unavailable unsatisfactory entered-in-error"`
|
||||
TypeCode string `json:"type_code" binding:"required"` // SNOMED CT Code (e.g., 119297000 for Blood)
|
||||
TypeDisplay string `json:"type_display" binding:"required"`
|
||||
CollectedDateTime time.Time `json:"collected_date_time" binding:"required"`
|
||||
CollectorID string `json:"collector_id" binding:"required"`
|
||||
CollectorName string `json:"collector_name" binding:"required"`
|
||||
OrganizationID string `json:"organization_id,omitempty"`
|
||||
SpecimenID string `json:"specimen_id,omitempty"`
|
||||
Status string `json:"status" binding:"required,oneof=available,unavailable,unsatisfactory,entered-in-error"`
|
||||
Type *common.CodeableConceptDTO `json:"type" binding:"required"`
|
||||
Subject *common.ReferenceDTO `json:"subject" binding:"required"`
|
||||
Collection *CollectionDTO `json:"collection,omitempty"`
|
||||
ReceivedDateTime *time.Time `json:"received_date_time,omitempty"`
|
||||
Processing []ProcessingDTO `json:"processing,omitempty"`
|
||||
Conditions []string `json:"conditions,omitempty"`
|
||||
Request []common.ReferenceDTO `json:"request,omitempty"`
|
||||
}
|
||||
|
||||
type CollectionDTO struct {
|
||||
CollectedDateTime *time.Time `json:"collected_date_time,omitempty"`
|
||||
Collector *common.ReferenceDTO `json:"collector,omitempty"`
|
||||
Quantity *common.QuantityDTO `json:"quantity,omitempty"`
|
||||
Method *common.CodeableConceptDTO `json:"method,omitempty"`
|
||||
BodySite *common.CodeableConceptDTO `json:"body_site,omitempty"`
|
||||
FastingStatus *common.CodeableConceptDTO `json:"fasting_status,omitempty"`
|
||||
}
|
||||
|
||||
type ProcessingDTO struct {
|
||||
Procedure *common.CodeableConceptDTO `json:"procedure,omitempty"`
|
||||
TimeDateTime *time.Time `json:"time_datetime,omitempty"`
|
||||
}
|
||||
|
||||
type SpecimenPatchRequest []map[string]interface{}
|
||||
@@ -1,27 +1,166 @@
|
||||
package specimen
|
||||
|
||||
import (
|
||||
"service/internal/interfaces/satusehat"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"service/internal/interfaces/satusehat"
|
||||
)
|
||||
|
||||
func MapRequestToFHIR(req SpecimenRequest) satusehat.FHIRPayload {
|
||||
return satusehat.NewFHIRPayload("Specimen").
|
||||
Set("status", req.Status).
|
||||
Set("type", map[string]interface{}{
|
||||
"coding": []map[string]interface{}{
|
||||
{"system": "http://snomed.info/sct", "code": req.TypeCode, "display": req.TypeDisplay},
|
||||
},
|
||||
}).
|
||||
Set("subject", map[string]interface{}{
|
||||
"reference": "Patient/" + req.PatientID,
|
||||
"display": req.PatientName,
|
||||
}).
|
||||
Set("collection", map[string]interface{}{
|
||||
"collectedDateTime": req.CollectedDateTime.Format(time.RFC3339),
|
||||
"collector": map[string]interface{}{
|
||||
"reference": "Practitioner/" + req.CollectorID,
|
||||
"display": req.CollectorName,
|
||||
payload := satusehat.NewFHIRPayload("Specimen")
|
||||
|
||||
orgID := os.Getenv("SATUSEHAT_ORG_ID")
|
||||
if orgID == "" {
|
||||
orgID = req.OrganizationID
|
||||
}
|
||||
|
||||
if orgID != "" && req.SpecimenID != "" {
|
||||
payload.Set("identifier", []map[string]interface{}{
|
||||
{
|
||||
"system": "http://sys-ids.kemkes.go.id/specimen/" + orgID,
|
||||
"value": req.SpecimenID,
|
||||
"assigner": map[string]interface{}{
|
||||
"reference": "Organization/" + orgID,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
if req.Status != "" {
|
||||
payload.Set("status", req.Status)
|
||||
}
|
||||
|
||||
if req.Type != nil {
|
||||
sys := req.Type.System
|
||||
if sys == "" {
|
||||
sys = "http://snomed.info/sct"
|
||||
}
|
||||
coding := map[string]interface{}{"system": sys, "code": req.Type.Code}
|
||||
if req.Type.Display != "" {
|
||||
coding["display"] = req.Type.Display
|
||||
}
|
||||
payload.Set("type", map[string]interface{}{"coding": []map[string]interface{}{coding}})
|
||||
}
|
||||
|
||||
if req.Collection != nil {
|
||||
coll := map[string]interface{}{}
|
||||
if req.Collection.Collector != nil {
|
||||
collector := map[string]interface{}{"reference": req.Collection.Collector.Reference}
|
||||
if req.Collection.Collector.Display != "" {
|
||||
collector["display"] = req.Collection.Collector.Display
|
||||
}
|
||||
coll["collector"] = collector
|
||||
}
|
||||
|
||||
if req.Collection.CollectedDateTime != nil && !req.Collection.CollectedDateTime.IsZero() {
|
||||
coll["collectedDateTime"] = req.Collection.CollectedDateTime.Format(time.RFC3339)
|
||||
}
|
||||
|
||||
if req.Collection.Quantity != nil {
|
||||
sys := req.Collection.Quantity.System
|
||||
if sys == "" {
|
||||
sys = "http://unitsofmeasure.org"
|
||||
}
|
||||
coll["quantity"] = map[string]interface{}{
|
||||
"value": req.Collection.Quantity.Value,
|
||||
"unit": req.Collection.Quantity.Unit,
|
||||
"system": sys,
|
||||
"code": req.Collection.Quantity.Code,
|
||||
}
|
||||
}
|
||||
|
||||
if req.Collection.Method != nil {
|
||||
sys := req.Collection.Method.System
|
||||
if sys == "" {
|
||||
sys = "http://snomed.info/sct"
|
||||
}
|
||||
coding := map[string]interface{}{"system": sys, "code": req.Collection.Method.Code}
|
||||
if req.Collection.Method.Display != "" {
|
||||
coding["display"] = req.Collection.Method.Display
|
||||
}
|
||||
coll["method"] = map[string]interface{}{"coding": []map[string]interface{}{coding}}
|
||||
}
|
||||
|
||||
if req.Collection.BodySite != nil {
|
||||
sys := req.Collection.BodySite.System
|
||||
if sys == "" {
|
||||
sys = "http://snomed.info/sct"
|
||||
}
|
||||
coding := map[string]interface{}{"system": sys, "code": req.Collection.BodySite.Code}
|
||||
if req.Collection.BodySite.Display != "" {
|
||||
coding["display"] = req.Collection.BodySite.Display
|
||||
}
|
||||
coll["bodySite"] = map[string]interface{}{"coding": []map[string]interface{}{coding}}
|
||||
}
|
||||
|
||||
if req.Collection.FastingStatus != nil {
|
||||
sys := req.Collection.FastingStatus.System
|
||||
if sys == "" {
|
||||
sys = "http://terminology.hl7.org/CodeSystem/v2-0916"
|
||||
}
|
||||
coding := map[string]interface{}{"system": sys, "code": req.Collection.FastingStatus.Code}
|
||||
if req.Collection.FastingStatus.Display != "" {
|
||||
coding["display"] = req.Collection.FastingStatus.Display
|
||||
}
|
||||
coll["fastingStatusCodeableConcept"] = map[string]interface{}{"coding": []map[string]interface{}{coding}}
|
||||
}
|
||||
|
||||
if len(coll) > 0 {
|
||||
payload.Set("collection", coll)
|
||||
}
|
||||
}
|
||||
|
||||
if len(req.Processing) > 0 {
|
||||
var processings []map[string]interface{}
|
||||
for _, proc := range req.Processing {
|
||||
p := map[string]interface{}{}
|
||||
if proc.Procedure != nil {
|
||||
sys := proc.Procedure.System
|
||||
if sys == "" {
|
||||
sys = "http://snomed.info/sct"
|
||||
}
|
||||
p["procedure"] = map[string]interface{}{
|
||||
"coding": []map[string]interface{}{
|
||||
{"system": sys, "code": proc.Procedure.Code, "display": proc.Procedure.Display},
|
||||
},
|
||||
}
|
||||
}
|
||||
if proc.TimeDateTime != nil && !proc.TimeDateTime.IsZero() {
|
||||
p["timeDateTime"] = proc.TimeDateTime.Format(time.RFC3339)
|
||||
}
|
||||
processings = append(processings, p)
|
||||
}
|
||||
payload.Set("processing", processings)
|
||||
}
|
||||
|
||||
if len(req.Conditions) > 0 {
|
||||
var conditions []map[string]interface{}
|
||||
for _, c := range req.Conditions {
|
||||
conditions = append(conditions, map[string]interface{}{"text": c})
|
||||
}
|
||||
payload.Set("condition", conditions)
|
||||
}
|
||||
|
||||
if req.Subject != nil {
|
||||
subject := map[string]interface{}{"reference": req.Subject.Reference}
|
||||
if req.Subject.Display != "" {
|
||||
subject["display"] = req.Subject.Display
|
||||
}
|
||||
payload.Set("subject", subject)
|
||||
}
|
||||
|
||||
if len(req.Request) > 0 {
|
||||
var requests []map[string]interface{}
|
||||
for _, r := range req.Request {
|
||||
requests = append(requests, map[string]interface{}{"reference": r.Reference})
|
||||
}
|
||||
payload.Set("request", requests)
|
||||
}
|
||||
|
||||
if req.ReceivedDateTime != nil && !req.ReceivedDateTime.IsZero() {
|
||||
payload.Set("receivedTime", req.ReceivedDateTime.Format(time.RFC3339))
|
||||
}
|
||||
|
||||
return payload
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"service/internal/interfaces/satusehat"
|
||||
"service/pkg/errors"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
@@ -19,14 +20,21 @@ type repository struct{ client satusehat.SatuSehatClient }
|
||||
|
||||
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
|
||||
if id, ok := result["id"].(string); ok {
|
||||
|
||||
@@ -22,14 +22,25 @@ func (s *service) Create(ctx context.Context, req SpecimenRequest) (*satusehat.F
|
||||
}
|
||||
func (s *service) Update(ctx context.Context, id string, req SpecimenRequest) (*satusehat.FHIRResponse, error) {
|
||||
if id == "" {
|
||||
return nil, errors.NewValidationError().Message("ID is required").Build()
|
||||
return nil, errors.NewValidationError().Message("Specimen ID is required").Build()
|
||||
}
|
||||
return s.repo.Update(ctx, id, MapRequestToFHIR(req).Set("id", id))
|
||||
payload := MapRequestToFHIR(req)
|
||||
payload.Set("id", id)
|
||||
return s.repo.Update(ctx, id, payload)
|
||||
}
|
||||
func (s *service) Patch(ctx context.Context, id string, req SpecimenPatchRequest) (*satusehat.FHIRResponse, error) {
|
||||
if id == "" {
|
||||
return nil, errors.NewValidationError().Message("Specimen ID is required").Build()
|
||||
}
|
||||
if len(req) == 0 {
|
||||
return nil, errors.NewValidationError().Message("Patch payload cannot be empty").Build()
|
||||
}
|
||||
return s.repo.Patch(ctx, id, req)
|
||||
}
|
||||
func (s *service) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
|
||||
if id == "" {
|
||||
return nil, errors.NewValidationError().Message("Specimen ID is required").Build()
|
||||
}
|
||||
return s.repo.GetByID(ctx, id)
|
||||
}
|
||||
func (s *service) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
|
||||
|
||||
Reference in New Issue
Block a user