Skip to content

Commit fe8aa2d

Browse files
committed
Self-review
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent ae3b854 commit fe8aa2d

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

coderd/database/modelmethods.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,6 @@ func (a GetOAuth2ProviderAppsByUserIDRow) RBACObject() rbac.Object {
290290
return a.OAuth2ProviderApp.RBACObject()
291291
}
292292

293-
func (n NotificationPreference) RBACObject() rbac.Object {
294-
return rbac.ResourceNotificationPreference.WithOwner(n.UserID.String())
295-
}
296-
297293
type WorkspaceAgentConnectionStatus struct {
298294
Status WorkspaceAgentStatus `json:"status"`
299295
FirstConnectedAt *time.Time `json:"first_connected_at"`

coderd/notifications.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,17 @@ func (api *API) userNotificationPreferences(rw http.ResponseWriter, r *http.Requ
161161

162162
prefs, err := api.Database.GetUserNotificationPreferences(ctx, user.ID)
163163
if err != nil {
164-
logger.Error(ctx, "failed to retrieve")
164+
logger.Error(ctx, "failed to retrieve preferences", slog.Error(err))
165165

166-
httpapi.Write(r.Context(), rw, http.StatusInternalServerError, codersdk.Response{
166+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
167167
Message: "Failed to retrieve user notification preferences.",
168168
Detail: err.Error(),
169169
})
170170
return
171171
}
172172

173173
out := convertNotificationPreferences(prefs)
174-
httpapi.Write(r.Context(), rw, http.StatusOK, out)
174+
httpapi.Write(ctx, rw, http.StatusOK, out)
175175
}
176176

177177
// @Summary Update user notification preferences
@@ -191,11 +191,13 @@ func (api *API) putUserNotificationPreferences(rw http.ResponseWriter, r *http.R
191191
logger = api.Logger.Named("notifications.preferences").With(slog.F("user_id", user.ID))
192192
)
193193

194+
// Parse request.
194195
var prefs codersdk.UpdateUserNotificationPreferences
195196
if !httpapi.Read(ctx, rw, r, &prefs) {
196197
return
197198
}
198199

200+
// Build query params.
199201
input := database.UpdateUserNotificationPreferencesParams{
200202
UserID: user.ID,
201203
NotificationTemplateIds: make([]uuid.UUID, 0, len(prefs.TemplateDisabledMap)),
@@ -204,9 +206,9 @@ func (api *API) putUserNotificationPreferences(rw http.ResponseWriter, r *http.R
204206
for tmplID, disabled := range prefs.TemplateDisabledMap {
205207
id, err := uuid.Parse(tmplID)
206208
if err != nil {
207-
logger.Warn(ctx, "failed to parse notification template UUID", slog.F("input", tmplID))
209+
logger.Warn(ctx, "failed to parse notification template UUID", slog.F("input", tmplID), slog.Error(err))
208210

209-
httpapi.Write(r.Context(), rw, http.StatusBadRequest, codersdk.Response{
211+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
210212
Message: "Unable to parse notification template UUID.",
211213
Detail: err.Error(),
212214
})
@@ -217,32 +219,34 @@ func (api *API) putUserNotificationPreferences(rw http.ResponseWriter, r *http.R
217219
input.Disableds = append(input.Disableds, disabled)
218220
}
219221

222+
// Update preferences with params.
220223
updated, err := api.Database.UpdateUserNotificationPreferences(ctx, input)
221224
if err != nil {
222225
logger.Error(ctx, "failed to update preferences", slog.Error(err))
223226

224-
httpapi.Write(r.Context(), rw, http.StatusInternalServerError, codersdk.Response{
227+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
225228
Message: "Failed to update user notifications preferences.",
226229
Detail: err.Error(),
227230
})
228231
return
229232
}
230233

234+
// Preferences updated, now fetch all preferences belonging to this user.
231235
logger.Info(ctx, "updated preferences", slog.F("count", updated))
232236

233237
userPrefs, err := api.Database.GetUserNotificationPreferences(ctx, user.ID)
234238
if err != nil {
235239
logger.Error(ctx, "failed to retrieve preferences", slog.Error(err))
236240

237-
httpapi.Write(r.Context(), rw, http.StatusInternalServerError, codersdk.Response{
241+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
238242
Message: "Failed to retrieve user notifications preferences.",
239243
Detail: err.Error(),
240244
})
241245
return
242246
}
243247

244248
out := convertNotificationPreferences(userPrefs)
245-
httpapi.Write(r.Context(), rw, http.StatusOK, out)
249+
httpapi.Write(ctx, rw, http.StatusOK, out)
246250
}
247251

248252
func convertNotificationTemplates(in []database.NotificationTemplate) (out []codersdk.NotificationTemplate) {

codersdk/notifications.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (c *Client) GetSystemNotificationTemplates(ctx context.Context) ([]Notifica
112112
return templates, nil
113113
}
114114

115-
// GetUserNotificationPreferences TODO
115+
// GetUserNotificationPreferences retrieves notification preferences for a given user.
116116
func (c *Client) GetUserNotificationPreferences(ctx context.Context, userID uuid.UUID) ([]NotificationPreference, error) {
117117
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/users/%s/notifications/preferences", userID.String()), nil)
118118
if err != nil {
@@ -137,7 +137,7 @@ func (c *Client) GetUserNotificationPreferences(ctx context.Context, userID uuid
137137
return prefs, nil
138138
}
139139

140-
// UpdateUserNotificationPreferences TODO
140+
// UpdateUserNotificationPreferences updates notification preferences for a given user.
141141
func (c *Client) UpdateUserNotificationPreferences(ctx context.Context, userID uuid.UUID, req UpdateUserNotificationPreferences) ([]NotificationPreference, error) {
142142
res, err := c.Request(ctx, http.MethodPut, fmt.Sprintf("/api/v2/users/%s/notifications/preferences", userID.String()), req)
143143
if err != nil {

0 commit comments

Comments
 (0)