first commit

This commit is contained in:
meninjar
2026-04-14 01:23:34 +00:00
commit edfaa886ff
443 changed files with 1245931 additions and 0 deletions
@@ -0,0 +1,17 @@
package allergyintolerance
import "time"
type AllergyIntoleranceRequest struct {
PatientID string `json:"patient_id" binding:"required"`
PatientName string `json:"patient_name" binding:"required"`
EncounterID string `json:"encounter_id" binding:"required"`
ClinicalStatus string `json:"clinical_status" binding:"required,oneof=active inactive resolved"`
VerificationStatus string `json:"verification_status" binding:"required,oneof=unconfirmed presumed confirmed refuted entered-in-error"`
Category string `json:"category" binding:"required,oneof=food medication environment biologic"`
Code string `json:"code" binding:"required"` // SNOMED CT Code
Display string `json:"display" binding:"required"` // Display Name
RecordedDate time.Time `json:"recorded_date" binding:"required"`
}
type AllergyIntolerancePatchRequest []map[string]interface{}
@@ -0,0 +1,42 @@
package allergyintolerance
import (
"time"
"service/internal/interfaces/satusehat"
)
func MapRequestToFHIR(req AllergyIntoleranceRequest) satusehat.FHIRPayload {
return satusehat.NewFHIRPayload("AllergyIntolerance").
Set("clinicalStatus", map[string]interface{}{
"coding": []map[string]interface{}{
{
"system": "http://terminology.hl7.org/CodeSystem/allergyintolerance-clinical",
"code": req.ClinicalStatus,
},
},
}).
Set("verificationStatus", map[string]interface{}{
"coding": []map[string]interface{}{
{
"system": "http://terminology.hl7.org/CodeSystem/allergyintolerance-verification",
"code": req.VerificationStatus,
},
},
}).
Set("category", []string{req.Category}).
Set("code", map[string]interface{}{
"coding": []map[string]interface{}{
{
"system": "http://snomed.info/sct",
"code": req.Code,
"display": req.Display,
},
},
}).
Set("patient", map[string]interface{}{
"reference": "Patient/" + req.PatientID,
"display": req.PatientName,
}).
Set("recordedDate", req.RecordedDate.Format(time.RFC3339))
}
@@ -0,0 +1,51 @@
package allergyintolerance
import (
"context"
"encoding/json"
"fmt"
"net/url"
"service/internal/interfaces/satusehat"
)
type Repository interface {
Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req AllergyIntolerancePatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type repository struct{ client satusehat.SatuSehatClient }
func NewRepository(client satusehat.SatuSehatClient) Repository { return &repository{client: client} }
func (r *repository) executeRequest(ctx context.Context, method, endpoint string, req interface{}) (*satusehat.FHIRResponse, error) {
resp, err := r.client.DoRequest(ctx, method, endpoint, req)
if err != nil {
return nil, err
}
var result map[string]interface{}
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
var resourceID string
if id, ok := result["id"].(string); ok {
resourceID = id
}
return &satusehat.FHIRResponse{ID: resourceID, FullResponse: result, RawResponse: resp}, nil
}
func (r *repository) Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "POST", "/AllergyIntolerance", payload)
}
func (r *repository) Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "PUT", fmt.Sprintf("/AllergyIntolerance/%s", id), payload)
}
func (r *repository) Patch(ctx context.Context, id string, req AllergyIntolerancePatchRequest) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "PATCH", fmt.Sprintf("/AllergyIntolerance/%s", id), req)
}
func (r *repository) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "GET", fmt.Sprintf("/AllergyIntolerance/%s", id), nil)
}
func (r *repository) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "GET", fmt.Sprintf("/AllergyIntolerance?%s", queryParams.Encode()), nil)
}
@@ -0,0 +1,38 @@
package allergyintolerance
import (
"context"
"net/url"
"service/internal/interfaces/satusehat"
"service/pkg/errors"
)
type Service interface {
Create(ctx context.Context, req AllergyIntoleranceRequest) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, req AllergyIntoleranceRequest) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req AllergyIntolerancePatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type service struct{ repo Repository }
func NewService(repo Repository) Service { return &service{repo: repo} }
func (s *service) Create(ctx context.Context, req AllergyIntoleranceRequest) (*satusehat.FHIRResponse, error) {
return s.repo.Create(ctx, MapRequestToFHIR(req))
}
func (s *service) Update(ctx context.Context, id string, req AllergyIntoleranceRequest) (*satusehat.FHIRResponse, error) {
if id == "" {
return nil, errors.NewValidationError().Message("ID is required").Build()
}
return s.repo.Update(ctx, id, MapRequestToFHIR(req).Set("id", id))
}
func (s *service) Patch(ctx context.Context, id string, req AllergyIntolerancePatchRequest) (*satusehat.FHIRResponse, error) {
return s.repo.Patch(ctx, id, req)
}
func (s *service) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
return s.repo.GetByID(ctx, id)
}
func (s *service) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
return s.repo.Search(ctx, queryParams)
}
@@ -0,0 +1,18 @@
package careplan
import "time"
type CarePlanRequest struct {
PatientID string `json:"patient_id" binding:"required"`
PatientName string `json:"patient_name" binding:"required"`
EncounterID string `json:"encounter_id" binding:"required"`
PractitionerID string `json:"practitioner_id" binding:"required"`
PractitionerName string `json:"practitioner_name" binding:"required"`
Status string `json:"status" binding:"required,oneof=draft active on-hold revoked completed entered-in-error unknown"`
Intent string `json:"intent" binding:"required,oneof=proposal plan order option"`
Title string `json:"title" binding:"required"`
Description string `json:"description" binding:"required"`
CreatedDate time.Time `json:"created_date" binding:"required"`
}
type CarePlanPatchRequest []map[string]interface{}
@@ -0,0 +1,24 @@
package careplan
import (
"service/internal/interfaces/satusehat"
"time"
)
func MapRequestToFHIR(req CarePlanRequest) satusehat.FHIRPayload {
return satusehat.NewFHIRPayload("CarePlan").
Set("status", req.Status).
Set("intent", req.Intent).
Set("title", req.Title).
Set("description", req.Description).
Set("subject", map[string]interface{}{
"reference": "Patient/" + req.PatientID,
"display": req.PatientName,
}).
Set("encounter", map[string]interface{}{"reference": "Encounter/" + req.EncounterID}).
Set("author", map[string]interface{}{
"reference": "Practitioner/" + req.PractitionerID,
"display": req.PractitionerName,
}).
Set("created", req.CreatedDate.Format(time.RFC3339))
}
@@ -0,0 +1,51 @@
package careplan
import (
"context"
"encoding/json"
"fmt"
"net/url"
"service/internal/interfaces/satusehat"
)
type Repository interface {
Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req CarePlanPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type repository struct{ client satusehat.SatuSehatClient }
func NewRepository(client satusehat.SatuSehatClient) Repository { return &repository{client: client} }
func (r *repository) executeRequest(ctx context.Context, method, endpoint string, req interface{}) (*satusehat.FHIRResponse, error) {
resp, err := r.client.DoRequest(ctx, method, endpoint, req)
if err != nil {
return nil, err
}
var result map[string]interface{}
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
var resourceID string
if id, ok := result["id"].(string); ok {
resourceID = id
}
return &satusehat.FHIRResponse{ID: resourceID, FullResponse: result, RawResponse: resp}, nil
}
func (r *repository) Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "POST", "/CarePlan", payload)
}
func (r *repository) Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "PUT", fmt.Sprintf("/CarePlan/%s", id), payload)
}
func (r *repository) Patch(ctx context.Context, id string, req CarePlanPatchRequest) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "PATCH", fmt.Sprintf("/CarePlan/%s", id), req)
}
func (r *repository) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "GET", fmt.Sprintf("/CarePlan/%s", id), nil)
}
func (r *repository) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "GET", fmt.Sprintf("/CarePlan?%s", queryParams.Encode()), nil)
}
@@ -0,0 +1,37 @@
package careplan
import (
"context"
"net/url"
"service/internal/interfaces/satusehat"
"service/pkg/errors"
)
type Service interface {
Create(ctx context.Context, req CarePlanRequest) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, req CarePlanRequest) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req CarePlanPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type service struct{ repo Repository }
func NewService(repo Repository) Service { return &service{repo: repo} }
func (s *service) Create(ctx context.Context, req CarePlanRequest) (*satusehat.FHIRResponse, error) {
return s.repo.Create(ctx, MapRequestToFHIR(req))
}
func (s *service) Update(ctx context.Context, id string, req CarePlanRequest) (*satusehat.FHIRResponse, error) {
if id == "" {
return nil, errors.NewValidationError().Message("ID is required").Build()
}
return s.repo.Update(ctx, id, MapRequestToFHIR(req).Set("id", id))
}
func (s *service) Patch(ctx context.Context, id string, req CarePlanPatchRequest) (*satusehat.FHIRResponse, error) {
return s.repo.Patch(ctx, id, req)
}
func (s *service) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
return s.repo.GetByID(ctx, id)
}
func (s *service) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
return s.repo.Search(ctx, queryParams)
}
@@ -0,0 +1,17 @@
package clinicalimpression
import "time"
type ClinicalImpressionRequest struct {
PatientID string `json:"patient_id" binding:"required"`
PatientName string `json:"patient_name" binding:"required"`
EncounterID string `json:"encounter_id" binding:"required"`
PractitionerID string `json:"practitioner_id" binding:"required"`
PractitionerName string `json:"practitioner_name" binding:"required"`
Status string `json:"status" binding:"required,oneof=in-progress completed entered-in-error"`
Date time.Time `json:"date" binding:"required"`
Summary string `json:"summary" binding:"required"`
Description string `json:"description" binding:"required"`
}
type ClinicalImpressionPatchRequest []map[string]interface{}
@@ -0,0 +1,23 @@
package clinicalimpression
import (
"service/internal/interfaces/satusehat"
"time"
)
func MapRequestToFHIR(req ClinicalImpressionRequest) satusehat.FHIRPayload {
return satusehat.NewFHIRPayload("ClinicalImpression").
Set("status", req.Status).
Set("description", req.Description).
Set("summary", req.Summary).
Set("subject", map[string]interface{}{
"reference": "Patient/" + req.PatientID,
"display": req.PatientName,
}).
Set("encounter", map[string]interface{}{"reference": "Encounter/" + req.EncounterID}).
Set("assessor", map[string]interface{}{
"reference": "Practitioner/" + req.PractitionerID,
"display": req.PractitionerName,
}).
Set("date", req.Date.Format(time.RFC3339))
}
@@ -0,0 +1,51 @@
package clinicalimpression
import (
"context"
"encoding/json"
"fmt"
"net/url"
"service/internal/interfaces/satusehat"
)
type Repository interface {
Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req ClinicalImpressionPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type repository struct{ client satusehat.SatuSehatClient }
func NewRepository(client satusehat.SatuSehatClient) Repository { return &repository{client: client} }
func (r *repository) executeRequest(ctx context.Context, method, endpoint string, req interface{}) (*satusehat.FHIRResponse, error) {
resp, err := r.client.DoRequest(ctx, method, endpoint, req)
if err != nil {
return nil, err
}
var result map[string]interface{}
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
var resourceID string
if id, ok := result["id"].(string); ok {
resourceID = id
}
return &satusehat.FHIRResponse{ID: resourceID, FullResponse: result, RawResponse: resp}, nil
}
func (r *repository) Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "POST", "/ClinicalImpression", payload)
}
func (r *repository) Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "PUT", fmt.Sprintf("/ClinicalImpression/%s", id), payload)
}
func (r *repository) Patch(ctx context.Context, id string, req ClinicalImpressionPatchRequest) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "PATCH", fmt.Sprintf("/ClinicalImpression/%s", id), req)
}
func (r *repository) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "GET", fmt.Sprintf("/ClinicalImpression/%s", id), nil)
}
func (r *repository) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "GET", fmt.Sprintf("/ClinicalImpression?%s", queryParams.Encode()), nil)
}
@@ -0,0 +1,37 @@
package clinicalimpression
import (
"context"
"net/url"
"service/internal/interfaces/satusehat"
"service/pkg/errors"
)
type Service interface {
Create(ctx context.Context, req ClinicalImpressionRequest) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, req ClinicalImpressionRequest) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req ClinicalImpressionPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type service struct{ repo Repository }
func NewService(repo Repository) Service { return &service{repo: repo} }
func (s *service) Create(ctx context.Context, req ClinicalImpressionRequest) (*satusehat.FHIRResponse, error) {
return s.repo.Create(ctx, MapRequestToFHIR(req))
}
func (s *service) Update(ctx context.Context, id string, req ClinicalImpressionRequest) (*satusehat.FHIRResponse, error) {
if id == "" {
return nil, errors.NewValidationError().Message("ID is required").Build()
}
return s.repo.Update(ctx, id, MapRequestToFHIR(req).Set("id", id))
}
func (s *service) Patch(ctx context.Context, id string, req ClinicalImpressionPatchRequest) (*satusehat.FHIRResponse, error) {
return s.repo.Patch(ctx, id, req)
}
func (s *service) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
return s.repo.GetByID(ctx, id)
}
func (s *service) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
return s.repo.Search(ctx, queryParams)
}
@@ -0,0 +1,17 @@
package composition
import "time"
type CompositionRequest struct {
PatientID string `json:"patient_id" binding:"required"`
PatientName string `json:"patient_name" binding:"required"`
EncounterID string `json:"encounter_id" binding:"required"`
PractitionerID string `json:"practitioner_id" binding:"required"`
PractitionerName string `json:"practitioner_name" binding:"required"`
Title string `json:"title" binding:"required"`
Status string `json:"status" binding:"required,oneof=preliminary final amended entered-in-error"` // preliminary, final, amended, entered-in-error
Date time.Time `json:"date" binding:"required"`
}
// CompositionPatchRequest merepresentasikan payload operasi JSON Patch.
type CompositionPatchRequest []map[string]interface{}
@@ -0,0 +1,38 @@
package composition
import (
"time"
"service/internal/interfaces/satusehat"
)
func MapRequestToFHIR(req CompositionRequest) satusehat.FHIRPayload {
payload := satusehat.NewFHIRPayload("Composition").
Set("status", req.Status).
Set("type", map[string]interface{}{
"coding": []map[string]interface{}{
{
"system": "http://loinc.org",
"code": "11503-0", // Example: Medical records
"display": "Medical records",
},
},
}).
Set("subject", map[string]interface{}{
"reference": "Patient/" + req.PatientID,
"display": req.PatientName,
}).
Set("encounter", map[string]interface{}{
"reference": "Encounter/" + req.EncounterID,
}).
Set("date", req.Date.Format(time.RFC3339)).
Set("title", req.Title)
if req.PractitionerID != "" {
payload.Append("author", map[string]interface{}{
"reference": "Practitioner/" + req.PractitionerID,
"display": req.PractitionerName,
})
}
return payload
}
@@ -0,0 +1,73 @@
package composition
import (
"context"
"encoding/json"
"fmt"
"net/url"
"service/internal/interfaces/satusehat"
)
type Repository interface {
Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req CompositionPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type repository struct {
client satusehat.SatuSehatClient
}
func NewRepository(client satusehat.SatuSehatClient) Repository {
return &repository{client: client}
}
func (r *repository) executeRequest(ctx context.Context, method, endpoint string, req interface{}) (*satusehat.FHIRResponse, error) {
resp, err := r.client.DoRequest(ctx, method, endpoint, req)
if err != nil {
return nil, err
}
var result map[string]interface{}
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
var resourceID string
if id, ok := result["id"].(string); ok {
resourceID = id
}
return &satusehat.FHIRResponse{
ID: resourceID,
FullResponse: result,
RawResponse: resp,
}, nil
}
func (r *repository) Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "POST", "/Composition", payload)
}
func (r *repository) Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/Composition/%s", id)
return r.executeRequest(ctx, "PUT", endpoint, payload)
}
func (r *repository) Patch(ctx context.Context, id string, req CompositionPatchRequest) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/Composition/%s", id)
return r.executeRequest(ctx, "PATCH", endpoint, req)
}
func (r *repository) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/Composition/%s", id)
return r.executeRequest(ctx, "GET", endpoint, nil)
}
func (r *repository) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/Composition?%s", queryParams.Encode())
return r.executeRequest(ctx, "GET", endpoint, nil)
}
@@ -0,0 +1,55 @@
package composition
import (
"context"
"net/url"
"service/internal/interfaces/satusehat"
"service/pkg/errors"
)
type Service interface {
Create(ctx context.Context, req CompositionRequest) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, req CompositionRequest) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req CompositionPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type service struct {
repo Repository
}
func NewService(repo Repository) Service {
return &service{repo: repo}
}
func (s *service) Create(ctx context.Context, req CompositionRequest) (*satusehat.FHIRResponse, error) {
fhirPayload := MapRequestToFHIR(req)
return s.repo.Create(ctx, fhirPayload)
}
func (s *service) Update(ctx context.Context, id string, req CompositionRequest) (*satusehat.FHIRResponse, error) {
if id == "" {
return nil, errors.NewValidationError().Message("Composition ID is required").Build()
}
fhirPayload := MapRequestToFHIR(req)
fhirPayload.Set("id", id)
return s.repo.Update(ctx, id, fhirPayload)
}
func (s *service) Patch(ctx context.Context, id string, req CompositionPatchRequest) (*satusehat.FHIRResponse, error) {
if id == "" {
return nil, errors.NewValidationError().Message("Composition 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) {
return s.repo.GetByID(ctx, id)
}
func (s *service) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
return s.repo.Search(ctx, queryParams)
}
@@ -0,0 +1,18 @@
package condition
import "time"
type ConditionRequest struct {
PatientID string `json:"patient_id" binding:"required"`
PatientName string `json:"patient_name" binding:"required"`
EncounterID string `json:"encounter_id" binding:"required"`
ClinicalStatus string `json:"clinical_status" binding:"required,oneof=active recurrence relapse inactive remission resolved"` // active, recurrence, relapse, inactive, remission, resolved
CategoryCode string `json:"category_code" binding:"required"` // problem-list-item, encounter-diagnosis
CategoryDisplay string `json:"category_display" binding:"required"`
Code string `json:"code" binding:"required"` // SNOMED CT / ICD-10 code
Display string `json:"display" binding:"required"` // Diagnosis text
OnsetDateTime time.Time `json:"onset_date_time" binding:"required"`
RecordedDate time.Time `json:"recorded_date" binding:"required"`
}
type ConditionPatchRequest []map[string]interface{}
@@ -0,0 +1,50 @@
package condition
import (
"time"
"service/internal/interfaces/satusehat"
)
func MapRequestToFHIR(req ConditionRequest) satusehat.FHIRPayload {
payload := satusehat.NewFHIRPayload("Condition").
Set("clinicalStatus", map[string]interface{}{
"coding": []map[string]interface{}{
{
"system": "http://terminology.hl7.org/CodeSystem/condition-clinical",
"code": req.ClinicalStatus,
},
},
}).
Set("category", []map[string]interface{}{
{
"coding": []map[string]interface{}{
{
"system": "http://terminology.hl7.org/CodeSystem/condition-category",
"code": req.CategoryCode,
"display": req.CategoryDisplay,
},
},
},
}).
Set("code", map[string]interface{}{
"coding": []map[string]interface{}{
{
"system": "http://snomed.info/sct",
"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("onsetDateTime", req.OnsetDateTime.Format(time.RFC3339)).
Set("recordedDate", req.RecordedDate.Format(time.RFC3339))
return payload
}
@@ -0,0 +1,72 @@
package condition
import (
"context"
"encoding/json"
"fmt"
"net/url"
"service/internal/interfaces/satusehat"
)
type Repository interface {
Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, payload ConditionPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type repository struct {
client satusehat.SatuSehatClient
}
func NewRepository(client satusehat.SatuSehatClient) Repository {
return &repository{client: client}
}
func (r *repository) executeRequest(ctx context.Context, method, endpoint string, req interface{}) (*satusehat.FHIRResponse, error) {
resp, err := r.client.DoRequest(ctx, method, endpoint, req)
if err != nil {
return nil, err
}
var result map[string]interface{}
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
var resourceID string
if id, ok := result["id"].(string); ok {
resourceID = id
}
return &satusehat.FHIRResponse{
ID: resourceID,
FullResponse: result,
RawResponse: resp,
}, nil
}
func (r *repository) Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "POST", "/Condition", payload)
}
func (r *repository) Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/Condition/%s", id)
return r.executeRequest(ctx, "PUT", endpoint, payload)
}
func (r *repository) Patch(ctx context.Context, id string, payload ConditionPatchRequest) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/Condition/%s", id)
return r.executeRequest(ctx, "PATCH", endpoint, payload)
}
func (r *repository) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/Condition/%s", id)
return r.executeRequest(ctx, "GET", endpoint, nil)
}
func (r *repository) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/Condition?%s", queryParams.Encode())
return r.executeRequest(ctx, "GET", endpoint, nil)
}
@@ -0,0 +1,62 @@
package condition
import (
"context"
"net/url"
"service/internal/interfaces/satusehat"
"service/pkg/errors"
)
type Service interface {
Create(ctx context.Context, req ConditionRequest) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, req ConditionRequest) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req ConditionPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type service struct {
repo Repository
}
func NewService(repo Repository) Service {
return &service{repo: repo}
}
func (s *service) Create(ctx context.Context, req ConditionRequest) (*satusehat.FHIRResponse, error) {
fhirPayload := MapRequestToFHIR(req)
return s.repo.Create(ctx, fhirPayload)
}
func (s *service) Update(ctx context.Context, id string, req ConditionRequest) (*satusehat.FHIRResponse, error) {
if id == "" {
return nil, errors.NewValidationError().Message("Condition ID is required").Build()
}
fhirPayload := MapRequestToFHIR(req)
fhirPayload.Set("id", id)
return s.repo.Update(ctx, id, fhirPayload)
}
func (s *service) Patch(ctx context.Context, id string, req ConditionPatchRequest) (*satusehat.FHIRResponse, error) {
if id == "" {
return nil, errors.NewValidationError().Message("Condition 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("Condition ID is required").Build()
}
return s.repo.GetByID(ctx, id)
}
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)
}
@@ -0,0 +1,20 @@
package diagnosticreport
import "time"
type DiagnosticReportRequest struct {
PatientID string `json:"patient_id" binding:"required"`
PatientName string `json:"patient_name" binding:"required"`
EncounterID string `json:"encounter_id" binding:"required"`
PractitionerID string `json:"practitioner_id" binding:"required"`
PractitionerName string `json:"practitioner_name" binding:"required"`
Status string `json:"status" binding:"required,oneof=registered partial preliminary final amended corrected appended cancelled entered-in-error unknown"`
CategoryCode string `json:"category_code" binding:"required"` // e.g. LAB
CategoryDisplay string `json:"category_display" binding:"required"`
Code string `json:"code" binding:"required"` // LOINC code
Display string `json:"display" binding:"required"`
Issued time.Time `json:"issued" binding:"required"`
Conclusion string `json:"conclusion" binding:"required"`
}
type DiagnosticReportPatchRequest []map[string]interface{}
@@ -0,0 +1,41 @@
package diagnosticreport
import (
"service/internal/interfaces/satusehat"
"time"
)
func MapRequestToFHIR(req DiagnosticReportRequest) satusehat.FHIRPayload {
return satusehat.NewFHIRPayload("DiagnosticReport").
Set("status", req.Status).
Set("category", []map[string]interface{}{
{
"coding": []map[string]interface{}{
{
"system": "http://terminology.hl7.org/CodeSystem/v2-0074",
"code": req.CategoryCode,
"display": req.CategoryDisplay,
},
},
},
}).
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("performer", []map[string]interface{}{
{"reference": "Practitioner/" + req.PractitionerID, "display": req.PractitionerName},
}).
Set("issued", req.Issued.Format(time.RFC3339)).
Set("conclusion", req.Conclusion)
}
@@ -0,0 +1,51 @@
package diagnosticreport
import (
"context"
"encoding/json"
"fmt"
"net/url"
"service/internal/interfaces/satusehat"
)
type Repository interface {
Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req DiagnosticReportPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type repository struct{ client satusehat.SatuSehatClient }
func NewRepository(client satusehat.SatuSehatClient) Repository { return &repository{client: client} }
func (r *repository) executeRequest(ctx context.Context, method, endpoint string, req interface{}) (*satusehat.FHIRResponse, error) {
resp, err := r.client.DoRequest(ctx, method, endpoint, req)
if err != nil {
return nil, err
}
var result map[string]interface{}
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
var resourceID string
if id, ok := result["id"].(string); ok {
resourceID = id
}
return &satusehat.FHIRResponse{ID: resourceID, FullResponse: result, RawResponse: resp}, nil
}
func (r *repository) Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "POST", "/DiagnosticReport", payload)
}
func (r *repository) Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "PUT", fmt.Sprintf("/DiagnosticReport/%s", id), payload)
}
func (r *repository) Patch(ctx context.Context, id string, req DiagnosticReportPatchRequest) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "PATCH", fmt.Sprintf("/DiagnosticReport/%s", id), req)
}
func (r *repository) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "GET", fmt.Sprintf("/DiagnosticReport/%s", id), nil)
}
func (r *repository) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "GET", fmt.Sprintf("/DiagnosticReport?%s", queryParams.Encode()), nil)
}
@@ -0,0 +1,37 @@
package diagnosticreport
import (
"context"
"net/url"
"service/internal/interfaces/satusehat"
"service/pkg/errors"
)
type Service interface {
Create(ctx context.Context, req DiagnosticReportRequest) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, req DiagnosticReportRequest) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req DiagnosticReportPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type service struct{ repo Repository }
func NewService(repo Repository) Service { return &service{repo: repo} }
func (s *service) Create(ctx context.Context, req DiagnosticReportRequest) (*satusehat.FHIRResponse, error) {
return s.repo.Create(ctx, MapRequestToFHIR(req))
}
func (s *service) Update(ctx context.Context, id string, req DiagnosticReportRequest) (*satusehat.FHIRResponse, error) {
if id == "" {
return nil, errors.NewValidationError().Message("ID is required").Build()
}
return s.repo.Update(ctx, id, MapRequestToFHIR(req).Set("id", id))
}
func (s *service) Patch(ctx context.Context, id string, req DiagnosticReportPatchRequest) (*satusehat.FHIRResponse, error) {
return s.repo.Patch(ctx, id, req)
}
func (s *service) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
return s.repo.GetByID(ctx, id)
}
func (s *service) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
return s.repo.Search(ctx, queryParams)
}
@@ -0,0 +1,44 @@
package encounter
import (
"database/sql"
"time"
)
// EncounterRequest merepresentasikan data input untuk membuat atau memperbarui Encounter.
type EncounterRequest struct {
EncounterID string `json:"encounter_id" binding:"required"`
OrganizationID string `json:"organization_id" binding:"required"`
EpisodeOfCareID string `json:"episode_of_care_id,omitempty"`
PatientID string `json:"patient_id" binding:"required"`
PatientName string `json:"patient_name" binding:"required"`
PractitionerID string `json:"practitioner_id" binding:"required"`
PractitionerName string `json:"practitioner_name" binding:"required"`
LocationID string `json:"location_id" binding:"required"`
LocationName string `json:"location_name" binding:"required"`
Status string `json:"status" binding:"required,oneof=arrived in-progress finished cancelled"` // arrived, in-progress, finished, cancelled
Class string `json:"class" binding:"required,oneof=AMB IMP EMER"` // AMB (ambulatory), IMP (inpatient), EMER (emergency)
PeriodStart time.Time `json:"period_start" binding:"required"`
PeriodEnd *time.Time `json:"period_end,omitempty"`
}
// EncounterPatchRequest merepresentasikan payload operasi JSON Patch.
type EncounterPatchRequest []map[string]interface{}
// PendaftaranDB merepresentasikan baris data dari tabel t_pendaftaran
type PendaftaranDB struct {
IdxDaftar int64 `db:"idxdaftar"`
NoMR sql.NullString `db:"nomr"`
JamReg sql.NullTime `db:"jamreg"`
MasukPoly sql.NullTime `db:"masukpoly"`
KeluarPoly sql.NullTime `db:"keluarpoly"`
Batal sql.NullString `db:"batal"`
KdPoly sql.NullInt64 `db:"kdpoly"`
PoliNameHFIS sql.NullString `db:"poli_name_hfis"`
DokterIDHFIS sql.NullString `db:"dokter_id_hfis"`
DokterNameHFIS sql.NullString `db:"dokter_name_hfis"`
PasienNIK sql.NullString `db:"pasien_nik"`
DokterNIK sql.NullString `db:"dokter_nik"`
PatientIHS sql.NullString `db:"patient_ihs"`
PractitionerIHS sql.NullString `db:"practitioner_ihs"`
}
@@ -0,0 +1,141 @@
package encounter
import (
"fmt"
"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{}{
"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,
})
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.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",
},
},
},
},
"individual": map[string]interface{}{
"reference": "Practitioner/" + req.PractitionerID,
"display": req.PractitionerName,
},
})
}
if req.LocationID != "" {
payload.Append("location", map[string]interface{}{
"location": map[string]interface{}{
"reference": "Location/" + req.LocationID,
"display": req.LocationName,
},
})
}
period := map[string]interface{}{
"start": req.PeriodStart.Format(time.RFC3339),
}
if req.PeriodEnd != nil {
period["end"] = req.PeriodEnd.Format(time.RFC3339)
}
payload.Set("period", period)
payload.Append("statusHistory", map[string]interface{}{
"status": req.Status,
"period": period,
})
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"
}
}
@@ -0,0 +1,130 @@
package encounter
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"net/url"
"service/internal/infrastructure/database"
"service/internal/interfaces/satusehat"
)
type Repository interface {
Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, payload EncounterPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
GetPendaftaranByID(ctx context.Context, idxdaftar int64) (*PendaftaranDB, error)
SearchPatientByNIK(ctx context.Context, nik string) (*satusehat.FHIRResponse, error)
SearchPractitionerByNIK(ctx context.Context, nik string) (*satusehat.FHIRResponse, error)
}
type repository struct {
client satusehat.SatuSehatClient
db database.Service
}
func NewRepository(client satusehat.SatuSehatClient, db database.Service) Repository {
return &repository{client: client, db: db}
}
func (r *repository) executeRequest(ctx context.Context, method, endpoint string, payload interface{}) (*satusehat.FHIRResponse, error) {
resp, err := r.client.DoRequest(ctx, method, endpoint, payload)
if err != nil {
return nil, err
}
return r.parseAndProcessResponse(resp)
}
func (r *repository) parseAndProcessResponse(data []byte) (*satusehat.FHIRResponse, error) {
var result map[string]interface{}
if err := json.Unmarshal(data, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
// Ekstrak ID dari resource jika ada
var resourceID string
if id, ok := result["id"].(string); ok {
resourceID = id
}
// Buat response terstruktur
fhirResponse := &satusehat.FHIRResponse{
ID: resourceID,
FullResponse: result,
RawResponse: data, // Simpan data mentah untuk logging atau audit
}
return fhirResponse, nil
}
func (r *repository) Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "POST", "/Encounter", payload)
}
func (r *repository) Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/Encounter/%s", id)
return r.executeRequest(ctx, "PUT", endpoint, payload)
}
func (r *repository) Patch(ctx context.Context, id string, payload EncounterPatchRequest) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/Encounter/%s", id)
return r.executeRequest(ctx, "PATCH", endpoint, payload)
}
func (r *repository) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/Encounter/%s", id)
return r.executeRequest(ctx, "GET", endpoint, nil)
}
func (r *repository) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/Encounter?%s", queryParams.Encode())
return r.executeRequest(ctx, "GET", endpoint, nil)
}
func (r *repository) SearchPatientByNIK(ctx context.Context, nik string) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/Patient?identifier=https://fhir.kemkes.go.id/id/nik|%s", nik)
return r.executeRequest(ctx, "GET", endpoint, nil)
}
func (r *repository) SearchPractitionerByNIK(ctx context.Context, nik string) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/Practitioner?identifier=https://fhir.kemkes.go.id/id/nik|%s", nik)
return r.executeRequest(ctx, "GET", endpoint, nil)
}
func (r *repository) GetPendaftaranByID(ctx context.Context, idxdaftar int64) (*PendaftaranDB, error) {
// Ambil koneksi database internal
db, err := r.db.GetReadDB("default")
if err != nil {
return nil, err
}
var p PendaftaranDB
query := `
SELECT p.idxdaftar, p.nomr, p.jamreg, p.masukpoly, p.keluarpoly, p.batal,
p.kdpoly, p.poli_name_hfis, p.dokter_id_hfis, p.dokter_name_hfis,
COALESCE(NULLIF(mp.noktp_baru, ''), mp.noktp) AS pasien_nik,
dp.nik AS dokter_nik,
dpas."Nomor_satusehat" AS patient_ihs,
dp."Kode_satusehat" AS practitioner_ihs
FROM public.t_pendaftaran p
LEFT JOIN public.m_pasien mp ON p.nomr = mp.nomr
LEFT JOIN public.data_pasien dpas ON p.nomr = dpas."Nomor_rekamedik"
LEFT JOIN public.data_pegawai dp ON p.kddokter = dp."Kode_DPJP"
WHERE p.idxdaftar = $1`
err = db.QueryRowContext(ctx, query, idxdaftar).Scan(
&p.IdxDaftar, &p.NoMR, &p.JamReg, &p.MasukPoly, &p.KeluarPoly, &p.Batal,
&p.KdPoly, &p.PoliNameHFIS, &p.DokterIDHFIS, &p.DokterNameHFIS,
&p.PasienNIK, &p.DokterNIK, &p.PatientIHS, &p.PractitionerIHS,
)
if err != nil {
if err == sql.ErrNoRows {
return nil, fmt.Errorf("pendaftaran dengan idxdaftar %d tidak ditemukan", idxdaftar)
}
return nil, err
}
return &p, nil
}
@@ -0,0 +1,122 @@
package encounter
import (
"context"
"encoding/json"
"fmt"
"net/url"
"os"
"service/internal/interfaces/satusehat"
"service/pkg/errors"
)
type Service interface {
Create(ctx context.Context, req EncounterRequest) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, req EncounterRequest) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req EncounterPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
SyncFromSIMRS(ctx context.Context, idxdaftar int64) (*satusehat.FHIRResponse, error)
}
type service struct {
repo Repository
}
func NewService(repo Repository) Service {
return &service{repo: repo}
}
func (s *service) Create(ctx context.Context, req EncounterRequest) (*satusehat.FHIRResponse, error) {
fhirPayload := MapRequestToFHIR(req)
return s.repo.Create(ctx, fhirPayload)
}
func (s *service) Update(ctx context.Context, id string, req EncounterRequest) (*satusehat.FHIRResponse, error) {
if id == "" {
return nil, errors.NewValidationError().Message("Encounter ID is required").Build()
}
fhirPayload := MapRequestToFHIR(req)
fhirPayload.Set("id", id) // Untuk Update (PUT), parameter ID di payload diwajibkan oleh standar API Kemenkes
return s.repo.Update(ctx, id, fhirPayload)
}
func (s *service) Patch(ctx context.Context, id string, req EncounterPatchRequest) (*satusehat.FHIRResponse, error) {
if id == "" {
return nil, errors.NewValidationError().Message("Encounter 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("Encounter ID is required").Build()
}
return s.repo.GetByID(ctx, id)
}
func (s *service) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
// Bisa ditambahkan validasi query params wajib di sini jika perlu
return s.repo.Search(ctx, queryParams)
}
func (s *service) SyncFromSIMRS(ctx context.Context, idxdaftar int64) (*satusehat.FHIRResponse, error) {
// 1. Ambil data dari database SIMRS
pendaftaran, err := s.repo.GetPendaftaranByID(ctx, idxdaftar)
if err != nil {
return nil, fmt.Errorf("gagal mengambil data pendaftaran: %w", err)
}
// 2. Ambil Organization ID RS Anda (Bisa dari environment variable atau parameter config)
orgID := os.Getenv("SATUSEHAT_ORGANIZATION_ID") // Sesuaikan bila ada package config terpisah
// 3. Persiapkan Patient ID (IHS) dengan fallback pencarian ke NIK
patientIHS := pendaftaran.PatientIHS.String
if patientIHS == "" && pendaftaran.PasienNIK.Valid && pendaftaran.PasienNIK.String != "" {
resp, err := s.repo.SearchPatientByNIK(ctx, pendaftaran.PasienNIK.String)
if err == nil && resp != nil {
patientIHS = extractIDFromBundle(resp.RawResponse)
}
}
if patientIHS == "" { // Fallback terakhir (jaga-jaga agar mapping tak panic)
patientIHS = pendaftaran.NoMR.String
}
// 4. Persiapkan Practitioner ID (IHS) dengan fallback pencarian ke NIK
practitionerIHS := pendaftaran.PractitionerIHS.String
if practitionerIHS == "" && pendaftaran.DokterNIK.Valid && pendaftaran.DokterNIK.String != "" {
resp, err := s.repo.SearchPractitionerByNIK(ctx, pendaftaran.DokterNIK.String)
if err == nil && resp != nil {
practitionerIHS = extractIDFromBundle(resp.RawResponse)
}
}
if practitionerIHS == "" { // Fallback terakhir
practitionerIHS = pendaftaran.DokterIDHFIS.String
}
// 5. Mapping data database ke struktur Request Encounter API
req := MapPendaftaranToRequest(*pendaftaran, orgID, patientIHS, practitionerIHS)
// 6. Proses Create/Kirim ke Satu Sehat menggunakan fungsi Create yang sudah ada
return s.Create(ctx, req)
}
// extractIDFromBundle mem-parsing raw response FHIR Bundle untuk mengambil ID resource pertama
func extractIDFromBundle(data []byte) string {
var bundle struct {
Entry []struct {
Resource struct {
ID string `json:"id"`
} `json:"resource"`
} `json:"entry"`
}
if err := json.Unmarshal(data, &bundle); err == nil && len(bundle.Entry) > 0 {
return bundle.Entry[0].Resource.ID
}
return ""
}
@@ -0,0 +1,14 @@
package episodeofcare
import "time"
type EpisodeOfCareRequest struct {
PatientID string `json:"patient_id" binding:"required"`
PatientName string `json:"patient_name" binding:"required"`
OrganizationID string `json:"organization_id" binding:"required"`
Status string `json:"status" binding:"required,oneof=planned waitlist active onhold finished cancelled entered-in-error"` // planned, waitlist, active, onhold, finished, cancelled, entered-in-error
PeriodStart time.Time `json:"period_start" binding:"required"`
PeriodEnd *time.Time `json:"period_end,omitempty"`
}
type EpisodeOfCarePatchRequest []map[string]interface{}
@@ -0,0 +1,30 @@
package episodeofcare
import (
"time"
"service/internal/interfaces/satusehat"
)
func MapRequestToFHIR(req EpisodeOfCareRequest) satusehat.FHIRPayload {
payload := satusehat.NewFHIRPayload("EpisodeOfCare").
Set("status", req.Status).
Set("patient", map[string]interface{}{
"reference": "Patient/" + req.PatientID,
"display": req.PatientName,
})
if req.OrganizationID != "" {
payload.Set("managingOrganization", map[string]interface{}{
"reference": "Organization/" + req.OrganizationID,
})
}
period := map[string]interface{}{"start": req.PeriodStart.Format(time.RFC3339)}
if req.PeriodEnd != nil {
period["end"] = req.PeriodEnd.Format(time.RFC3339)
}
payload.Set("period", period)
return payload
}
@@ -0,0 +1,74 @@
package episodeofcare
import (
"context"
"encoding/json"
"fmt"
"net/url"
"service/internal/interfaces/satusehat"
)
// Repository mendefinisikan kontrak untuk operasi penyimpanan data EpisodeOfCare.
type Repository interface {
Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, payload EpisodeOfCarePatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type repository struct {
client satusehat.SatuSehatClient
}
// NewRepository membuat repositori EpisodeOfCare baru.
func NewRepository(client satusehat.SatuSehatClient) Repository {
return &repository{client: client}
}
func (r *repository) executeRequest(ctx context.Context, method, endpoint string, req interface{}) (*satusehat.FHIRResponse, error) {
resp, err := r.client.DoRequest(ctx, method, endpoint, req)
if err != nil {
return nil, err
}
var result map[string]interface{}
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
var resourceID string
if id, ok := result["id"].(string); ok {
resourceID = id
}
return &satusehat.FHIRResponse{
ID: resourceID,
FullResponse: result,
RawResponse: resp,
}, nil
}
func (r *repository) Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "POST", "/EpisodeOfCare", payload)
}
func (r *repository) Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/EpisodeOfCare/%s", id)
return r.executeRequest(ctx, "PUT", endpoint, payload)
}
func (r *repository) Patch(ctx context.Context, id string, payload EpisodeOfCarePatchRequest) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/EpisodeOfCare/%s", id)
return r.executeRequest(ctx, "PATCH", endpoint, payload)
}
func (r *repository) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/EpisodeOfCare/%s", id)
return r.executeRequest(ctx, "GET", endpoint, nil)
}
func (r *repository) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/EpisodeOfCare?%s", queryParams.Encode())
return r.executeRequest(ctx, "GET", endpoint, nil)
}
@@ -0,0 +1,63 @@
package episodeofcare
import (
"context"
"net/url"
"service/internal/interfaces/satusehat"
"service/pkg/errors"
)
// Service mendefinisikan kontrak untuk logika bisnis EpisodeOfCare.
type Service interface {
Create(ctx context.Context, req EpisodeOfCareRequest) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, req EpisodeOfCareRequest) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req EpisodeOfCarePatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type service struct {
repo Repository
}
// NewService membuat service EpisodeOfCare baru.
func NewService(repo Repository) Service {
return &service{repo: repo}
}
func (s *service) Create(ctx context.Context, req EpisodeOfCareRequest) (*satusehat.FHIRResponse, error) {
fhirPayload := MapRequestToFHIR(req)
return s.repo.Create(ctx, fhirPayload)
}
func (s *service) Update(ctx context.Context, id string, req EpisodeOfCareRequest) (*satusehat.FHIRResponse, error) {
if id == "" {
return nil, errors.NewValidationError().Message("EpisodeOfCare ID is required").Build()
}
fhirPayload := MapRequestToFHIR(req)
fhirPayload.Set("id", id)
return s.repo.Update(ctx, id, fhirPayload)
}
func (s *service) Patch(ctx context.Context, id string, req EpisodeOfCarePatchRequest) (*satusehat.FHIRResponse, error) {
if id == "" {
return nil, errors.NewValidationError().Message("EpisodeOfCare 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("EpisodeOfCare ID is required").Build()
}
return s.repo.GetByID(ctx, id)
}
func (s *service) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
return s.repo.Search(ctx, queryParams)
}
@@ -0,0 +1,20 @@
package imagingstudy
import "time"
type ImagingStudyRequest struct {
PatientID string `json:"patient_id"`
PatientName string `json:"patient_name"`
EncounterID string `json:"encounter_id"`
PractitionerID string `json:"practitioner_id"`
PractitionerName string `json:"practitioner_name"`
Status string `json:"status"` // registered, available, cancelled, entered-in-error, unknown
Started time.Time `json:"started"`
NumberOfSeries int `json:"number_of_series"`
NumberOfInstances int `json:"number_of_instances"`
ProcedureCode string `json:"procedure_code"` // SNOMED CT
ProcedureDisplay string `json:"procedure_display"`
Description string `json:"description"`
}
type ImagingStudyPatchRequest []map[string]interface{}
@@ -0,0 +1,39 @@
package imagingstudy
import (
"time"
"service/internal/interfaces/satusehat"
)
func MapRequestToFHIR(req ImagingStudyRequest) satusehat.FHIRPayload {
payload := satusehat.NewFHIRPayload("ImagingStudy").
Set("status", req.Status).
Set("subject", map[string]interface{}{
"reference": "Patient/" + req.PatientID,
"display": req.PatientName,
}).
Set("encounter", map[string]interface{}{
"reference": "Encounter/" + req.EncounterID,
}).
Set("started", req.Started.Format(time.RFC3339)).
Set("numberOfSeries", req.NumberOfSeries).
Set("numberOfInstances", req.NumberOfInstances).
Set("description", req.Description)
if req.ProcedureCode != "" {
payload.Set("procedureCode", []map[string]interface{}{
{
"coding": []map[string]interface{}{
{
"system": "http://snomed.info/sct",
"code": req.ProcedureCode,
"display": req.ProcedureDisplay,
},
},
},
})
}
return payload
}
@@ -0,0 +1,74 @@
package imagingstudy
import (
"context"
"encoding/json"
"fmt"
"net/url"
"service/internal/interfaces/satusehat"
)
// Repository mendefinisikan kontrak untuk operasi penyimpanan data ImagingStudy.
type Repository interface {
Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, payload ImagingStudyPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type repository struct {
client satusehat.SatuSehatClient
}
// NewRepository membuat repositori ImagingStudy baru.
func NewRepository(client satusehat.SatuSehatClient) Repository {
return &repository{client: client}
}
func (r *repository) executeRequest(ctx context.Context, method, endpoint string, req interface{}) (*satusehat.FHIRResponse, error) {
resp, err := r.client.DoRequest(ctx, method, endpoint, req)
if err != nil {
return nil, err
}
var result map[string]interface{}
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
var resourceID string
if id, ok := result["id"].(string); ok {
resourceID = id
}
return &satusehat.FHIRResponse{
ID: resourceID,
FullResponse: result,
RawResponse: resp,
}, nil
}
func (r *repository) Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "POST", "/ImagingStudy", payload)
}
func (r *repository) Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/ImagingStudy/%s", id)
return r.executeRequest(ctx, "PUT", endpoint, payload)
}
func (r *repository) Patch(ctx context.Context, id string, payload ImagingStudyPatchRequest) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/ImagingStudy/%s", id)
return r.executeRequest(ctx, "PATCH", endpoint, payload)
}
func (r *repository) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/ImagingStudy/%s", id)
return r.executeRequest(ctx, "GET", endpoint, nil)
}
func (r *repository) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/ImagingStudy?%s", queryParams.Encode())
return r.executeRequest(ctx, "GET", endpoint, nil)
}
@@ -0,0 +1,63 @@
package imagingstudy
import (
"context"
"net/url"
"service/internal/interfaces/satusehat"
"service/pkg/errors"
)
// Service mendefinisikan kontrak untuk logika bisnis ImagingStudy.
type Service interface {
Create(ctx context.Context, req ImagingStudyRequest) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, req ImagingStudyRequest) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req ImagingStudyPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type service struct {
repo Repository
}
// NewService membuat service ImagingStudy baru.
func NewService(repo Repository) Service {
return &service{repo: repo}
}
func (s *service) Create(ctx context.Context, req ImagingStudyRequest) (*satusehat.FHIRResponse, error) {
fhirPayload := MapRequestToFHIR(req)
return s.repo.Create(ctx, fhirPayload)
}
func (s *service) Update(ctx context.Context, id string, req ImagingStudyRequest) (*satusehat.FHIRResponse, error) {
if id == "" {
return nil, errors.NewValidationError().Message("ImagingStudy ID is required").Build()
}
fhirPayload := MapRequestToFHIR(req)
fhirPayload.Set("id", id)
return s.repo.Update(ctx, id, fhirPayload)
}
func (s *service) Patch(ctx context.Context, id string, req ImagingStudyPatchRequest) (*satusehat.FHIRResponse, error) {
if id == "" {
return nil, errors.NewValidationError().Message("ImagingStudy 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("ImagingStudy ID is required").Build()
}
return s.repo.GetByID(ctx, id)
}
func (s *service) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
return s.repo.Search(ctx, queryParams)
}
@@ -0,0 +1,19 @@
package immunization
import "time"
type ImmunizationRequest 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=completed entered-in-error not-done"`
VaccineCode string `json:"vaccine_code" binding:"required"` // KFA Code for Vaccine
VaccineDisplay string `json:"vaccine_display" binding:"required"`
OccurrenceDateTime time.Time `json:"occurrence_date_time" binding:"required"`
PrimarySource bool `json:"primary_source"`
LotNumber string `json:"lot_number,omitempty"`
PractitionerID string `json:"practitioner_id" binding:"required"`
PractitionerName string `json:"practitioner_name" binding:"required"`
}
type ImmunizationPatchRequest []map[string]interface{}
@@ -0,0 +1,33 @@
package immunization
import (
"service/internal/interfaces/satusehat"
"time"
)
func MapRequestToFHIR(req ImmunizationRequest) satusehat.FHIRPayload {
payload := satusehat.NewFHIRPayload("Immunization").
Set("status", req.Status).
Set("vaccineCode", map[string]interface{}{
"coding": []map[string]interface{}{
{"system": "http://sys-ids.kemkes.go.id/kfa", "code": req.VaccineCode, "display": req.VaccineDisplay},
},
}).
Set("patient", map[string]interface{}{"reference": "Patient/" + req.PatientID, "display": req.PatientName}).
Set("encounter", map[string]interface{}{"reference": "Encounter/" + req.EncounterID}).
Set("occurrenceDateTime", req.OccurrenceDateTime.Format(time.RFC3339)).
Set("primarySource", req.PrimarySource).
Set("performer", []map[string]interface{}{
{
"actor": map[string]interface{}{
"reference": "Practitioner/" + req.PractitionerID,
"display": req.PractitionerName,
},
},
})
if req.LotNumber != "" {
payload.Set("lotNumber", req.LotNumber)
}
return payload
}
@@ -0,0 +1,51 @@
package immunization
import (
"context"
"encoding/json"
"fmt"
"net/url"
"service/internal/interfaces/satusehat"
)
type Repository interface {
Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req ImmunizationPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type repository struct{ client satusehat.SatuSehatClient }
func NewRepository(client satusehat.SatuSehatClient) Repository { return &repository{client: client} }
func (r *repository) executeRequest(ctx context.Context, method, endpoint string, req interface{}) (*satusehat.FHIRResponse, error) {
resp, err := r.client.DoRequest(ctx, method, endpoint, req)
if err != nil {
return nil, err
}
var result map[string]interface{}
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
var resourceID string
if id, ok := result["id"].(string); ok {
resourceID = id
}
return &satusehat.FHIRResponse{ID: resourceID, FullResponse: result, RawResponse: resp}, nil
}
func (r *repository) Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "POST", "/Immunization", payload)
}
func (r *repository) Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "PUT", fmt.Sprintf("/Immunization/%s", id), payload)
}
func (r *repository) Patch(ctx context.Context, id string, req ImmunizationPatchRequest) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "PATCH", fmt.Sprintf("/Immunization/%s", id), req)
}
func (r *repository) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "GET", fmt.Sprintf("/Immunization/%s", id), nil)
}
func (r *repository) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "GET", fmt.Sprintf("/Immunization?%s", queryParams.Encode()), nil)
}
@@ -0,0 +1,37 @@
package immunization
import (
"context"
"net/url"
"service/internal/interfaces/satusehat"
"service/pkg/errors"
)
type Service interface {
Create(ctx context.Context, req ImmunizationRequest) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, req ImmunizationRequest) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req ImmunizationPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type service struct{ repo Repository }
func NewService(repo Repository) Service { return &service{repo: repo} }
func (s *service) Create(ctx context.Context, req ImmunizationRequest) (*satusehat.FHIRResponse, error) {
return s.repo.Create(ctx, MapRequestToFHIR(req))
}
func (s *service) Update(ctx context.Context, id string, req ImmunizationRequest) (*satusehat.FHIRResponse, error) {
if id == "" {
return nil, errors.NewValidationError().Message("ID is required").Build()
}
return s.repo.Update(ctx, id, MapRequestToFHIR(req).Set("id", id))
}
func (s *service) Patch(ctx context.Context, id string, req ImmunizationPatchRequest) (*satusehat.FHIRResponse, error) {
return s.repo.Patch(ctx, id, req)
}
func (s *service) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
return s.repo.GetByID(ctx, id)
}
func (s *service) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
return s.repo.Search(ctx, queryParams)
}
@@ -0,0 +1,16 @@
package medication
import "time"
type MedicationRequest struct {
StatusCode string `json:"status_code" binding:"required,oneof=active inactive entered-in-error"`
KfaCode string `json:"kfa_code" binding:"required"`
KfaDisplay string `json:"kfa_display" binding:"required"`
FormCode string `json:"form_code,omitempty"`
FormDisplay string `json:"form_display,omitempty"`
ManufacturerID string `json:"manufacturer_id,omitempty"` // Reference ke ID Organization (Pabrik/Distributor)
BatchNumber string `json:"batch_number,omitempty"`
ExpirationDate *time.Time `json:"expiration_date,omitempty"`
}
type MedicationPatchRequest []map[string]interface{}
@@ -0,0 +1,48 @@
package medication
import (
"time"
"service/internal/interfaces/satusehat"
)
func MapRequestToFHIR(req MedicationRequest) satusehat.FHIRPayload {
payload := satusehat.NewFHIRPayload("Medication").
Set("status", req.StatusCode).
Set("code", map[string]interface{}{
"coding": []map[string]interface{}{
{
"system": "http://sys-ids.kemkes.go.id/kfa",
"code": req.KfaCode,
"display": req.KfaDisplay,
},
},
})
if req.FormCode != "" {
payload.Set("form", map[string]interface{}{
"coding": []map[string]interface{}{
{
"system": "http://terminology.kemkes.go.id/CodeSystem/medication-form",
"code": req.FormCode,
"display": req.FormDisplay,
},
},
})
}
if req.ManufacturerID != "" {
payload.Set("manufacturer", map[string]interface{}{
"reference": "Organization/" + req.ManufacturerID,
})
}
if req.BatchNumber != "" && req.ExpirationDate != nil {
payload.Set("batch", map[string]interface{}{
"lotNumber": req.BatchNumber,
"expirationDate": req.ExpirationDate.Format(time.RFC3339),
})
}
return payload
}
@@ -0,0 +1,65 @@
package medication
import (
"context"
"encoding/json"
"fmt"
"net/url"
"service/internal/interfaces/satusehat"
)
type Repository interface {
Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req MedicationPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type repository struct {
client satusehat.SatuSehatClient
}
func NewRepository(client satusehat.SatuSehatClient) Repository {
return &repository{client: client}
}
func (r *repository) executeRequest(ctx context.Context, method, endpoint string, req interface{}) (*satusehat.FHIRResponse, error) {
resp, err := r.client.DoRequest(ctx, method, endpoint, req)
if err != nil {
return nil, err
}
var result map[string]interface{}
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
var resourceID string
if id, ok := result["id"].(string); ok {
resourceID = id
}
return &satusehat.FHIRResponse{
ID: resourceID,
FullResponse: result,
RawResponse: resp,
}, nil
}
func (r *repository) Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "POST", "/Medication", payload)
}
func (r *repository) Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "PUT", fmt.Sprintf("/Medication/%s", id), payload)
}
func (r *repository) Patch(ctx context.Context, id string, req MedicationPatchRequest) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "PATCH", fmt.Sprintf("/Medication/%s", id), req)
}
func (r *repository) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "GET", fmt.Sprintf("/Medication/%s", id), nil)
}
func (r *repository) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "GET", fmt.Sprintf("/Medication?%s", queryParams.Encode()), nil)
}
@@ -0,0 +1,57 @@
package medication
import (
"context"
"net/url"
"service/internal/interfaces/satusehat"
"service/pkg/errors"
)
type Service interface {
Create(ctx context.Context, req MedicationRequest) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, req MedicationRequest) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req MedicationPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type service struct {
repo Repository
}
func NewService(repo Repository) Service {
return &service{repo: repo}
}
func (s *service) Create(ctx context.Context, req MedicationRequest) (*satusehat.FHIRResponse, error) {
fhirPayload := MapRequestToFHIR(req)
return s.repo.Create(ctx, fhirPayload)
}
func (s *service) Update(ctx context.Context, id string, req MedicationRequest) (*satusehat.FHIRResponse, error) {
if id == "" {
return nil, errors.NewValidationError().Message("Medication ID is required").Build()
}
fhirPayload := MapRequestToFHIR(req)
fhirPayload.Set("id", id)
return s.repo.Update(ctx, id, fhirPayload)
}
func (s *service) Patch(ctx context.Context, id string, req MedicationPatchRequest) (*satusehat.FHIRResponse, error) {
if id == "" {
return nil, errors.NewValidationError().Message("Medication 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) {
return s.repo.GetByID(ctx, id)
}
func (s *service) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
return s.repo.Search(ctx, queryParams)
}
@@ -0,0 +1,24 @@
package medicationdispense
import "time"
type MedicationDispenseRequest struct {
PatientID string `json:"patient_id" binding:"required"`
PatientName string `json:"patient_name" binding:"required"`
EncounterID string `json:"encounter_id" binding:"required"`
MedicationID string `json:"medication_id" binding:"required"`
MedicationDisplay string `json:"medication_display" binding:"required"`
PractitionerID string `json:"practitioner_id" binding:"required"`
PractitionerName string `json:"practitioner_name" binding:"required"`
LocationID string `json:"location_id" binding:"required"`
LocationName string `json:"location_name" binding:"required"`
PrescriptionID string `json:"prescription_id" binding:"required"` // Reference to MedicationRequest
Status string `json:"status" binding:"required,oneof=preparation in-progress cancelled on-hold completed entered-in-error stopped declined unknown"`
PreparedDate time.Time `json:"prepared_date" binding:"required"`
HandedOverDate time.Time `json:"handed_over_date" binding:"required"`
DispenseValue float64 `json:"dispense_value" binding:"required"`
DispenseUnit string `json:"dispense_unit" binding:"required"`
DosagePatientInstr string `json:"dosage_patient_instruction"`
}
type MedicationDispensePatchRequest []map[string]interface{}
@@ -0,0 +1,51 @@
package medicationdispense
import (
"time"
"service/internal/interfaces/satusehat"
)
func MapRequestToFHIR(req MedicationDispenseRequest) satusehat.FHIRPayload {
payload := satusehat.NewFHIRPayload("MedicationDispense").
Set("status", req.Status).
Set("category", map[string]interface{}{
"coding": []map[string]interface{}{
{
"system": "http://terminology.hl7.org/fhir/CodeSystem/medicationdispense-category",
"code": "outpatient",
"display": "Outpatient",
},
},
}).
Set("medicationReference", map[string]interface{}{
"reference": "Medication/" + req.MedicationID,
"display": req.MedicationDisplay,
}).
Set("subject", map[string]interface{}{
"reference": "Patient/" + req.PatientID,
"display": req.PatientName,
}).
Set("context", map[string]interface{}{
"reference": "Encounter/" + req.EncounterID,
}).
Set("performer", []map[string]interface{}{
{
"actor": map[string]interface{}{
"reference": "Practitioner/" + req.PractitionerID,
"display": req.PractitionerName,
},
},
}).
Set("location", map[string]interface{}{
"reference": "Location/" + req.LocationID,
"display": req.LocationName,
}).
Set("authorizingPrescription", []map[string]interface{}{
{"reference": "MedicationRequest/" + req.PrescriptionID},
}).
Set("whenPrepared", req.PreparedDate.Format(time.RFC3339)).
Set("whenHandedOver", req.HandedOverDate.Format(time.RFC3339))
return payload
}
@@ -0,0 +1,58 @@
package medicationdispense
import (
"context"
"encoding/json"
"fmt"
"net/url"
"service/internal/interfaces/satusehat"
)
type Repository interface {
Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req MedicationDispensePatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type repository struct {
client satusehat.SatuSehatClient
}
func NewRepository(client satusehat.SatuSehatClient) Repository {
return &repository{client: client}
}
func (r *repository) executeRequest(ctx context.Context, method, endpoint string, req interface{}) (*satusehat.FHIRResponse, error) {
resp, err := r.client.DoRequest(ctx, method, endpoint, req)
if err != nil {
return nil, err
}
var result map[string]interface{}
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
var resourceID string
if id, ok := result["id"].(string); ok {
resourceID = id
}
return &satusehat.FHIRResponse{ID: resourceID, FullResponse: result, RawResponse: resp}, nil
}
func (r *repository) Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "POST", "/MedicationDispense", payload)
}
func (r *repository) Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "PUT", fmt.Sprintf("/MedicationDispense/%s", id), payload)
}
func (r *repository) Patch(ctx context.Context, id string, req MedicationDispensePatchRequest) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "PATCH", fmt.Sprintf("/MedicationDispense/%s", id), req)
}
func (r *repository) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "GET", fmt.Sprintf("/MedicationDispense/%s", id), nil)
}
func (r *repository) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "GET", fmt.Sprintf("/MedicationDispense?%s", queryParams.Encode()), nil)
}
@@ -0,0 +1,46 @@
package medicationdispense
import (
"context"
"net/url"
"service/internal/interfaces/satusehat"
"service/pkg/errors"
)
type Service interface {
Create(ctx context.Context, req MedicationDispenseRequest) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, req MedicationDispenseRequest) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req MedicationDispensePatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type service struct {
repo Repository
}
func NewService(repo Repository) Service {
return &service{repo: repo}
}
func (s *service) Create(ctx context.Context, req MedicationDispenseRequest) (*satusehat.FHIRResponse, error) {
return s.repo.Create(ctx, MapRequestToFHIR(req))
}
func (s *service) Update(ctx context.Context, id string, req MedicationDispenseRequest) (*satusehat.FHIRResponse, error) {
if id == "" {
return nil, errors.NewValidationError().Message("MedicationDispense ID is required").Build()
}
payload := MapRequestToFHIR(req)
payload.Set("id", id)
return s.repo.Update(ctx, id, payload)
}
func (s *service) Patch(ctx context.Context, id string, req MedicationDispensePatchRequest) (*satusehat.FHIRResponse, error) {
return s.repo.Patch(ctx, id, req)
}
func (s *service) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
return s.repo.GetByID(ctx, id)
}
func (s *service) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
return s.repo.Search(ctx, queryParams)
}
@@ -0,0 +1,23 @@
package medicationrequest
import "time"
type MedicationRequestRequest struct {
PatientID string `json:"patient_id" binding:"required"`
PatientName string `json:"patient_name" binding:"required"`
EncounterID string `json:"encounter_id" binding:"required"`
PractitionerID string `json:"practitioner_id" binding:"required"`
PractitionerName string `json:"practitioner_name" binding:"required"`
MedicationID string `json:"medication_id" binding:"required"` // Reference to Medication resource
MedicationDisplay string `json:"medication_display" binding:"required"`
Status string `json:"status" binding:"required,oneof=active on-hold cancelled completed entered-in-error stopped draft unknown"`
Intent string `json:"intent" binding:"required,oneof=proposal plan order original-order reflex-order filler-order instance-order option"`
AuthoredOn time.Time `json:"authored_on" binding:"required"`
DosageText string `json:"dosage_text" binding:"required"`
PatientInstr string `json:"patient_instruction"`
DispenseValue float64 `json:"dispense_value" binding:"required"`
DispenseUnit string `json:"dispense_unit" binding:"required"`
SupplyDuration int `json:"supply_duration" binding:"required"` // in days
}
type MedicationRequestPatchRequest []map[string]interface{}
@@ -0,0 +1,60 @@
package medicationrequest
import (
"time"
"service/internal/interfaces/satusehat"
)
func MapRequestToFHIR(req MedicationRequestRequest) satusehat.FHIRPayload {
payload := satusehat.NewFHIRPayload("MedicationRequest").
Set("status", req.Status).
Set("intent", req.Intent).
Set("category", []map[string]interface{}{
{
"coding": []map[string]interface{}{
{
"system": "http://terminology.hl7.org/CodeSystem/medicationrequest-category",
"code": "outpatient",
"display": "Outpatient",
},
},
},
}).
Set("medicationReference", map[string]interface{}{
"reference": "Medication/" + req.MedicationID,
"display": req.MedicationDisplay,
}).
Set("subject", map[string]interface{}{
"reference": "Patient/" + req.PatientID,
"display": req.PatientName,
}).
Set("encounter", map[string]interface{}{
"reference": "Encounter/" + req.EncounterID,
}).
Set("authoredOn", req.AuthoredOn.Format(time.RFC3339)).
Set("requester", map[string]interface{}{
"reference": "Practitioner/" + req.PractitionerID,
"display": req.PractitionerName,
}).
Set("dosageInstruction", []map[string]interface{}{
{
"sequence": 1,
"text": req.DosageText,
"patientInstruction": req.PatientInstr,
},
}).
Set("dispenseRequest", map[string]interface{}{
"quantity": map[string]interface{}{
"value": req.DispenseValue,
"code": req.DispenseUnit,
},
"expectedSupplyDuration": map[string]interface{}{
"value": req.SupplyDuration,
"unit": "days",
"system": "http://unitsofmeasure.org",
"code": "d",
},
})
return payload
}
@@ -0,0 +1,58 @@
package medicationrequest
import (
"context"
"encoding/json"
"fmt"
"net/url"
"service/internal/interfaces/satusehat"
)
type Repository interface {
Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req MedicationRequestPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type repository struct {
client satusehat.SatuSehatClient
}
func NewRepository(client satusehat.SatuSehatClient) Repository {
return &repository{client: client}
}
func (r *repository) executeRequest(ctx context.Context, method, endpoint string, req interface{}) (*satusehat.FHIRResponse, error) {
resp, err := r.client.DoRequest(ctx, method, endpoint, req)
if err != nil {
return nil, err
}
var result map[string]interface{}
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
var resourceID string
if id, ok := result["id"].(string); ok {
resourceID = id
}
return &satusehat.FHIRResponse{ID: resourceID, FullResponse: result, RawResponse: resp}, nil
}
func (r *repository) Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "POST", "/MedicationRequest", payload)
}
func (r *repository) Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "PUT", fmt.Sprintf("/MedicationRequest/%s", id), payload)
}
func (r *repository) Patch(ctx context.Context, id string, req MedicationRequestPatchRequest) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "PATCH", fmt.Sprintf("/MedicationRequest/%s", id), req)
}
func (r *repository) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "GET", fmt.Sprintf("/MedicationRequest/%s", id), nil)
}
func (r *repository) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "GET", fmt.Sprintf("/MedicationRequest?%s", queryParams.Encode()), nil)
}
@@ -0,0 +1,46 @@
package medicationrequest
import (
"context"
"net/url"
"service/internal/interfaces/satusehat"
"service/pkg/errors"
)
type Service interface {
Create(ctx context.Context, req MedicationRequestRequest) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, req MedicationRequestRequest) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req MedicationRequestPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type service struct {
repo Repository
}
func NewService(repo Repository) Service {
return &service{repo: repo}
}
func (s *service) Create(ctx context.Context, req MedicationRequestRequest) (*satusehat.FHIRResponse, error) {
return s.repo.Create(ctx, MapRequestToFHIR(req))
}
func (s *service) Update(ctx context.Context, id string, req MedicationRequestRequest) (*satusehat.FHIRResponse, error) {
if id == "" {
return nil, errors.NewValidationError().Message("MedicationRequest ID is required").Build()
}
payload := MapRequestToFHIR(req)
payload.Set("id", id)
return s.repo.Update(ctx, id, payload)
}
func (s *service) Patch(ctx context.Context, id string, req MedicationRequestPatchRequest) (*satusehat.FHIRResponse, error) {
return s.repo.Patch(ctx, id, req)
}
func (s *service) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
return s.repo.GetByID(ctx, id)
}
func (s *service) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
return s.repo.Search(ctx, queryParams)
}
@@ -0,0 +1,18 @@
package medicationstatement
import "time"
type MedicationStatementRequest 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=active completed entered-in-error intended stopped on-hold unknown not-taken"`
CategoryCode string `json:"category_code" binding:"required,oneof=inpatient outpatient community"`
MedicationCode string `json:"medication_code" binding:"required"` // KFA Code
MedicationDisplay string `json:"medication_display" binding:"required"` // KFA Name
EffectiveDateTime time.Time `json:"effective_date_time" binding:"required"`
DateAsserted time.Time `json:"date_asserted" binding:"required"`
DosageText string `json:"dosage_text" binding:"required"`
}
type MedicationStatementPatchRequest []map[string]interface{}
@@ -0,0 +1,50 @@
package medicationstatement
import (
"time"
"service/internal/interfaces/satusehat"
)
func MapRequestToFHIR(req MedicationStatementRequest) satusehat.FHIRPayload {
payload := satusehat.NewFHIRPayload("MedicationStatement").
Set("status", req.Status).
Set("category", map[string]interface{}{
"coding": []map[string]interface{}{
{
"system": "http://terminology.hl7.org/CodeSystem/medication-statement-category",
"code": req.CategoryCode,
"display": req.CategoryCode,
},
},
}).
Set("medicationCodeableConcept", map[string]interface{}{
"coding": []map[string]interface{}{
{
"system": "http://sys-ids.kemkes.go.id/kfa",
"code": req.MedicationCode,
"display": req.MedicationDisplay,
},
},
}).
Set("subject", map[string]interface{}{
"reference": "Patient/" + req.PatientID,
"display": req.PatientName,
}).
Set("context", map[string]interface{}{
"reference": "Encounter/" + req.EncounterID,
}).
Set("informationSource", map[string]interface{}{
"reference": "Patient/" + req.PatientID,
"display": req.PatientName,
}).
Set("effectiveDateTime", req.EffectiveDateTime.Format(time.RFC3339)).
Set("dateAsserted", req.DateAsserted.Format(time.RFC3339)).
Set("dosage", []map[string]interface{}{
{
"text": req.DosageText,
},
})
return payload
}
@@ -0,0 +1,58 @@
package medicationstatement
import (
"context"
"encoding/json"
"fmt"
"net/url"
"service/internal/interfaces/satusehat"
)
type Repository interface {
Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req MedicationStatementPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type repository struct {
client satusehat.SatuSehatClient
}
func NewRepository(client satusehat.SatuSehatClient) Repository {
return &repository{client: client}
}
func (r *repository) executeRequest(ctx context.Context, method, endpoint string, req interface{}) (*satusehat.FHIRResponse, error) {
resp, err := r.client.DoRequest(ctx, method, endpoint, req)
if err != nil {
return nil, err
}
var result map[string]interface{}
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
var resourceID string
if id, ok := result["id"].(string); ok {
resourceID = id
}
return &satusehat.FHIRResponse{ID: resourceID, FullResponse: result, RawResponse: resp}, nil
}
func (r *repository) Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "POST", "/MedicationStatement", payload)
}
func (r *repository) Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "PUT", fmt.Sprintf("/MedicationStatement/%s", id), payload)
}
func (r *repository) Patch(ctx context.Context, id string, req MedicationStatementPatchRequest) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "PATCH", fmt.Sprintf("/MedicationStatement/%s", id), req)
}
func (r *repository) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "GET", fmt.Sprintf("/MedicationStatement/%s", id), nil)
}
func (r *repository) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "GET", fmt.Sprintf("/MedicationStatement?%s", queryParams.Encode()), nil)
}
@@ -0,0 +1,46 @@
package medicationstatement
import (
"context"
"net/url"
"service/internal/interfaces/satusehat"
"service/pkg/errors"
)
type Service interface {
Create(ctx context.Context, req MedicationStatementRequest) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, req MedicationStatementRequest) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req MedicationStatementPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type service struct {
repo Repository
}
func NewService(repo Repository) Service {
return &service{repo: repo}
}
func (s *service) Create(ctx context.Context, req MedicationStatementRequest) (*satusehat.FHIRResponse, error) {
return s.repo.Create(ctx, MapRequestToFHIR(req))
}
func (s *service) Update(ctx context.Context, id string, req MedicationStatementRequest) (*satusehat.FHIRResponse, error) {
if id == "" {
return nil, errors.NewValidationError().Message("MedicationStatement ID is required").Build()
}
payload := MapRequestToFHIR(req)
payload.Set("id", id)
return s.repo.Update(ctx, id, payload)
}
func (s *service) Patch(ctx context.Context, id string, req MedicationStatementPatchRequest) (*satusehat.FHIRResponse, error) {
return s.repo.Patch(ctx, id, req)
}
func (s *service) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
return s.repo.GetByID(ctx, id)
}
func (s *service) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
return s.repo.Search(ctx, queryParams)
}
@@ -0,0 +1,20 @@
package observation
import "time"
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"`
}
type ObservationPatchRequest []map[string]interface{}
@@ -0,0 +1,48 @@
package observation
import (
"time"
"service/internal/interfaces/satusehat"
)
func MapRequestToFHIR(req ObservationRequest) satusehat.FHIRPayload {
payload := satusehat.NewFHIRPayload("Observation").
Set("status", req.Status).
Set("category", []map[string]interface{}{
{
"coding": []map[string]interface{}{
{
"system": "http://terminology.hl7.org/CodeSystem/observation-category",
"code": req.CategoryCode,
"display": req.CategoryDisplay,
},
},
},
}).
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,
})
return payload
}
@@ -0,0 +1,72 @@
package observation
import (
"context"
"encoding/json"
"fmt"
"net/url"
"service/internal/interfaces/satusehat"
)
type Repository interface {
Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, payload ObservationPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type repository struct {
client satusehat.SatuSehatClient
}
// NewRepository membuat repositori observasi baru.
func NewRepository(client satusehat.SatuSehatClient) Repository {
return &repository{client: client}
}
func (r *repository) executeRequest(ctx context.Context, method, endpoint string, req interface{}) (*satusehat.FHIRResponse, error) {
resp, err := r.client.DoRequest(ctx, method, endpoint, req)
if err != nil {
return nil, err
}
var result map[string]interface{}
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
var resourceID string
if id, ok := result["id"].(string); ok {
resourceID = id
}
return &satusehat.FHIRResponse{
ID: resourceID,
FullResponse: result,
RawResponse: resp,
}, nil
}
func (r *repository) Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "POST", "/Observation", payload)
}
func (r *repository) Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/Observation/%s", id)
return r.executeRequest(ctx, "PUT", endpoint, payload)
}
func (r *repository) Patch(ctx context.Context, id string, payload ObservationPatchRequest) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/Observation/%s", id)
return r.executeRequest(ctx, "PATCH", endpoint, payload)
}
func (r *repository) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/Observation/%s", id)
return r.executeRequest(ctx, "GET", endpoint, nil)
}
func (r *repository) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/Observation?%s", queryParams.Encode())
return r.executeRequest(ctx, "GET", endpoint, nil)
}
@@ -0,0 +1,62 @@
package observation
import (
"context"
"net/url"
"service/internal/interfaces/satusehat"
"service/pkg/errors"
)
type Service interface {
Create(ctx context.Context, req ObservationRequest) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, req ObservationRequest) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req ObservationPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type service struct {
repo Repository
}
// NewService membuat service observasi baru.
func NewService(repo Repository) Service {
return &service{repo: repo}
}
func (s *service) Create(ctx context.Context, req ObservationRequest) (*satusehat.FHIRResponse, error) {
fhirPayload := MapRequestToFHIR(req)
return s.repo.Create(ctx, fhirPayload)
}
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)
}
func (s *service) Patch(ctx context.Context, id string, req ObservationPatchRequest) (*satusehat.FHIRResponse, error) {
if id == "" {
return nil, errors.NewValidationError().Message("Observation 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("Observation ID is required").Build()
}
return s.repo.GetByID(ctx, id)
}
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)
}
@@ -0,0 +1,19 @@
package procedure
import "time"
type ProcedureRequest 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=preparation in-progress not-done on-hold stopped completed entered-in-error unknown"` // preparation, in-progress, not-done, on-hold, stopped, completed, entered-in-error, unknown
CategoryCode string `json:"category_code" binding:"required"`
CategoryDisplay string `json:"category_display" binding:"required"`
Code string `json:"code" binding:"required"` // SNOMED-CT or ICD9CM code
Display string `json:"display" binding:"required"` // Procedure name
PractitionerID string `json:"practitioner_id" binding:"required"`
PractitionerName string `json:"practitioner_name" binding:"required"`
PerformedDateTime time.Time `json:"performed_date_time" binding:"required"`
}
type ProcedurePatchRequest []map[string]interface{}
@@ -0,0 +1,49 @@
package procedure
import (
"time"
"service/internal/interfaces/satusehat"
)
func MapRequestToFHIR(req ProcedureRequest) satusehat.FHIRPayload {
payload := satusehat.NewFHIRPayload("Procedure").
Set("status", req.Status).
Set("category", map[string]interface{}{
"coding": []map[string]interface{}{
{
"system": "http://snomed.info/sct",
"code": req.CategoryCode,
"display": req.CategoryDisplay,
},
},
}).
Set("code", map[string]interface{}{
"coding": []map[string]interface{}{
{
"system": "http://snomed.info/sct",
"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("performedDateTime", req.PerformedDateTime.Format(time.RFC3339))
if req.PractitionerID != "" {
payload.Append("performer", map[string]interface{}{
"actor": map[string]interface{}{
"reference": "Practitioner/" + req.PractitionerID,
"display": req.PractitionerName,
},
})
}
return payload
}
@@ -0,0 +1,72 @@
package procedure
import (
"context"
"encoding/json"
"fmt"
"net/url"
"service/internal/interfaces/satusehat"
)
type Repository interface {
Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, payload ProcedurePatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type repository struct {
client satusehat.SatuSehatClient
}
func NewRepository(client satusehat.SatuSehatClient) Repository {
return &repository{client: client}
}
func (r *repository) executeRequest(ctx context.Context, method, endpoint string, req interface{}) (*satusehat.FHIRResponse, error) {
resp, err := r.client.DoRequest(ctx, method, endpoint, req)
if err != nil {
return nil, err
}
var result map[string]interface{}
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
var resourceID string
if id, ok := result["id"].(string); ok {
resourceID = id
}
return &satusehat.FHIRResponse{
ID: resourceID,
FullResponse: result,
RawResponse: resp,
}, nil
}
func (r *repository) Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "POST", "/Procedure", payload)
}
func (r *repository) Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/Procedure/%s", id)
return r.executeRequest(ctx, "PUT", endpoint, payload)
}
func (r *repository) Patch(ctx context.Context, id string, payload ProcedurePatchRequest) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/Procedure/%s", id)
return r.executeRequest(ctx, "PATCH", endpoint, payload)
}
func (r *repository) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/Procedure/%s", id)
return r.executeRequest(ctx, "GET", endpoint, nil)
}
func (r *repository) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/Procedure?%s", queryParams.Encode())
return r.executeRequest(ctx, "GET", endpoint, nil)
}
@@ -0,0 +1,61 @@
package procedure
import (
"context"
"net/url"
"service/internal/interfaces/satusehat"
"service/pkg/errors"
)
type Service interface {
Create(ctx context.Context, req ProcedureRequest) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, req ProcedureRequest) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req ProcedurePatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type service struct {
repo Repository
}
func NewService(repo Repository) Service {
return &service{repo: repo}
}
func (s *service) Create(ctx context.Context, req ProcedureRequest) (*satusehat.FHIRResponse, error) {
fhirPayload := MapRequestToFHIR(req)
return s.repo.Create(ctx, fhirPayload)
}
func (s *service) Update(ctx context.Context, id string, req ProcedureRequest) (*satusehat.FHIRResponse, error) {
if id == "" {
return nil, errors.NewValidationError().Message("Procedure ID is required").Build()
}
fhirPayload := MapRequestToFHIR(req)
fhirPayload.Set("id", id)
return s.repo.Update(ctx, id, fhirPayload)
}
func (s *service) Patch(ctx context.Context, id string, req ProcedurePatchRequest) (*satusehat.FHIRResponse, error) {
if id == "" {
return nil, errors.NewValidationError().Message("Procedure 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("Procedure ID is required").Build()
}
return s.repo.GetByID(ctx, id)
}
func (s *service) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
return s.repo.Search(ctx, queryParams)
}
@@ -0,0 +1,17 @@
package questionnaireresponse
import "time"
type QuestionnaireResponseRequest struct {
QuestionnaireURL string `json:"questionnaire_url" binding:"required"` // e.g. "https://fhir.kemkes.go.id/Questionnaire/Q0007"
Status string `json:"status" binding:"required,oneof=in-progress completed amended entered-in-error stopped"`
PatientID string `json:"patient_id" binding:"required"`
PatientName string `json:"patient_name" binding:"required"`
EncounterID string `json:"encounter_id" binding:"required"`
AuthoredDate time.Time `json:"authored_date" binding:"required"`
AuthorID string `json:"author_id" binding:"required"`
AuthorName string `json:"author_name" binding:"required"`
Items []map[string]interface{} `json:"items" binding:"required"` // Raw FHIR formatted items to handle high nesting flexibility
}
type QuestionnaireResponsePatchRequest []map[string]interface{}
@@ -0,0 +1,28 @@
package questionnaireresponse
import (
"time"
"service/internal/interfaces/satusehat"
)
func MapRequestToFHIR(req QuestionnaireResponseRequest) satusehat.FHIRPayload {
payload := satusehat.NewFHIRPayload("QuestionnaireResponse").
Set("questionnaire", req.QuestionnaireURL).
Set("status", req.Status).
Set("subject", map[string]interface{}{
"reference": "Patient/" + req.PatientID,
"display": req.PatientName,
}).
Set("encounter", map[string]interface{}{
"reference": "Encounter/" + req.EncounterID,
}).
Set("authored", req.AuthoredDate.Format(time.RFC3339)).
Set("author", map[string]interface{}{
"reference": "Practitioner/" + req.AuthorID,
"display": req.AuthorName,
}).
Set("item", req.Items)
return payload
}
@@ -0,0 +1,58 @@
package questionnaireresponse
import (
"context"
"encoding/json"
"fmt"
"net/url"
"service/internal/interfaces/satusehat"
)
type Repository interface {
Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req QuestionnaireResponsePatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type repository struct {
client satusehat.SatuSehatClient
}
func NewRepository(client satusehat.SatuSehatClient) Repository {
return &repository{client: client}
}
func (r *repository) executeRequest(ctx context.Context, method, endpoint string, req interface{}) (*satusehat.FHIRResponse, error) {
resp, err := r.client.DoRequest(ctx, method, endpoint, req)
if err != nil {
return nil, err
}
var result map[string]interface{}
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
var resourceID string
if id, ok := result["id"].(string); ok {
resourceID = id
}
return &satusehat.FHIRResponse{ID: resourceID, FullResponse: result, RawResponse: resp}, nil
}
func (r *repository) Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "POST", "/QuestionnaireResponse", payload)
}
func (r *repository) Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "PUT", fmt.Sprintf("/QuestionnaireResponse/%s", id), payload)
}
func (r *repository) Patch(ctx context.Context, id string, req QuestionnaireResponsePatchRequest) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "PATCH", fmt.Sprintf("/QuestionnaireResponse/%s", id), req)
}
func (r *repository) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "GET", fmt.Sprintf("/QuestionnaireResponse/%s", id), nil)
}
func (r *repository) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "GET", fmt.Sprintf("/QuestionnaireResponse?%s", queryParams.Encode()), nil)
}
@@ -0,0 +1,46 @@
package questionnaireresponse
import (
"context"
"net/url"
"service/internal/interfaces/satusehat"
"service/pkg/errors"
)
type Service interface {
Create(ctx context.Context, req QuestionnaireResponseRequest) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, req QuestionnaireResponseRequest) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req QuestionnaireResponsePatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type service struct {
repo Repository
}
func NewService(repo Repository) Service {
return &service{repo: repo}
}
func (s *service) Create(ctx context.Context, req QuestionnaireResponseRequest) (*satusehat.FHIRResponse, error) {
return s.repo.Create(ctx, MapRequestToFHIR(req))
}
func (s *service) Update(ctx context.Context, id string, req QuestionnaireResponseRequest) (*satusehat.FHIRResponse, error) {
if id == "" {
return nil, errors.NewValidationError().Message("QuestionnaireResponse ID is required").Build()
}
payload := MapRequestToFHIR(req)
payload.Set("id", id)
return s.repo.Update(ctx, id, payload)
}
func (s *service) Patch(ctx context.Context, id string, req QuestionnaireResponsePatchRequest) (*satusehat.FHIRResponse, error) {
return s.repo.Patch(ctx, id, req)
}
func (s *service) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
return s.repo.GetByID(ctx, id)
}
func (s *service) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
return s.repo.Search(ctx, queryParams)
}
@@ -0,0 +1,18 @@
package servicerequest
import "time"
type ServiceRequestRequest struct {
PatientID string `json:"patient_id" binding:"required"`
PatientName string `json:"patient_name" binding:"required"`
EncounterID string `json:"encounter_id" binding:"required"`
RequesterID string `json:"requester_id" binding:"required"`
RequesterName string `json:"requester_name" binding:"required"`
Status string `json:"status" binding:"required,oneof=draft active on-hold revoked completed entered-in-error unknown"` // draft, active, on-hold, revoked, completed, entered-in-error, unknown
Intent string `json:"intent" binding:"required,oneof=proposal plan directive order original-order reflex-order filler-order instance-order option"` // proposal, plan, directive, order, original-order, reflex-order, filler-order, instance-order, option
Code string `json:"code" binding:"required"`
Display string `json:"display" binding:"required"`
AuthoredOn time.Time `json:"authored_on" binding:"required"`
}
type ServiceRequestPatchRequest []map[string]interface{}
@@ -0,0 +1,39 @@
package servicerequest
import (
"time"
"service/internal/interfaces/satusehat"
)
func MapRequestToFHIR(req ServiceRequestRequest) satusehat.FHIRPayload {
payload := satusehat.NewFHIRPayload("ServiceRequest").
Set("status", req.Status).
Set("intent", req.Intent).
Set("code", map[string]interface{}{
"coding": []map[string]interface{}{
{
"system": "http://snomed.info/sct",
"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("authoredOn", req.AuthoredOn.Format(time.RFC3339))
if req.RequesterID != "" {
payload.Set("requester", map[string]interface{}{
"reference": "Practitioner/" + req.RequesterID,
"display": req.RequesterName,
})
}
return payload
}
@@ -0,0 +1,74 @@
package servicerequest
import (
"context"
"encoding/json"
"fmt"
"net/url"
"service/internal/interfaces/satusehat"
)
// Repository mendefinisikan kontrak untuk operasi penyimpanan data ServiceRequest.
type Repository interface {
Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, payload ServiceRequestPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type repository struct {
client satusehat.SatuSehatClient
}
// NewRepository membuat repositori ServiceRequest baru.
func NewRepository(client satusehat.SatuSehatClient) Repository {
return &repository{client: client}
}
func (r *repository) executeRequest(ctx context.Context, method, endpoint string, req interface{}) (*satusehat.FHIRResponse, error) {
resp, err := r.client.DoRequest(ctx, method, endpoint, req)
if err != nil {
return nil, err
}
var result map[string]interface{}
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
var resourceID string
if id, ok := result["id"].(string); ok {
resourceID = id
}
return &satusehat.FHIRResponse{
ID: resourceID,
FullResponse: result,
RawResponse: resp,
}, nil
}
func (r *repository) Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "POST", "/ServiceRequest", payload)
}
func (r *repository) Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/ServiceRequest/%s", id)
return r.executeRequest(ctx, "PUT", endpoint, payload)
}
func (r *repository) Patch(ctx context.Context, id string, payload ServiceRequestPatchRequest) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/ServiceRequest/%s", id)
return r.executeRequest(ctx, "PATCH", endpoint, payload)
}
func (r *repository) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/ServiceRequest/%s", id)
return r.executeRequest(ctx, "GET", endpoint, nil)
}
func (r *repository) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
endpoint := fmt.Sprintf("/ServiceRequest?%s", queryParams.Encode())
return r.executeRequest(ctx, "GET", endpoint, nil)
}
@@ -0,0 +1,63 @@
package servicerequest
import (
"context"
"net/url"
"service/internal/interfaces/satusehat"
"service/pkg/errors"
)
// Service mendefinisikan kontrak untuk logika bisnis ServiceRequest.
type Service interface {
Create(ctx context.Context, req ServiceRequestRequest) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, req ServiceRequestRequest) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req ServiceRequestPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type service struct {
repo Repository
}
// NewService membuat service ServiceRequest baru.
func NewService(repo Repository) Service {
return &service{repo: repo}
}
func (s *service) Create(ctx context.Context, req ServiceRequestRequest) (*satusehat.FHIRResponse, error) {
fhirPayload := MapRequestToFHIR(req)
return s.repo.Create(ctx, fhirPayload)
}
func (s *service) Update(ctx context.Context, id string, req ServiceRequestRequest) (*satusehat.FHIRResponse, error) {
if id == "" {
return nil, errors.NewValidationError().Message("ServiceRequest ID is required").Build()
}
fhirPayload := MapRequestToFHIR(req)
fhirPayload.Set("id", id)
return s.repo.Update(ctx, id, fhirPayload)
}
func (s *service) Patch(ctx context.Context, id string, req ServiceRequestPatchRequest) (*satusehat.FHIRResponse, error) {
if id == "" {
return nil, errors.NewValidationError().Message("ServiceRequest 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("ServiceRequest ID is required").Build()
}
return s.repo.GetByID(ctx, id)
}
func (s *service) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
return s.repo.Search(ctx, queryParams)
}
@@ -0,0 +1,16 @@
package specimen
import "time"
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"`
}
type SpecimenPatchRequest []map[string]interface{}
@@ -0,0 +1,27 @@
package specimen
import (
"service/internal/interfaces/satusehat"
"time"
)
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,
},
})
}
@@ -0,0 +1,51 @@
package specimen
import (
"context"
"encoding/json"
"fmt"
"net/url"
"service/internal/interfaces/satusehat"
)
type Repository interface {
Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req SpecimenPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type repository struct{ client satusehat.SatuSehatClient }
func NewRepository(client satusehat.SatuSehatClient) Repository { return &repository{client: client} }
func (r *repository) executeRequest(ctx context.Context, method, endpoint string, req interface{}) (*satusehat.FHIRResponse, error) {
resp, err := r.client.DoRequest(ctx, method, endpoint, req)
if err != nil {
return nil, err
}
var result map[string]interface{}
if err := json.Unmarshal(resp, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
var resourceID string
if id, ok := result["id"].(string); ok {
resourceID = id
}
return &satusehat.FHIRResponse{ID: resourceID, FullResponse: result, RawResponse: resp}, nil
}
func (r *repository) Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "POST", "/Specimen", payload)
}
func (r *repository) Update(ctx context.Context, id string, payload interface{}) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "PUT", fmt.Sprintf("/Specimen/%s", id), payload)
}
func (r *repository) Patch(ctx context.Context, id string, req SpecimenPatchRequest) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "PATCH", fmt.Sprintf("/Specimen/%s", id), req)
}
func (r *repository) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "GET", fmt.Sprintf("/Specimen/%s", id), nil)
}
func (r *repository) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
return r.executeRequest(ctx, "GET", fmt.Sprintf("/Specimen?%s", queryParams.Encode()), nil)
}
@@ -0,0 +1,37 @@
package specimen
import (
"context"
"net/url"
"service/internal/interfaces/satusehat"
"service/pkg/errors"
)
type Service interface {
Create(ctx context.Context, req SpecimenRequest) (*satusehat.FHIRResponse, error)
Update(ctx context.Context, id string, req SpecimenRequest) (*satusehat.FHIRResponse, error)
Patch(ctx context.Context, id string, req SpecimenPatchRequest) (*satusehat.FHIRResponse, error)
GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error)
Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error)
}
type service struct{ repo Repository }
func NewService(repo Repository) Service { return &service{repo: repo} }
func (s *service) Create(ctx context.Context, req SpecimenRequest) (*satusehat.FHIRResponse, error) {
return s.repo.Create(ctx, MapRequestToFHIR(req))
}
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 s.repo.Update(ctx, id, MapRequestToFHIR(req).Set("id", id))
}
func (s *service) Patch(ctx context.Context, id string, req SpecimenPatchRequest) (*satusehat.FHIRResponse, error) {
return s.repo.Patch(ctx, id, req)
}
func (s *service) GetByID(ctx context.Context, id string) (*satusehat.FHIRResponse, error) {
return s.repo.GetByID(ctx, id)
}
func (s *service) Search(ctx context.Context, queryParams url.Values) (*satusehat.FHIRResponse, error) {
return s.repo.Search(ctx, queryParams)
}