protocol chemo finish

This commit is contained in:
vanilia
2025-12-12 13:48:34 +07:00
parent f5568dfc61
commit cfdfac223f
14 changed files with 261 additions and 120 deletions
+14 -2
View File
@@ -42,7 +42,7 @@ func Create(input e.CreateDto) (*d.Data, error) {
return nil, pl.SetLogError(&event, input)
}
chemoPlan, err := validateIfEncounterIsChemo(*input.Encounter_Id, &event)
chemoPlan, err := validateIfEncounterIsChemo(*input.Encounter_Id, "c", &event)
if err != nil {
return nil, err
}
@@ -57,7 +57,7 @@ func Create(input e.CreateDto) (*d.Data, error) {
// update chemoPlan
if chemoPlan != nil {
chemoPlan.Encounter_Id = input.Encounter_Id
if err = ucp.UpdateData(chemoPlan, &event, tx); err != nil {
if err = ucp.UpdateData(chemoPlan, "c", &event, tx); err != nil {
return err
}
}
@@ -252,6 +252,18 @@ func Delete(input e.DeleteDto) (*d.Data, error) {
return err
}
// update chemoPlan
chemoPlan, err := validateIfEncounterIsChemo(*data.Encounter_Id, "d", &event)
if err != nil {
return err
}
if chemoPlan != nil {
if err = ucp.UpdateData(chemoPlan, "d", &event, tx); err != nil {
return err
}
}
mwRunner.setMwType(pu.MWTPre)
// Run pre-middleware
if err = mwRunner.ExecuteIfSyncOn(func() error {
+18 -56
View File
@@ -6,11 +6,7 @@ package soapi
import (
"errors"
"fmt"
erc "simrs-vx/internal/domain/references/common"
pl "simrs-vx/pkg/logger"
pu "simrs-vx/pkg/use-case-helper"
"time"
dg "github.com/karincake/apem/db-gorm-pg"
"gorm.io/gorm"
@@ -55,7 +51,7 @@ func setBulkData(input []e.CreateDto, encounterId uint) []e.Soapi {
return data
}
func validateIfEncounterIsChemo(encounterId uint, event *pl.Event) (*ecpl.ChemoPlan, error) {
func validateIfEncounterIsChemo(encounterId uint, method string, event *pl.Event) (*ecpl.ChemoPlan, error) {
// get encounter
enc, err := getEncounter(encounterId, event)
if err != nil {
@@ -64,12 +60,12 @@ func validateIfEncounterIsChemo(encounterId uint, event *pl.Event) (*ecpl.ChemoP
// Encounter must be Ambulatory and NOT Rehab
a := enc.Ambulatory
if enc.Class_Code != ere.ECAmbulatory || a == nil || a.Class_Code == ere.ACCRehab {
if a == nil || a.Class_Code == ere.ACCRehab {
return nil, nil
}
// get chemo protocol
chemo, err := getChemo(*enc.Patient_Id, event)
chemo, err := getChemo(*enc.Patient_Id, enc.Id, event)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
@@ -77,10 +73,6 @@ func validateIfEncounterIsChemo(encounterId uint, event *pl.Event) (*ecpl.ChemoP
return nil, pl.SetLogError(event, nil)
}
if err = chemoProtocoValidation(chemo, event); err != nil {
}
if chemo.ChemoPlans == nil || len(*chemo.ChemoPlans) == 0 {
return nil, nil
}
@@ -89,69 +81,39 @@ func validateIfEncounterIsChemo(encounterId uint, event *pl.Event) (*ecpl.ChemoP
return &(*chemo.ChemoPlans)[0], nil
}
func chemoProtocoValidation(chemo *ecp.ChemoProtocol, event *pl.Event) error {
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 pl.SetLogError(event, chemo)
}
now := time.Now()
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 pl.SetLogError(event, chemo)
}
return nil
}
func getEncounter(encounterId uint, event *pl.Event) (*ee.Encounter, error) {
pl.SetLogInfo(event, nil, "started", "getEncounter")
data := ee.Encounter{}
var tx = dg.I
if err := tx.Model(&ee.Encounter{}).
Where(`"Encounter_Id" = ?`, encounterId).
Scopes(withConditionalAmbulatory()).
First(&data).Error; err != nil {
if processedErr := pu.HandleReadError(err, event, source, nil, encounterId); processedErr != nil {
return nil, processedErr
}
err := tx.Model(&ee.Encounter{}).
Joins(`LEFT JOIN "Ambulatory" a ON a."Encounter_Id" = "Encounter"."Id"`).
Where(`"Encounter"."Id" = ?`, encounterId).
Where(`"Encounter"."Class_Code" = ?`, ere.ECAmbulatory).
Preload("Ambulatory").
First(&data).Error
if err != nil {
return nil, err
}
pl.SetLogInfo(event, nil, "complete")
return &data, nil
}
func withConditionalAmbulatory() func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.Preload(`"Ambulatory"`, func(db *gorm.DB) *gorm.DB {
return db.Joins(`JOIN "Encounter" e ON "e"."Id" = "Ambulatory"."Encounter_Id"`)
})
}
}
func getChemo(patientId uint, event *pl.Event) (*ecp.ChemoProtocol, error) {
pl.SetLogInfo(event, nil, "started", "getChemoFromEncounter")
func getChemo(patientId, encounterId uint, event *pl.Event) (*ecp.ChemoProtocol, error) {
pl.SetLogInfo(event, nil, "started", "getChemo")
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" = ?`, patientId, ere.SPCPlanned).
Joins(`LEFT JOIN "ChemoPlan" cp ON cp."Protocol_Id" = "ChemoProtocol"."Id"`).
Where(`"ChemoProtocol"."Patient_Id" = ?`, patientId).
Preload("ChemoPlans", func(db *gorm.DB) *gorm.DB {
return tx.Order(`"Id" ASC`).Limit(1)
return db.
Where(`"Encounter_Id" = ?`, encounterId)
}).
First(&data)