Skip to content

Commit

Permalink
fix: add db utils for email pref table
Browse files Browse the repository at this point in the history
  • Loading branch information
mansaj committed Aug 27, 2024
1 parent f30ede0 commit a59d99b
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 76 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"dev:cron:db-pull-breaches": "tsx --tsconfig tsconfig.cronjobs.json src/scripts/cronjobs/syncBreaches.ts",
"dev:cron:remote-settings-pull-breaches": "tsx --tsconfig tsconfig.cronjobs.json src/scripts/cronjobs/updateBreachesInRemoteSettings.ts",
"dev:cron:onerep-limits-alert": "tsx --tsconfig tsconfig.cronjobs.json src/scripts/cronjobs/onerepStatsAlert.ts",
"dev:oneoff:migrate-email-prefs": "tsx --tsconfig tsconfig.cronjobs.json src/scripts/oneoff/migrate_subscribers_email_preferences.ts",
"dev:nimbus": "node --watch-path config/nimbus.yaml src/scripts/build/nimbusTypes.js",
"build": "npm run get-location-data && npm run build-glean && npm run build-nimbus && next build && npm run build-cronjobs",
"cloudrun": "npm run db:migrate && npm start",
Expand Down
91 changes: 76 additions & 15 deletions src/db/tables/subscriber_email_preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,24 @@ import { logger } from "../../app/functions/server/logging";

const knex = createDbConnection();

interface SubscriberEmailPreferences {
instant_breach_alert?: boolean;
all_emails_to_primary?: boolean;
// NOTE: The "subscriber_email_preferences" table only has attributes for free reports
// TODO: modify the CRUD utils after MNTOR-3557
interface SubscriberFreeEmailPreferences {
primary_email?: string;
monthly_monitor_report_free?: boolean;
monthly_monitor_report_free_at?: Date;
}

interface SubscriberPlusEmailPreferences {
monthly_monitor_report?: boolean;
monthly_monitor_report_at?: Date;
}

// TODO: modify after MNTOR-3557 - pref currently lives in two tables
// this function only adds email prefs for free reports
async function addEmailPreferenceForSubscriber(
subscriberId: number,
preference: SubscriberEmailPreferences,
preference: SubscriberFreeEmailPreferences,
) {
logger.info("add_email_preference_for_subscriber", {
subscriberId,
Expand All @@ -28,12 +36,16 @@ async function addEmailPreferenceForSubscriber(
res = await knex("subscriber_email_preferences")
.insert({
subscriber_id: subscriberId,
instant_breach_alert: preference.instant_breach_alert || true,
all_emails_to_primary: preference.all_emails_to_primary || true,
monthly_monitor_report: preference.monthly_monitor_report || true,
monthly_monitor_report_at: preference.monthly_monitor_report_at || null,
primary_email: preference.primary_email || "",
monthly_monitor_report_free:
preference.monthly_monitor_report_free || true,
monthly_monitor_report_free_at:
preference.monthly_monitor_report_free_at || null,
})
.onConflict("subscriber_id")
.ignore()
.returning("*");
logger.debug("add_email_preference_for_subscriber_success");
} catch (e) {
logger.error("error_add_subscriber_email_preference", {
exception: e as string,
Expand All @@ -46,19 +58,49 @@ async function addEmailPreferenceForSubscriber(

async function updateEmailPreferenceForSubscriber(
subscriberId: number,
preference: SubscriberEmailPreferences,
isFree: boolean,
preference: SubscriberFreeEmailPreferences | SubscriberPlusEmailPreferences,
) {
logger.info("update_email_preference_for_subscriber", {
subscriberId,
isFree,
preference,
});

let res;
try {
res = await knex("subscriber_email_preferences")
.where("subscriber_id", subscriberId)
.update({ ...preference })
.returning(["*"]);
if (isFree) {
res = await knex("subscriber_email_preferences")
.where("subscriber_id", subscriberId)
.update({ ...(preference as SubscriberFreeEmailPreferences) })
.onConflict("subscriber_id")
.merge()
.returning(["*"]);

if (res.length !== 1) {
throw new Error(
`Update subscriber ${subscriberId} failed, response: ${JSON.stringify(res)}`,
);
}
} else {
// TODO: modify after MNTOR-3557 - pref currently lives in two tables
res = await knex("subscribers")
.where("id", subscriberId)
.update({
...(preference as SubscriberPlusEmailPreferences),
// @ts-ignore knex.fn.now() results in it being set to a date,
// even if it's not typed as a JS date object:
updated_at: knex.fn.now(),
})
.returning("*");

if (res.length !== 1) {
throw new Error(
`Update subscriber ${subscriberId} failed, response: ${JSON.stringify(res)}`,
);
}
}
logger.debug("update_email_preference_for_subscriber_success");
} catch (e) {
logger.error("error_update_subscriber_email_preference", {
exception: e as string,
Expand All @@ -75,10 +117,29 @@ async function getEmailPreferenceForSubscriber(subscriberId: number) {
});

let res;
// TODO: modify after MNTOR-3557 - pref currently lives in two tables, we have to join the tables
try {
res = await knex("subscriber_email_preferences")
.where("subscriber_id", subscriberId)
res = await knex
.select(
"subscribers.id",
"primary_email",
"all_emails_to_primary",
"monthly_monitor_report",
"monthly_monitor_report_at",
"first_broker_removal_email_sent",
)
.from("subscribers")
.where("subscribers.id", subscriberId)
.innerJoin(
"subscriber_email_preferences",
"subscribers.id",
"subscriber_email_preferences.subscriber_id",
)
.returning(["*"]);
logger.debug("get_email_preference_for_subscriber_success");
logger.debug(
`getEmailPreferenceForSubscriber innerjoin: ${JSON.stringify(res)}`,
);
} catch (e) {
logger.error("error_get_subscriber_email_preference", {
exception: e as string,
Expand Down
60 changes: 0 additions & 60 deletions src/scripts/oneoff/migrate_subscribers_email_preferences.ts

This file was deleted.

0 comments on commit a59d99b

Please sign in to comment.