updateInboxNotificationReadStatus changes the read status of a notification. @Summary Update read status of a notification @ID update-read-status-of-a-notification @Security CoderSessionToken @Produce json @Tags Notifications @Param id path string true "id of the notification" @Success 200 {object}
(rw http.ResponseWriter, r *http.Request)
| 374 | // @Success 200 {object} codersdk.Response |
| 375 | // @Router /api/v2/notifications/inbox/{id}/read-status [put] |
| 376 | func (api *API) updateInboxNotificationReadStatus(rw http.ResponseWriter, r *http.Request) { |
| 377 | var ( |
| 378 | ctx = r.Context() |
| 379 | apikey = httpmw.APIKey(r) |
| 380 | ) |
| 381 | |
| 382 | notificationID, ok := httpmw.ParseUUIDParam(rw, r, "id") |
| 383 | if !ok { |
| 384 | return |
| 385 | } |
| 386 | |
| 387 | var body codersdk.UpdateInboxNotificationReadStatusRequest |
| 388 | if !httpapi.Read(ctx, rw, r, &body) { |
| 389 | return |
| 390 | } |
| 391 | |
| 392 | err := api.Database.UpdateInboxNotificationReadStatus(ctx, database.UpdateInboxNotificationReadStatusParams{ |
| 393 | ID: notificationID, |
| 394 | ReadAt: func() sql.NullTime { |
| 395 | if body.IsRead { |
| 396 | return sql.NullTime{ |
| 397 | Time: dbtime.Now(), |
| 398 | Valid: true, |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | return sql.NullTime{} |
| 403 | }(), |
| 404 | }) |
| 405 | if err != nil { |
| 406 | api.Logger.Error(ctx, "failed to update inbox notification read status", slog.Error(err)) |
| 407 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 408 | Message: "Failed to update inbox notification read status.", |
| 409 | }) |
| 410 | return |
| 411 | } |
| 412 | |
| 413 | unreadCount, err := api.Database.CountUnreadInboxNotificationsByUserID(ctx, apikey.UserID) |
| 414 | if err != nil { |
| 415 | api.Logger.Error(ctx, "failed to call count unread inbox notifications", slog.Error(err)) |
| 416 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 417 | Message: "Failed to call count unread inbox notifications.", |
| 418 | }) |
| 419 | return |
| 420 | } |
| 421 | |
| 422 | updatedNotification, err := api.Database.GetInboxNotificationByID(ctx, notificationID) |
| 423 | if err != nil { |
| 424 | api.Logger.Error(ctx, "failed to get notification by id", slog.Error(err)) |
| 425 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 426 | Message: "Failed to get notification by id.", |
| 427 | }) |
| 428 | return |
| 429 | } |
| 430 | |
| 431 | httpapi.Write(ctx, rw, http.StatusOK, codersdk.UpdateInboxNotificationReadStatusResponse{ |
| 432 | Notification: convertInboxNotificationResponse(ctx, api.Logger, updatedNotification), |
| 433 | UnreadCount: int(unreadCount), |
no test coverage detected