161 lines
3.9 KiB
Go
161 lines
3.9 KiB
Go
// backend\notification_preferences.go
|
|
|
|
package main
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
type notificationPreferencesResponse struct {
|
|
InAppEnabled bool `json:"inAppEnabled"`
|
|
SoundEnabled bool `json:"soundEnabled"`
|
|
EmailEnabled bool `json:"emailEnabled"`
|
|
OperationUpdates bool `json:"operationUpdates"`
|
|
OperationJournals bool `json:"operationJournals"`
|
|
DeviceUpdates bool `json:"deviceUpdates"`
|
|
DeviceJournals bool `json:"deviceJournals"`
|
|
SystemMessages bool `json:"systemMessages"`
|
|
}
|
|
|
|
func defaultNotificationPreferences() notificationPreferencesResponse {
|
|
return notificationPreferencesResponse{
|
|
InAppEnabled: true,
|
|
SoundEnabled: false,
|
|
EmailEnabled: false,
|
|
OperationUpdates: true,
|
|
OperationJournals: true,
|
|
DeviceUpdates: true,
|
|
DeviceJournals: true,
|
|
SystemMessages: true,
|
|
}
|
|
}
|
|
|
|
func (s *Server) handleGetNotificationPreferences(w http.ResponseWriter, r *http.Request) {
|
|
user, ok := userFromContext(r.Context())
|
|
if !ok {
|
|
writeError(w, http.StatusUnauthorized, "Unauthorized")
|
|
return
|
|
}
|
|
|
|
settings, err := s.getNotificationPreferences(r, user.ID)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
writeJSON(w, http.StatusOK, map[string]any{
|
|
"settings": defaultNotificationPreferences(),
|
|
})
|
|
return
|
|
}
|
|
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "Benachrichtigungseinstellungen konnten nicht geladen werden")
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{
|
|
"settings": settings,
|
|
})
|
|
}
|
|
|
|
func (s *Server) handleUpdateNotificationPreferences(w http.ResponseWriter, r *http.Request) {
|
|
user, ok := userFromContext(r.Context())
|
|
if !ok {
|
|
writeError(w, http.StatusUnauthorized, "Unauthorized")
|
|
return
|
|
}
|
|
|
|
var input notificationPreferencesResponse
|
|
|
|
if err := readJSON(r, &input); err != nil {
|
|
writeError(w, http.StatusBadRequest, "Invalid JSON")
|
|
return
|
|
}
|
|
|
|
_, err := s.db.Exec(
|
|
r.Context(),
|
|
`
|
|
INSERT INTO notification_preferences (
|
|
user_id,
|
|
in_app_enabled,
|
|
sound_enabled,
|
|
email_enabled,
|
|
operation_updates,
|
|
operation_journals,
|
|
device_updates,
|
|
device_journals,
|
|
system_messages
|
|
)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
|
ON CONFLICT (user_id)
|
|
DO UPDATE SET
|
|
in_app_enabled = EXCLUDED.in_app_enabled,
|
|
sound_enabled = EXCLUDED.sound_enabled,
|
|
email_enabled = EXCLUDED.email_enabled,
|
|
operation_updates = EXCLUDED.operation_updates,
|
|
operation_journals = EXCLUDED.operation_journals,
|
|
device_updates = EXCLUDED.device_updates,
|
|
device_journals = EXCLUDED.device_journals,
|
|
system_messages = EXCLUDED.system_messages,
|
|
updated_at = now()
|
|
`,
|
|
user.ID,
|
|
input.InAppEnabled,
|
|
input.SoundEnabled,
|
|
input.EmailEnabled,
|
|
input.OperationUpdates,
|
|
input.OperationJournals,
|
|
input.DeviceUpdates,
|
|
input.DeviceJournals,
|
|
input.SystemMessages,
|
|
)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "Benachrichtigungseinstellungen konnten nicht gespeichert werden")
|
|
return
|
|
}
|
|
|
|
settings, err := s.getNotificationPreferences(r, user.ID)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "Benachrichtigungseinstellungen wurden gespeichert, konnten aber nicht neu geladen werden")
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{
|
|
"settings": settings,
|
|
})
|
|
}
|
|
|
|
func (s *Server) getNotificationPreferences(r *http.Request, userID string) (notificationPreferencesResponse, error) {
|
|
settings := defaultNotificationPreferences()
|
|
|
|
err := s.db.QueryRow(
|
|
r.Context(),
|
|
`
|
|
SELECT
|
|
in_app_enabled,
|
|
sound_enabled,
|
|
email_enabled,
|
|
operation_updates,
|
|
operation_journals,
|
|
device_updates,
|
|
device_journals,
|
|
system_messages
|
|
FROM notification_preferences
|
|
WHERE user_id = $1
|
|
LIMIT 1
|
|
`,
|
|
userID,
|
|
).Scan(
|
|
&settings.InAppEnabled,
|
|
&settings.SoundEnabled,
|
|
&settings.EmailEnabled,
|
|
&settings.OperationUpdates,
|
|
&settings.OperationJournals,
|
|
&settings.DeviceUpdates,
|
|
&settings.DeviceJournals,
|
|
&settings.SystemMessages,
|
|
)
|
|
|
|
return settings, err
|
|
}
|