teg/backend/device_events.go
2026-05-28 15:06:37 +02:00

70 lines
1015 B
Go

// backend/device_events.go
package main
import (
"context"
"encoding/json"
)
func (s *Server) publishDeviceChangeEvent(
ctx context.Context,
actorUserID string,
deviceID string,
notificationType string,
title string,
message string,
data map[string]any,
) error {
if data == nil {
data = map[string]any{}
}
data["deviceId"] = deviceID
data["actorUserId"] = actorUserID
dataJSON, err := json.Marshal(data)
if err != nil {
return err
}
rows, err := s.db.Query(
ctx,
`
SELECT id::TEXT
FROM users
WHERE 'admin' = ANY(rights)
OR 'devices:read' = ANY(rights)
OR 'devices:write' = ANY(rights)
`,
)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var userID string
if err := rows.Scan(&userID); err != nil {
return err
}
if err := s.createAndPublishNotification(
ctx,
userID,
notificationType,
title,
message,
"device",
deviceID,
dataJSON,
true,
); err != nil {
return err
}
}
return rows.Err()
}