-
Notifications
You must be signed in to change notification settings - Fork 40
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 services email et sms #4066
Changes from 21 commits
dce9cf3
4783f15
d52e7dd
ae3723e
7370835
7d41dd2
5a99399
c06f0f8
470d904
0943e29
f9e06fb
93d5f9b
796b14c
9159807
5644f94
5c2fba9
a95bf65
1f2e56b
30c238c
949c02a
e53f734
5740053
9491fd9
258b8aa
22f7321
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import { EmailType } from "../../backend/enums/email.js" | ||
import emailRender from "../../backend/lib/mes-aides/emails/email-render.js" | ||
import { EmailType } from "../../lib/enums/messaging.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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Si on rajoute ce There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Shamzic je sais pas si tu avais vu There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. J'ai supprimé la condition car, comme tu l'indiques, |
||
res.send(result["html"]) | ||
} | ||
} catch (err) { | ||
next(err) | ||
} | ||
|
This file was deleted.
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" | ||
baptou12 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
} |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pour avancé on va pas changer cette PR, mais ici j'ai l'impression finalement que c'est exactement la fonction
emailRender
qu'on veut recoder (en inversant le sens des argument ? Je créé un ticket pour plus tard.