Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor pour renommer des enums #4073

Merged
merged 2 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions backend/controllers/emails.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import { EmailCategory } from "../../backend/enums/email.js"
import { EmailType } from "../../backend/enums/email.js"
import emailRender from "../../backend/lib/mes-aides/emails/email-render.js"
import { SurveyCategory } from "../../lib/enums/survey.js"
import { SurveyType } from "../../lib/enums/survey.js"

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

switch (emailType) {
case EmailCategory.SimulationResults:
return emailRender(EmailCategory.SimulationResults, followup)
case EmailCategory.SimulationUsefulness:
surveyType = SurveyCategory.TrackClickOnSimulationUsefulnessEmail
case EmailType.SimulationResults:
return emailRender(EmailType.SimulationResults, followup)
case EmailType.SimulationUsefulness:
surveyType = SurveyType.TrackClickOnSimulationUsefulnessEmail
break
case EmailCategory.BenefitAction:
surveyType = SurveyCategory.TrackClickOnBenefitActionEmail
case EmailType.BenefitAction:
surveyType = SurveyType.TrackClickOnBenefitActionEmail
break
default:
throw new Error(`Unknown email type: ${emailType}`)
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
26 changes: 13 additions & 13 deletions backend/controllers/followups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Followup } from "../../lib/types/followup.d.js"
import Benefits from "../../data/all.js"
import pollResult from "../lib/mattermost-bot/poll-result.js"
import simulationController from "./simulation.js"
import { SurveyCategory } from "../../lib/enums/survey.js"
import { SurveyType } from "../../lib/enums/survey.js"
import { FollowupFactory } from "../lib/followup-factory.js"
import { FetchSurvey } from "../../lib/types/survey.d.js"
import Request from "../types/express.d.js"
Expand Down Expand Up @@ -110,7 +110,7 @@ export function showSurveyResults(req: Request, res: Response) {
surveys: {
$elemMatch: {
repliedAt: { $exists: true },
type: SurveyCategory.BenefitAction,
type: SurveyType.BenefitAction,
},
},
})
Expand Down Expand Up @@ -149,7 +149,7 @@ export async function followupByAccessToken(
}

export function postSurvey(req: Request, res: Response) {
req.followup.updateSurvey(SurveyCategory.BenefitAction, req.body).then(() => {
req.followup.updateSurvey(SurveyType.BenefitAction, req.body).then(() => {
res.sendStatus(201)
})
pollResult.postPollResult(req.followup, req.body)
Expand All @@ -164,30 +164,30 @@ export async function updateWasUseful(req: Request) {
]
const { followup } = req
await followup.updateSurvey(
SurveyCategory.TrackClickOnSimulationUsefulnessEmail,
SurveyType.TrackClickOnSimulationUsefulnessEmail,
answers
)
await followup.save()
}

async function updateTrackClickOnBenefitActionEmail(req: Request) {
const { followup } = req
await followup.updateSurvey(SurveyCategory.TrackClickOnBenefitActionEmail)
await followup.updateSurvey(SurveyType.TrackClickOnBenefitActionEmail)
}

async function updateSurveyInFollowup(req: Request) {
const { surveyType } = req.params
const { followup } = req

switch (surveyType) {
case SurveyCategory.TrackClickOnSimulationUsefulnessEmail:
case SurveyType.TrackClickOnSimulationUsefulnessEmail:
await updateWasUseful(req)
break
case SurveyCategory.TrackClickOnBenefitActionEmail:
case SurveyType.TrackClickOnBenefitActionEmail:
await updateTrackClickOnBenefitActionEmail(req)
break
case SurveyCategory.TousABordNotification:
await followup.updateSurvey(SurveyCategory.TousABordNotification)
case SurveyType.TousABordNotification:
await followup.updateSurvey(SurveyType.TousABordNotification)
break
default:
throw new Error("Unknown survey type")
Expand All @@ -199,13 +199,13 @@ async function getRedirectUrl(req: Request) {
const { followup } = req

switch (surveyType) {
case SurveyCategory.TrackClickOnSimulationUsefulnessEmail:
case SurveyCategory.TrackClickOnBenefitActionEmail:
await followup.addSurveyIfMissing(SurveyCategory.BenefitAction)
case SurveyType.TrackClickOnSimulationUsefulnessEmail:
case SurveyType.TrackClickOnBenefitActionEmail:
await followup.addSurveyIfMissing(SurveyType.BenefitAction)
await followup.save()

return followup.surveyPath
case SurveyCategory.TousABordNotification:
case SurveyType.TousABordNotification:
return "https://www.tadao.fr/713-Demandeur-d-emploi.html"
default:
throw new Error("Unknown survey type")
Expand Down
2 changes: 1 addition & 1 deletion backend/enums/email.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 backend/lib/email.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ArgumentParser } from "argparse"
import config from "../config/index.js"
import { EmailCategory } from "../enums/email.js"
import { EmailType } from "../enums/email.js"
import mongoose from "mongoose"
import mongooseConfig from "../config/mongoose.js"
import { processSendEmails } from "./emails/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
41 changes: 19 additions & 22 deletions backend/lib/emails/sending.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import dayjs from "dayjs"

import { EmailCategory } from "../../enums/email.js"
import { SurveyCategory } from "../../../lib/enums/survey.js"
import { EmailType } from "../../enums/email.js"
import { SurveyType } 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 All @@ -28,9 +28,9 @@ async function sendMultipleInitialEmails(limit: number) {
$elemMatch: {
type: {
$in: [
SurveyCategory.BenefitAction,
SurveyCategory.TrackClickOnSimulationUsefulnessEmail,
SurveyCategory.TrackClickOnBenefitActionEmail,
SurveyType.BenefitAction,
SurveyType.TrackClickOnSimulationUsefulnessEmail,
SurveyType.TrackClickOnBenefitActionEmail,
],
},
},
Expand All @@ -48,8 +48,8 @@ async function sendMultipleInitialEmails(limit: number) {
followups.map(async (followup: Followup) => {
const surveyType =
Math.random() > 0.5
? SurveyCategory.TrackClickOnBenefitActionEmail
: SurveyCategory.TrackClickOnSimulationUsefulnessEmail
? SurveyType.TrackClickOnBenefitActionEmail
: SurveyType.TrackClickOnSimulationUsefulnessEmail

try {
const result = await followup.sendSurvey(surveyType)
Expand Down Expand Up @@ -80,7 +80,7 @@ async function sendMultipleTousABordNotificationEmails(limit: number) {
surveys: {
$not: {
$elemMatch: {
type: SurveyCategory.TousABordNotification,
type: SurveyType.TousABordNotification,
},
},
},
Expand All @@ -92,7 +92,7 @@ async function sendMultipleTousABordNotificationEmails(limit: number) {
followups.map(async (followup: Followup) => {
try {
const result = await followup.sendSurvey(
SurveyCategory.TousABordNotification
SurveyType.TousABordNotification
)
return { ok: result._id }
} catch (error) {
Expand All @@ -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,17 +112,17 @@ 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
SurveyType.TrackClickOnBenefitActionEmail
)
break
case EmailCategory.SimulationUsefulness:
case EmailType.SimulationUsefulness:
emailPromise = followup.sendSurvey(
SurveyCategory.TrackClickOnSimulationUsefulnessEmail
SurveyType.TrackClickOnSimulationUsefulnessEmail
)
break
default:
Expand All @@ -137,7 +134,7 @@ async function processSingleEmail(
}

export async function processSendEmails(
emailType: EmailCategory,
emailType: EmailType,
followupId: string,
multiple: number | null
) {
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 "../../../enums/email.js"
import { EmailType } from "../../../enums/email.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
Loading