From dce9cf36ea65d386d2039aab92977de49151057b Mon Sep 17 00:00:00 2001 From: Shamzic Date: Thu, 9 Nov 2023 12:33:33 +0100 Subject: [PATCH 01/25] =?UTF-8?q?refactor:=20d=C3=A9place=20enum=20email?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/controllers/emails.ts | 2 +- backend/lib/email.ts | 2 +- backend/lib/emails/sending.ts | 2 +- backend/lib/mes-aides/emails/email-render.ts | 2 +- backend/models/followup.ts | 2 +- {backend => lib}/enums/email.ts | 0 tools/mjml.ts | 2 +- 7 files changed, 6 insertions(+), 6 deletions(-) rename {backend => lib}/enums/email.ts (100%) diff --git a/backend/controllers/emails.ts b/backend/controllers/emails.ts index 1a428e129a..e14c4ffb3c 100644 --- a/backend/controllers/emails.ts +++ b/backend/controllers/emails.ts @@ -1,4 +1,4 @@ -import { EmailType } from "../../backend/enums/email.js" +import { EmailType } from "../../lib/enums/email.js" import emailRender from "../../backend/lib/mes-aides/emails/email-render.js" import { SurveyType } from "../../lib/enums/survey.js" diff --git a/backend/lib/email.ts b/backend/lib/email.ts index 3766d51954..8c95c37826 100644 --- a/backend/lib/email.ts +++ b/backend/lib/email.ts @@ -1,6 +1,6 @@ import { ArgumentParser } from "argparse" import config from "../config/index.js" -import { EmailType } from "../enums/email.js" +import { EmailType } from "../../lib/enums/email.js" import mongoose from "mongoose" import mongooseConfig from "../config/mongoose.js" import { processSendEmails } from "./emails/sending.js" diff --git a/backend/lib/emails/sending.ts b/backend/lib/emails/sending.ts index f2c9ff32d3..91d63d20d0 100644 --- a/backend/lib/emails/sending.ts +++ b/backend/lib/emails/sending.ts @@ -1,6 +1,6 @@ import dayjs from "dayjs" -import { EmailType } from "../../enums/email.js" +import { EmailType } from "../../../lib/enums/email.js" import { SurveyType } from "../../../lib/enums/survey.js" import Followups from "../../models/followup.js" import { Followup } from "../../../lib/types/followup.js" diff --git a/backend/lib/mes-aides/emails/email-render.ts b/backend/lib/mes-aides/emails/email-render.ts index e33ab7f330..2eca12db91 100644 --- a/backend/lib/mes-aides/emails/email-render.ts +++ b/backend/lib/mes-aides/emails/email-render.ts @@ -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 { EmailType } from "../../../enums/email.js" +import { EmailType } from "../../../../lib/enums/email.js" const __dirname = new URL(".", import.meta.url).pathname diff --git a/backend/models/followup.ts b/backend/models/followup.ts index 27a020d9b0..1935305241 100644 --- a/backend/models/followup.ts +++ b/backend/models/followup.ts @@ -6,7 +6,7 @@ import { Survey } from "../../lib/types/survey.js" import { SurveyType } from "../../lib/enums/survey.js" import emailRender from "../lib/mes-aides/emails/email-render.js" import SurveySchema from "./survey-schema.js" -import { EmailType } from "../enums/email.js" +import { EmailType } from "../../lib/enums/email.js" import config from "../config/index.js" import { Followup } from "../../lib/types/followup.d.js" import { FollowupModel } from "../types/models.d.js" diff --git a/backend/enums/email.ts b/lib/enums/email.ts similarity index 100% rename from backend/enums/email.ts rename to lib/enums/email.ts diff --git a/tools/mjml.ts b/tools/mjml.ts index fab60ae881..d0e4366ca9 100644 --- a/tools/mjml.ts +++ b/tools/mjml.ts @@ -3,7 +3,7 @@ import mongoose from "mongoose" import configMongoose from "../backend/config/mongoose.js" import config from "../backend/config/index.js" -import { EmailType } from "../backend/enums/email.js" +import { EmailType } from "../lib/enums/email.js" import express from "express" import Followups from "../backend/models/followup.js" // To load the simulation model in mongoose From 4783f1596f3e0b02bba761d417b6f32b61b5a647 Mon Sep 17 00:00:00 2001 From: Shamzic Date: Tue, 14 Nov 2023 14:45:37 +0100 Subject: [PATCH 02/25] refactor: outil d'envoi d'email --- backend/lib/{email.ts => email-sending-tool.ts} | 0 backend/lib/email.sh | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename backend/lib/{email.ts => email-sending-tool.ts} (100%) diff --git a/backend/lib/email.ts b/backend/lib/email-sending-tool.ts similarity index 100% rename from backend/lib/email.ts rename to backend/lib/email-sending-tool.ts diff --git a/backend/lib/email.sh b/backend/lib/email.sh index 0e05918555..8a68bcb8a5 100755 --- a/backend/lib/email.sh +++ b/backend/lib/email.sh @@ -2,4 +2,4 @@ cd `dirname $0` -node ../../dist-server/backend/lib/email.js send initial-survey --multiple 1000 +node ../../dist-server/backend/lib/email-sending-tool.js send initial-survey --multiple 1000 From d52e7dd819d64dd3c4023daccb35c473fc3514cf Mon Sep 17 00:00:00 2001 From: Shamzic Date: Fri, 10 Nov 2023 15:41:10 +0100 Subject: [PATCH 03/25] =?UTF-8?q?refactor:=20d=C3=A9place=20le=20schema=20?= =?UTF-8?q?du=20followup=20dans=20un=20fichier=20d=C3=A9di=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/models/followup-schema.ts | 49 +++++++++++++++++++++++++++++++ backend/models/followup.ts | 45 +--------------------------- 2 files changed, 50 insertions(+), 44 deletions(-) create mode 100644 backend/models/followup-schema.ts diff --git a/backend/models/followup-schema.ts b/backend/models/followup-schema.ts new file mode 100644 index 0000000000..569eef5d7c --- /dev/null +++ b/backend/models/followup-schema.ts @@ -0,0 +1,49 @@ +import mongoose from "mongoose" +import validator from "validator" +import { Followup } from "../../lib/types/followup.d.js" +import { FollowupModel } from "../types/models.d.js" +import SurveySchema from "./survey-schema.js" + +const FollowupSchema = new mongoose.Schema( + { + simulation: { + type: mongoose.Schema.Types.ObjectId, + ref: "Simulation", + }, + email: { + type: String, + validate: { + validator: validator.isEmail, + message: "Email invalide", + isAsync: false, + }, + }, + phone: { + type: String, + validate: { + validator: validator.isMobilePhone, + message: "Numéro de téléphone invalide", + isAsync: false, + }, + }, + createdAt: { type: Date, default: Date.now }, + sentAt: { type: Date }, + smsSentAt: { type: Date }, + messageId: { type: String }, + smsMessageId: { type: String }, + surveySentAt: { type: Date }, + benefits: { type: Object }, + surveyOptin: { type: Boolean, default: false }, + surveys: { + type: [SurveySchema], + default: [], + }, + version: Number, + error: { type: Object }, + smsError: { type: Object }, + accessToken: { type: String }, + }, + { minimize: false, id: false } +) + +export default FollowupSchema diff --git a/backend/models/followup.ts b/backend/models/followup.ts index 1935305241..1e261bef34 100644 --- a/backend/models/followup.ts +++ b/backend/models/followup.ts @@ -1,58 +1,15 @@ import mongoose from "mongoose" -import validator from "validator" import { sendMail } from "../lib/smtp.js" import axios from "axios" import { Survey } from "../../lib/types/survey.js" import { SurveyType } from "../../lib/enums/survey.js" import emailRender from "../lib/mes-aides/emails/email-render.js" -import SurveySchema from "./survey-schema.js" import { EmailType } from "../../lib/enums/email.js" import config from "../config/index.js" import { Followup } from "../../lib/types/followup.d.js" import { FollowupModel } from "../types/models.d.js" import { phoneNumberFormatting } from "../../lib/phone-number.js" - -const FollowupSchema = new mongoose.Schema( - { - simulation: { - type: mongoose.Schema.Types.ObjectId, - ref: "Simulation", - }, - email: { - type: String, - validate: { - validator: validator.isEmail, - message: "Email invalide", - isAsync: false, - }, - }, - phone: { - type: String, - validate: { - validator: validator.isMobilePhone, - message: "Numéro de téléphone invalide", - isAsync: false, - }, - }, - createdAt: { type: Date, default: Date.now }, - sentAt: { type: Date }, - smsSentAt: { type: Date }, - messageId: { type: String }, - smsMessageId: { type: String }, - surveySentAt: { type: Date }, - benefits: { type: Object }, - surveyOptin: { type: Boolean, default: false }, - surveys: { - type: [SurveySchema], - default: [], - }, - version: Number, - error: { type: Object }, - smsError: { type: Object }, - accessToken: { type: String }, - }, - { minimize: false, id: false } -) +import FollowupSchema from "./followup-schema.js" FollowupSchema.static("findByEmail", function (email: string) { return this.find({ email }) From ae3723ea683615ade8c8795f22ab529666205631 Mon Sep 17 00:00:00 2001 From: Shamzic Date: Fri, 10 Nov 2023 16:29:26 +0100 Subject: [PATCH 04/25] =?UTF-8?q?refactor:=20cr=C3=A9ation=20de=20email-se?= =?UTF-8?q?rvice=20(separation=20des=20responsabilit=C3=A9s)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/lib/email-service.ts | 20 ++++++++++++++++++++ backend/models/followup.ts | 33 +++++++++++++-------------------- 2 files changed, 33 insertions(+), 20 deletions(-) create mode 100644 backend/lib/email-service.ts diff --git a/backend/lib/email-service.ts b/backend/lib/email-service.ts new file mode 100644 index 0000000000..1116c212bf --- /dev/null +++ b/backend/lib/email-service.ts @@ -0,0 +1,20 @@ +import { sendMail } from "./smtp.js" +import emailRender from "./mes-aides/emails/email-render.js" +import { EmailCategory } from "../../lib/enums/email.js" + +export async function renderAndSendEmail(category: EmailCategory, followup) { + try { + const render: any = await emailRender(category, followup) + const response = await sendMail({ + to: followup.email, + subject: render.subject, + text: render.text, + html: render.html, + tags: [category], + }) + return response.messageId + } catch (err) { + console.error("error", err) + throw err + } +} diff --git a/backend/models/followup.ts b/backend/models/followup.ts index 1e261bef34..7b7793f126 100644 --- a/backend/models/followup.ts +++ b/backend/models/followup.ts @@ -10,6 +10,7 @@ import { Followup } from "../../lib/types/followup.d.js" import { FollowupModel } from "../types/models.d.js" import { phoneNumberFormatting } from "../../lib/phone-number.js" import FollowupSchema from "./followup-schema.js" +import { renderAndSendEmail } from "../lib/email-service.js" FollowupSchema.static("findByEmail", function (email: string) { return this.find({ email }) @@ -39,26 +40,18 @@ FollowupSchema.method("renderSimulationResultsEmail", function () { return emailRender(EmailType.SimulationResults, this) }) -FollowupSchema.method("sendSimulationResultsEmail", function () { - const followup = this - return this.renderSimulationResultsEmail() - .then((render) => { - return sendMail({ - to: followup.email, - subject: render.subject, - text: render.text, - html: render.html, - tags: [EmailType.SimulationResults], - }) - }) - .then((response) => { - return followup.postSimulationResultsEmail(response.messageId) - }) - .catch((err) => { - console.log("error", err) - followup.error = JSON.stringify(err, null, 2) - return followup.save() - }) +FollowupSchema.method("sendSimulationResultsEmail", async function () { + try { + const messageId = await renderAndSendEmail( + EmailType.SimulationResults, + this + ) + return this.postSimulationResultsEmail(messageId) + } catch (err) { + console.log("error", err) + this.error = JSON.stringify(err, null, 2) + return this.save() + } }) FollowupSchema.method( From 7370835dfc70cac16f1b41da7534bc19879e1600 Mon Sep 17 00:00:00 2001 From: Shamzic Date: Fri, 10 Nov 2023 19:59:54 +0100 Subject: [PATCH 05/25] feat: ajoute un enum commun pour le service de messages --- lib/enums/messaging.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 lib/enums/messaging.ts diff --git a/lib/enums/messaging.ts b/lib/enums/messaging.ts new file mode 100644 index 0000000000..f41c84bd67 --- /dev/null +++ b/lib/enums/messaging.ts @@ -0,0 +1,19 @@ +export enum MessageType { + Email = "email", + Sms = "sms", +} + +export enum EmailCategory { + SimulationResults = "simulation-results", + BenefitAction = "benefit-action", + SimulationUsefulness = "simulation-usefulness", + InitialSurvey = "initial-survey", + TousABordNotification = "tous-a-bord-notification", +} + +export enum SmsCategory { + SimulationResults = "simulation-results", + BenefitAction = "benefit-action", + SimulationUsefulness = "simulation-usefulness", + InitialSurvey = "initial-survey", +} From 7d41dd27ffe42a195af36e17c2c34f743b6d337c Mon Sep 17 00:00:00 2001 From: Shamzic Date: Wed, 15 Nov 2023 11:16:24 +0100 Subject: [PATCH 06/25] refactor: arborescence service messaging --- backend/lib/email.sh | 5 ----- backend/lib/{ => messaging/email}/email-sending-tool.ts | 8 ++++---- backend/lib/{ => messaging/email}/email-service.ts | 6 +++--- backend/lib/messaging/email/email.sh | 5 +++++ backend/lib/{emails => messaging}/sending.ts | 0 backend/models/followup.ts | 2 +- 6 files changed, 13 insertions(+), 13 deletions(-) delete mode 100755 backend/lib/email.sh rename backend/lib/{ => messaging/email}/email-sending-tool.ts (88%) rename backend/lib/{ => messaging/email}/email-service.ts (71%) create mode 100755 backend/lib/messaging/email/email.sh rename backend/lib/{emails => messaging}/sending.ts (100%) diff --git a/backend/lib/email.sh b/backend/lib/email.sh deleted file mode 100755 index 8a68bcb8a5..0000000000 --- a/backend/lib/email.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash - -cd `dirname $0` - -node ../../dist-server/backend/lib/email-sending-tool.js send initial-survey --multiple 1000 diff --git a/backend/lib/email-sending-tool.ts b/backend/lib/messaging/email/email-sending-tool.ts similarity index 88% rename from backend/lib/email-sending-tool.ts rename to backend/lib/messaging/email/email-sending-tool.ts index 8c95c37826..a792e6532d 100644 --- a/backend/lib/email-sending-tool.ts +++ b/backend/lib/messaging/email/email-sending-tool.ts @@ -1,9 +1,9 @@ import { ArgumentParser } from "argparse" -import config from "../config/index.js" -import { EmailType } from "../../lib/enums/email.js" +import config from "../../../config/index.js" +import { EmailType } from "../../../../lib/enums/email.js" import mongoose from "mongoose" -import mongooseConfig from "../config/mongoose.js" -import { processSendEmails } from "./emails/sending.js" +import mongooseConfig from "../../../config/mongoose.js" +import { processSendEmails } from "../sending.js" mongooseConfig(mongoose, config) diff --git a/backend/lib/email-service.ts b/backend/lib/messaging/email/email-service.ts similarity index 71% rename from backend/lib/email-service.ts rename to backend/lib/messaging/email/email-service.ts index 1116c212bf..6f048a1601 100644 --- a/backend/lib/email-service.ts +++ b/backend/lib/messaging/email/email-service.ts @@ -1,6 +1,6 @@ -import { sendMail } from "./smtp.js" -import emailRender from "./mes-aides/emails/email-render.js" -import { EmailCategory } from "../../lib/enums/email.js" +import { sendMail } from "../../smtp.js" +import emailRender from "../../mes-aides/emails/email-render.js" +import { EmailCategory } from "../../../../lib/enums/email.js" export async function renderAndSendEmail(category: EmailCategory, followup) { try { diff --git a/backend/lib/messaging/email/email.sh b/backend/lib/messaging/email/email.sh new file mode 100755 index 0000000000..0ca36c8abf --- /dev/null +++ b/backend/lib/messaging/email/email.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +cd `dirname $0` + +node ../../../../dist-server/backend/lib/messaging/email/email-sending-tool.js send initial-survey --multiple 1000 diff --git a/backend/lib/emails/sending.ts b/backend/lib/messaging/sending.ts similarity index 100% rename from backend/lib/emails/sending.ts rename to backend/lib/messaging/sending.ts diff --git a/backend/models/followup.ts b/backend/models/followup.ts index 7b7793f126..ee41fbc212 100644 --- a/backend/models/followup.ts +++ b/backend/models/followup.ts @@ -10,7 +10,7 @@ import { Followup } from "../../lib/types/followup.d.js" import { FollowupModel } from "../types/models.d.js" import { phoneNumberFormatting } from "../../lib/phone-number.js" import FollowupSchema from "./followup-schema.js" -import { renderAndSendEmail } from "../lib/email-service.js" +import { renderAndSendEmail } from "../lib/messaging/email/email-service.js" FollowupSchema.static("findByEmail", function (email: string) { return this.find({ email }) From 5a99399789b22dbc1c8a3d32d33f858d7979fda0 Mon Sep 17 00:00:00 2001 From: Shamzic Date: Wed, 15 Nov 2023 11:19:46 +0100 Subject: [PATCH 07/25] refacto: messaging enum path --- backend/controllers/emails.ts | 2 +- backend/lib/mes-aides/emails/email-render.ts | 2 +- backend/lib/messaging/email/email-sending-tool.ts | 2 +- backend/lib/messaging/email/email-service.ts | 4 ++-- backend/lib/messaging/sending.ts | 2 +- backend/models/followup.ts | 2 +- lib/enums/email.ts | 7 ------- lib/enums/messaging.ts | 2 +- tools/mjml.ts | 2 +- 9 files changed, 9 insertions(+), 16 deletions(-) delete mode 100644 lib/enums/email.ts diff --git a/backend/controllers/emails.ts b/backend/controllers/emails.ts index e14c4ffb3c..94ef8954d7 100644 --- a/backend/controllers/emails.ts +++ b/backend/controllers/emails.ts @@ -1,4 +1,4 @@ -import { EmailType } from "../../lib/enums/email.js" +import { EmailType } from "../../lib/enums/messaging.js" import emailRender from "../../backend/lib/mes-aides/emails/email-render.js" import { SurveyType } from "../../lib/enums/survey.js" diff --git a/backend/lib/mes-aides/emails/email-render.ts b/backend/lib/mes-aides/emails/email-render.ts index 2eca12db91..726a7aa17e 100644 --- a/backend/lib/mes-aides/emails/email-render.ts +++ b/backend/lib/mes-aides/emails/email-render.ts @@ -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 { EmailType } from "../../../../lib/enums/email.js" +import { EmailType } from "../../../../lib/enums/messaging.js" const __dirname = new URL(".", import.meta.url).pathname diff --git a/backend/lib/messaging/email/email-sending-tool.ts b/backend/lib/messaging/email/email-sending-tool.ts index a792e6532d..5101b4198c 100644 --- a/backend/lib/messaging/email/email-sending-tool.ts +++ b/backend/lib/messaging/email/email-sending-tool.ts @@ -1,6 +1,6 @@ import { ArgumentParser } from "argparse" import config from "../../../config/index.js" -import { EmailType } from "../../../../lib/enums/email.js" +import { EmailType } from "../../../../lib/enums/messaging.js" import mongoose from "mongoose" import mongooseConfig from "../../../config/mongoose.js" import { processSendEmails } from "../sending.js" diff --git a/backend/lib/messaging/email/email-service.ts b/backend/lib/messaging/email/email-service.ts index 6f048a1601..eaf9ef2ef8 100644 --- a/backend/lib/messaging/email/email-service.ts +++ b/backend/lib/messaging/email/email-service.ts @@ -1,8 +1,8 @@ import { sendMail } from "../../smtp.js" import emailRender from "../../mes-aides/emails/email-render.js" -import { EmailCategory } from "../../../../lib/enums/email.js" +import { EmailType } from "../../../../lib/enums/messaging.js" -export async function renderAndSendEmail(category: EmailCategory, followup) { +export async function renderAndSendEmail(category: EmailType, followup) { try { const render: any = await emailRender(category, followup) const response = await sendMail({ diff --git a/backend/lib/messaging/sending.ts b/backend/lib/messaging/sending.ts index 91d63d20d0..6a79148bec 100644 --- a/backend/lib/messaging/sending.ts +++ b/backend/lib/messaging/sending.ts @@ -1,6 +1,6 @@ import dayjs from "dayjs" -import { EmailType } from "../../../lib/enums/email.js" +import { EmailType } from "../../../lib/enums/messaging.js" import { SurveyType } from "../../../lib/enums/survey.js" import Followups from "../../models/followup.js" import { Followup } from "../../../lib/types/followup.js" diff --git a/backend/models/followup.ts b/backend/models/followup.ts index ee41fbc212..f132e63c4b 100644 --- a/backend/models/followup.ts +++ b/backend/models/followup.ts @@ -4,7 +4,7 @@ import axios from "axios" import { Survey } from "../../lib/types/survey.js" import { SurveyType } from "../../lib/enums/survey.js" import emailRender from "../lib/mes-aides/emails/email-render.js" -import { EmailType } from "../../lib/enums/email.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" diff --git a/lib/enums/email.ts b/lib/enums/email.ts deleted file mode 100644 index ba3223f345..0000000000 --- a/lib/enums/email.ts +++ /dev/null @@ -1,7 +0,0 @@ -export enum EmailType { - SimulationResults = "simulation-results", - BenefitAction = "benefit-action", - SimulationUsefulness = "simulation-usefulness", - InitialSurvey = "initial-survey", - TousABordNotification = "tous-a-bord-notification", -} diff --git a/lib/enums/messaging.ts b/lib/enums/messaging.ts index f41c84bd67..b99fed1fb0 100644 --- a/lib/enums/messaging.ts +++ b/lib/enums/messaging.ts @@ -3,7 +3,7 @@ export enum MessageType { Sms = "sms", } -export enum EmailCategory { +export enum EmailType { SimulationResults = "simulation-results", BenefitAction = "benefit-action", SimulationUsefulness = "simulation-usefulness", diff --git a/tools/mjml.ts b/tools/mjml.ts index d0e4366ca9..4a38e583f1 100644 --- a/tools/mjml.ts +++ b/tools/mjml.ts @@ -3,7 +3,7 @@ import mongoose from "mongoose" import configMongoose from "../backend/config/mongoose.js" import config from "../backend/config/index.js" -import { EmailType } from "../lib/enums/email.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 From c06f0f8cf11287cb7e0f1eed9fd9ef4f72563009 Mon Sep 17 00:00:00 2001 From: Shamzic Date: Thu, 16 Nov 2023 11:51:08 +0100 Subject: [PATCH 08/25] refactor : renderAndSendEmail en sendEmail + sendMail en sendEmailSmtp --- backend/lib/messaging/email/email-service.ts | 6 +++--- backend/lib/smtp.ts | 2 +- backend/models/followup.ts | 11 ++++------- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/backend/lib/messaging/email/email-service.ts b/backend/lib/messaging/email/email-service.ts index eaf9ef2ef8..18dd54e6ed 100644 --- a/backend/lib/messaging/email/email-service.ts +++ b/backend/lib/messaging/email/email-service.ts @@ -1,11 +1,11 @@ -import { sendMail } from "../../smtp.js" +import { sendEmailSmtp } from "../../smtp.js" import emailRender from "../../mes-aides/emails/email-render.js" import { EmailType } from "../../../../lib/enums/messaging.js" -export async function renderAndSendEmail(category: EmailType, followup) { +export async function sendEmail(category: EmailType, followup) { try { const render: any = await emailRender(category, followup) - const response = await sendMail({ + const response = await sendEmailSmtp({ to: followup.email, subject: render.subject, text: render.text, diff --git a/backend/lib/smtp.ts b/backend/lib/smtp.ts index 2cfe7a2dd4..5097655cc2 100644 --- a/backend/lib/smtp.ts +++ b/backend/lib/smtp.ts @@ -4,7 +4,7 @@ import { Email } from "../../lib/types/email.js" const transporter = nodemailer.createTransport(config.smtp) -export function sendMail(email: Email) { +export function sendEmailSmtp(email: Email) { const { tags, ...emailParameters } = email const tagsFormatted = tags?.map((tag) => tag.replace(/-/g, "_")) || [] diff --git a/backend/models/followup.ts b/backend/models/followup.ts index f132e63c4b..26ad227edc 100644 --- a/backend/models/followup.ts +++ b/backend/models/followup.ts @@ -1,5 +1,5 @@ import mongoose from "mongoose" -import { sendMail } from "../lib/smtp.js" +import { sendEmailSmtp } from "../lib/smtp.js" import axios from "axios" import { Survey } from "../../lib/types/survey.js" import { SurveyType } from "../../lib/enums/survey.js" @@ -10,7 +10,7 @@ import { Followup } from "../../lib/types/followup.d.js" import { FollowupModel } from "../types/models.d.js" import { phoneNumberFormatting } from "../../lib/phone-number.js" import FollowupSchema from "./followup-schema.js" -import { renderAndSendEmail } from "../lib/messaging/email/email-service.js" +import { sendEmail } from "../lib/messaging/email/email-service.js" FollowupSchema.static("findByEmail", function (email: string) { return this.find({ email }) @@ -42,10 +42,7 @@ FollowupSchema.method("renderSimulationResultsEmail", function () { FollowupSchema.method("sendSimulationResultsEmail", async function () { try { - const messageId = await renderAndSendEmail( - EmailType.SimulationResults, - this - ) + const messageId = await sendEmail(EmailType.SimulationResults, this) return this.postSimulationResultsEmail(messageId) } catch (err) { console.log("error", err) @@ -129,7 +126,7 @@ FollowupSchema.method("sendSurvey", function (surveyType: SurveyType) { return this.addSurveyIfMissing(surveyType).then((survey: Survey) => { return this.renderSurveyEmail(surveyType) .then((render) => { - return sendMail({ + return sendEmailSmtp({ to: followup.email, subject: render.subject, text: render.text, From 470d9042b555a49f3183c89865cee361a73f47d6 Mon Sep 17 00:00:00 2001 From: Shamzic Date: Thu, 16 Nov 2023 12:07:59 +0100 Subject: [PATCH 09/25] refactor: followup sendSurvey --- backend/models/followup.ts | 50 +++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/backend/models/followup.ts b/backend/models/followup.ts index 26ad227edc..e98d43cabb 100644 --- a/backend/models/followup.ts +++ b/backend/models/followup.ts @@ -121,35 +121,29 @@ FollowupSchema.method("addSurveyIfMissing", async function (type: SurveyType) { return survey }) -FollowupSchema.method("sendSurvey", function (surveyType: SurveyType) { +FollowupSchema.method("sendSurvey", async function (surveyType: SurveyType) { const followup = this - return this.addSurveyIfMissing(surveyType).then((survey: Survey) => { - return this.renderSurveyEmail(surveyType) - .then((render) => { - return sendEmailSmtp({ - to: followup.email, - subject: render.subject, - text: render.text, - html: render.html, - tags: ["survey", surveyType], - }) - .then((response) => { - return response.messageId - }) - .then((messageId) => { - survey.messageId = messageId - return survey - }) - }) - .catch((err: Error) => { - console.log("error", err) - survey.error = err - return survey - }) - .then(() => { - return followup.save() - }) - }) + let survey + try { + survey = await this.addSurveyIfMissing(surveyType) + const render = await this.renderSurveyEmail(surveyType) + const response = await sendEmailSmtp({ + to: followup.email, + subject: render.subject, + text: render.text, + html: render.html, + tags: ["survey", surveyType], + }) + + survey.messageId = response.messageId + } catch (err) { + console.error("error", err) + survey.error = err + throw err + } finally { + await followup.save() + } + return survey }) FollowupSchema.method("updateSurvey", function (type, answers) { From 0943e29c641f0d4b8c10935cb29fe7945e66c757 Mon Sep 17 00:00:00 2001 From: Shamzic Date: Thu, 16 Nov 2023 14:30:34 +0100 Subject: [PATCH 10/25] refactor: supprime enum non utile --- lib/enums/messaging.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/enums/messaging.ts b/lib/enums/messaging.ts index b99fed1fb0..bbeb33babd 100644 --- a/lib/enums/messaging.ts +++ b/lib/enums/messaging.ts @@ -1,8 +1,3 @@ -export enum MessageType { - Email = "email", - Sms = "sms", -} - export enum EmailType { SimulationResults = "simulation-results", BenefitAction = "benefit-action", From f9e06fb05dab69d094c3a304051ae5d8b83b6488 Mon Sep 17 00:00:00 2001 From: Shamzic Date: Thu, 16 Nov 2023 14:51:21 +0100 Subject: [PATCH 11/25] =?UTF-8?q?refactor:=20d=C3=A9place=20m=C3=A9thode?= =?UTF-8?q?=20followup=20dans=20email-service?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/lib/messaging/email/email-service.ts | 8 +++++++- backend/models/followup.ts | 13 +------------ lib/types/followup.d.ts | 1 - 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/backend/lib/messaging/email/email-service.ts b/backend/lib/messaging/email/email-service.ts index 18dd54e6ed..eab0422a9d 100644 --- a/backend/lib/messaging/email/email-service.ts +++ b/backend/lib/messaging/email/email-service.ts @@ -12,7 +12,13 @@ export async function sendEmail(category: EmailType, followup) { html: render.html, tags: [category], }) - return response.messageId + followup.sentAt = Date.now() + followup.messageId = response.messageId + if (!followup.surveyOptin) { + followup.email = undefined + } + followup.error = undefined + return followup.save() } catch (err) { console.error("error", err) throw err diff --git a/backend/models/followup.ts b/backend/models/followup.ts index e98d43cabb..cddb29f8dc 100644 --- a/backend/models/followup.ts +++ b/backend/models/followup.ts @@ -16,16 +16,6 @@ FollowupSchema.static("findByEmail", function (email: string) { return this.find({ email }) }) -FollowupSchema.method("postSimulationResultsEmail", function (messageId) { - this.sentAt = Date.now() - this.messageId = messageId - if (!this.surveyOptin) { - this.email = undefined - } - this.error = undefined - return this.save() -}) - FollowupSchema.method("postSimulationResultsSms", function (messageId) { this.smsSentAt = Date.now() this.smsMessageId = messageId @@ -42,8 +32,7 @@ FollowupSchema.method("renderSimulationResultsEmail", function () { FollowupSchema.method("sendSimulationResultsEmail", async function () { try { - const messageId = await sendEmail(EmailType.SimulationResults, this) - return this.postSimulationResultsEmail(messageId) + return await sendEmail(EmailType.SimulationResults, this) } catch (err) { console.log("error", err) this.error = JSON.stringify(err, null, 2) diff --git a/lib/types/followup.d.ts b/lib/types/followup.d.ts index 88b0c614d4..4357bd6bbf 100644 --- a/lib/types/followup.d.ts +++ b/lib/types/followup.d.ts @@ -25,7 +25,6 @@ interface FollowupAttributes { } interface FollowupMethods { - postSimulationResultsEmail(messageId: string): void postSimulationResultsSms(messageId: string): void renderSimulationResultsEmail(): any renderSimulationResultsSms(): any From 93d5f9b8e0b8cdf70f1060682369be06c61ca149 Mon Sep 17 00:00:00 2001 From: Shamzic Date: Thu, 16 Nov 2023 14:59:09 +0100 Subject: [PATCH 12/25] =?UTF-8?q?refactor:=20d=C3=A9place=20le=20script=20?= =?UTF-8?q?outil=20d'envoi=20d'email?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/lib/messaging/email/email.sh | 2 +- .../lib/messaging/email => tools}/email-sending-tool.ts | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) rename {backend/lib/messaging/email => tools}/email-sending-tool.ts (87%) diff --git a/backend/lib/messaging/email/email.sh b/backend/lib/messaging/email/email.sh index 0ca36c8abf..7c04306af4 100755 --- a/backend/lib/messaging/email/email.sh +++ b/backend/lib/messaging/email/email.sh @@ -2,4 +2,4 @@ cd `dirname $0` -node ../../../../dist-server/backend/lib/messaging/email/email-sending-tool.js send initial-survey --multiple 1000 +node ../../../../dist-server/tools/email-sending-tool.js send initial-survey --multiple 1000 diff --git a/backend/lib/messaging/email/email-sending-tool.ts b/tools/email-sending-tool.ts similarity index 87% rename from backend/lib/messaging/email/email-sending-tool.ts rename to tools/email-sending-tool.ts index 5101b4198c..13eeb90392 100644 --- a/backend/lib/messaging/email/email-sending-tool.ts +++ b/tools/email-sending-tool.ts @@ -1,9 +1,9 @@ import { ArgumentParser } from "argparse" -import config from "../../../config/index.js" -import { EmailType } from "../../../../lib/enums/messaging.js" +import config from "../backend/config/index.js" +import { EmailType } from "../lib/enums/messaging.js" import mongoose from "mongoose" -import mongooseConfig from "../../../config/mongoose.js" -import { processSendEmails } from "../sending.js" +import mongooseConfig from "../backend/config/mongoose.js" +import { processSendEmails } from "../backend/lib/messaging/sending.js" mongooseConfig(mongoose, config) From 796b14cac02bf5d088ca904bac8e74a6f6f608d3 Mon Sep 17 00:00:00 2001 From: Shamzic Date: Thu, 16 Nov 2023 15:05:57 +0100 Subject: [PATCH 13/25] refactor: supprime email.sh et ajoute son contenu dans une commande du package.json --- backend/lib/messaging/email/email.sh | 5 ----- package.json | 1 + 2 files changed, 1 insertion(+), 5 deletions(-) delete mode 100755 backend/lib/messaging/email/email.sh diff --git a/backend/lib/messaging/email/email.sh b/backend/lib/messaging/email/email.sh deleted file mode 100755 index 7c04306af4..0000000000 --- a/backend/lib/messaging/email/email.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash - -cd `dirname $0` - -node ../../../../dist-server/tools/email-sending-tool.js send initial-survey --multiple 1000 diff --git a/package.json b/package.json index d4e1f5332b..e6af2cd040 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "tools:get-all-steps": "ts-node ./tools/get-all-steps", "tools:institutions-logo-check": "ts-node ./tools/institutions-logo-check", "tools:serve-mail": "ts-node ./tools/mjml", + "tools:send-initial-survey-email": "ts-node ./tools/email-sending-tool send initial-survey --multiple 1000", "tools:test-benefits-geographical-constraint-consistency": "ts-node ./tools/test-benefits-geographical-constraint-consistency", "tools:test-definition-periods": "ts-node ./tools/test-definition-periods", "tools:test-iframe-generation": "./tools/iframe-build-control.sh", From 91598076de25b1d3ee95f1879e5ff2c33b2553ab Mon Sep 17 00:00:00 2001 From: Shamzic Date: Mon, 20 Nov 2023 13:53:00 +0100 Subject: [PATCH 14/25] =?UTF-8?q?refactor:=20smsCategory=20pas=20utilis?= =?UTF-8?q?=C3=A9=20ici?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/enums/messaging.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/lib/enums/messaging.ts b/lib/enums/messaging.ts index bbeb33babd..ba3223f345 100644 --- a/lib/enums/messaging.ts +++ b/lib/enums/messaging.ts @@ -5,10 +5,3 @@ export enum EmailType { InitialSurvey = "initial-survey", TousABordNotification = "tous-a-bord-notification", } - -export enum SmsCategory { - SimulationResults = "simulation-results", - BenefitAction = "benefit-action", - SimulationUsefulness = "simulation-usefulness", - InitialSurvey = "initial-survey", -} From 5644f9454b13f1f921830facde00ec64663dcc9c Mon Sep 17 00:00:00 2001 From: Shamzic Date: Mon, 20 Nov 2023 14:21:04 +0100 Subject: [PATCH 15/25] refactor: enum EmailCategory -> EmailType --- backend/lib/messaging/email/email-service.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/lib/messaging/email/email-service.ts b/backend/lib/messaging/email/email-service.ts index eab0422a9d..b9376371cb 100644 --- a/backend/lib/messaging/email/email-service.ts +++ b/backend/lib/messaging/email/email-service.ts @@ -2,15 +2,15 @@ import { sendEmailSmtp } from "../../smtp.js" import emailRender from "../../mes-aides/emails/email-render.js" import { EmailType } from "../../../../lib/enums/messaging.js" -export async function sendEmail(category: EmailType, 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 From 5c2fba98362f61b8ad77ac22e13fab0d8b1100b5 Mon Sep 17 00:00:00 2001 From: Shamzic Date: Mon, 20 Nov 2023 16:37:01 +0100 Subject: [PATCH 16/25] refactor: service email --- backend/controllers/emails.ts | 16 ++-- backend/lib/mes-aides/emails/email-render.ts | 27 +++++- backend/lib/messaging/email/email-service.ts | 87 ++++++++++++++++---- backend/lib/messaging/sending.ts | 7 +- backend/models/followup.ts | 82 ++++++------------ lib/types/followup.d.ts | 6 +- tools/mjml.ts | 7 +- 7 files changed, 143 insertions(+), 89 deletions(-) diff --git a/backend/controllers/emails.ts b/backend/controllers/emails.ts index 94ef8954d7..be7bf8ba41 100644 --- a/backend/controllers/emails.ts +++ b/backend/controllers/emails.ts @@ -1,8 +1,11 @@ import { EmailType } from "../../lib/enums/messaging.js" -import emailRender from "../../backend/lib/mes-aides/emails/email-render.js" import { SurveyType } from "../../lib/enums/survey.js" +import { + emailRender, + emailRenderBySurveyType, +} from "../../backend/lib/mes-aides/emails/email-render.js" -const renderFollowupEmailByType = async (followup, emailType: EmailType) => { +const renderEmailByType = async (followup, emailType: EmailType) => { let surveyType: SurveyType | undefined switch (emailType) { @@ -17,16 +20,17 @@ const renderFollowupEmailByType = async (followup, emailType: EmailType) => { default: throw new Error(`Unknown email type: ${emailType}`) } - - return followup.renderSurveyEmail(surveyType) + return emailRenderBySurveyType(surveyType, followup) } const getFollowupEmail = async (req, res, next) => { try { const { emailType }: { emailType: EmailType } = req.query const followup = req.followup - const result = await renderFollowupEmailByType(followup, emailType) - res.send(result["html"]) + const result = await renderEmailByType(followup, emailType) + if (result) { + res.send(result["html"]) + } } catch (err) { next(err) } diff --git a/backend/lib/mes-aides/emails/email-render.ts b/backend/lib/mes-aides/emails/email-render.ts index 726a7aa17e..8e030ed208 100644 --- a/backend/lib/mes-aides/emails/email-render.ts +++ b/backend/lib/mes-aides/emails/email-render.ts @@ -8,6 +8,7 @@ import openfiscaController from "../../openfisca/parameters.js" import { formatBenefits, basicBenefitText } from "./simulation-results.js" import { mjml } from "./index.js" import { EmailType } from "../../../../lib/enums/messaging.js" +import { SurveyType } from "../../../../lib/enums/survey.js" const __dirname = new URL(".", import.meta.url).pathname @@ -90,7 +91,31 @@ function renderAsHtml(emailType: EmailType, dataTemplate) { }) } -export default async function emailRender(emailType: EmailType, followup) { +export async function emailRenderBySurveyType( + surveyType: SurveyType, + followup +) { + switch (surveyType) { + case SurveyType.TrackClickOnBenefitActionEmail: + return emailRender(EmailType.BenefitAction, followup) + case SurveyType.TrackClickOnSimulationUsefulnessEmail: + return emailRender(EmailType.SimulationUsefulness, followup) + case SurveyType.TousABordNotification: + return emailRender(EmailType.TousABordNotification, followup) + case SurveyType.BenefitAction: + return Promise.reject( + new Error( + `This surveyType "${surveyType}" is not supposed to be sent through an email` + ) + ) + default: + return Promise.reject( + new Error(`This surveyType "${surveyType}" has no email template`) + ) + } +} + +export async function emailRender(emailType: EmailType, followup) { let benefits: any = null let parameters: any = null let formatedBenefits: any = {} diff --git a/backend/lib/messaging/email/email-service.ts b/backend/lib/messaging/email/email-service.ts index b9376371cb..7345aced53 100644 --- a/backend/lib/messaging/email/email-service.ts +++ b/backend/lib/messaging/email/email-service.ts @@ -1,24 +1,79 @@ import { sendEmailSmtp } from "../../smtp.js" -import emailRender from "../../mes-aides/emails/email-render.js" +import { + emailRender, + emailRenderBySurveyType, +} from "../../mes-aides/emails/email-render.js" import { EmailType } from "../../../../lib/enums/messaging.js" +import { SurveyType } from "@lib/enums/survey.js" +import { Survey } from "@lib/types/survey.js" +import { Followup } from "@lib/types/followup.js" +import dayjs from "dayjs" -export async function sendEmail(emailType: EmailType, followup) { +async function sendSimulationResultsEmail( + followup: Followup +): Promise { + const render: any = await emailRender(EmailType.SimulationResults, followup) + const sendEmailSmtpResponse = await sendEmailSmtp({ + to: followup.email ?? "", + subject: render.subject, + text: render.text, + html: render.html, + tags: [EmailType.SimulationResults], + }) + + followup.sentAt = dayjs().toDate() + followup.messageId = sendEmailSmtpResponse.messageId + + if (!followup.surveyOptin) { + followup.email = undefined + } + followup.error = undefined + + return followup.save() +} + +async function sendSurveyEmail( + followup: Followup, + surveyType: SurveyType, + survey: Survey +): Promise { + const render: any = await emailRenderBySurveyType(surveyType, followup) + + const sendEmailSmtpResponse = await sendEmailSmtp({ + to: followup.email ?? "", + subject: render.subject, + text: render.text, + html: render.html, + tags: ["survey", surveyType], + }) + + survey.messageId = sendEmailSmtpResponse.messageId + + return await followup.save() +} + +export async function sendEmail( + emailType: EmailType, + followup, + surveyType?: SurveyType, + survey?: Survey +) { try { - const render: any = await emailRender(emailType, followup) - const response = await sendEmailSmtp({ - to: followup.email, - subject: render.subject, - text: render.text, - html: render.html, - tags: [emailType], - }) - followup.sentAt = Date.now() - followup.messageId = response.messageId - if (!followup.surveyOptin) { - followup.email = undefined + switch (emailType) { + case EmailType.SimulationResults: + return sendSimulationResultsEmail(followup) + case EmailType.BenefitAction: + case EmailType.SimulationUsefulness: + if (surveyType && survey) { + return sendSurveyEmail(followup, surveyType, survey) + } + break + case EmailType.TousABordNotification: + // Todo: send tousabord email + break + default: + throw new Error(`Unknown email type ${emailType}`) } - followup.error = undefined - return followup.save() } catch (err) { console.error("error", err) throw err diff --git a/backend/lib/messaging/sending.ts b/backend/lib/messaging/sending.ts index 6a79148bec..1610b99247 100644 --- a/backend/lib/messaging/sending.ts +++ b/backend/lib/messaging/sending.ts @@ -4,6 +4,7 @@ import { EmailType } from "../../../lib/enums/messaging.js" import { SurveyType } from "../../../lib/enums/survey.js" import Followups from "../../models/followup.js" import { Followup } from "../../../lib/types/followup.js" +import { Survey } from "../../../lib/types/survey.js" const DaysBeforeInitialEmail = 6 const DaysBeforeTousABordNotificationEmail = 2 @@ -52,8 +53,8 @@ async function sendMultipleInitialEmails(limit: number) { : SurveyType.TrackClickOnSimulationUsefulnessEmail try { - const result = await followup.sendSurvey(surveyType) - return { ok: result._id } + const survey = await followup.sendSurvey(surveyType) + return { "survey sent id": survey._id } } catch (error) { return { ko: error } } @@ -109,7 +110,7 @@ async function processSingleEmail(emailType: EmailType, followupId: string) { throw new Error("Followup not found") } - let emailPromise: Promise + let emailPromise: Promise | null = null switch (emailType) { case EmailType.SimulationResults: diff --git a/backend/models/followup.ts b/backend/models/followup.ts index cddb29f8dc..11f9345b37 100644 --- a/backend/models/followup.ts +++ b/backend/models/followup.ts @@ -1,9 +1,7 @@ import mongoose from "mongoose" -import { sendEmailSmtp } from "../lib/smtp.js" import axios from "axios" import { Survey } from "../../lib/types/survey.js" import { SurveyType } from "../../lib/enums/survey.js" -import emailRender from "../lib/mes-aides/emails/email-render.js" import { EmailType } from "../../lib/enums/messaging.js" import config from "../config/index.js" import { Followup } from "../../lib/types/followup.d.js" @@ -26,10 +24,6 @@ FollowupSchema.method("postSimulationResultsSms", function (messageId) { return this.save() }) -FollowupSchema.method("renderSimulationResultsEmail", function () { - return emailRender(EmailType.SimulationResults, this) -}) - FollowupSchema.method("sendSimulationResultsEmail", async function () { try { return await sendEmail(EmailType.SimulationResults, this) @@ -80,60 +74,34 @@ FollowupSchema.method("sendSimulationResultsSms", async function () { } }) -FollowupSchema.method("renderSurveyEmail", function (surveyType) { - switch (surveyType) { - case SurveyType.TrackClickOnBenefitActionEmail: - return emailRender(EmailType.BenefitAction, this) - case SurveyType.TrackClickOnSimulationUsefulnessEmail: - return emailRender(EmailType.SimulationUsefulness, this) - case SurveyType.TousABordNotification: - return emailRender(EmailType.TousABordNotification, this) - case SurveyType.BenefitAction: - return Promise.reject( - new Error( - `This surveyType "${surveyType}" is not supposed to be sent through an email` - ) - ) - default: - return Promise.reject( - new Error(`This surveyType "${surveyType}" has no email template`) - ) - } -}) - -FollowupSchema.method("addSurveyIfMissing", async function (type: SurveyType) { - let survey = this.surveys.find((survey) => survey.type === type) - if (!survey) { - survey = await this.surveys.create({ type }) - this.surveys.push(survey) +FollowupSchema.method( + "addSurveyIfMissing", + async function (type: SurveyType): Promise { + let survey = this.surveys.find((survey) => survey.type === type) + if (!survey) { + survey = await this.surveys.create({ type }) + this.surveys.push(survey) + } + return survey } - return survey -}) - -FollowupSchema.method("sendSurvey", async function (surveyType: SurveyType) { - const followup = this - let survey - try { - survey = await this.addSurveyIfMissing(surveyType) - const render = await this.renderSurveyEmail(surveyType) - const response = await sendEmailSmtp({ - to: followup.email, - subject: render.subject, - text: render.text, - html: render.html, - tags: ["survey", surveyType], - }) +) - survey.messageId = response.messageId - } catch (err) { - console.error("error", err) - survey.error = err - throw err - } finally { - await followup.save() +FollowupSchema.method( + "sendSurvey", + async function (surveyType: SurveyType): Promise { + const followup = this + let survey + try { + const survey = await this.addSurveyIfMissing(surveyType) + await sendEmail(EmailType.BenefitAction, followup, surveyType, survey) + return survey + } catch (err) { + console.error("error", err) + survey.error = err + throw err + } } - return survey -}) +) FollowupSchema.method("updateSurvey", function (type, answers) { const surveys: Survey[] = Array.from(this.surveys) diff --git a/lib/types/followup.d.ts b/lib/types/followup.d.ts index 4357bd6bbf..cc668c376f 100644 --- a/lib/types/followup.d.ts +++ b/lib/types/followup.d.ts @@ -26,13 +26,11 @@ interface FollowupAttributes { interface FollowupMethods { postSimulationResultsSms(messageId: string): void - renderSimulationResultsEmail(): any renderSimulationResultsSms(): any sendSimulationResultsEmail(): Promise sendSimulationResultsSms(): Promise - renderSurveyEmail(surveyType: SurveyType): any - addSurveyIfMissing(surveyType: SurveyType): Promise - sendSurvey(surveyType: SurveyType): Promise + addSurveyIfMissing(surveyType: SurveyType): Promise + sendSurvey(surveyType: SurveyType): Promise updateSurvey(action: SurveyType, data?: any) } diff --git a/tools/mjml.ts b/tools/mjml.ts index 4a38e583f1..8a4c07b4d3 100644 --- a/tools/mjml.ts +++ b/tools/mjml.ts @@ -8,7 +8,10 @@ import express from "express" import Followups from "../backend/models/followup.js" // To load the simulation model in mongoose import "../backend/models/simulation.js" -import emailRender from "../backend/lib/mes-aides/emails/email-render.js" +import { + emailRender, + emailRenderBySurveyType, +} from "../backend/lib/mes-aides/emails/email-render.js" import { SurveyType } from "../lib/enums/survey.js" import { __express } from "ejs" import "../backend/lib/mongo-connector.js" @@ -62,7 +65,7 @@ const followupRendering = async (req: Request) => { await followup.addSurveyIfMissing(surveyType) await followup.save() - return followup.renderSurveyEmail(surveyType) + return emailRenderBySurveyType(surveyType, followup) } app.route("/mjml/:id/:type").get( From a95bf654b83ecc39e75379b7c4a3fc46f83a0562 Mon Sep 17 00:00:00 2001 From: Shamzic Date: Mon, 20 Nov 2023 17:12:46 +0100 Subject: [PATCH 17/25] =?UTF-8?q?fix:=20Lever=20des=20erreurs=20si=20acc?= =?UTF-8?q?=C3=A8s=20=C3=A0=20une=20props=20d'un=20objet=20undefined?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/lib/messaging/email/email-service.ts | 11 ++++++++--- tools/mjml.ts | 3 +++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/backend/lib/messaging/email/email-service.ts b/backend/lib/messaging/email/email-service.ts index 7345aced53..4887e6788c 100644 --- a/backend/lib/messaging/email/email-service.ts +++ b/backend/lib/messaging/email/email-service.ts @@ -12,9 +12,12 @@ import dayjs from "dayjs" async function sendSimulationResultsEmail( followup: Followup ): Promise { + if (!followup.email) { + throw new Error("Missing followup email") + } const render: any = await emailRender(EmailType.SimulationResults, followup) const sendEmailSmtpResponse = await sendEmailSmtp({ - to: followup.email ?? "", + to: followup.email, subject: render.subject, text: render.text, html: render.html, @@ -37,10 +40,12 @@ async function sendSurveyEmail( surveyType: SurveyType, survey: Survey ): Promise { + if (!followup.email) { + throw new Error("Missing followup email") + } const render: any = await emailRenderBySurveyType(surveyType, followup) - const sendEmailSmtpResponse = await sendEmailSmtp({ - to: followup.email ?? "", + to: followup.email, subject: render.subject, text: render.text, html: render.html, diff --git a/tools/mjml.ts b/tools/mjml.ts index 8a4c07b4d3..888a4a07ea 100644 --- a/tools/mjml.ts +++ b/tools/mjml.ts @@ -88,6 +88,9 @@ app.route("/mjml/:id/:type").get( function (req, res) { followupRendering(req).then((result) => { const mode = req.query.mode || "html" + if (!result) { + throw new Error("Failed to render email") + } if (mode == "html") { res.send(result[mode]) } else { From 1f2e56b72545bf87ab2673e9c0778ca19e56dc99 Mon Sep 17 00:00:00 2001 From: Shamzic Date: Mon, 20 Nov 2023 17:37:16 +0100 Subject: [PATCH 18/25] refactor: sort sendSimulationResultsEmail du followup -> email-service --- backend/controllers/followups.ts | 3 ++- backend/lib/messaging/email/email-service.ts | 8 ++++---- backend/lib/messaging/sending.ts | 5 +++-- backend/models/followup.ts | 10 ---------- lib/types/followup.d.ts | 1 - 5 files changed, 9 insertions(+), 18 deletions(-) diff --git a/backend/controllers/followups.ts b/backend/controllers/followups.ts index c5a2e0c7b8..28026ab3de 100644 --- a/backend/controllers/followups.ts +++ b/backend/controllers/followups.ts @@ -12,6 +12,7 @@ import { FetchSurvey } from "../../lib/types/survey.d.js" import Request from "../types/express.d.js" import { phoneNumberValidation } from "../../lib/phone-number.js" import config from "../config/index.js" +import { sendSimulationResultsEmail } from "../lib/messaging/email/email-service.js" export function followup( req: Request, @@ -58,7 +59,7 @@ export async function persist(req: Request, res: Response) { phone )) as Followup if (email) { - await followup.sendSimulationResultsEmail() + await sendSimulationResultsEmail(followup) } if (phone) { if ( diff --git a/backend/lib/messaging/email/email-service.ts b/backend/lib/messaging/email/email-service.ts index 4887e6788c..5ba8e1306c 100644 --- a/backend/lib/messaging/email/email-service.ts +++ b/backend/lib/messaging/email/email-service.ts @@ -9,7 +9,7 @@ import { Survey } from "@lib/types/survey.js" import { Followup } from "@lib/types/followup.js" import dayjs from "dayjs" -async function sendSimulationResultsEmail( +export async function sendSimulationResultsEmail( followup: Followup ): Promise { if (!followup.email) { @@ -32,10 +32,10 @@ async function sendSimulationResultsEmail( } followup.error = undefined - return followup.save() + return await followup.save() } -async function sendSurveyEmail( +export async function sendSurveyEmail( followup: Followup, surveyType: SurveyType, survey: Survey @@ -66,7 +66,7 @@ export async function sendEmail( try { switch (emailType) { case EmailType.SimulationResults: - return sendSimulationResultsEmail(followup) + return await sendSimulationResultsEmail(followup) case EmailType.BenefitAction: case EmailType.SimulationUsefulness: if (surveyType && survey) { diff --git a/backend/lib/messaging/sending.ts b/backend/lib/messaging/sending.ts index 1610b99247..9bc3a271d3 100644 --- a/backend/lib/messaging/sending.ts +++ b/backend/lib/messaging/sending.ts @@ -5,6 +5,7 @@ import { SurveyType } from "../../../lib/enums/survey.js" import Followups from "../../models/followup.js" import { Followup } from "../../../lib/types/followup.js" import { Survey } from "../../../lib/types/survey.js" +import { sendSimulationResultsEmail } from "../messaging/email/email-service.js" const DaysBeforeInitialEmail = 6 const DaysBeforeTousABordNotificationEmail = 2 @@ -110,11 +111,11 @@ async function processSingleEmail(emailType: EmailType, followupId: string) { throw new Error("Followup not found") } - let emailPromise: Promise | null = null + let emailPromise: Promise switch (emailType) { case EmailType.SimulationResults: - emailPromise = followup.sendSimulationResultsEmail() + emailPromise = sendSimulationResultsEmail(followup) break case EmailType.BenefitAction: emailPromise = followup.sendSurvey( diff --git a/backend/models/followup.ts b/backend/models/followup.ts index 11f9345b37..9318ce2551 100644 --- a/backend/models/followup.ts +++ b/backend/models/followup.ts @@ -24,16 +24,6 @@ FollowupSchema.method("postSimulationResultsSms", function (messageId) { return this.save() }) -FollowupSchema.method("sendSimulationResultsEmail", async function () { - try { - return await sendEmail(EmailType.SimulationResults, this) - } catch (err) { - console.log("error", err) - this.error = JSON.stringify(err, null, 2) - return this.save() - } -}) - FollowupSchema.method( "renderSimulationResultsSmsUrl", function (username: string, password: string) { diff --git a/lib/types/followup.d.ts b/lib/types/followup.d.ts index cc668c376f..2c75c3e9ed 100644 --- a/lib/types/followup.d.ts +++ b/lib/types/followup.d.ts @@ -27,7 +27,6 @@ interface FollowupAttributes { interface FollowupMethods { postSimulationResultsSms(messageId: string): void renderSimulationResultsSms(): any - sendSimulationResultsEmail(): Promise sendSimulationResultsSms(): Promise addSurveyIfMissing(surveyType: SurveyType): Promise sendSurvey(surveyType: SurveyType): Promise From 30c238c5d7f8bf5de23959fc9cceb8de571b6a84 Mon Sep 17 00:00:00 2001 From: Shamzic Date: Mon, 20 Nov 2023 22:56:03 +0100 Subject: [PATCH 19/25] =?UTF-8?q?refactor:=20d=C3=A9place=20sendSurvey=20(?= =?UTF-8?q?modele=20Followup=20->=20email-service)=20&=20suppr=20sendEmail?= =?UTF-8?q?=20d'email-service?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/lib/messaging/email/email-service.ts | 44 ++++---------------- backend/lib/messaging/sending.ts | 24 +++++++---- backend/models/followup.ts | 25 ++--------- lib/types/followup.d.ts | 1 - 4 files changed, 26 insertions(+), 68 deletions(-) diff --git a/backend/lib/messaging/email/email-service.ts b/backend/lib/messaging/email/email-service.ts index 5ba8e1306c..c79e7fec5c 100644 --- a/backend/lib/messaging/email/email-service.ts +++ b/backend/lib/messaging/email/email-service.ts @@ -4,9 +4,9 @@ import { emailRenderBySurveyType, } from "../../mes-aides/emails/email-render.js" import { EmailType } from "../../../../lib/enums/messaging.js" -import { SurveyType } from "@lib/enums/survey.js" -import { Survey } from "@lib/types/survey.js" -import { Followup } from "@lib/types/followup.js" +import { SurveyType } from "../../../../lib/enums/survey.js" +import { Survey } from "../../../../lib/types/survey.js" +import { Followup } from "../../../../lib/types/followup.js" import dayjs from "dayjs" export async function sendSimulationResultsEmail( @@ -37,12 +37,12 @@ export async function sendSimulationResultsEmail( export async function sendSurveyEmail( followup: Followup, - surveyType: SurveyType, - survey: Survey -): Promise { + surveyType: SurveyType +): Promise { if (!followup.email) { throw new Error("Missing followup email") } + const survey = await followup.addSurveyIfMissing(surveyType) const render: any = await emailRenderBySurveyType(surveyType, followup) const sendEmailSmtpResponse = await sendEmailSmtp({ to: followup.email, @@ -53,34 +53,6 @@ export async function sendSurveyEmail( }) survey.messageId = sendEmailSmtpResponse.messageId - - return await followup.save() -} - -export async function sendEmail( - emailType: EmailType, - followup, - surveyType?: SurveyType, - survey?: Survey -) { - try { - switch (emailType) { - case EmailType.SimulationResults: - return await sendSimulationResultsEmail(followup) - case EmailType.BenefitAction: - case EmailType.SimulationUsefulness: - if (surveyType && survey) { - return sendSurveyEmail(followup, surveyType, survey) - } - break - case EmailType.TousABordNotification: - // Todo: send tousabord email - break - default: - throw new Error(`Unknown email type ${emailType}`) - } - } catch (err) { - console.error("error", err) - throw err - } + await followup.save() + return survey } diff --git a/backend/lib/messaging/sending.ts b/backend/lib/messaging/sending.ts index 9bc3a271d3..c2013fbc0e 100644 --- a/backend/lib/messaging/sending.ts +++ b/backend/lib/messaging/sending.ts @@ -5,7 +5,10 @@ import { SurveyType } from "../../../lib/enums/survey.js" import Followups from "../../models/followup.js" import { Followup } from "../../../lib/types/followup.js" import { Survey } from "../../../lib/types/survey.js" -import { sendSimulationResultsEmail } from "../messaging/email/email-service.js" +import { + sendSimulationResultsEmail, + sendSurveyEmail, +} from "../messaging/email/email-service.js" const DaysBeforeInitialEmail = 6 const DaysBeforeTousABordNotificationEmail = 2 @@ -54,8 +57,8 @@ async function sendMultipleInitialEmails(limit: number) { : SurveyType.TrackClickOnSimulationUsefulnessEmail try { - const survey = await followup.sendSurvey(surveyType) - return { "survey sent id": survey._id } + const survey = await sendSurveyEmail(followup, surveyType) + return { "survey id": survey._id } } catch (error) { return { ko: error } } @@ -93,10 +96,11 @@ async function sendMultipleTousABordNotificationEmails(limit: number) { const results = await Promise.all( followups.map(async (followup: Followup) => { try { - const result = await followup.sendSurvey( + const survey = await sendSurveyEmail( + followup, SurveyType.TousABordNotification ) - return { ok: result._id } + return { ok: survey._id } } catch (error) { return { ko: error } } @@ -111,19 +115,21 @@ async function processSingleEmail(emailType: EmailType, followupId: string) { throw new Error("Followup not found") } - let emailPromise: Promise + let emailPromise: Survey | Followup switch (emailType) { case EmailType.SimulationResults: - emailPromise = sendSimulationResultsEmail(followup) + emailPromise = await sendSimulationResultsEmail(followup) break case EmailType.BenefitAction: - emailPromise = followup.sendSurvey( + emailPromise = await sendSurveyEmail( + followup, SurveyType.TrackClickOnBenefitActionEmail ) break case EmailType.SimulationUsefulness: - emailPromise = followup.sendSurvey( + emailPromise = await sendSurveyEmail( + followup, SurveyType.TrackClickOnSimulationUsefulnessEmail ) break diff --git a/backend/models/followup.ts b/backend/models/followup.ts index 9318ce2551..c2d35e0bdc 100644 --- a/backend/models/followup.ts +++ b/backend/models/followup.ts @@ -2,13 +2,11 @@ import mongoose from "mongoose" import axios from "axios" import { Survey } from "../../lib/types/survey.js" import { SurveyType } from "../../lib/enums/survey.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" import { phoneNumberFormatting } from "../../lib/phone-number.js" import FollowupSchema from "./followup-schema.js" -import { sendEmail } from "../lib/messaging/email/email-service.js" FollowupSchema.static("findByEmail", function (email: string) { return this.find({ email }) @@ -66,33 +64,16 @@ FollowupSchema.method("sendSimulationResultsSms", async function () { FollowupSchema.method( "addSurveyIfMissing", - async function (type: SurveyType): Promise { - let survey = this.surveys.find((survey) => survey.type === type) + async function (surveyType: SurveyType): Promise { + let survey = this.surveys.find((survey) => survey.type === surveyType) if (!survey) { - survey = await this.surveys.create({ type }) + survey = await this.surveys.create({ surveyType }) this.surveys.push(survey) } return survey } ) -FollowupSchema.method( - "sendSurvey", - async function (surveyType: SurveyType): Promise { - const followup = this - let survey - try { - const survey = await this.addSurveyIfMissing(surveyType) - await sendEmail(EmailType.BenefitAction, followup, surveyType, survey) - return survey - } catch (err) { - console.error("error", err) - survey.error = err - throw err - } - } -) - FollowupSchema.method("updateSurvey", function (type, answers) { const surveys: Survey[] = Array.from(this.surveys) const survey = surveys.find((s) => s.type === type) diff --git a/lib/types/followup.d.ts b/lib/types/followup.d.ts index 2c75c3e9ed..a994fbf360 100644 --- a/lib/types/followup.d.ts +++ b/lib/types/followup.d.ts @@ -29,7 +29,6 @@ interface FollowupMethods { renderSimulationResultsSms(): any sendSimulationResultsSms(): Promise addSurveyIfMissing(surveyType: SurveyType): Promise - sendSurvey(surveyType: SurveyType): Promise updateSurvey(action: SurveyType, data?: any) } From 949c02a67a40cba88b25bcdc9c2b199e604efa10 Mon Sep 17 00:00:00 2001 From: Shamzic Date: Tue, 21 Nov 2023 10:51:38 +0100 Subject: [PATCH 20/25] refactor: supprime emailPromise --- backend/lib/messaging/sending.ts | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/backend/lib/messaging/sending.ts b/backend/lib/messaging/sending.ts index c2013fbc0e..a8ce1a3a09 100644 --- a/backend/lib/messaging/sending.ts +++ b/backend/lib/messaging/sending.ts @@ -115,20 +115,15 @@ async function processSingleEmail(emailType: EmailType, followupId: string) { throw new Error("Followup not found") } - let emailPromise: Survey | Followup - switch (emailType) { case EmailType.SimulationResults: - emailPromise = await sendSimulationResultsEmail(followup) + sendSimulationResultsEmail(followup) break case EmailType.BenefitAction: - emailPromise = await sendSurveyEmail( - followup, - SurveyType.TrackClickOnBenefitActionEmail - ) + await sendSurveyEmail(followup, SurveyType.TrackClickOnBenefitActionEmail) break case EmailType.SimulationUsefulness: - emailPromise = await sendSurveyEmail( + await sendSurveyEmail( followup, SurveyType.TrackClickOnSimulationUsefulnessEmail ) @@ -136,9 +131,6 @@ async function processSingleEmail(emailType: EmailType, followupId: string) { default: throw new Error(`Unknown email type: ${emailType}`) } - - const email = await emailPromise - console.log("Email sent", email) } export async function processSendEmails( From e53f734a1339d87fc747389def481096d4fd9ab6 Mon Sep 17 00:00:00 2001 From: Shamzic Date: Tue, 21 Nov 2023 14:28:36 +0100 Subject: [PATCH 21/25] =?UTF-8?q?fix:=20manquait=20champ=20type=20=C3=A0?= =?UTF-8?q?=20la=20cr=C3=A9ation=20du=20survey?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/lib/messaging/sending.ts | 1 - backend/models/followup.ts | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/backend/lib/messaging/sending.ts b/backend/lib/messaging/sending.ts index a8ce1a3a09..e1fe556915 100644 --- a/backend/lib/messaging/sending.ts +++ b/backend/lib/messaging/sending.ts @@ -4,7 +4,6 @@ import { EmailType } from "../../../lib/enums/messaging.js" import { SurveyType } from "../../../lib/enums/survey.js" import Followups from "../../models/followup.js" import { Followup } from "../../../lib/types/followup.js" -import { Survey } from "../../../lib/types/survey.js" import { sendSimulationResultsEmail, sendSurveyEmail, diff --git a/backend/models/followup.ts b/backend/models/followup.ts index c2d35e0bdc..c2ded315dd 100644 --- a/backend/models/followup.ts +++ b/backend/models/followup.ts @@ -67,7 +67,7 @@ FollowupSchema.method( async function (surveyType: SurveyType): Promise { let survey = this.surveys.find((survey) => survey.type === surveyType) if (!survey) { - survey = await this.surveys.create({ surveyType }) + survey = await this.surveys.create({ type: surveyType }) this.surveys.push(survey) } return survey From 57400537f1648c12c6c619148ff8bcab561453e4 Mon Sep 17 00:00:00 2001 From: Shamzic Date: Thu, 23 Nov 2023 11:32:45 +0100 Subject: [PATCH 22/25] =?UTF-8?q?refactor:=20ordre=20des=20m=C3=A9thodes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/lib/mes-aides/emails/email-render.ts | 48 ++++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/backend/lib/mes-aides/emails/email-render.ts b/backend/lib/mes-aides/emails/email-render.ts index 8e030ed208..074c9ba4ee 100644 --- a/backend/lib/mes-aides/emails/email-render.ts +++ b/backend/lib/mes-aides/emails/email-render.ts @@ -91,30 +91,6 @@ function renderAsHtml(emailType: EmailType, dataTemplate) { }) } -export async function emailRenderBySurveyType( - surveyType: SurveyType, - followup -) { - switch (surveyType) { - case SurveyType.TrackClickOnBenefitActionEmail: - return emailRender(EmailType.BenefitAction, followup) - case SurveyType.TrackClickOnSimulationUsefulnessEmail: - return emailRender(EmailType.SimulationUsefulness, followup) - case SurveyType.TousABordNotification: - return emailRender(EmailType.TousABordNotification, followup) - case SurveyType.BenefitAction: - return Promise.reject( - new Error( - `This surveyType "${surveyType}" is not supposed to be sent through an email` - ) - ) - default: - return Promise.reject( - new Error(`This surveyType "${surveyType}" has no email template`) - ) - } -} - export async function emailRender(emailType: EmailType, followup) { let benefits: any = null let parameters: any = null @@ -177,3 +153,27 @@ export async function emailRender(emailType: EmailType, followup) { } }) } + +export async function emailRenderBySurveyType( + surveyType: SurveyType, + followup +) { + switch (surveyType) { + case SurveyType.TrackClickOnBenefitActionEmail: + return emailRender(EmailType.BenefitAction, followup) + case SurveyType.TrackClickOnSimulationUsefulnessEmail: + return emailRender(EmailType.SimulationUsefulness, followup) + case SurveyType.TousABordNotification: + return emailRender(EmailType.TousABordNotification, followup) + case SurveyType.BenefitAction: + return Promise.reject( + new Error( + `This surveyType "${surveyType}" is not supposed to be sent through an email` + ) + ) + default: + return Promise.reject( + new Error(`This surveyType "${surveyType}" has no email template`) + ) + } +} From 9491fd980fb8ff151225b0986e80b34e401aac56 Mon Sep 17 00:00:00 2001 From: Shamzic Date: Thu, 23 Nov 2023 11:33:49 +0100 Subject: [PATCH 23/25] refactor: manque un await --- backend/lib/messaging/sending.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/lib/messaging/sending.ts b/backend/lib/messaging/sending.ts index e1fe556915..9e12c1cd13 100644 --- a/backend/lib/messaging/sending.ts +++ b/backend/lib/messaging/sending.ts @@ -116,7 +116,7 @@ async function processSingleEmail(emailType: EmailType, followupId: string) { switch (emailType) { case EmailType.SimulationResults: - sendSimulationResultsEmail(followup) + await sendSimulationResultsEmail(followup) break case EmailType.BenefitAction: await sendSurveyEmail(followup, SurveyType.TrackClickOnBenefitActionEmail) From 258b8aa5436385790f594ae9d1ba08426b00f953 Mon Sep 17 00:00:00 2001 From: Shamzic Date: Mon, 27 Nov 2023 12:36:35 +0100 Subject: [PATCH 24/25] =?UTF-8?q?refactor:=20homog=C3=A9n=C3=A9ise=20->=20?= =?UTF-8?q?msg=20de=20succ=C3=A8s=20dans=20l'envoi=20de=20l'email=20de=20s?= =?UTF-8?q?ondage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/lib/messaging/sending.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/lib/messaging/sending.ts b/backend/lib/messaging/sending.ts index 9e12c1cd13..c3e1acbea4 100644 --- a/backend/lib/messaging/sending.ts +++ b/backend/lib/messaging/sending.ts @@ -57,7 +57,7 @@ async function sendMultipleInitialEmails(limit: number) { try { const survey = await sendSurveyEmail(followup, surveyType) - return { "survey id": survey._id } + return { survey_id: survey._id } } catch (error) { return { ko: error } } @@ -99,7 +99,7 @@ async function sendMultipleTousABordNotificationEmails(limit: number) { followup, SurveyType.TousABordNotification ) - return { ok: survey._id } + return { survey_id: survey._id } } catch (error) { return { ko: error } } From 22f7321b12dda3c046c012c8bcf122fa8a086fcc Mon Sep 17 00:00:00 2001 From: Shamzic Date: Tue, 28 Nov 2023 12:07:59 +0100 Subject: [PATCH 25/25] refactor: supprime une condition de rendu par email (getFollowupEmail) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit La gestion d'erreur est traitée dans la fonction renderEmailByType --- backend/controllers/emails.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/backend/controllers/emails.ts b/backend/controllers/emails.ts index be7bf8ba41..7db8ec8932 100644 --- a/backend/controllers/emails.ts +++ b/backend/controllers/emails.ts @@ -28,9 +28,7 @@ const getFollowupEmail = async (req, res, next) => { const { emailType }: { emailType: EmailType } = req.query const followup = req.followup const result = await renderEmailByType(followup, emailType) - if (result) { - res.send(result["html"]) - } + res.send(result?.html) } catch (err) { next(err) }