penambahan All case Satu sehat
This commit is contained in:
No files matched your search
@@ -2,12 +2,12 @@ package encounter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"service/internal/infrastructure/database"
|
||||
"service/internal/interfaces/satusehat"
|
||||
"service/pkg/errors"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
@@ -16,7 +16,6 @@ type Repository interface {
|
||||
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)
|
||||
}
|
||||
@@ -30,10 +29,14 @@ func NewRepository(client satusehat.SatuSehatClient, db database.Service) Reposi
|
||||
return &repository{client: client, db: db}
|
||||
}
|
||||
|
||||
type hiddenCtx struct {
|
||||
context.Context
|
||||
}
|
||||
|
||||
func (r *repository) executeRequest(ctx context.Context, method, endpoint string, payload interface{}) (*satusehat.FHIRResponse, error) {
|
||||
resp, err := r.client.DoRequest(ctx, method, endpoint, payload)
|
||||
resp, err := r.client.DoRequest(hiddenCtx{ctx}, method, endpoint, payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errors.InternalError().Message("Failed to execute request to SatuSehat").Cause(err).Build()
|
||||
}
|
||||
return r.parseAndProcessResponse(resp)
|
||||
}
|
||||
@@ -41,23 +44,16 @@ func (r *repository) executeRequest(ctx context.Context, method, endpoint string
|
||||
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)
|
||||
return nil, errors.InternalError().Message("Failed to parse SatuSehat response").Cause(err).Metadata("raw_response", string(data)).Build()
|
||||
}
|
||||
if resourceType, ok := result["resourceType"].(string); ok && resourceType == "OperationOutcome" {
|
||||
return nil, errors.ParseSatuSehatError(result)
|
||||
}
|
||||
|
||||
// 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
|
||||
return &satusehat.FHIRResponse{ID: resourceID, FullResponse: result, RawResponse: data}, nil
|
||||
}
|
||||
|
||||
func (r *repository) Create(ctx context.Context, payload interface{}) (*satusehat.FHIRResponse, error) {
|
||||
@@ -65,66 +61,25 @@ func (r *repository) Create(ctx context.Context, payload interface{}) (*satuseha
|
||||
}
|
||||
|
||||
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)
|
||||
return r.executeRequest(ctx, "PUT", fmt.Sprintf("/Encounter/%s", id), 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)
|
||||
return r.executeRequest(ctx, "PATCH", fmt.Sprintf("/Encounter/%s", id), 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)
|
||||
return r.executeRequest(ctx, "GET", fmt.Sprintf("/Encounter/%s", id), 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)
|
||||
return r.executeRequest(ctx, "GET", fmt.Sprintf("/Encounter?%s", queryParams.Encode()), 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)
|
||||
return r.executeRequest(ctx, "GET", fmt.Sprintf("/Patient?identifier=https://fhir.kemkes.go.id/id/nik|%s", nik), 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
|
||||
return r.executeRequest(ctx, "GET", fmt.Sprintf("/Practitioner?identifier=https://fhir.kemkes.go.id/id/nik|%s", nik), nil)
|
||||
}
|
||||
Reference in New Issue
Block a user