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