Add/edit notifications

This commit is contained in:
Magnus Åhall 2024-06-28 15:28:52 +02:00
parent 1ede36b8aa
commit c7ad2aa1b6
19 changed files with 531 additions and 11 deletions

View file

@ -3,6 +3,7 @@ package main
import (
// External
werr "git.gibonuddevalla.se/go/wrappederror"
"github.com/lib/pq"
// Internal
"smon/notification"
@ -67,13 +68,61 @@ func InitNotificationManager() (nm notification.Manager, err error) { // {{{
return
} // }}}
func UpdateNotificationService(svc notification.Service) (created bool, err error) { // {{{
if svc.Exists() {
_, err = service.Db.Conn.Exec(
`
UPDATE public.notification
SET
prio=$2,
configuration=$3
WHERE
prio=$1
`,
svc.GetPrio(),
svc.Updated().GetPrio(),
svc.Updated().JSON(),
)
} else {
_, err = service.Db.Conn.Exec(
`
INSERT INTO public.notification(prio, configuration, service)
VALUES($1, $2, $3)
`,
svc.Updated().GetPrio(),
svc.Updated().JSON(),
svc.GetType(),
)
created = true
}
if err != nil {
// Check if this is just a duplicated prio, which isn't allowed.
pgErr, isPgErr := err.(*pq.Error)
if isPgErr && pgErr.Code == "23505" {
return false, werr.New("Prio %d is already used by another service", svc.GetPrio())
}
return false, werr.Wrap(err).WithData(
struct {
Prio int
Configuration []byte
}{
svc.GetPrio(),
svc.JSON(),
},
)
}
return
} // }}}
func AcknowledgeNotification(uuid string) (err error) { // {{{
/*
_, err = service.Db.Conn.Exec(`UPDATE schedule SET acknowledged=true WHERE schedule_uuid=$1`, uuid)
if err != nil {
err = werr.Wrap(err).WithData(uuid)
}
_, err = service.Db.Conn.Exec(`UPDATE schedule SET acknowledged=true WHERE schedule_uuid=$1`, uuid)
if err != nil {
err = werr.Wrap(err).WithData(uuid)
}
*/
return
} // }}}