Skip to content

Commit

Permalink
refactor: enum EmailCategory -> EmailType
Browse files Browse the repository at this point in the history
  • Loading branch information
Shamzic committed Nov 20, 2023
1 parent 03a1302 commit 1d530eb
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 64 deletions.
17 changes: 7 additions & 10 deletions backend/controllers/emails.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import { EmailCategory } from "../../lib/enums/messaging.js"
import { EmailType } from "../../lib/enums/messaging.js"
import emailRender from "../../backend/lib/mes-aides/emails/email-render.js"
import { SurveyCategory } from "../../lib/enums/survey.js"

const renderFollowupEmailByType = async (
followup,
emailType: EmailCategory
) => {
const renderFollowupEmailByType = async (followup, emailType: EmailType) => {
let surveyType: SurveyCategory | undefined

switch (emailType) {
case EmailCategory.SimulationResults:
return emailRender(EmailCategory.SimulationResults, followup)
case EmailCategory.SimulationUsefulness:
case EmailType.SimulationResults:
return emailRender(EmailType.SimulationResults, followup)
case EmailType.SimulationUsefulness:
surveyType = SurveyCategory.TrackClickOnSimulationUsefulnessEmail
break
case EmailCategory.BenefitAction:
case EmailType.BenefitAction:
surveyType = SurveyCategory.TrackClickOnBenefitActionEmail
break
default:
Expand All @@ -26,7 +23,7 @@ const renderFollowupEmailByType = async (

const getFollowupEmail = async (req, res, next) => {
try {
const { emailType }: { emailType: EmailCategory } = req.query
const { emailType }: { emailType: EmailType } = req.query
const followup = req.followup
const result = await renderFollowupEmailByType(followup, emailType)
res.send(result["html"])
Expand Down
36 changes: 18 additions & 18 deletions backend/lib/mes-aides/emails/email-render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import config from "../../../config/index.js"
import openfiscaController from "../../openfisca/parameters.js"
import { formatBenefits, basicBenefitText } from "./simulation-results.js"
import { mjml } from "./index.js"
import { EmailCategory } from "../../../../lib/enums/messaging.js"
import { EmailType } from "../../../../lib/enums/messaging.js"

const __dirname = new URL(".", import.meta.url).pathname

Expand All @@ -26,10 +26,10 @@ const tousABordNotificationTemplate = readFile(
"templates/tous-a-bord-notification.mjml"
)
const emailTemplates = {
[EmailCategory.SimulationResults]: simulationResultsTemplate,
[EmailCategory.BenefitAction]: benefitActionTemplate,
[EmailCategory.SimulationUsefulness]: simulationUsefulnessTemplate,
[EmailCategory.TousABordNotification]: tousABordNotificationTemplate,
[EmailType.SimulationResults]: simulationResultsTemplate,
[EmailType.BenefitAction]: benefitActionTemplate,
[EmailType.SimulationUsefulness]: simulationUsefulnessTemplate,
[EmailType.TousABordNotification]: tousABordNotificationTemplate,
}
const simulationResultsTextTemplate = readFile(
"templates/simulation-results.txt"
Expand All @@ -42,14 +42,14 @@ const tousABordNotificationTextTemplate = readFile(
"templates/tous-a-bord-notification.txt"
)
const textTemplates = {
[EmailCategory.SimulationResults]: simulationResultsTextTemplate,
[EmailCategory.BenefitAction]: benefitActionTextTemplate,
[EmailCategory.SimulationUsefulness]: simulationUsefulnessTextTemplate,
[EmailCategory.TousABordNotification]: tousABordNotificationTextTemplate,
[EmailType.SimulationResults]: simulationResultsTextTemplate,
[EmailType.BenefitAction]: benefitActionTextTemplate,
[EmailType.SimulationUsefulness]: simulationUsefulnessTextTemplate,
[EmailType.TousABordNotification]: tousABordNotificationTextTemplate,
}

const dataTemplateBuilder = (
emailType: EmailCategory,
emailType: EmailType,
followup,
formatedBenefits,
benefitTexts
Expand All @@ -72,11 +72,11 @@ const dataTemplateBuilder = (
}
}

function renderAsText(emailType: EmailCategory, dataTemplate) {
function renderAsText(emailType: EmailType, dataTemplate) {
return mustache.render(textTemplates[emailType], dataTemplate)
}

function renderAsHtml(emailType: EmailCategory, dataTemplate) {
function renderAsHtml(emailType: EmailType, dataTemplate) {
if (!(emailType in emailTemplates)) {
throw new Error(`Unknown email type: ${emailType}`)
}
Expand All @@ -90,13 +90,13 @@ function renderAsHtml(emailType: EmailCategory, dataTemplate) {
})
}

export default async function emailRender(emailType: EmailCategory, followup) {
export default async function emailRender(emailType: EmailType, followup) {
let benefits: any = null
let parameters: any = null
let formatedBenefits: any = {}
let benefitTexts: any = {}

if (emailType === EmailCategory.SimulationResults) {
if (emailType === EmailType.SimulationResults) {
const populated = await (followup.populated("simulation")
? Promise.resolve(followup)
: followup.populate("simulation"))
Expand Down Expand Up @@ -125,16 +125,16 @@ export default async function emailRender(emailType: EmailCategory, followup) {
renderAsText(emailType, dataTemplate),
renderAsHtml(emailType, dataTemplate),
]).then((values) => {
if (emailType === EmailCategory.SimulationResults) {
if (emailType === EmailType.SimulationResults) {
return {
subject: `Récapitulatif de votre simulation sur 1jeune1solution.gouv.fr [${followup.simulation._id}]`,
text: values[0],
html: values[1].html,
attachments: values[1].attachments,
}
} else if (
emailType === EmailCategory.BenefitAction ||
emailType === EmailCategory.SimulationUsefulness
emailType === EmailType.BenefitAction ||
emailType === EmailType.SimulationUsefulness
) {
return {
subject: `Votre simulation sur 1jeune1solution.gouv.fr vous a-t-elle été utile ? [${
Expand All @@ -143,7 +143,7 @@ export default async function emailRender(emailType: EmailCategory, followup) {
text: values[0],
html: values[1].html,
}
} else if (emailType === EmailCategory.TousABordNotification) {
} else if (emailType === EmailType.TousABordNotification) {
return {
subject: `Déplacez-vous pour 5€ / mois sur votre réseau bus et TER`,
text: values[0],
Expand Down
8 changes: 4 additions & 4 deletions backend/lib/messaging/email/email-service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { sendEmailSmtp } from "../../smtp.js"
import emailRender from "../../mes-aides/emails/email-render.js"
import { EmailCategory } from "../../../../lib/enums/messaging.js"
import { EmailType } from "../../../../lib/enums/messaging.js"

export async function sendEmail(category: EmailCategory, followup) {
export async function sendEmail(emailType: EmailType, followup) {
try {
const render: any = await emailRender(category, followup)
const render: any = await emailRender(emailType, followup)
const response = await sendEmailSmtp({
to: followup.email,
subject: render.subject,
text: render.text,
html: render.html,
tags: [category],
tags: [emailType],
})
followup.sentAt = Date.now()
followup.messageId = response.messageId
Expand Down
21 changes: 9 additions & 12 deletions backend/lib/messaging/sending.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import dayjs from "dayjs"

import { EmailCategory } from "../../../lib/enums/messaging.js"
import { EmailType } from "../../../lib/enums/messaging.js"
import { SurveyCategory } from "../../../lib/enums/survey.js"
import Followups from "../../models/followup.js"
import { Followup } from "../../../lib/types/followup.js"

const DaysBeforeInitialEmail = 6
const DaysBeforeTousABordNotificationEmail = 2

async function sendMultipleEmails(emailType: EmailCategory, limit: number) {
async function sendMultipleEmails(emailType: EmailType, limit: number) {
switch (emailType) {
case EmailCategory.InitialSurvey:
case EmailType.InitialSurvey:
await sendMultipleInitialEmails(limit)
break
case EmailCategory.TousABordNotification:
case EmailType.TousABordNotification:
await sendMultipleTousABordNotificationEmails(limit)
break
default:
Expand Down Expand Up @@ -103,10 +103,7 @@ async function sendMultipleTousABordNotificationEmails(limit: number) {
console.log(results)
}

async function processSingleEmail(
emailType: EmailCategory,
followupId: string
) {
async function processSingleEmail(emailType: EmailType, followupId: string) {
const followup: Followup | null = await Followups.findById(followupId)
if (!followup) {
throw new Error("Followup not found")
Expand All @@ -115,15 +112,15 @@ async function processSingleEmail(
let emailPromise: Promise<void>

switch (emailType) {
case EmailCategory.SimulationResults:
case EmailType.SimulationResults:
emailPromise = followup.sendSimulationResultsEmail()
break
case EmailCategory.BenefitAction:
case EmailType.BenefitAction:
emailPromise = followup.sendSurvey(
SurveyCategory.TrackClickOnBenefitActionEmail
)
break
case EmailCategory.SimulationUsefulness:
case EmailType.SimulationUsefulness:
emailPromise = followup.sendSurvey(
SurveyCategory.TrackClickOnSimulationUsefulnessEmail
)
Expand All @@ -137,7 +134,7 @@ async function processSingleEmail(
}

export async function processSendEmails(
emailType: EmailCategory,
emailType: EmailType,
followupId: string,
multiple: number | null
) {
Expand Down
12 changes: 6 additions & 6 deletions backend/models/followup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import axios from "axios"
import { Survey } from "../../lib/types/survey.js"
import { SurveyCategory } from "../../lib/enums/survey.js"
import emailRender from "../lib/mes-aides/emails/email-render.js"
import { EmailCategory } from "../../lib/enums/messaging.js"
import { EmailType } from "../../lib/enums/messaging.js"
import config from "../config/index.js"
import { Followup } from "../../lib/types/followup.d.js"
import { FollowupModel } from "../types/models.d.js"
Expand All @@ -27,12 +27,12 @@ FollowupSchema.method("postSimulationResultsSms", function (messageId) {
})

FollowupSchema.method("renderSimulationResultsEmail", function () {
return emailRender(EmailCategory.SimulationResults, this)
return emailRender(EmailType.SimulationResults, this)
})

FollowupSchema.method("sendSimulationResultsEmail", async function () {
try {
return await sendEmail(EmailCategory.SimulationResults, this)
return await sendEmail(EmailType.SimulationResults, this)
} catch (err) {
console.log("error", err)
this.error = JSON.stringify(err, null, 2)
Expand Down Expand Up @@ -83,11 +83,11 @@ FollowupSchema.method("sendSimulationResultsSms", async function () {
FollowupSchema.method("renderSurveyEmail", function (surveyType) {
switch (surveyType) {
case SurveyCategory.TrackClickOnBenefitActionEmail:
return emailRender(EmailCategory.BenefitAction, this)
return emailRender(EmailType.BenefitAction, this)
case SurveyCategory.TrackClickOnSimulationUsefulnessEmail:
return emailRender(EmailCategory.SimulationUsefulness, this)
return emailRender(EmailType.SimulationUsefulness, this)
case SurveyCategory.TousABordNotification:
return emailRender(EmailCategory.TousABordNotification, this)
return emailRender(EmailType.TousABordNotification, this)
case SurveyCategory.BenefitAction:
return Promise.reject(
new Error(
Expand Down
2 changes: 1 addition & 1 deletion lib/enums/messaging.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export enum EmailCategory {
export enum EmailType {
SimulationResults = "simulation-results",
BenefitAction = "benefit-action",
SimulationUsefulness = "simulation-usefulness",
Expand Down
12 changes: 6 additions & 6 deletions tools/email-sending-tool.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ArgumentParser } from "argparse"
import config from "../backend/config/index.js"
import { EmailCategory } from "../lib/enums/messaging.js"
import { EmailType } from "../lib/enums/messaging.js"
import mongoose from "mongoose"
import mongooseConfig from "../backend/config/mongoose.js"
import { processSendEmails } from "../backend/lib/messaging/sending.js"
Expand All @@ -25,9 +25,9 @@ const send_types = send.add_subparsers({

// Single emails types parsers
const singleEmailTypes = [
EmailCategory.SimulationResults,
EmailCategory.BenefitAction,
EmailCategory.SimulationUsefulness,
EmailType.SimulationResults,
EmailType.BenefitAction,
EmailType.SimulationUsefulness,
]
singleEmailTypes.forEach((emailType) => {
const parser = send_types.add_parser(emailType)
Expand All @@ -38,8 +38,8 @@ singleEmailTypes.forEach((emailType) => {

// Multiple emails types parsers
const multipleEmailTypes = [
EmailCategory.InitialSurvey,
EmailCategory.TousABordNotification,
EmailType.InitialSurvey,
EmailType.TousABordNotification,
]
multipleEmailTypes.forEach((emailType) => {
const parser = send_types.add_parser(emailType)
Expand Down
14 changes: 7 additions & 7 deletions tools/mjml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import mongoose from "mongoose"
import configMongoose from "../backend/config/mongoose.js"
import config from "../backend/config/index.js"
import { EmailCategory } from "../lib/enums/messaging.js"
import { EmailType } from "../lib/enums/messaging.js"
import express from "express"
import Followups from "../backend/models/followup.js"
// To load the simulation model in mongoose
Expand All @@ -22,7 +22,7 @@ const port = process.env.PORT || 9001
// Setup Express
const app = express()

const typeKeys = EmailCategory
const typeKeys = EmailType

app.engine(".html", __express)
app.set("views", new URL(".", import.meta.url).pathname + "/views")
Expand All @@ -42,16 +42,16 @@ app.route("/").get(function (req, res) {

const followupRendering = async (req: Request) => {
const { followup } = req
const emailType = req.params.type as EmailCategory
const emailType = req.params.type as EmailType
let surveyType: SurveyCategory | undefined

switch (emailType) {
case EmailCategory.SimulationResults:
return emailRender(EmailCategory.SimulationResults, followup)
case EmailCategory.SimulationUsefulness:
case EmailType.SimulationResults:
return emailRender(EmailType.SimulationResults, followup)
case EmailType.SimulationUsefulness:
surveyType = SurveyCategory.TrackClickOnSimulationUsefulnessEmail
break
case EmailCategory.BenefitAction:
case EmailType.BenefitAction:
surveyType = SurveyCategory.TrackClickOnBenefitActionEmail
break
default:
Expand Down

0 comments on commit 1d530eb

Please sign in to comment.