Skip to content

Commit 81d9261

Browse files
committed
UpdateUserNotificationPreferences
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent a30ee5c commit 81d9261

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

coderd/database/dbmem/dbmem.go

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func New() database.Store {
6565
files: make([]database.File, 0),
6666
gitSSHKey: make([]database.GitSSHKey, 0),
6767
notificationMessages: make([]database.NotificationMessage, 0),
68+
notificationPreferences: make([]database.NotificationPreference, 0),
6869
parameterSchemas: make([]database.ParameterSchema, 0),
6970
provisionerDaemons: make([]database.ProvisionerDaemon, 0),
7071
workspaceAgents: make([]database.WorkspaceAgent, 0),
@@ -8143,41 +8144,51 @@ func (q *FakeQuerier) UpdateUserLoginType(_ context.Context, arg database.Update
81438144
return database.User{}, sql.ErrNoRows
81448145
}
81458146

8146-
func (q *FakeQuerier) UpdateUserNotificationPreferences(ctx context.Context, arg database.UpdateUserNotificationPreferencesParams) (int64, error) {
8147+
func (q *FakeQuerier) UpdateUserNotificationPreferences(_ context.Context, arg database.UpdateUserNotificationPreferencesParams) (int64, error) {
81478148
err := validateDatabaseType(arg)
81488149
if err != nil {
8149-
return 0, err
8150+
return -1, err
81508151
}
81518152

81528153
q.mutex.Lock()
81538154
defer q.mutex.Unlock()
81548155

81558156
var upserted int64
8156-
for i, templateID := range arg.NotificationTemplateIds {
8157+
for i := range arg.NotificationTemplateIds {
81578158
var (
8158-
found *database.NotificationPreference
8159-
disabled = arg.Disableds[i]
8159+
found bool
8160+
templateID = arg.NotificationTemplateIds[i]
8161+
disabled = arg.Disableds[i]
81608162
)
81618163

8162-
for _, np := range q.notificationPreferences {
8163-
if np.UserID != arg.UserID && np.NotificationTemplateID != templateID {
8164+
for j, np := range q.notificationPreferences {
8165+
if np.UserID != arg.UserID {
8166+
continue
8167+
}
8168+
8169+
if np.NotificationTemplateID != templateID {
81648170
continue
81658171
}
81668172

8167-
found = &np
8173+
np.Disabled = disabled
8174+
np.UpdatedAt = time.Now()
8175+
q.notificationPreferences[j] = np
8176+
8177+
upserted++
8178+
found = true
8179+
break
81688180
}
81698181

8170-
if found != nil {
8171-
found.Disabled = disabled
8172-
found.UpdatedAt = time.Now()
8173-
} else {
8174-
q.notificationPreferences = append(q.notificationPreferences, database.NotificationPreference{
8182+
if !found {
8183+
np := database.NotificationPreference{
81758184
Disabled: disabled,
81768185
UserID: arg.UserID,
81778186
NotificationTemplateID: templateID,
81788187
CreatedAt: time.Now(),
81798188
UpdatedAt: time.Now(),
8180-
})
8189+
}
8190+
q.notificationPreferences = append(q.notificationPreferences, np)
8191+
upserted++
81818192
}
81828193
}
81838194

0 commit comments

Comments
 (0)