Skip to content
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

Save email preferences changes #4548

Merged
merged 5 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export const AlertAddressForm = (props: Props) => {
const chosenOption = newValue as EmailUpdateCommTypeOfOptions;
const body: EmailUpdateCommOptionRequest = {
instantBreachAlerts: chosenOption,
monthlyMonitorReport: monitorReportAllowed,
};
void fetch("/api/v1/user/update-comm-option", {
method: "POST",
Expand All @@ -85,11 +84,6 @@ export const AlertAddressForm = (props: Props) => {
// Fetch a new token with up-to-date subscriber info - specifically,
// with this setting updated.
void session.update();
// Make sure the dashboard re-fetches the breaches on the next visit,
// in order to make resolved breaches move to the "Fixed" tab.
// If we had used server actions, we could've called
// `revalidatePath("/user/dashboard")` there, but the API doesn't appear
// to necessarily share a cache with the client.
router.refresh();
});
},
Expand All @@ -98,12 +92,12 @@ export const AlertAddressForm = (props: Props) => {
const handleMonthlyMonitorReportToggle = () => {
const newValue = !activateMonthlyMonitorReport;
setActivateMonthlyMonitorReport(newValue);
const body: EmailUpdateCommOptionRequest = {
monthlyMonitorReport: newValue,
};
void fetch("/api/v1/user/update-comm-option", {
method: "POST",
body: JSON.stringify({
instantBreachAlerts: commsValue(),
monthlyMonitorReport: newValue,
}),
body: JSON.stringify(body),
}).then(() => {
void session.update();
router.refresh();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,6 @@ it("sends an API call to disable monthly monitor reports", async () => {

expect(global.fetch).toHaveBeenCalledWith("/api/v1/user/update-comm-option", {
body: JSON.stringify({
instantBreachAlerts: "primary",
monthlyMonitorReport: false,
}),
method: "POST",
Expand Down
13 changes: 9 additions & 4 deletions src/app/api/v1/user/update-comm-option/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import {
export type EmailUpdateCommTypeOfOptions = "null" | "affected" | "primary";

export interface EmailUpdateCommOptionRequest {
instantBreachAlerts: EmailUpdateCommTypeOfOptions;
monthlyMonitorReport: boolean;
instantBreachAlerts?: EmailUpdateCommTypeOfOptions;
monthlyMonitorReport?: boolean;
}

export async function POST(req: NextRequest) {
Expand Down Expand Up @@ -48,8 +48,13 @@ export async function POST(req: NextRequest) {
default:
allEmailsToPrimary = null;
}
await setAllEmailsToPrimary(subscriber, allEmailsToPrimary);
await setMonthlyMonitorReport(subscriber, monthlyMonitorReport);

Copy link
Collaborator

Choose a reason for hiding this comment

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

I can't leave a comment on the relevant lines because they're unchanged, but it's best to update the type representing the request body to indicate that one of the properties should now be left empty:

export interface EmailUpdateCommOptionRequest {
  instantBreachAlerts?: EmailUpdateCommTypeOfOptions;
  monthlyMonitorReport?: boolean;
}

And then TS will show you what code to wrap in an if (typeof <x> !== "undefined"), and you won't need the custom type guard (isEmailUpdateCommType).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed in 22b41c4

if (typeof instantBreachAlerts !== "undefined") {
await setAllEmailsToPrimary(subscriber, allEmailsToPrimary);
}
if (typeof monthlyMonitorReport === "boolean") {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@Vinnl Can the condition here be !== undefined too?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yep!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It ended up just being an issue of passing the wrong var! my bad

await setMonthlyMonitorReport(subscriber, monthlyMonitorReport);
}

return NextResponse.json({
success: true,
Expand Down
Loading