Skip to content

Commit

Permalink
add script to send email to pre-fxa subscribers
Browse files Browse the repository at this point in the history
  • Loading branch information
groovecoder committed Oct 31, 2019
1 parent 3ff522f commit 71bc0bb
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 41 deletions.
39 changes: 0 additions & 39 deletions controllers/hibp.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const { negotiateLanguages, acceptedLanguages } = require("fluent-langneg");
const AppConstants = require("../app-constants");
const DB = require("../db/DB");
const EmailUtils = require("../email-utils");
const EmailHelpers = require("../template-helpers/emails.js");
const HIBP = require("../hibp");
const { LocaleUtils } = require ("../locale-utils");
const mozlog = require("../log");
Expand Down Expand Up @@ -123,42 +122,6 @@ async function notify (req, res) {
);
}

async function sendEmailToPreFxaSubscribers (req, res) {
const subscribers = [ /* summoning groooooovecoder */ ];
const notifiedSubscribers = [];
const utmID = "pre-fxa";
for (const subscriber of subscribers) {
const signupLanguage = subscriber.signup_language;
const subscriberEmail = subscriber.email;
const requestedLanguage = signupLanguage ? acceptedLanguages(signupLanguage) : "";
const supportedLocales = negotiateLanguages(
requestedLanguage,
req.app.locals.AVAILABLE_LANGUAGES,
{defaultLocale: "en"}
);

if (!notifiedSubscribers.includes(subscriberEmail)) {
await EmailUtils.sendEmail(
subscriberEmail,
LocaleUtils.fluentFormat(supportedLocales, "pre-fxa-subject"), // email subject
"default_email", // email template
{
supportedLocales,
SERVER_URL: req.app.locals.SERVER_URL,
unsubscribeUrl: EmailUtils.getUnsubscribeUrl(subscriber, utmID), // need to test the flow for legacy users who want to unsubscribe
ctaHref: EmailHelpers.getPreFxaUtmParams(req.app.locals.SERVER_URL, "create-account-button", subscriberEmail),
whichPartial: "email_partials/pre-fxa",
preFxaEmail: true,
email: subscriberEmail,
},
);
notifiedSubscribers.push(subscriberEmail);
}
}

res.redirect("/email-l10n");

}

async function breaches (req, res, next) {
res.append("Last-Modified", req.app.locals.mostRecentBreachDateTime);
Expand All @@ -167,9 +130,7 @@ async function breaches (req, res, next) {




module.exports = {
notify,
breaches,
sendEmailToPreFxaSubscribers,
};
12 changes: 12 additions & 0 deletions db/DB.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ const DB = {
return subscriberAndEmails;
},

async getPreFxaSubscribers() {
// We don't want to get subscribers records where the email address has
// since been added "under" an FxA subscription
const allEmailAddressesSubquery = knex("email_addresses").select("email");
const preFxaSubscribers = await knex("subscribers").where({
"fxa_uid": "",
}).andWhere(
"primary_email", "not in", allEmailAddressesSubquery
);
return preFxaSubscribers;
},

async getSubscriberByEmail(email) {
const [subscriber] = await knex("subscribers").where({
"primary_email": email,
Expand Down
1 change: 1 addition & 0 deletions locale-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const LocaleUtils = {
}
log.info("LocaleUtils.init", {availableLanguages});
log.info("LocaleUtils.init", {fluentBundles});
return {availableLanguages, fluentBundles};
},

loadLanguagesIntoApp (app) {
Expand Down
3 changes: 1 addition & 2 deletions routes/hibp.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const bodyParser = require("body-parser");
const bearerToken = require("express-bearer-token");

const {asyncMiddleware} = require("../middleware");
const {notify, breaches, sendEmailToPreFxaSubscribers} = require("../controllers/hibp");
const {notify, breaches} = require("../controllers/hibp");


const router = express.Router();
Expand All @@ -15,6 +15,5 @@ const jsonParser = bodyParser.json();
router.use("/notify", bearerToken());
router.post("/notify", jsonParser, asyncMiddleware(notify));
router.get("/breaches", jsonParser, asyncMiddleware(breaches));
router.get("/sendPreFxaEmail", jsonParser, asyncMiddleware(sendEmailToPreFxaSubscribers));

module.exports = router;
49 changes: 49 additions & 0 deletions scripts/send-email-to-pre-fxa-subscribers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"use strict";

const { negotiateLanguages, acceptedLanguages } = require("fluent-langneg");

const AppConstants = require("../app-constants");
const DB = require("../db/DB");
const EmailHelpers = require("../template-helpers/emails.js");
const EmailUtils = require("../email-utils");
const { LocaleUtils } = require ("../locale-utils");


(async (req) => {
const localeUtils = LocaleUtils.init();
EmailUtils.init();

const subscribers = await DB.getPreFxaSubscribers();
console.log(`Found ${subscribers.length} pre-FxA subscribers whose email hasn't been added to an FxA subscription.`);
const notifiedSubscribers = [];
const utmID = "pre-fxa";
for (const subscriber of subscribers) {
const signupLanguage = subscriber.signup_language;
const subscriberEmail = subscriber.primary_email;
const requestedLanguage = signupLanguage ? acceptedLanguages(signupLanguage) : "";
const supportedLocales = negotiateLanguages(
requestedLanguage,
localeUtils.availableLanguages,
{defaultLocale: "en"}
);

if (!notifiedSubscribers.includes(subscriberEmail)) {
await EmailUtils.sendEmail(
subscriberEmail,
LocaleUtils.fluentFormat(supportedLocales, "pre-fxa-subject"), // email subject
"default_email", // email template
{
supportedLocales,
SERVER_URL: AppConstants.SERVER_URL,
unsubscribeUrl: EmailUtils.getUnsubscribeUrl(subscriber, utmID), // need to test the flow for legacy users who want to unsubscribe
ctaHref: EmailHelpers.getPreFxaUtmParams(AppConstants.SERVER_URL, "create-account-button", subscriberEmail),
whichPartial: "email_partials/pre-fxa",
preFxaEmail: true,
email: subscriberEmail,
},
);
notifiedSubscribers.push(subscriberEmail);
}
}
console.log(`Notified subscribers: ${JSON.stringify(notifiedSubscribers)}`);
})();

0 comments on commit 71bc0bb

Please sign in to comment.