11import { toast } from "react-toastify"
2- import { Variation } from "../types"
2+ import { Variation , VariationsTypes } from "../types"
33import editVariations from "./edit-variations"
44import { notificationGetTemplate } from "../config/notifications"
55
66const messageTemplate = ( variation : Variation ) =>
7- `<ul><li>Nom de la structure: ${ variation . structure . name } </li><li>Identifiant de la structure: ${
7+ `<ul><li>Nom de la structure: ${
8+ variation . structure . name
9+ } </li><li>Identifiant de la structure: ${
810 variation . structure ?. id || "Non renseigné"
9- } </li><li>Date de la demande: ${ new Date ( variation . created_at ) . toLocaleDateString ( ) } </li></ul>`
11+ } </li><li>Date de la demande: ${ new Date (
12+ variation . created_at
13+ ) . toLocaleDateString ( ) } </li></ul>`
1014
11- async function sendEmail ( variation : Variation , response : string ) {
15+ async function sendEmail (
16+ api : VariationsTypes ,
17+ variation : Variation ,
18+ response : string
19+ ) {
1220 const url = `/api/send-email`
1321 const selectedProfile = localStorage . getItem ( "selectedProfile" )
1422 const formattedResponse = response . replace ( / \n / g, "<br/>" )
@@ -20,7 +28,7 @@ async function sendEmail(variation: Variation, response: string) {
2028 subject : `Réponse à votre demande de déclinaison locale, référence bso-${ variation . id } ` ,
2129 userResponse : formattedResponse ,
2230 message : messageTemplate ( variation ) ,
23- collectionName : "bso_local_variations_publications" ,
31+ collectionName : `bso_local_variations_ ${ api } ` ,
2432 selectedProfile,
2533 }
2634
@@ -32,34 +40,44 @@ async function sendEmail(variation: Variation, response: string) {
3240 body : JSON . stringify ( emailPayload ) ,
3341 } )
3442 . then ( ( response ) => {
35- if ( ! response . ok ) throw new Error ( `Error while sending email: ${ response . status } ` )
43+ if ( ! response . ok )
44+ throw new Error ( `Error while sending email: ${ response . status } ` )
3645 } )
3746 . catch ( ( error ) => {
3847 throw error
3948 } )
4049}
4150
4251export default async function sendEmails (
43- api : string ,
52+ api : VariationsTypes ,
4453 variations : Array < Variation > ,
4554 notification : string ,
4655 response : string ,
4756 useTemplate ?: boolean ,
4857 getCommentsName ?: ( id : string ) => string
4958) {
5059 // Set status as treated if final notification sent
51- const inputs = { tags : { notification : notification } , status : notification === "done" ? "treated" : "ongoing" }
60+ const inputs = {
61+ tags : { notification : notification } ,
62+ status : notification === "done" ? "treated" : "ongoing" ,
63+ }
5264
5365 // If single variation and notification is custom, set correct status (should only happen from email-box)
5466 if ( variations . length === 1 && notification === "custom" )
55- inputs . status = variations [ 0 ] . tags ?. notification === "done" ? "treated" : "ongoing"
67+ inputs . status =
68+ variations [ 0 ] . tags ?. notification === "done" ? "treated" : "ongoing"
5669
5770 Promise . all (
5871 variations . map ( ( variation ) =>
5972 sendEmail (
73+ api ,
6074 variation ,
6175 useTemplate
62- ? notificationGetTemplate ( notification , variation . structure ?. id , getCommentsName ( variation . structure ?. id ) )
76+ ? notificationGetTemplate (
77+ notification ,
78+ variation . structure ?. id ,
79+ getCommentsName ( variation . structure ?. id )
80+ )
6381 : response
6482 )
6583 )
@@ -73,7 +91,9 @@ export default async function sendEmails(
7391 )
7492 }
7593 toast . success (
76- variations ?. length > 1 ? "Les emails ont été envoyés avec succès !" : "L'email à été envoyé avec succès !"
94+ variations ?. length > 1
95+ ? "Les emails ont été envoyés avec succès !"
96+ : "L'email à été envoyé avec succès !"
7797 )
7898 } )
7999 . catch ( ( error ) => {
0 commit comments