mirror of
https://github.com/coder/coder.git
synced 2025-07-03 16:13:58 +00:00
feat: add tool to send a test notification (#16611)
Relates to https://github.com/coder/coder/issues/16463 Adds a CLI command, and API endpoint, to trigger a test notification for administrators of a deployment.
This commit is contained in:
@ -11,9 +11,12 @@ import (
|
||||
|
||||
"github.com/coder/coder/v2/coderd/audit"
|
||||
"github.com/coder/coder/v2/coderd/database"
|
||||
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
||||
"github.com/coder/coder/v2/coderd/httpapi"
|
||||
"github.com/coder/coder/v2/coderd/httpmw"
|
||||
"github.com/coder/coder/v2/coderd/notifications"
|
||||
"github.com/coder/coder/v2/coderd/rbac"
|
||||
"github.com/coder/coder/v2/coderd/rbac/policy"
|
||||
"github.com/coder/coder/v2/codersdk"
|
||||
)
|
||||
|
||||
@ -163,6 +166,53 @@ func (api *API) notificationDispatchMethods(rw http.ResponseWriter, r *http.Requ
|
||||
})
|
||||
}
|
||||
|
||||
// @Summary Send a test notification
|
||||
// @ID send-a-test-notification
|
||||
// @Security CoderSessionToken
|
||||
// @Tags Notifications
|
||||
// @Success 200
|
||||
// @Router /notifications/test [post]
|
||||
func (api *API) postTestNotification(rw http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
key = httpmw.APIKey(r)
|
||||
)
|
||||
|
||||
if !api.Authorize(r, policy.ActionUpdate, rbac.ResourceDeploymentConfig) {
|
||||
httpapi.Forbidden(rw)
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := api.NotificationsEnqueuer.EnqueueWithData(
|
||||
//nolint:gocritic // We need to be notifier to send the notification.
|
||||
dbauthz.AsNotifier(ctx),
|
||||
key.UserID,
|
||||
notifications.TemplateTestNotification,
|
||||
map[string]string{},
|
||||
map[string]any{
|
||||
// NOTE(DanielleMaywood):
|
||||
// When notifications are enqueued, they are checked to be
|
||||
// unique within a single day. This means that if we attempt
|
||||
// to send two test notifications to the same user on
|
||||
// the same day, the enqueuer will prevent us from sending
|
||||
// a second one. We are injecting a timestamp to make the
|
||||
// notifications appear different enough to circumvent this
|
||||
// deduplication logic.
|
||||
"timestamp": api.Clock.Now(),
|
||||
},
|
||||
"send-test-notification",
|
||||
); err != nil {
|
||||
api.Logger.Error(ctx, "send notification", slog.Error(err))
|
||||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
||||
Message: "Failed to send test notification",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
httpapi.Write(ctx, rw, http.StatusOK, nil)
|
||||
}
|
||||
|
||||
// @Summary Get user notification preferences
|
||||
// @ID get-user-notification-preferences
|
||||
// @Security CoderSessionToken
|
||||
|
Reference in New Issue
Block a user