This commit is contained in:
Magnus Åhall 2024-04-05 08:59:59 +02:00
parent 49fd943110
commit 48c1227d9f
2 changed files with 31 additions and 26 deletions

View File

@ -67,7 +67,7 @@ func logCallback(e WrappedError.Error) { // {{{
Month int
Day int
Time string
Error any
Error error
}{now.Year(), int(now.Month()), now.Day(), now.Format("15:04:05"), e}
j, _ := json.Marshal(out)

View File

@ -192,37 +192,42 @@ func ExpiredSchedules() (schedules []Schedule) { // {{{
func FutureSchedules(userID int) (schedules []Schedule, err error) {// {{{
schedules = []Schedule{}
res, err := service.Db.Conn.Queryx(`
res := service.Db.Conn.QueryRow(`
WITH schedule_events AS (
SELECT
id,
user_id,
node_id,
schedule_uuid,
time,
description
FROM schedule
s.id,
s.user_id,
to_jsonb(n.*) AS node,
s.schedule_uuid,
(time - MAKE_INTERVAL(mins => s.remind_minutes)) AT TIME ZONE u.timezone AS time,
s.description,
s.acknowledged
FROM schedule s
INNER JOIN public.user u ON s.user_id = u.id
INNER JOIN node n ON s.node_id = n.id
WHERE
user_id = $1 AND
s.user_id=$1 AND
time >= NOW() AND
NOT acknowledged
ORDER BY
time ASC
)
SELECT
COALESCE(jsonb_agg(s.*), '[]'::jsonb)
FROM schedule_events s
`,
userID,
)
var j []byte
err = res.Scan(&j)
if err != nil {
err = werr.Wrap(err).WithCode("002-000B")
err = werr.Wrap(err).WithCode("002-000B").WithData(userID)
return
}
defer res.Close()
for res.Next() {
s := Schedule{}
if err = res.Scan(&s.ID, &s.UserID, &s.Node.ID, &s.ScheduleUUID, &s.Time, &s.Description); err != nil {
err = werr.Wrap(err).WithCode("002-000C")
err = json.Unmarshal(j, &schedules)
if err != nil {
err = werr.Wrap(err).WithCode("002-0010").WithData(string(j)).Log()
return
}
schedules = append(schedules, s)
}
return
}// }}}