first commit
This commit is contained in:
No files matched your search
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user