8000 feat: turn off notification via email by joobisb · Pull Request #14520 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat: turn off notification via email #14520

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 9 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
17 changes: 10 additions & 7 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions coderd/database/queries/notifications.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-- name: FetchNewMessageMetadata :one
-- This is used to build up the notification_message's JSON payload.
SELECT nt.name AS notification_name,
nt.id AS notification_template_id,
nt.actions AS actions,
nt.method AS custom_method,
u.id AS user_id,
Expand Down
1 change: 1 addition & 0 deletions coderd/notifications/dispatch/smtp/html.gotmpl
8000
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<div style="border-top: 1px solid #e2e8f0; color: #475569; font-size: 12px; margin-top: 64px; padding-top: 24px; line-height: 1.6;">
<p>&copy;&nbsp;{{ current_year }}&nbsp;Coder. All rights reserved&nbsp;-&nbsp;<a href="{{ base_url }}" style="color: #2563eb; text-decoration: none;">{{ base_url }}</a></p>
<p><a href="{{ base_url }}/settings/notifications" style="color: #2563eb; text-decoration: none;">Click here to manage your notification settings</a></p>
<p><a href="{{ base_url }}/settings/notifications?unsubscribe={{ .NotificationTemplateID }}" style="color: #2563eb; text-decoration: none;">Stop receiving emails like this</a></p>
</div>
</div>
</body>
Expand Down
3 changes: 2 additions & 1 deletion coderd/notifications/enqueuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ func (s *StoreEnqueuer) buildPayload(metadata database.FetchNewMessageMetadataRo
payload := types.MessagePayload{
Version: "1.0",

NotificationName: metadata.NotificationName,
NotificationName: metadata.NotificationName,
NotificationTemplateID: metadata.NotificationTemplateID.String(),

UserID: metadata.UserID.String(),
UserEmail: metadata.UserEmail,
Expand Down
9 changes: 9 additions & 0 deletions coderd/notifications/render/gotmpl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ func TestGoTemplate(t *testing.T) {
"url": "https://mocked-server-address/@johndoe/my-workspace"
}]`,
},
{
name: "render notification template ID",
in: `{{ .NotificationTemplateID }}`,
payload: types.MessagePayload{
NotificationTemplateID: "4e19c0ac-94e1-4532-9515-d1801aa283b2",
},
expectedOutput: "4e19c0ac-94e1-4532-9515-d1801aa283b2",
expectedErr: nil,
},
}

for _, tc := range tests {
Expand Down
17 changes: 8 additions & 9 deletions coderd/notifications/types/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ package types
type MessagePayload struct {
Version string `json:"_version"`

NotificationName string `json:"notification_name"`

UserID string `json:"user_id"`
UserEmail string `json:"user_email"`
UserName string `json:"user_name"`
UserUsername string `json:"user_username"`

Actions []TemplateAction `json:"actions"`
Labels map[string]string `json:"labels"`
NotificationName string `json:"notification_name"`
NotificationTemplateID string `json:"notification_template_id"`
UserID string 8000 `json:"user_id"`
UserEmail string `json:"user_email"`
UserName string `json:"user_name"`
UserUsername string `json:"user_username"`
Actions []TemplateAction `json:"actions"`
Labels map[string]string `json:"labels"`
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I need to remember to check for the following scenarios:

  1. When a valid template ID is disabled and the request is successful, check if the success message is displayed. Also, ensure that the UI is updated correctly. Verify if the request is being sent properly. Additionally, confirm if the error message is displayed when the request fails.

  2. When a not found template ID is disabled, check if an error message is displayed.

You can check how we do that using Storybook interaction tests on the NotificationsPage.stories.tsx.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have verified these scnearios

Copy link
Contributor

Choose a reason for hiding this comment

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

@joobisb can you add a test please? Manual verification is good but it only helps us know if this works currently, and doesn't catch future degradations.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@joobisb we are close, we just need to automate these tests using the way I shared before. Thanks for your hard work! 🙏

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@BrunoQuaresma the tests needed to be added to NotificationsPage.stories.tsx right ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@joobisb we are close, we just need to automate these tests using the way I shared before. Thanks for your hard work! 🙏

done, please have a look

Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ import {
methodLabels,
} from "modules/notifications/utils";
import { type FC, Fragment } from "react";
import { useEffect } from "react";
import { Helmet } from "react-helmet-async";
import { useMutation, useQueries, useQueryClient } from "react-query";
import { useSearchParams } from "react-router-dom";
import { pageTitle } from "utils/page";
import { Section } from "../Section";

Expand Down Expand Up @@ -60,6 +62,27 @@ export const NotificationsPage: FC = () => {
const updatePreferences = useMutation(
updateUserNotificationPreferences(user.id, queryClient),
);
const [searchParams] = useSearchParams();
const unsubscribeTemplateId = searchParams.get("unsubscribe");

useEffect(() => {
if (unsubscribeTemplateId) {
handleUnsubscribe(unsubscribeTemplateId);
}
}, [unsubscribeTemplateId]);

const handleUnsubscribe = async (templateId: string) => {
await updatePreferences.mutateAsync({
template_disabled_map: {
[templateId]: true,
},
});
displaySuccess("Notification preferences updated");
queryClient.invalidateQueries(
userNotificationPreferences(user.id).queryKey,
);
};

const ready =
disabledPreferences.data && templatesByGroup.data && dispatchMethods.data;

Expand Down
Loading
0