Files
simrsx-be/internal/use-case/main-use-case/encounter/helper.go
T
2025-09-10 13:11:18 +07:00

101 lines
2.9 KiB
Go

/*
DESCRIPTION:
Any functions that are used internally by the use-case
*/
package encounter
import (
"errors"
e "simrs-vx/internal/domain/main-entities/encounter"
es "simrs-vx/internal/domain/main-entities/soapi"
pl "simrs-vx/pkg/logger"
erc "simrs-vx/internal/domain/references/common"
ero "simrs-vx/internal/domain/references/organization"
"gorm.io/gorm"
)
func setData[T *e.CreateDto | *e.UpdateDto](input T, data *e.Encounter) {
var inputSrc *e.CreateDto
if inputT, ok := any(input).(*e.CreateDto); ok {
inputSrc = inputT
} else {
inputTemp := any(input).(*e.UpdateDto)
inputSrc = &inputTemp.CreateDto
}
data.Patient_Id = inputSrc.Patient_Id
data.RegisteredAt = inputSrc.RegisteredAt
data.Class_Code = inputSrc.Class_Code
data.Unit_Id = inputSrc.Unit_Id
data.Specialist_Id = inputSrc.Specialist_Id
data.Subspecialist_Id = inputSrc.Subspecialist_Id
data.VisitDate = inputSrc.VisitDate
data.Appointment_Doctor_Id = inputSrc.Appointment_Doctor_Id
data.Responsible_Doctor_Id = inputSrc.Responsible_Doctor_Id
data.RefSource_Name = inputSrc.RefSource_Name
data.Appointment_Id = inputSrc.Appointment_Id
data.Status_Code = erc.DSCNew
}
func setDataUpdate(src e.UpdateDto, dst *e.Encounter) {
dst.Appointment_Doctor_Id = src.Appointment_Doctor_Id
dst.Responsible_Doctor_Id = src.Responsible_Doctor_Id
dst.Unit_Id = src.Unit_Id
dst.Specialist_Id = src.Specialist_Id
dst.Subspecialist_Id = src.Subspecialist_Id
dst.VisitDate = src.VisitDate
}
func setDataDischarge(src e.DischargeDto, dst *e.Encounter) {
dst.DischargeMethod_Code = src.DischargeMethod_Code
dst.EarlyEducation = src.EarlyEducation
dst.MedicalDischargeEducation = src.MedicalDischargeEducation
dst.AdmDischargeEducation = src.AdmDischargeEducation
dst.DischargeReason = src.DischargeReason
dst.Status_Code = erc.DSCDone
}
func checkSoapiByDocExists(encounter_id uint, event *pl.Event, tx *gorm.DB) error {
pl.SetLogInfo(event, nil, "started", "checkSoapiByDocExists")
var soapies []es.Soapi
err := tx.
Preload("Employee").
Preload("Employee.User").
Where("\"Encounter_Id\" = ?", encounter_id).Find(&soapies).Error
if err != nil {
event.Status = "failed"
event.ErrInfo = pl.ErrorInfo{
Code: "data-get-fail",
Detail: "get soapi failed",
Raw: err,
}
return pl.SetLogError(event, nil)
}
if len(soapies) == 0 {
event.Status = "failed"
event.ErrInfo = pl.ErrorInfo{
Code: "data-notFound",
Detail: "no soapi found for encounter",
Raw: errors.New("soapi not found"),
}
return pl.SetLogError(event, nil)
}
for _, s := range soapies {
if s.Employee != nil && s.Employee.User != nil && s.Employee.User.Position_Code == ero.UPCDoc {
return nil
}
}
event.Status = "failed"
event.ErrInfo = pl.ErrorInfo{
Code: "data-update-fail",
Detail: "no soapi written by a doctor found",
Raw: errors.New("all soapi employees are not doctors"),
}
return pl.SetLogError(event, nil)
}