Skip to content

feat: add notification for suspended/activated account #14367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Aug 22, 2024
Prev Previous commit
Next Next commit
notifyUserStatusChanged
  • Loading branch information
mtojek committed Aug 22, 2024
commit 96611b1f630c0c0fc5df3df28fbdf82071ae5a3b
72 changes: 38 additions & 34 deletions coderd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -859,47 +859,15 @@
}
aReq.New = targetUser

// Notify about the change of user status
var key string
var templateID uuid.UUID
switch status {
case database.UserStatusSuspended:
key = "suspended_account_name"
templateID = notifications.TemplateUserAccountSuspended
case database.UserStatusActive:
key = "activated_account_name"
templateID = notifications.TemplateUserAccountActivated
default:
api.Logger.Error(ctx, "unable to notify admins as the user's status is unsupported", slog.F("username", user.Username), slog.F("user_status", string(status)))

httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error preparing notifications",
})
}

// Fetch all users with user admin permissions
userAdmins, err := findUserAdmins(ctx, api.Database)
err = api.notifyUserStatusChanged()

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / lint

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / lint

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / lint

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-e2e

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-e2e-enterprise

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-race

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-race

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-race

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-race

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-race

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-race

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-race

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-race

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-race

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-race

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-pg

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-pg

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-pg

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-pg

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-pg

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-pg

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-pg

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-pg

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-pg

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-pg

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-pg-16

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-pg-16

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-pg-16

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-pg-16

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-pg-16

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-pg-16

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-pg-16

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-pg-16

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-pg-16

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go-pg-16

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

not enough arguments in call to api.notifyUserStatusChanged

Check failure on line 862 in coderd/users.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

not enough arguments in call to api.notifyUserStatusChanged
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error fetching user admins",
Message: "Internal error notifying about changed user status.",
Detail: err.Error(),
})
return
}

// Send notifications to user admins and affected user
for _, u := range append(userAdmins, database.GetUsersRow{ID: user.ID}) {
if _, err := api.NotificationsEnqueuer.Enqueue(ctx, u.ID, templateID,
map[string]string{
key: user.Username,
}, "api-put-user-status",
user.ID,
); err != nil {
api.Logger.Warn(ctx, "unable to notify about changed user's status", slog.F("affected_user", user.Username), slog.Error(err))
}
}

// Finish: build final response
organizations, err := userOrganizationIDs(ctx, api, user)
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Expand All @@ -912,6 +880,42 @@
}
}

func (api *API) notifyUserStatusChanged(ctx context.Context, user database.User, status database.UserStatus) error {
// Notify about the change of user status
var key string
var templateID uuid.UUID
switch status {
case database.UserStatusSuspended:
key = "suspended_account_name"
templateID = notifications.TemplateUserAccountSuspended
case database.UserStatusActive:
key = "activated_account_name"
templateID = notifications.TemplateUserAccountActivated
default:
api.Logger.Error(ctx, "user status is not supported", slog.F("username", user.Username), slog.F("user_status", string(status)))
return xerrors.Errorf("unable to notify admins as the user's status is unsupported")
}

// Fetch all users with user admin permissions
userAdmins, err := findUserAdmins(ctx, api.Database)
if err != nil {
return xerrors.Errorf("unable to find user admins: %w", err)
}

// Send notifications to user admins and affected user
for _, u := range append(userAdmins, database.GetUsersRow{ID: user.ID}) {
if _, err := api.NotificationsEnqueuer.Enqueue(ctx, u.ID, templateID,
map[string]string{
key: user.Username,
}, "api-put-user-status",
user.ID,
); err != nil {
api.Logger.Warn(ctx, "unable to notify about changed user's status", slog.F("affected_user", user.Username), slog.Error(err))
}
}
return nil
}

// @Summary Update user appearance settings
// @ID update-user-appearance-settings
// @Security CoderSessionToken
Expand Down
Loading