2022-11-16 14:54:46 -06:00
|
|
|
package health
|
|
|
|
|
|
|
|
import (
|
2022-11-18 23:37:47 -06:00
|
|
|
"log"
|
2022-11-16 14:54:46 -06:00
|
|
|
"sync"
|
2022-11-18 23:37:47 -06:00
|
|
|
"time"
|
2022-11-16 14:54:46 -06:00
|
|
|
|
|
|
|
"github.com/eternal-flame-AD/yoake/internal/auth"
|
|
|
|
"github.com/eternal-flame-AD/yoake/internal/comm"
|
2022-11-18 23:37:47 -06:00
|
|
|
"github.com/eternal-flame-AD/yoake/internal/comm/model"
|
2022-11-16 14:54:46 -06:00
|
|
|
"github.com/eternal-flame-AD/yoake/internal/db"
|
2022-11-18 23:37:47 -06:00
|
|
|
"github.com/eternal-flame-AD/yoake/internal/util"
|
2022-11-16 14:54:46 -06:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
2022-11-18 23:37:47 -06:00
|
|
|
func Register(g *echo.Group, database db.DB, comm *comm.Communicator) {
|
2022-11-16 14:54:46 -06:00
|
|
|
megsG := g.Group("/meds")
|
|
|
|
{
|
|
|
|
shortHands := megsG.Group("/shorthand")
|
|
|
|
{
|
|
|
|
shortHands.GET("/parse", RESTParseShorthand())
|
|
|
|
shortHands.POST("/parse", RESTParseShorthand())
|
|
|
|
|
|
|
|
shortHands.POST("/format", RESTFormatShorthand())
|
|
|
|
}
|
|
|
|
|
|
|
|
writeMutex := new(sync.Mutex)
|
|
|
|
directions := megsG.Group("/directions", auth.RequireMiddleware(auth.RoleAdmin))
|
|
|
|
{
|
2022-11-18 23:37:47 -06:00
|
|
|
directions.GET("", RESTMedGetDirections(database))
|
|
|
|
directions.POST("", RESTMedPostDirections(database, writeMutex))
|
|
|
|
directions.DELETE("/:name", RESTMedDeleteDirections(database, writeMutex))
|
2022-11-16 14:54:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
compliance := megsG.Group("/compliance", auth.RequireMiddleware(auth.RoleAdmin))
|
|
|
|
{
|
|
|
|
complianceByMed := compliance.Group("/med/:med")
|
|
|
|
{
|
2022-11-18 23:37:47 -06:00
|
|
|
complianceByMed.GET("/log", RESTComplianceLogGet(database))
|
|
|
|
complianceByMed.GET("/project", RESTComplianceLogProjectMed(database))
|
|
|
|
|
2022-11-16 14:54:46 -06:00
|
|
|
}
|
|
|
|
|
2022-11-18 23:37:47 -06:00
|
|
|
compliance.GET("/log", RESTComplianceLogGet(database))
|
|
|
|
|
|
|
|
compliance.POST("/log", RESTComplianceLogPost(database, writeMutex))
|
2022-11-16 14:54:46 -06:00
|
|
|
|
2022-11-18 23:37:47 -06:00
|
|
|
compliance.POST("/recalc", RESTRecalcMedComplianceLog(database, writeMutex))
|
2022-11-16 14:54:46 -06:00
|
|
|
}
|
|
|
|
}
|
2022-11-18 23:37:47 -06:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
ticker := time.NewTicker(5 * time.Minute)
|
|
|
|
notified := make(map[string]time.Time)
|
|
|
|
for {
|
|
|
|
func() {
|
|
|
|
txn := database.NewTransaction(false)
|
|
|
|
defer txn.Discard()
|
|
|
|
|
|
|
|
existingNotified := make(map[string]time.Time)
|
|
|
|
err := db.GetJSON(txn, []byte("health_meds_compliance_notified_meds"), &existingNotified)
|
|
|
|
|
|
|
|
if err != nil && !db.IsNotFound(err) {
|
|
|
|
log.Println("Error getting notified meds: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for k, v := range existingNotified {
|
|
|
|
o := notified[k]
|
|
|
|
if o.Before(v) {
|
|
|
|
notified[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
txn.Discard()
|
|
|
|
txn = database.NewTransaction(true)
|
|
|
|
err = db.SetJSON(txn, []byte("health_meds_compliance_notified_meds"), notified)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error setting notified meds: ", err)
|
|
|
|
return
|
|
|
|
} else if err := txn.Commit(); err != nil {
|
|
|
|
log.Println("Error committing notified meds: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
meds, err := DBMedListGet(database)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error getting med list:", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
logs, err := DBMedComplianceLogGet(database, util.DateRangeAround(time.Now(), 1))
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error getting med compliance log:", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var notifications []CommCtx
|
|
|
|
|
|
|
|
hasNew := false
|
|
|
|
for _, med := range meds {
|
|
|
|
nextDose := logs.ProjectNextDose(med)
|
|
|
|
if nextDose.Expected.Time.Before(time.Now()) {
|
|
|
|
if lastNotified, ok := notified[med.KeyName()]; !ok ||
|
|
|
|
lastNotified.Before(nextDose.Expected.Time) ||
|
|
|
|
lastNotified.Add(4*time.Hour).Before(time.Now()) {
|
|
|
|
{
|
|
|
|
if !util.Contain(med.Flags, DirectionFlagPRN) {
|
|
|
|
hasNew = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
notifications = append(notifications, CommCtx{
|
|
|
|
Med: med,
|
|
|
|
Dose: nextDose,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if hasNew {
|
|
|
|
if err := comm.SendGenericMessage("gotify", model.GenericMessage{
|
|
|
|
Subject: "Medications Due",
|
|
|
|
Body: commTemplate,
|
|
|
|
MIME: "text/markdown+html/template",
|
|
|
|
Context: notifications,
|
|
|
|
}, true); err != nil {
|
|
|
|
log.Println("Error sending med compliance notification:", err)
|
|
|
|
}
|
|
|
|
for _, n := range notifications {
|
|
|
|
notified[n.Med.KeyName()] = time.Now()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
<-ticker.C
|
|
|
|
}
|
|
|
|
}()
|
2022-11-16 14:54:46 -06:00
|
|
|
}
|