-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'MNTOR-3919' into MNTOR-3918
- Loading branch information
Showing
12 changed files
with
298 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/db/migrations/20250109140803_add_churn_prevention_email_sent.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
/** | ||
* @param { import("knex").Knex } knex | ||
* @returns { Promise<void> } | ||
*/ | ||
export function up (knex) { | ||
return knex.schema.table("subscribers", table => { | ||
table.timestamp("churn_prevention_email_sent_at"); | ||
table.index("churn_prevention_email_sent_at"); | ||
}); | ||
} | ||
|
||
/** | ||
* @param { import("knex").Knex } knex | ||
* @returns { Promise<void> } | ||
*/ | ||
export function down (knex) { | ||
return knex.schema.table("subscribers", table => { | ||
table.dropIndex("churn_prevention_email_sent_at") | ||
table.dropColumn("churn_prevention_email_sent_at"); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
export async function up(knex) { | ||
await knex.schema | ||
.createTable('subscriber_churns', function(table) { | ||
table.increments('id').primary(); | ||
table.string('userid').unique(); | ||
table.string('customer').unique(); | ||
table.string('plan_id'); | ||
table.string('product_id'); | ||
table.string('intervl'); | ||
table.string('nickname'); | ||
table.timestamp('created'); | ||
table.timestamp('current_period_end'); | ||
}) | ||
} | ||
|
||
/** | ||
* @param { import("knex").Knex } knex | ||
* @returns { Promise<void> } | ||
*/ | ||
export async function down(knex) { | ||
return knex.schema | ||
.dropTableIfExists("subscriber_churns") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import createDbConnection from "../connect"; | ||
import { logger } from "../../app/functions/server/logging"; | ||
import { SubscriberChurnRow } from "knex/types/tables"; | ||
|
||
const knex = createDbConnection(); | ||
|
||
async function upsertSubscriberChurns( | ||
churningSubscribers: SubscriberChurnRow[], | ||
): Promise<SubscriberChurnRow[]> { | ||
logger.info("upsert_subscriber_churns", { | ||
count: churningSubscribers.length, | ||
}); | ||
|
||
try { | ||
const res = await knex("onerep_subscriber_churns") | ||
.insert(churningSubscribers) | ||
.onConflict("userid") | ||
.merge(["intervl", "current_period_end"]) | ||
.returning("*"); | ||
|
||
logger.info("upsert_subscriber_churns_success", { count: res.length }); | ||
return res as SubscriberChurnRow[]; | ||
} catch (e) { | ||
logger.error("upsert_subscriber_churns_error", { | ||
error: JSON.stringify(e), | ||
}); | ||
throw e; | ||
} | ||
} | ||
|
||
export { upsertSubscriberChurns }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
import { | ||
getChurnPreventionEmailSentAt, | ||
markChurnPreventionEmailAsJustSent, | ||
} from "../../db/tables/subscribers"; | ||
// import { getFreeSubscribersWaitingForMonthlyEmail } from "../../db/tables/subscribers"; | ||
// import { getScanResultsWithBroker } from "../../db/tables/onerep_scans"; | ||
// import { updateEmailPreferenceForSubscriber } from "../../db/tables/subscriber_email_preferences"; | ||
// import { renderEmail } from "../../emails/renderEmail"; | ||
// import { MonthlyActivityFreeEmail } from "../../emails/templates/monthlyActivityFree/MonthlyActivityFreeEmail"; | ||
// import { getCronjobL10n } from "../../app/functions/l10n/cronjobs"; | ||
// import { sanitizeSubscriberRow } from "../../app/functions/server/sanitize"; | ||
// import { getDashboardSummary } from "../../app/functions/server/dashboard"; | ||
// import { getSubscriberBreaches } from "../../app/functions/server/getSubscriberBreaches"; | ||
// import { refreshStoredScanResults } from "../../app/functions/server/refreshStoredScanResults"; | ||
// import { getSignupLocaleCountry } from "../../emails/functions/getSignupLocaleCountry"; | ||
// import { getMonthlyActivityFreeUnsubscribeLink } from "../../app/functions/cronjobs/unsubscribeLinks"; | ||
// import { hasPremium } from "../../app/functions/universal/user"; | ||
// import { SubscriberRow } from "knex/types/tables"; | ||
import createDbConnection from "../../db/connect"; | ||
import { logger } from "../../app/functions/server/logging"; | ||
import { initEmail, sendEmail, closeEmailPool } from "../../utils/email"; | ||
// Imports the Google Cloud client library | ||
import { Storage } from "@google-cloud/storage"; | ||
import csv from "csv-parser"; | ||
|
||
await run(); | ||
await createDbConnection().destroy(); | ||
|
||
interface FxaChurnSubscriber { | ||
userid: string; | ||
customer: string; | ||
created: string; | ||
nickname: string; | ||
intervl: "monthly" | "yearly"; | ||
intervl_count: number; | ||
plan_id: string; | ||
product_id: string; | ||
current_period_end: string; | ||
} | ||
|
||
async function readCSVFromBucket( | ||
bucketName: string, | ||
fileName: string, | ||
): Promise<FxaChurnSubscriber[]> { | ||
const storage = new Storage(); | ||
const bucket = storage.bucket(bucketName); | ||
const file = bucket.file(fileName); | ||
|
||
const results: FxaChurnSubscriber[] = []; | ||
|
||
return new Promise((resolve, reject) => { | ||
file | ||
.createReadStream() | ||
.pipe(csv()) | ||
.on("data", (row: FxaChurnSubscriber) => { | ||
/** | ||
* Verifies the interval is yearly | ||
* Ensures current_period_end exists | ||
* Checks if the time difference is less than or equal to 7 days (in milliseconds) | ||
* Makes sure the date is in the future | ||
*/ | ||
if ( | ||
row.intervl === "yearly" && | ||
row.current_period_end && | ||
new Date(row.current_period_end).getTime() - new Date().getTime() <= | ||
7 * 24 * 60 * 60 * 1000 && | ||
new Date(row.current_period_end).getTime() > new Date().getTime() | ||
) { | ||
results.push(row); | ||
} | ||
}) | ||
.on("error", reject) | ||
.on("end", () => { | ||
logger.info( | ||
`CSV file successfully processed. Num of rows: ${results.length}`, | ||
); | ||
resolve(results); | ||
}); | ||
}); | ||
} | ||
|
||
async function run() { | ||
const bucketName = process.env.GCP_BUCKET; | ||
if (!bucketName) { | ||
throw `Bucket name isn't set ( process.env.GCP_BUCKET = ${process.env.GCP_BUCKET}), please set: 'GCP_BUCKET'`; | ||
} | ||
const fileName = "churningSubscribers.csv"; | ||
const subscribersToEmail = await readCSVFromBucket(bucketName, fileName); | ||
|
||
await initEmail(); | ||
|
||
for (const subscriber of subscribersToEmail) { | ||
try { | ||
// we need to query our db to make sure the email wasn't sent in the past | ||
const sentDate = await getChurnPreventionEmailSentAt( | ||
parseInt(subscriber.userid, 10), | ||
); | ||
if (sentDate) { | ||
logger.warn("send_churn_discount_email_warn", { | ||
subscriberId: subscriber.userid, | ||
message: `email already sent for the user at: ${sentDate}`, | ||
}); | ||
continue; | ||
} | ||
// send email | ||
await sendChurnDiscountEmail(subscriber); | ||
logger.info("send_churn_discount_email_success", { | ||
subscriberId: subscriber.userid, | ||
}); | ||
} catch (error) { | ||
logger.error("send_churn_discount_email_error", { | ||
subscriberId: subscriber.userid, | ||
error, | ||
}); | ||
} | ||
} | ||
|
||
closeEmailPool(); | ||
logger.info( | ||
`[${new Date(Date.now()).toISOString()}] Sent [${subscribersToEmail.length}] churn email to relevant subscribers.`, | ||
); | ||
} | ||
|
||
async function sendChurnDiscountEmail(subscriber: FxaChurnSubscriber) { | ||
logger.info(`sent email to: ${subscriber.userid}`); | ||
// mark as sent | ||
// await markChurnPreventionEmailAsJustSent(parseInt(subscriber.userid, 10)) | ||
} |
Oops, something went wrong.