on going chemo plan

This commit is contained in:
vanilia
2025-12-09 15:07:01 +07:00
parent 69cfda6f63
commit be34c66ffd
4 changed files with 172 additions and 43 deletions

No files matched your search

@@ -7,7 +7,6 @@ package encounter
import (
"errors"
"fmt"
"strings"
"time"
@@ -27,6 +26,7 @@ import (
ea "simrs-vx/internal/domain/main-entities/ambulatory"
ec "simrs-vx/internal/domain/main-entities/chemo"
ecpl "simrs-vx/internal/domain/main-entities/chemo-plan"
ecp "simrs-vx/internal/domain/main-entities/chemo-protocol"
edo "simrs-vx/internal/domain/main-entities/device-order"
ed "simrs-vx/internal/domain/main-entities/doctor"
ee "simrs-vx/internal/domain/main-entities/emergency"
@@ -663,6 +663,20 @@ func insertDataSubClassAmbulatory(input e.CreateDto, soapiData []es.CreateDto, e
switch {
case subCode == ere.ACCChemo:
// validate if encounter Chemo valid
chemoProtoc, err := validateChemoAdm(*input.Patient_Id, event)
if err != nil {
return err
}
if chemoProtoc == nil {
event.Status = "failed"
event.ErrInfo = pl.ErrorInfo{
Code: "data-not-found",
Detail: "chemo plan not found",
}
}
chemoCreate := ec.CreateDto{
Encounter_Id: &input.Id,
Status_Code: erc.DVCVerified,
@@ -677,7 +691,7 @@ func insertDataSubClassAmbulatory(input e.CreateDto, soapiData []es.CreateDto, e
}
// set chemo-plan to planned
_, err = updateChemoPlan(*input.Patient_Id, event, tx)
err = updateChemoPlan(chemoProtoc, event, tx)
if err != nil {
return err
}
@@ -875,10 +889,8 @@ func validateDoctorCodes(doctorCodes map[string]struct{}, event *pl.Event) error
return nil
}
func updateChemoPlan(patientId uint, event *pl.Event, dbx ...*gorm.DB) (*ecpl.ChemoPlan, error) {
func updateChemoPlan(data *ecpl.ChemoPlan, event *pl.Event, dbx ...*gorm.DB) error {
pl.SetLogInfo(event, nil, "started", "getChemoFromEncounter")
data := ecpl.ChemoPlan{}
var tx *gorm.DB
if len(dbx) > 0 {
tx = dbx[0]
@@ -886,20 +898,65 @@ func updateChemoPlan(patientId uint, event *pl.Event, dbx ...*gorm.DB) (*ecpl.Ch
tx = dg.I
}
tx = tx.Model(&ecpl.ChemoPlan{}).
Joins(`"ChemoProtocol" cp ON cp."Id" = "ChemoPlan"."Protocol_Id"`).
Where(`cp."Patient_Id" = ? AND ("ChemoPlan"."Status" IS NULL OR "ChemoPlan"."Status" = ?)`, patientId, ere.SPCSchedule).
Order(`"Id" ASC`).
Limit(1).First(&data)
if err := tx.Error; err != nil {
return nil, err
if err := tx.Model(&data).Update(`"Status"`, ere.SPCPlanned).Error; err != nil {
return err
}
if err := tx.Model(&data).Update(`"Status"`, ere.SPCPlanned).Error; err != nil {
pl.SetLogInfo(event, nil, "complete")
return nil
}
func getChemoProtocol(patientId uint, event *pl.Event) (*ecp.ChemoProtocol, error) {
pl.SetLogInfo(event, nil, "started", "getChemoProtocol")
data := ecp.ChemoProtocol{}
var tx = dg.I
tx = tx.Model(&ecp.ChemoProtocol{}).
Joins(`"ChemoPlan" cp ON cp."Protocol_Id" = "ChemoProtocol"."Id"`).
Where(`"Patient.Id" = ? AND cp."Status" IS NULL`, patientId).
Preload("ChemoPlans", func(db *gorm.DB) *gorm.DB {
return tx.Order(`"Id" ASC`).Limit(1)
}).
First(&data)
if err := tx.Error; err != nil {
return nil, err
}
pl.SetLogInfo(event, nil, "complete")
return &data, nil
}
func validateChemoAdm(patientId uint, event *pl.Event) (*ecpl.ChemoPlan, error) {
// get chemo protocol
chemo, err := getChemoProtocol(patientId, event)
if err != nil {
return nil, err
}
if chemo.Status_Code != erc.DVCVerified {
event.Status = "failed"
event.ErrInfo = pl.ErrorInfo{
Code: "data-not-match",
Detail: fmt.Sprintf("protocol chemo not yet verified"),
}
return nil, pl.SetLogError(event, chemo)
}
if now.Before(*chemo.StartDate) || now.After(*chemo.EndDate) {
event.Status = "failed"
event.ErrInfo = pl.ErrorInfo{
Code: "invalid-date-range",
Detail: "chemo cannot be performed because the current date is outside the allowed treatment window.",
}
return nil, pl.SetLogError(event, chemo)
}
if chemo.ChemoPlans == nil || len(*chemo.ChemoPlans) == 0 {
return nil, nil
}
// Return the first chemo plan
return &(*chemo.ChemoPlans)[0], nil
}