Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: add Mark All as Read feature for inbox notifications #16975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,7 @@ func New(options *Options) *API {
r.Get("/", api.listInboxNotifications)
r.Get("/watch", api.watchInboxNotifications)
r.Put("/{id}/read-status", api.updateInboxNotificationReadStatus)
r.Put("/mark-all-read", api.updateAllInboxNotificationsReadStatus)
})
r.Get("/settings", api.notificationsSettings)
r.Put("/settings", api.putNotificationsSettings)
Expand Down
10 changes: 10 additions & 0 deletions coderd/database/queries/notificationsinbox.sql
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,13 @@ SET
read_at = $1
WHERE
id = $2;

-- name: UpdateAllInboxNotificationsReadStatusByUserID :exec
-- Marks all unread notifications as read for a user
UPDATE
inbox_notifications
SET
read_at = $1
WHERE
user_id = $2 AND
read_at IS NULL;
44 changes: 44 additions & 0 deletions coderd/inboxnotifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,47 @@
UnreadCount: int(unreadCount),
})
}

// updateAllInboxNotificationsReadStatus marks all notifications as read for the user.
// @Summary Mark all notifications as read
// @ID mark-all-notifications-as-read
// @Security CoderSessionToken
// @Produce json
// @Tags Notifications
// @Success 200 {object} codersdk.Response
// @Router /notifications/inbox/mark-all-read [put]
func (api *API) updateAllInboxNotificationsReadStatus(rw http.ResponseWriter, r *http.Request) {
var (
ctx = r.Context()
apikey = httpmw.APIKey(r)
)

err := api.Database.UpdateAllInboxNotificationsReadStatusByUserID(ctx, database.UpdateAllInboxNotificationsReadStatusByUserIDParams{

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-e2e-premium

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-e2e-premium

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-e2e

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-e2e

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / lint

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / lint

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams) (typecheck)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / lint

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / lint

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams) (typecheck)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / lint

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / lint

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams) (typecheck)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-cli (macos-latest)

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-cli (macos-latest)

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-cli (macos-latest)

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-cli (macos-latest)

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-pg-16

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-pg-16

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-pg-16

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-pg-16

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-pg-16

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-pg-16

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-pg-16

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-pg-16

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-pg-16

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-pg-16

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-pg (ubuntu-latest)

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-pg (ubuntu-latest)

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-pg (ubuntu-latest)

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-pg (ubuntu-latest)

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-pg (ubuntu-latest)

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-pg (ubuntu-latest)

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-pg (ubuntu-latest)

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-pg (ubuntu-latest)

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-pg (ubuntu-latest)

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-pg (ubuntu-latest)

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-race

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-race

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-race

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-race

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-race

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-race

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-race

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-race

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-race

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-race

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-race-pg

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-race-pg

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-race-pg

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-race-pg

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-race-pg

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-race-pg

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-race-pg

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-race-pg

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-race-pg

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go-race-pg

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

api.Database.UpdateAllInboxNotificationsReadStatusByUserID undefined (type database.Store has no field or method UpdateAllInboxNotificationsReadStatusByUserID)

Check failure on line 363 in coderd/inboxnotifications.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

undefined: database.UpdateAllInboxNotificationsReadStatusByUserIDParams
ReadAt: sql.NullTime{
Time: dbtime.Now(),
Valid: true,
},
UserID: apikey.UserID,
})
if err != nil {
api.Logger.Error(ctx, "failed to update all inbox notifications read status", slog.Error(err))
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to update all inbox notifications read status.",
})
return
}

// Get the updated unread count
unreadCount, err := api.Database.CountUnreadInboxNotificationsByUserID(ctx, apikey.UserID)
if err != nil {
api.Logger.Error(ctx, "failed to call count unread inbox notifications", slog.Error(err))
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to call count unread inbox notifications.",
})
return
}

httpapi.Write(ctx, rw, http.StatusOK, codersdk.MarkAllInboxNotificationsReadResponse{
UnreadCount: int(unreadCount),
})
}
61 changes: 61 additions & 0 deletions coderd/inboxnotifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,3 +723,64 @@ func TestInboxNotifications_ReadStatus(t *testing.T) {
require.Empty(t, updatedNotif.Notification)
})
}

func TestInboxNotifications_MarkAllRead(t *testing.T) {
t.Parallel()

// I skip these tests specifically on windows as for now they are flaky - only on Windows.
// For now the idea is that the runner takes too long to insert the entries, could be worth
// investigating a manual Tx.
if runtime.GOOS == "windows" {
t.Skip("our runners are randomly taking too long to insert entries")
}

t.Run("ok", func(t *testing.T) {
t.Parallel()
client, _, api := coderdtest.NewWithAPI(t, &coderdtest.Options{})
firstUser := coderdtest.CreateFirstUser(t, client)
client, member := coderdtest.CreateAnotherUser(t, client, firstUser.OrganizationID)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

notifs, err := client.ListInboxNotifications(ctx, codersdk.ListInboxNotificationsRequest{})
require.NoError(t, err)
require.NotNil(t, notifs)
require.Equal(t, 0, notifs.UnreadCount)
require.Empty(t, notifs.Notifications)

for i := range 20 {
dbgen.NotificationInbox(t, api.Database, database.InsertInboxNotificationParams{
ID: uuid.New(),
UserID: member.ID,
TemplateID: notifications.TemplateWorkspaceOutOfMemory,
Title: fmt.Sprintf("Notification %d", i),
Actions: json.RawMessage("[]"),
Content: fmt.Sprintf("Content of the notif %d", i),
CreatedAt: dbtime.Now(),
})
}

notifs, err = client.ListInboxNotifications(ctx, codersdk.ListInboxNotificationsRequest{})
require.NoError(t, err)
require.NotNil(t, notifs)
require.Equal(t, 20, notifs.UnreadCount)
require.Len(t, notifs.Notifications, 20)

// Mark all as read
response, err := client.MarkAllInboxNotificationsRead(ctx)
require.NoError(t, err)
require.NotNil(t, response)
require.Equal(t, 0, response.UnreadCount)

// Check that all notifications are marked as read
notifs, err = client.ListInboxNotifications(ctx, codersdk.ListInboxNotificationsRequest{})
require.NoError(t, err)
require.Equal(t, 0, notifs.UnreadCount)

// All notifications should be marked as read
for _, notif := range notifs.Notifications {
require.NotNil(t, notif.ReadAt)
}
})
}
23 changes: 23 additions & 0 deletions codersdk/inboxnotification.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,26 @@ func (c *Client) UpdateInboxNotificationReadStatus(ctx context.Context, notifID
var resp UpdateInboxNotificationReadStatusResponse
return resp, json.NewDecoder(res.Body).Decode(&resp)
}

type MarkAllInboxNotificationsReadResponse struct {
UnreadCount int `json:"unread_count"`
}

func (c *Client) MarkAllInboxNotificationsRead(ctx context.Context) (MarkAllInboxNotificationsReadResponse, error) {
res, err := c.Request(
ctx, http.MethodPut,
"/api/v2/notifications/inbox/mark-all-read",
nil,
)
if err != nil {
return MarkAllInboxNotificationsReadResponse{}, err
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
return MarkAllInboxNotificationsReadResponse{}, ReadBodyAsError(res)
}

var resp MarkAllInboxNotificationsReadResponse
return resp, json.NewDecoder(res.Body).Decode(&resp)
}
Loading