-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifications.go
More file actions
127 lines (107 loc) · 3.95 KB
/
notifications.go
File metadata and controls
127 lines (107 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package handler
import (
"context"
"errors"
"log/slog"
"github.com/generate/selfserve/internal/errs"
"github.com/generate/selfserve/internal/httpx"
"github.com/generate/selfserve/internal/models"
"github.com/gofiber/fiber/v2"
)
type NotificationsRepository interface {
FindByUserID(ctx context.Context, userID string) ([]*models.Notification, error)
MarkRead(ctx context.Context, id, userID string) error
MarkAllRead(ctx context.Context, userID string) error
UpsertDeviceToken(ctx context.Context, userID, token, platform string) error
}
type NotificationsHandler struct {
repo NotificationsRepository
}
func NewNotificationsHandler(repo NotificationsRepository) *NotificationsHandler {
return &NotificationsHandler{repo: repo}
}
// ListNotifications godoc
// @Summary List notifications
// @Description Returns the most recent notifications for the authenticated user
// @Tags notifications
// @Produce json
// @Success 200 {array} models.Notification
// @Failure 500 {object} errs.HTTPError
// @Security BearerAuth
// @Router /notifications [get]
func (h *NotificationsHandler) ListNotifications(c *fiber.Ctx) error {
userID := c.Locals("userId").(string)
notifications, err := h.repo.FindByUserID(c.Context(), userID)
if err != nil {
slog.Error("failed to list notifications", "err", err)
return errs.InternalServerError()
}
if notifications == nil {
notifications = []*models.Notification{}
}
return c.JSON(notifications)
}
// MarkRead godoc
// @Summary Mark notification as read
// @Description Marks a single notification as read for the authenticated user
// @Tags notifications
// @Param id path string true "Notification ID"
// @Success 204
// @Failure 404 {object} errs.HTTPError
// @Failure 500 {object} errs.HTTPError
// @Security BearerAuth
// @Router /notifications/{id}/read [put]
func (h *NotificationsHandler) MarkRead(c *fiber.Ctx) error {
id := c.Params("id")
if id == "" {
return errs.BadRequest("id is required")
}
userID := c.Locals("userId").(string)
if err := h.repo.MarkRead(c.Context(), id, userID); err != nil {
if errors.Is(err, errs.ErrNotFoundInDB) {
return errs.NotFound("notification", "id", id)
}
slog.Error("failed to mark notification as read", "err", err)
return errs.InternalServerError()
}
return c.SendStatus(fiber.StatusNoContent)
}
// MarkAllRead godoc
// @Summary Mark all notifications as read
// @Description Marks all unread notifications as read for the authenticated user
// @Tags notifications
// @Success 204
// @Failure 500 {object} errs.HTTPError
// @Security BearerAuth
// @Router /notifications/read-all [put]
func (h *NotificationsHandler) MarkAllRead(c *fiber.Ctx) error {
userID := c.Locals("userId").(string)
if err := h.repo.MarkAllRead(c.Context(), userID); err != nil {
slog.Error("failed to mark all notifications as read", "err", err)
return errs.InternalServerError()
}
return c.SendStatus(fiber.StatusNoContent)
}
// RegisterDeviceToken godoc
// @Summary Register device token
// @Description Registers an Expo push token so the user receives mobile push notifications
// @Tags notifications
// @Accept json
// @Param request body models.RegisterDeviceTokenInput true "Device token"
// @Success 204
// @Failure 400 {object} errs.HTTPError
// @Failure 500 {object} errs.HTTPError
// @Security BearerAuth
// @Router /device-tokens [post]
func (h *NotificationsHandler) RegisterDeviceToken(c *fiber.Ctx) error {
var input models.RegisterDeviceTokenInput
if err := httpx.BindAndValidate(c, &input); err != nil {
return err
}
userID := c.Locals("userId").(string)
if err := h.repo.UpsertDeviceToken(c.Context(), userID, input.Token, input.Platform); err != nil {
slog.Error("failed to register device token", "err", err)
return errs.InternalServerError()
}
return c.SendStatus(fiber.StatusNoContent)
}