Skip to content

Commit f4818df

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

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),
@@ -8232,41 +8233,51 @@ func (q *FakeQuerier) UpdateUserLoginType(_ context.Context, arg database.Update
82328233
return database.User{}, sql.ErrNoRows
82338234
}
82348235

8235-
func (q *FakeQuerier) UpdateUserNotificationPreferences(ctx context.Context, arg database.UpdateUserNotificationPreferencesParams) (int64, error) {
8236+
func (q *FakeQuerier) UpdateUserNotificationPreferences(_ context.Context, arg database.UpdateUserNotificationPreferencesParams) (int64, error) {
82368237
err := validateDatabaseType(arg)
82378238
if err != nil {
8238-
return 0, err
8239+
return -1, err
82398240
}
82408241

82418242
q.mutex.Lock()
82428243
defer q.mutex.Unlock()
82438244

82448245
var upserted int64
8245-
for i, templateID := range arg.NotificationTemplateIds {
8246+
for i := range arg.NotificationTemplateIds {
82468247
var (
8247-
found *database.NotificationPreference
8248-
disabled = arg.Disableds[i]
8248+
found bool
8249+
templateID = arg.NotificationTemplateIds[i]
8250+
disabled = arg.Disableds[i]
82498251
)
82508252

8251-
for _, np := range q.notificationPreferences {
8252-
if np.UserID != arg.UserID && np.NotificationTemplateID != templateID {
8253+
for j, np := range q.notificationPreferences {
8254+
if np.UserID != arg.UserID {
8255+
continue
8256+
}
8257+
8258+
if np.NotificationTemplateID != templateID {
82538259
continue
82548260
}
82558261

8256-
found = &np
8262+
np.Disabled = disabled
8263+
np.UpdatedAt = time.Now()
8264+
q.notificationPreferences[j] = np
8265+
8266+
upserted++
8267+
found = true
8268+
break
82578269
}
82588270

8259-
if found != nil {
8260-
found.Disabled = disabled
8261-
found.UpdatedAt = time.Now()
8262-
} else {
8263-
q.notificationPreferences = append(q.notificationPreferences, database.NotificationPreference{
8271+
if !found {
8272+
np := database.NotificationPreference{
82648273
Disabled: disabled,
82658274
UserID: arg.UserID,
82668275
NotificationTemplateID: templateID,
82678276
CreatedAt: time.Now(),
82688277
UpdatedAt: time.Now(),
8269-
})
8278+
}
8279+
q.notificationPreferences = append(q.notificationPreferences, np)
8280+
upserted++
82708281
}
82718282
}
82728283

0 commit comments

Comments
 (0)