Notes/notification_manager.go
Magnus Åhall f9f083367e Fixed #1
2024-03-30 22:26:26 +01:00

76 lines
1.5 KiB
Go

package main
import (
// External
werr "git.gibonuddevalla.se/go/wrappederror"
// Internal
"notes/notification"
// Standard
"database/sql"
"encoding/json"
)
type DbNotificationService struct {
ID int
UserID int `json:"user_id"`
Service string
Configuration string
Prio int
}
func InitNotificationManager() (err error) {// {{{
var dbServices []DbNotificationService
var row *sql.Row
row = service.Db.Conn.QueryRow(`
WITH services AS (
SELECT
id,
user_id,
prio,
service,
configuration::varchar
FROM notification n
ORDER BY
user_id ASC,
prio ASC
)
SELECT COALESCE(jsonb_agg(s.*), '[]')
FROM services s
`,
)
var dbData []byte
err = row.Scan(&dbData)
if err != nil {
err = werr.Wrap(err).WithCode("002-0006")
return
}
err = json.Unmarshal(dbData, &dbServices)
if err != nil {
err = werr.Wrap(err).WithCode("002-0007")
return
}
notificationManager = notification.NewManager()
var service notification.Service
for _, dbService := range dbServices {
service, err = notification.ServiceFactory(
dbService.Service,
[]byte(dbService.Configuration),
dbService.Prio,
config.Application.NotificationBaseURL,
)
notificationManager.AddService(dbService.UserID, service)
}
return
}// }}}
func AcknowledgeNotification(uuid string) (err error) {// {{{
_, err = service.Db.Conn.Exec(`UPDATE schedule SET acknowledged=true WHERE schedule_uuid=$1`, uuid)
return
}// }}}