Skip to content

Commit af75c74

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

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),
@@ -8123,41 +8124,51 @@ func (q *FakeQuerier) UpdateUserLoginType(_ context.Context, arg database.Update
81238124
return database.User{}, sql.ErrNoRows
81248125
}
81258126

8126-
func (q *FakeQuerier) UpdateUserNotificationPreferences(ctx context.Context, arg database.UpdateUserNotificationPreferencesParams) (int64, error) {
8127+
func (q *FakeQuerier) UpdateUserNotificationPreferences(_ context.Context, arg database.UpdateUserNotificationPreferencesParams) (int64, error) {
81278128
err := validateDatabaseType(arg)
81288129
if err != nil {
8129-
return 0, err
8130+
return -1, err
81308131
}
81318132

81328133
q.mutex.Lock()
81338134
defer q.mutex.Unlock()
81348135

81358136
var upserted int64
8136-
for i, templateID := range arg.NotificationTemplateIds {
8137+
for i := range arg.NotificationTemplateIds {
81378138
var (
8138-
found *database.NotificationPreference
8139-
disabled = arg.Disableds[i]
8139+
found bool
8140+
templateID = arg.NotificationTemplateIds[i]
8141+
disabled = arg.Disableds[i]
81408142
)
81418143

8142-
for _, np := range q.notificationPreferences {
8143-
if np.UserID != arg.UserID && np.NotificationTemplateID != templateID {
8144+
for j, np := range q.notificationPreferences {
8145+
if np.UserID != arg.UserID {
8146+
continue
8147+
}
8148+
8149+
if np.NotificationTemplateID != templateID {
81448150
continue
81458151
}
81468152

8147-
found = &np
8153+
np.Disabled = disabled
8154+
np.UpdatedAt = time.Now()
8155+
q.notificationPreferences[j] = np
8156+
8157+
upserted++
8158+
found = true
8159+
break
81488160
}
81498161

8150-
if found != nil {
8151-
found.Disabled = disabled
8152-
found.UpdatedAt = time.Now()
8153-
} else {
8154-
q.notificationPreferences = append(q.notificationPreferences, database.NotificationPreference{
8162+
if !found {
8163+
np := database.NotificationPreference{
81558164
Disabled: disabled,
81568165
UserID: arg.UserID,
81578166
NotificationTemplateID: templateID,
81588167
CreatedAt: time.Now(),
81598168
UpdatedAt: time.Now(),
8160-
})
8169+
}
8170+
q.notificationPreferences = append(q.notificationPreferences, np)
8171+
upserted++
81618172
}
81628173
}
81638174

0 commit comments

Comments
 (0)