Skip to content

Commit

Permalink
Remove localstorage.js
Browse files Browse the repository at this point in the history
The last remaining reference was in
`src/scripts/cronjobs/emailBreachAlerts`, which didn't actually use
it; it only stored the list of relevant locales there. There's no
more code remaining that read that.
  • Loading branch information
Vinnl committed Sep 17, 2024
1 parent 894412e commit 577724d
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 131 deletions.
218 changes: 96 additions & 122 deletions src/scripts/cronjobs/emailBreachAlerts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import React from "react";
import Sentry from "@sentry/nextjs";
import { acceptedLanguages, negotiateLanguages } from "@fluent/langneg";
import { localStorage } from "../../utils/localStorage.js";

import * as pubsub from "@google-cloud/pubsub";
import * as grpc from "@grpc/grpc-js";
Expand Down Expand Up @@ -242,133 +240,109 @@ export async function poll(
console.info("Subscriber already notified, skipping: ", subscriberId);
continue;
}
const { recipientEmail, breachedEmail, signupLanguage } =
const { recipientEmail, breachedEmail } =
getAddressesAndLanguageForEmail(recipient);

/* c8 ignore start */
const requestedLanguage = signupLanguage
? acceptedLanguages(signupLanguage)
: [];
/* c8 ignore stop */

const availableLanguages = process.env.SUPPORTED_LOCALES!.split(",");
const supportedLocales = negotiateLanguages(
requestedLanguage,
availableLanguages,
{ defaultLocale: "en" },
);

await localStorage.run(new Map(), async () => {
localStorage.getStore().set("locale", supportedLocales);
await (async () => {
if (!notifiedRecipients.includes(breachedEmail)) {
// try to append a new row into the email notifications table
// if the append fails, there might be already an entry, stop the script
try {
await addEmailNotification({
breachId,
subscriberId,
notified: false,
email: recipientEmail,
notificationType: "incident",
});

const l10n = getCronjobL10n(sanitizeSubscriberRow(recipient));
const enabledFeatureFlags = await getEnabledFeatureFlags({
email: recipient.primary_email,
});
if (enabledFeatureFlags.includes("BreachEmailRedesign")) {
/**
* Without an active user session, we don't know the user's country. This is
* our best guess based on their locale. At the time of writing, it's only
* used to determine whether to count SSN breaches (which we don't have
* recommendations for outside the US).
*/
const assumedCountryCode = getSignupLocaleCountry(recipient);

// The unit tests are currently too complex for me to write
// a proper test for this, and I need to understand the code
// better to be able to refactor it to make it more amenable
// to simple tests. Hence, I don't have a test for this yet:
/* c8 ignore next 3 */
if (typeof recipient.onerep_profile_id === "number") {
await refreshStoredScanResults(recipient.onerep_profile_id);
}

let dataSummary: DashboardSummary | undefined;
if (
isEligibleForPremium(assumedCountryCode) &&
!hasPremium(recipient)
) {
const scanData = await getLatestOnerepScanResults(
recipient.onerep_profile_id,
);
const allSubscriberBreaches = await getSubscriberBreaches({
fxaUid: recipient.fxa_uid,
countryCode: assumedCountryCode,
});
dataSummary = getDashboardSummary(
scanData.results,
allSubscriberBreaches,
);
}

const subject = l10n.getString(
"email-breach-alert-all-subject",
);

await sendEmail(
recipientEmail,
subject,
renderEmail(
<RedesignedBreachAlertEmail
l10n={l10n}
breach={breachAlert}
breachedEmail={breachedEmail}
utmCampaignId={utmCampaignId}
enabledFeatureFlags={enabledFeatureFlags}
subscriber={recipient}
dataSummary={dataSummary}
/>,
),
);
} else {
const subject = l10n.getString("breach-alert-subject");
await sendEmail(
recipientEmail,
subject,
renderEmail(
<BreachAlertEmail
l10n={l10n}
breach={breachAlert}
breachedEmail={breachedEmail}
utmCampaignId={utmCampaignId}
subscriber={recipient}
/>,
),
);
}
} catch (e) {
console.error("Failed to add email notification to table: ", e);
setTimeout(process.exit, 1000);
if (!notifiedRecipients.includes(breachedEmail)) {
// try to append a new row into the email notifications table
// if the append fails, there might be already an entry, stop the script
try {
await addEmailNotification({
breachId,
subscriberId,
notified: false,
email: recipientEmail,
notificationType: "incident",
});

const l10n = getCronjobL10n(sanitizeSubscriberRow(recipient));
const enabledFeatureFlags = await getEnabledFeatureFlags({
email: recipient.primary_email,
});
if (enabledFeatureFlags.includes("BreachEmailRedesign")) {
/**
* Without an active user session, we don't know the user's country. This is
* our best guess based on their locale. At the time of writing, it's only
* used to determine whether to count SSN breaches (which we don't have
* recommendations for outside the US).
*/
const assumedCountryCode = getSignupLocaleCountry(recipient);

// The unit tests are currently too complex for me to write
// a proper test for this, and I need to understand the code
// better to be able to refactor it to make it more amenable
// to simple tests. Hence, I don't have a test for this yet:
/* c8 ignore next 3 */
if (typeof recipient.onerep_profile_id === "number") {
await refreshStoredScanResults(recipient.onerep_profile_id);
}

// mark email as notified in database
// if this call ever fails, stop stop the script with an error
try {
await markEmailAsNotified(
subscriberId,
breachId,
recipientEmail,
let dataSummary: DashboardSummary | undefined;
if (
isEligibleForPremium(assumedCountryCode) &&
!hasPremium(recipient)
) {
const scanData = await getLatestOnerepScanResults(
recipient.onerep_profile_id,
);
const allSubscriberBreaches = await getSubscriberBreaches({
fxaUid: recipient.fxa_uid,
countryCode: assumedCountryCode,
});
dataSummary = getDashboardSummary(
scanData.results,
allSubscriberBreaches,
);
} catch (e: any) {
console.error("Failed to mark email as notified: ", e);
throw new Error(e);
}
notifiedRecipients.push(breachedEmail);

const subject = l10n.getString("email-breach-alert-all-subject");

await sendEmail(
recipientEmail,
subject,
renderEmail(
<RedesignedBreachAlertEmail
l10n={l10n}
breach={breachAlert}
breachedEmail={breachedEmail}
utmCampaignId={utmCampaignId}
enabledFeatureFlags={enabledFeatureFlags}
subscriber={recipient}
dataSummary={dataSummary}
/>,
),
);
} else {
const subject = l10n.getString("breach-alert-subject");
await sendEmail(
recipientEmail,
subject,
renderEmail(
<BreachAlertEmail
l10n={l10n}
breach={breachAlert}
breachedEmail={breachedEmail}
utmCampaignId={utmCampaignId}
subscriber={recipient}
/>,
),
);
}
})();
});
} catch (e) {
console.error("Failed to add email notification to table: ", e);
setTimeout(process.exit, 1000);
}

// mark email as notified in database
// if this call ever fails, stop stop the script with an error
try {
await markEmailAsNotified(subscriberId, breachId, recipientEmail);
} catch (e: any) {
console.error("Failed to mark email as notified: ", e);
throw new Error(e);
}
notifiedRecipients.push(breachedEmail);
}
}

console.info("notified", { length: notifiedRecipients.length });
Expand Down
9 changes: 0 additions & 9 deletions src/utils/localStorage.js

This file was deleted.

0 comments on commit 577724d

Please sign in to comment.