first commit
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package medicationstatement
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
// MedicationStatementDB merepresentasikan tabel data riwayat pengobatan / pengakuan pasien
|
||||
// TODO: Sesuaikan field dan tag `db` dengan tabel SIMRS Anda
|
||||
type MedicationStatementDB struct {
|
||||
IdxRiwayatObat int64 `db:"idxriwayatobat"`
|
||||
IdxDaftar int64 `db:"idxdaftar"`
|
||||
NoMR sql.NullString `db:"nomr"`
|
||||
TglDicatat sql.NullTime `db:"tgl_dicatat"`
|
||||
KFA sql.NullString `db:"kfa_code"`
|
||||
Instruksi sql.NullString `db:"instruksi"`
|
||||
}
|
||||
|
||||
type MedicationStatementSyncLog struct {
|
||||
IdxRiwayatObat int64 `db:"idxriwayatobat"`
|
||||
MedicationStatementID string `db:"medicationstatement_id"`
|
||||
RequestPayload string `db:"request_payload"`
|
||||
ResponsePayload string `db:"response_payload"`
|
||||
Status string `db:"status"`
|
||||
ErrorMessage string `db:"error_message"`
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package medicationstatement
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TODO: Sesuaikan dengan payload internal API MedicationStatement
|
||||
func MapToInternalAPI(dbData *MedicationStatementDB, orgID string) map[string]interface{} {
|
||||
waktu := time.Now()
|
||||
if dbData.TglDicatat.Valid {
|
||||
waktu = dbData.TglDicatat.Time
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"medicationstatement_id": fmt.Sprintf("%d", dbData.IdxRiwayatObat),
|
||||
"encounter_id": fmt.Sprintf("%d", dbData.IdxDaftar),
|
||||
"patient_id": dbData.NoMR.String,
|
||||
"medication_code": dbData.KFA.String,
|
||||
"date_asserted": waktu.Format(time.RFC3339),
|
||||
"dosage_text": dbData.Instruksi.String,
|
||||
}
|
||||
}
|
||||
|
||||
func MapToSyncLog(idx int64, fhirID string, reqPayload interface{}, respBody []byte, status string, errMsg string) MedicationStatementSyncLog {
|
||||
reqBytes, _ := json.MarshalIndent(reqPayload, "", " ")
|
||||
return MedicationStatementSyncLog{
|
||||
IdxRiwayatObat: idx,
|
||||
MedicationStatementID: fhirID,
|
||||
RequestPayload: string(reqBytes),
|
||||
ResponsePayload: string(respBody),
|
||||
Status: status,
|
||||
ErrorMessage: errMsg,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package medicationstatement
|
||||
|
||||
import (
|
||||
"context"
|
||||
"service/internal/infrastructure/database"
|
||||
"service/pkg/utils/query"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
GetNextMedicationStatement(ctx context.Context, lastID int64) (*MedicationStatementDB, error)
|
||||
SaveSatuSehatID(ctx context.Context, idx int64, fhirID string) error
|
||||
SaveSyncLog(ctx context.Context, logData MedicationStatementSyncLog) error
|
||||
}
|
||||
|
||||
type repository struct {
|
||||
dbManager database.Service
|
||||
}
|
||||
|
||||
func NewRepository(dbManager database.Service) Repository {
|
||||
return &repository{dbManager: dbManager}
|
||||
}
|
||||
|
||||
func (r *repository) GetNextMedicationStatement(ctx context.Context, lastID int64) (*MedicationStatementDB, error) {
|
||||
var ms MedicationStatementDB
|
||||
simrsDB, err := r.dbManager.GetSQLXDB("simrs")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
qb := query.NewSQLQueryBuilder(query.DBTypePostgreSQL).SetSecurityOptions(false, 0)
|
||||
|
||||
// TODO: Sesuaikan nama tabel riwayat obat
|
||||
qSimrs := query.DynamicQuery{
|
||||
From: "public.t_riwayat_obat",
|
||||
Filters: []query.FilterGroup{
|
||||
{
|
||||
Filters: []query.DynamicFilter{
|
||||
query.CreateFilter("idxriwayatobat", query.OpGreaterThan, lastID),
|
||||
},
|
||||
},
|
||||
},
|
||||
Sort: []query.SortField{
|
||||
query.CreateAscSort("idxriwayatobat"),
|
||||
},
|
||||
Limit: 1,
|
||||
}
|
||||
|
||||
err = qb.ExecuteQueryRow(ctx, simrsDB, qSimrs, &ms)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ms, nil
|
||||
}
|
||||
|
||||
func (r *repository) SaveSatuSehatID(ctx context.Context, idx int64, fhirID string) error {
|
||||
simrsDB, err := r.dbManager.GetSQLXDB("simrs")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
qb := query.NewSQLQueryBuilder(query.DBTypePostgreSQL).SetSecurityOptions(false, 0)
|
||||
_, err = qb.ExecuteUpdate(ctx, simrsDB, "public.t_riwayat_obat", query.UpdateData{Columns: []string{"status_bridging"}, Values: []interface{}{fhirID}}, []query.FilterGroup{query.CreateAndFilterGroup([]query.DynamicFilter{query.CreateEqualFilter("idxriwayatobat", idx)})})
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *repository) SaveSyncLog(ctx context.Context, logData MedicationStatementSyncLog) error {
|
||||
simrsDB, err := r.dbManager.GetSQLXDB("simrs")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
qb := query.NewSQLQueryBuilder(query.DBTypePostgreSQL).SetSecurityOptions(false, 0)
|
||||
insertData := query.InsertData{
|
||||
Columns: []string{"idxriwayatobat", "medicationstatement_id", "request_payload", "response_payload", "status", "error_message"},
|
||||
Values: []interface{}{logData.IdxRiwayatObat, logData.MedicationStatementID, logData.RequestPayload, logData.ResponsePayload, logData.Status, logData.ErrorMessage},
|
||||
}
|
||||
_, err = qb.ExecuteUpsert(ctx, simrsDB, "public.log_satusehat_medicationstatement", insertData, []string{"idxriwayatobat"}, []string{"medicationstatement_id", "request_payload", "response_payload", "status", "error_message"})
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package medicationstatement
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"service/internal/infrastructure/database"
|
||||
extapi "service/internal/worker/interface"
|
||||
)
|
||||
|
||||
const trackerFile = "last_migrated_medicationstatement_id.txt"
|
||||
|
||||
type Config struct {
|
||||
DBManager database.Service
|
||||
InternalBaseURL string
|
||||
InternalToken string
|
||||
OrganizationID string
|
||||
}
|
||||
|
||||
type WorkerService interface {
|
||||
Run(ctx context.Context)
|
||||
}
|
||||
|
||||
type worker struct {
|
||||
cfg Config
|
||||
repo Repository
|
||||
apiClient extapi.Client
|
||||
}
|
||||
|
||||
func NewWorker(cfg Config) WorkerService {
|
||||
return &worker{cfg: cfg, repo: NewRepository(cfg.DBManager), apiClient: extapi.NewClient(15 * time.Second)}
|
||||
}
|
||||
|
||||
func (w *worker) Run(ctx context.Context) {
|
||||
log.Println("[MEDICATIONSTATEMENT WORKER] Memulai proses sinkronisasi...")
|
||||
lastID := w.readLastID()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
log.Println("[MEDICATIONSTATEMENT WORKER] Proses dihentikan.")
|
||||
return
|
||||
default:
|
||||
}
|
||||
|
||||
ms, err := w.repo.GetNextMedicationStatement(ctx, lastID)
|
||||
if err != nil {
|
||||
time.Sleep(5 * time.Second)
|
||||
continue
|
||||
}
|
||||
|
||||
internalPayload := MapToInternalAPI(ms, w.cfg.OrganizationID)
|
||||
reqURL := fmt.Sprintf("%s/satusehat/medicationstatement", strings.TrimRight(w.cfg.InternalBaseURL, "/"))
|
||||
respBody, statusCode, err := w.apiClient.PostJSON(ctx, reqURL, w.cfg.InternalToken, internalPayload)
|
||||
|
||||
var status, errMsg, fhirID string
|
||||
if err != nil || (statusCode != http.StatusOK && statusCode != http.StatusCreated) {
|
||||
status, errMsg = "FAILED", fmt.Sprintf("Err: %v, HTTP: %d", err, statusCode)
|
||||
} else {
|
||||
status = "SUCCESS"
|
||||
var res map[string]interface{}
|
||||
if json.Unmarshal(respBody, &res) == nil {
|
||||
if data, ok := res["data"].(map[string]interface{}); ok {
|
||||
fhirID, _ = data["id"].(string)
|
||||
} else {
|
||||
fhirID, _ = res["id"].(string)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_ = w.repo.SaveSyncLog(ctx, MapToSyncLog(ms.IdxRiwayatObat, fhirID, internalPayload, respBody, status, errMsg))
|
||||
if status == "SUCCESS" && fhirID != "" {
|
||||
_ = w.repo.SaveSatuSehatID(ctx, ms.IdxRiwayatObat, fhirID)
|
||||
}
|
||||
|
||||
lastID = ms.IdxRiwayatObat
|
||||
os.WriteFile(trackerFile, []byte(strconv.FormatInt(lastID, 10)), 0644)
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *worker) readLastID() int64 {
|
||||
data, _ := os.ReadFile(trackerFile)
|
||||
id, _ := strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64)
|
||||
return id
|
||||
}
|
||||
Reference in New Issue
Block a user