@Summary Update notifications settings @ID update-notifications-settings @Security CoderSessionToken @Accept json @Produce json @Tags Notifications @Param request body codersdk.NotificationsSettings true "Notifications settings request" @Success 200 {object} codersdk.NotificationsSettings @Success 3
(rw http.ResponseWriter, r *http.Request)
| 63 | // @Success 304 |
| 64 | // @Router /api/v2/notifications/settings [put] |
| 65 | func (api *API) putNotificationsSettings(rw http.ResponseWriter, r *http.Request) { |
| 66 | ctx := r.Context() |
| 67 | |
| 68 | var settings codersdk.NotificationsSettings |
| 69 | if !httpapi.Read(ctx, rw, r, &settings) { |
| 70 | return |
| 71 | } |
| 72 | |
| 73 | settingsJSON, err := json.Marshal(&settings) |
| 74 | if err != nil { |
| 75 | httpapi.Write(r.Context(), rw, http.StatusInternalServerError, codersdk.Response{ |
| 76 | Message: "Failed to marshal notifications settings.", |
| 77 | Detail: err.Error(), |
| 78 | }) |
| 79 | return |
| 80 | } |
| 81 | |
| 82 | currentSettingsJSON, err := api.Database.GetNotificationsSettings(ctx) |
| 83 | if err != nil { |
| 84 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 85 | Message: "Failed to fetch current notifications settings.", |
| 86 | Detail: err.Error(), |
| 87 | }) |
| 88 | return |
| 89 | } |
| 90 | |
| 91 | if bytes.Equal(settingsJSON, []byte(currentSettingsJSON)) { |
| 92 | // See: https://www.rfc-editor.org/rfc/rfc7232#section-4.1 |
| 93 | httpapi.Write(ctx, rw, http.StatusNotModified, nil) |
| 94 | return |
| 95 | } |
| 96 | |
| 97 | auditor := api.Auditor.Load() |
| 98 | aReq, commitAudit := audit.InitRequest[database.NotificationsSettings](rw, &audit.RequestParams{ |
| 99 | Audit: *auditor, |
| 100 | Log: api.Logger, |
| 101 | Request: r, |
| 102 | Action: database.AuditActionWrite, |
| 103 | }) |
| 104 | defer commitAudit() |
| 105 | |
| 106 | aReq.New = database.NotificationsSettings{ |
| 107 | ID: uuid.New(), |
| 108 | NotifierPaused: settings.NotifierPaused, |
| 109 | } |
| 110 | |
| 111 | err = api.Database.UpsertNotificationsSettings(ctx, string(settingsJSON)) |
| 112 | if err != nil { |
| 113 | if rbac.IsUnauthorizedError(err) { |
| 114 | httpapi.Forbidden(rw) |
| 115 | return |
| 116 | } |
| 117 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 118 | Message: "Failed to update notifications settings.", |
| 119 | Detail: err.Error(), |
| 120 | }) |
| 121 | |
| 122 | return |
nothing calls this directly
no test coverage detected