Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
chore: begin impl of test notification
  • Loading branch information
DanielleMaywood committed Feb 18, 2025
commit 53d69757c4ab215c6293e266fb4aa4386360739c
1 change: 1 addition & 0 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -1370,6 +1370,7 @@ func New(options *Options) *API {
r.Get("/system", api.systemNotificationTemplates)
})
r.Get("/dispatch-methods", api.notificationDispatchMethods)
r.Post("/test", api.postTestNotification)
})
r.Route("/tailnet", func(r chi.Router) {
r.Use(apiKeyMiddleware)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DELETE FROM notification_templates WHERE id = 'c425f63e-716a-4bf4-ae24-78348f706c3f';
11 changes: 11 additions & 0 deletions coderd/database/migrations/000291_test_notification.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
INSERT INTO notification_templates
(id, name, title_template, body_template, "group", actions)
VALUES (
'c425f63e-716a-4bf4-ae24-78348f706c3f',
'Test Notification',
E'A test notification',
E'Hi {{.UserName}},\n\n'||
E'This is a test notification.',
'Notification Events',
'[]'::jsonb
);
38 changes: 38 additions & 0 deletions coderd/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ 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/codersdk"
)
Expand Down Expand Up @@ -163,6 +165,42 @@ func (api *API) notificationDispatchMethods(rw http.ResponseWriter, r *http.Requ
})
}

// @Summary Send a test notification
// @ID post-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 _, 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{
// TODO: This is maybe not the best idea, but we want to avoid
// the notification de-duplication logic.
"timestamp": api.Clock.Now(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥇

},
"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
Expand Down
5 changes: 5 additions & 0 deletions coderd/notifications/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ var (

TemplateWorkspaceBuildsFailedReport = uuid.MustParse("34a20db2-e9cc-4a93-b0e4-8569699d7a00")
)

// Notification-related events.
var (
TemplateTestNotification = uuid.MustParse("c425f63e-716a-4bf4-ae24-78348f706c3f")
)
4 changes: 4 additions & 0 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2286,6 +2286,10 @@ class ApiMethods {
return res.data;
};

postTestNotification = async () => {
await this.axios.post<void>("/api/v2/notifications/test");
};

updateNotificationTemplateMethod = async (
templateId: string,
req: TypesGen.UpdateNotificationTemplateMethod,
Expand Down
10 changes: 10 additions & 0 deletions site/src/api/queries/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ export const notificationDispatchMethods = () => {
};
};

export const notificationTestKey = ["notifications", "test"];

export const sendTestNotification = (queryClient: QueryClient) => {
return {
mutationFn: async () => {
await API.postTestNotification();
},
} satisfies UseMutationOptions;
};

export const updateNotificationTemplateMethod = (
templateId: string,
queryClient: QueryClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Interpolation, Theme } from "@emotion/react";
import {
notificationDispatchMethods,
selectTemplatesByGroup,
sendTestNotification,
systemNotificationTemplates,
} from "api/queries/notifications";
import { Loader } from "components/Loader/Loader";
Expand All @@ -12,11 +13,13 @@ import { castNotificationMethod } from "modules/notifications/utils";
import { Section } from "pages/UserSettingsPage/Section";
import type { FC } from "react";
import { Helmet } from "react-helmet-async";
import { useQueries } from "react-query";
import { useMutation, useQueries, useQueryClient } from "react-query";
import { deploymentGroupHasParent } from "utils/deployOptions";
import { pageTitle } from "utils/page";
import OptionsTable from "../OptionsTable";
import { NotificationEvents } from "./NotificationEvents";
import { Stack } from "components/Stack/Stack";
import { Button } from "components/Button/Button";

export const NotificationsPage: FC = () => {
const { deploymentConfig } = useDeploymentSettings();
Expand All @@ -33,6 +36,8 @@ export const NotificationsPage: FC = () => {
key: "tab",
defaultValue: "events",
});
const queryClient = useQueryClient();
const sendNotification = useMutation(sendTestNotification(queryClient));

const ready = !!(templatesByGroup.data && dispatchMethods.data);
return (
Expand Down Expand Up @@ -71,11 +76,22 @@ export const NotificationsPage: FC = () => {
)}
/>
) : (
<OptionsTable
options={deploymentConfig.options.filter((o) =>
deploymentGroupHasParent(o.group, "Notifications"),
)}
/>
<Stack>
<Button
className="w-fit"
onClick={async () => {
await sendNotification.mutateAsync();
}}
>
Send test notification
</Button>

<OptionsTable
options={deploymentConfig.options.filter((o) =>
deploymentGroupHasParent(o.group, "Notifications"),
)}
/>
</Stack>
)
) : (
<Loader />
Expand Down