-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(mails): add sending mail after contribution received
- Loading branch information
Showing
9 changed files
with
340 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export const emailRecipients: { [key: string]: { to: string[] } } = { | ||
scanr: { | ||
to: process.env.SCANR_EMAIL_RECIPIENTS?.split(",") || [], | ||
}, | ||
paysage: { | ||
to: process.env.PAYSAGE_EMAIL_RECIPIENTS?.split(",") || [], | ||
}, | ||
bso: { | ||
to: process.env.BSO_EMAIL_RECIPIENTS?.split(",") || [], | ||
}, | ||
"works-magnet": { | ||
to: process.env.WORKS_MAGNET_EMAIL_RECIPIENTS?.split(",") || [], | ||
}, | ||
datasupr: { | ||
to: process.env.DATASUPR_EMAIL_RECIPIENTS?.split(",") || [], | ||
}, | ||
curiexplore: { | ||
to: process.env.CURIEXPLORE_EMAIL_RECIPIENTS?.split(",") || [], | ||
}, | ||
}; |
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 |
---|---|---|
|
@@ -4,10 +4,11 @@ import db from "../../../libs/mongo"; | |
import { contactSchema } from "../../../schemas/get/contactSchema"; | ||
import { errorSchema } from "../../../schemas/errors/errorSchema"; | ||
import { ObjectId } from "mongodb"; | ||
import { emailRecipients } from "./emailRecipents"; | ||
|
||
type postContactSchemaType = Static<typeof postContactSchema>; | ||
const postContactsRoutes = new Elysia(); | ||
|
||
const postContactsRoutes = new Elysia(); | ||
postContactsRoutes.post( | ||
"/contacts", | ||
async ({ error, body }: { error: any; body: postContactSchemaType }) => { | ||
|
@@ -21,10 +22,10 @@ postContactsRoutes.post( | |
]; | ||
|
||
if (!allowedFromApps.includes(body.fromApplication)) { | ||
return error( | ||
400, | ||
"Invalid fromApplication value, check child attributes" | ||
); | ||
return error(400, { | ||
message: "Invalid fromApplication value, check child attributes", | ||
code: "INVALID_FROM_APP", | ||
}); | ||
} | ||
|
||
const extraLowercase = Object.keys(body.extra || {}).reduce( | ||
|
@@ -46,14 +47,66 @@ postContactsRoutes.post( | |
|
||
const result = await db.collection("contacts").insertOne(newContribution); | ||
if (!result.insertedId) { | ||
return error(500, "Failed to create the contribution"); | ||
return error(500, { | ||
message: "Failed to create the contribution", | ||
code: "INSERTION_FAILED", | ||
}); | ||
} | ||
|
||
const finalContribution = { | ||
...newContribution, | ||
id: result.insertedId.toHexString(), | ||
}; | ||
|
||
const contributionLink = `https://ticket-office.staging.dataesr.ovh/${body.fromApplication}-contact?page=1&query=${finalContribution.id}&searchInMessage=false&sort=DESC&status=choose`; | ||
|
||
const BREVO_API_KEY = process.env.BREVO_API_KEY; | ||
if (!BREVO_API_KEY) { | ||
return error(500, { | ||
message: "BREVO_API_KEY is not defined", | ||
code: "MISSING_API_KEY", | ||
}); | ||
} | ||
|
||
const recipients = emailRecipients[body.fromApplication]; | ||
if (!recipients) { | ||
return error(400, { | ||
message: "No email recipients found for this application", | ||
code: "NO_RECIPIENTS_FOUND", | ||
}); | ||
} | ||
|
||
const dataForBrevo = { | ||
sender: { | ||
email: process.env.MAIL_SENDER, | ||
name: "L'équipe scanR", | ||
}, | ||
to: recipients.to.map((email) => ({ email, name: email.split("@")[0] })), | ||
replyTo: { email: "[email protected]", name: "L'équipe scanR" }, | ||
subject: "Nouvelle contribution créée", | ||
templateId: 268, | ||
params: { | ||
date: new Date().toLocaleDateString("fr-FR"), | ||
message: `La contribution avec l'ID ${finalContribution.id} a été ajoutée. Vous pouvez consulter la contribution en cliquant sur le lien suivant : ${contributionLink}`, | ||
}, | ||
}; | ||
|
||
const response = await fetch("https://api.brevo.com/v3/smtp/email", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"api-key": BREVO_API_KEY, | ||
}, | ||
body: JSON.stringify(dataForBrevo), | ||
}); | ||
|
||
if (!response.ok) { | ||
return error(500, { | ||
message: `Erreur d'envoi d'email: ${response.statusText}`, | ||
code: "EMAIL_SEND_FAILED", | ||
}); | ||
} | ||
|
||
return finalContribution; | ||
}, | ||
{ | ||
|
@@ -71,4 +124,5 @@ postContactsRoutes.post( | |
}, | ||
} | ||
); | ||
|
||
export default postContactsRoutes; |
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 |
---|---|---|
|
@@ -4,6 +4,7 @@ import { postContributionObjectSchema } from "../../../schemas/post/contribution | |
import { errorSchema } from "../../../schemas/errors/errorSchema"; | ||
import { contributionObjectSchema } from "../../../schemas/get/contributionsObjectSchema"; | ||
import { ObjectId } from "mongodb"; | ||
import { emailRecipients } from "../../contacts/post/emailRecipents"; | ||
|
||
type postContributionObjectSchemaType = Static< | ||
typeof postContributionObjectSchema | ||
|
@@ -38,12 +39,12 @@ postContributionObjectRoutes.post( | |
status: "new", | ||
}; | ||
|
||
const result = await db.collection("contribute").insertOne(newContribution); | ||
|
||
if (!body.objectId && !body.objectType) { | ||
return error(400, "objectId is required when objectType is provided"); | ||
} | ||
|
||
const result = await db.collection("contribute").insertOne(newContribution); | ||
|
||
if (!result.insertedId) { | ||
return error(500, "Failed to create the contribution"); | ||
} | ||
|
@@ -53,6 +54,53 @@ postContributionObjectRoutes.post( | |
id: result.insertedId.toHexString(), | ||
}; | ||
|
||
const contributionLink = `https://ticket-office.staging.dataesr.ovh/scanr-contributionPage?page=1&query=${finalContribution.id}&searchInMessage=false&sort=DESC&status=choose`; | ||
|
||
const BREVO_API_KEY = process.env.BREVO_API_KEY; | ||
if (!BREVO_API_KEY) { | ||
return error(500, { | ||
message: "BREVO_API_KEY is not defined", | ||
code: "MISSING_API_KEY", | ||
}); | ||
} | ||
|
||
const recipients = emailRecipients["contribute"] || { | ||
to: process.env.SCANR_EMAIL_RECIPIENTS?.split(",") || [], | ||
}; | ||
|
||
const dataForBrevo = { | ||
sender: { | ||
email: process.env.MAIL_SENDER, | ||
name: "L'équipe scanR", | ||
}, | ||
to: recipients.to.map((email: string) => ({ | ||
email, | ||
name: email.split("@")[0], | ||
})), | ||
replyTo: { email: "[email protected]", name: "L'équipe scanR" }, | ||
subject: "Nouvelle contribution par objet créée", | ||
templateId: 268, | ||
params: { | ||
date: new Date().toLocaleDateString("fr-FR"), | ||
message: `La contribution avec l'ID ${finalContribution.id} a été ajoutée. Vous pouvez consulter la contribution en cliquant sur le lien suivant : <a href="${contributionLink}">Consulter la contribution</a>`, | ||
}, | ||
}; | ||
|
||
const response = await fetch("https://api.brevo.com/v3/smtp/email", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"api-key": BREVO_API_KEY, | ||
}, | ||
body: JSON.stringify(dataForBrevo), | ||
}); | ||
if (!response.ok) { | ||
return error(500, { | ||
message: `Erreur d'envoi d'email: ${response.statusText}`, | ||
code: "EMAIL_SEND_FAILED", | ||
}); | ||
} | ||
|
||
return finalContribution; | ||
}, | ||
{ | ||
|
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 |
---|---|---|
|
@@ -4,6 +4,7 @@ import { postProductionsSchema } from "../../../schemas/post/productionsSchema"; | |
import { productionSchema } from "../../../schemas/get/productionSchema"; | ||
import { errorSchema } from "../../../schemas/errors/errorSchema"; | ||
import { ObjectId } from "mongodb"; | ||
import { emailRecipients } from "../../contacts/post/emailRecipents"; | ||
|
||
type postProductionSchemaType = Static<typeof postProductionsSchema>; | ||
|
||
|
@@ -43,6 +44,53 @@ postProductionRoutes.post( | |
id: result.insertedId.toHexString(), | ||
}; | ||
|
||
const contributionLink = `https://ticket-office.staging.dataesr.ovh/scanr-apioperations?page=1&query=${finalContribution.id}&searchInMessage=false&sort=DESC&status=choose`; | ||
|
||
const BREVO_API_KEY = process.env.BREVO_API_KEY; | ||
if (!BREVO_API_KEY) { | ||
return error(500, { | ||
message: "BREVO_API_KEY is not defined", | ||
code: "MISSING_API_KEY", | ||
}); | ||
} | ||
|
||
const recipients = emailRecipients["contribute_productions"] || { | ||
to: process.env.SCANR_EMAIL_RECIPIENTS?.split(",") || [], | ||
}; | ||
|
||
const dataForBrevo = { | ||
sender: { | ||
email: process.env.MAIL_SENDER, | ||
name: "L'équipe scanR", | ||
}, | ||
to: recipients.to.map((email: string) => ({ | ||
email, | ||
name: email.split("@")[0], | ||
})), | ||
replyTo: { email: "[email protected]", name: "L'équipe scanR" }, | ||
subject: "Nouvelle contribution d'affiliation de publication", | ||
templateId: 268, | ||
params: { | ||
date: new Date().toLocaleDateString("fr-FR"), | ||
message: `La contribution avec l'ID ${finalContribution.id} a été ajoutée. Vous pouvez consulter la contribution en cliquant sur le lien suivant : <a href="${contributionLink}">Consulter la contribution</a>`, | ||
}, | ||
}; | ||
|
||
const response = await fetch("https://api.brevo.com/v3/smtp/email", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"api-key": BREVO_API_KEY, | ||
}, | ||
body: JSON.stringify(dataForBrevo), | ||
}); | ||
if (!response.ok) { | ||
return error(500, { | ||
message: `Erreur d'envoi d'email: ${response.statusText}`, | ||
code: "EMAIL_SEND_FAILED", | ||
}); | ||
} | ||
|
||
return finalContribution; | ||
}, | ||
{ | ||
|
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
Oops, something went wrong.