-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor services email et sms (#4066)
* refactor: déplace enum email * refactor: outil d'envoi d'email * refactor: déplace le schema du followup dans un fichier dédié * refactor: création de email-service (separation des responsabilités) * feat: ajoute un enum commun pour le service de messages * refactor: arborescence service messaging * refacto: messaging enum path * refactor : renderAndSendEmail en sendEmail + sendMail en sendEmailSmtp * refactor: followup sendSurvey * refactor: supprime enum non utile * refactor: déplace méthode followup dans email-service * refactor: déplace le script outil d'envoi d'email * refactor: supprime email.sh et ajoute son contenu dans une commande du package.json * refactor: smsCategory pas utilisé ici * refactor: enum EmailCategory -> EmailType * refactor: service email * fix: Lever des erreurs si accès à une props d'un objet undefined * refactor: sort sendSimulationResultsEmail du followup -> email-service * refactor: déplace sendSurvey (modele Followup -> email-service) & suppr sendEmail d'email-service * refactor: supprime emailPromise * fix: manquait champ type à la création du survey * refactor: ordre des méthodes * refactor: manque un await * refactor: homogénéise -> msg de succès dans l'envoi de l'email de sondage * refactor: supprime une condition de rendu par email (getFollowupEmail) La gestion d'erreur est traitée dans la fonction renderEmailByType
- Loading branch information
Showing
14 changed files
with
186 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { sendEmailSmtp } from "../../smtp.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 sendSimulationResultsEmail( | ||
followup: Followup | ||
): Promise<Followup> { | ||
if (!followup.email) { | ||
throw new Error("Missing followup email") | ||
} | ||
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 await followup.save() | ||
} | ||
|
||
export async function sendSurveyEmail( | ||
followup: Followup, | ||
surveyType: SurveyType | ||
): Promise<Survey> { | ||
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, | ||
subject: render.subject, | ||
text: render.text, | ||
html: render.html, | ||
tags: ["survey", surveyType], | ||
}) | ||
|
||
survey.messageId = sendEmailSmtpResponse.messageId | ||
await followup.save() | ||
return survey | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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<Followup, FollowupModel>( | ||
{ | ||
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 |
Oops, something went wrong.