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 Month int
Day int Day int
Time string Time string
Error any Error error
}{now.Year(), int(now.Month()), now.Day(), now.Format("15:04:05"), e} }{now.Year(), int(now.Month()), now.Day(), now.Format("15:04:05"), e}
j, _ := json.Marshal(out) j, _ := json.Marshal(out)

View File

@ -192,37 +192,42 @@ func ExpiredSchedules() (schedules []Schedule) { // {{{
func FutureSchedules(userID int) (schedules []Schedule, err error) {// {{{ func FutureSchedules(userID int) (schedules []Schedule, err error) {// {{{
schedules = []Schedule{} schedules = []Schedule{}
res, err := service.Db.Conn.Queryx(` res := service.Db.Conn.QueryRow(`
WITH schedule_events AS (
SELECT
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
s.user_id=$1 AND
time >= NOW() AND
NOT acknowledged
)
SELECT SELECT
id, COALESCE(jsonb_agg(s.*), '[]'::jsonb)
user_id, FROM schedule_events s
node_id, `,
schedule_uuid, userID,
time,
description
FROM schedule
WHERE
user_id = $1 AND
time >= NOW() AND
NOT acknowledged
ORDER BY
time ASC
`,
userID,
) )
var j []byte
err = res.Scan(&j)
if err != nil { if err != nil {
err = werr.Wrap(err).WithCode("002-000B") err = werr.Wrap(err).WithCode("002-000B").WithData(userID)
return return
} }
defer res.Close()
for res.Next() { err = json.Unmarshal(j, &schedules)
s := Schedule{} if err != nil {
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-0010").WithData(string(j)).Log()
err = werr.Wrap(err).WithCode("002-000C") return
return
}
schedules = append(schedules, s)
} }
return return
}// }}} }// }}}