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