Skip to content

Commit

Permalink
Save email preferences changes (#4548)
Browse files Browse the repository at this point in the history
* Email pref saved

* fix unit test break

* remove typeguard func

* right val
  • Loading branch information
codemist committed May 21, 2024
1 parent 6f9e52b commit 6928aba
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
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);

if (typeof instantBreachAlerts !== "undefined") {
await setAllEmailsToPrimary(subscriber, allEmailsToPrimary);
}
if (typeof monthlyMonitorReport === "boolean") {
await setMonthlyMonitorReport(subscriber, monthlyMonitorReport);
}

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

0 comments on commit 6928aba

Please sign in to comment.