diff --git a/apxtri/models/Notifications.js b/apxtri/models/Notifications.js index 22809b6..54672eb 100644 --- a/apxtri/models/Notifications.js +++ b/apxtri/models/Notifications.js @@ -209,20 +209,14 @@ Notifications.sendsms = async (data, tribeId) => { } */ }; -Notifications.managepush = async(data,templatename,tribe,lg)=>{ - /** - * TODO - * open templatename get tplemail.notif renderit with data and push it - * with tribe parameter - */ - return { - status: 200, - ref: "Wwws", - msg: "pushmsgTODO", - data: {tribe,templatename}, - }; -} -Notifications.manageemail = async (data, templatename, tribe, lg,numberpersecond=10) => { +Notifications.manageemail = async ( + data, + templatename, + tribe, + lg, + numberpersecond = 10, + profils = ["anonymous"] +) => { const sleep = (ms) => { return new Promise((resolve) => setTimeout(resolve, ms)); }; @@ -273,29 +267,47 @@ Notifications.manageemail = async (data, templatename, tribe, lg,numberpersecond if (!["emailsto"].includes(k)) emailtosend[k] = data[k]; }); const tplemail = require(path.resolve(tplpath)); + // check allowed profil + if (!tplemail.allowedprofils){ + return { + status: 406, + ref: "Notification", + msg: "missingallowedprofils", + data: { template: tplpath }, + }; + } + if (!profils.includes('druid')){ + const setallow = new Set(tplemail.allowedprofils) + if (profils.filter(v=> setallow.has(v)).length==0){ + return { + status: 403, + ref: "Notification", + msg: "profilsnotallowed", + data: { template: tplpath,profils }, + }; + } + } + let sendit = { status: 200, ref: "Notifications", msg: "successfullsend" }; for (let i = 0; i < data.emailsto.length; i++) { const email = data.emailsto[i]; - if (typeof email.to === "string"){ - email.to=[email.to] + if (typeof email.to === "string") { + email.to = [email.to]; } - emailtosend.to="" - email.to.forEach(em=>{ - em=em.toLowerCase().trim() - if (Checkjson.testformat(em, "email")){ - emailtosend.to+=em+"," - } - else { + emailtosend.to = ""; + email.to.forEach((em) => { + em = em.toLowerCase().trim(); + if (Checkjson.testformat(em, "email")) { + emailtosend.to += em + ","; + } else { results.fail.push({ to: em, err: ["errorformat"] }); - } - }) - console.log(email.to,"--",emailtosend.to) - //emailtosend.to = email.to.toLowerCase().trim(); - if (emailtosend.to.length>2) { - emailtosend.to=emailtosend.to.slice(0,-1); - emailtosend.templatename=templatename; - emailtosend.lg=lg + } + }); + if (emailtosend.to.length > 2) { + emailtosend.to = emailtosend.to.slice(0, -1); + emailtosend.templatename = templatename; + emailtosend.lg = lg; emailtosend.subject = Mustache.render(tplemail.subject, email); emailtosend.html = Mustache.render(tplemail.html, email); emailtosend.text = Mustache.render(tplemail.text, email); @@ -318,7 +330,7 @@ Notifications.manageemail = async (data, templatename, tribe, lg,numberpersecond results.success.push(emailtosend.to); } else { results.fail.push({ to: emailtosend.to, status: sendit.status }); - console.log(sendit) + console.log(sendit); } if ((i + 1) % numberpersecond === 0) { console.log("Prochain lot attend 1 seconde"); @@ -337,7 +349,7 @@ Notifications.manageemail = async (data, templatename, tribe, lg,numberpersecond data: { emailsucces: results.success.length, emailfail: results.fail.length, - failsdetail:results.fail, + failsdetail: results.fail, timems: Date.now() - start, }, }; @@ -390,11 +402,11 @@ Notifications.sendmail = async (data, tribe) => { data: { tribe: tribe }, }; } - + if (!data.from) { data.from = conf.emailcontact; } - + let missingk = []; ["from", "to", "subject", "html", "text"].forEach((k) => { if (!data[k]) { @@ -462,7 +474,10 @@ Notifications.sendmail = async (data, tribe) => { data.subject ); */ - fs.outputFile(`../${tribe}/logs/template/${data.templatename}_${data.lg}.html`, data.html); + fs.outputFile( + `../${tribe}/logs/template/${data.templatename}_${data.lg}.html`, + data.html + ); try { res = await transporter.sendMail(data); diff --git a/apxtri/routes/notifications.js b/apxtri/routes/notifications.js index a81a4e0..3c9d25d 100644 --- a/apxtri/routes/notifications.js +++ b/apxtri/routes/notifications.js @@ -88,7 +88,8 @@ router.post( req.params.template, req.params.tribe, req.session.header.xlang, - 1 + 1, + req.session.header.xprofils ); sendret = sendcampain; } else { @@ -98,30 +99,8 @@ router.post( msg: "bodymisformat", data: { body: req.body } }; - // To delete when Bhavesh removed direct email requesting registration - - /* - const campain = { - emailsto: [ - { - to: req.body.emails, - }, - ], - }; - Object.keys(req.body).forEach(k=>{ - const newk= (k=="Cc")?"cc":k; - campain.emailsto[0][newk]=req.body[k] - }) - sendret = await Notifications.manageemail( - campain, - req.params.template, - req.params.tribe, - req.session.header.xlang, - 1 - ); - */ } - console.log(sendret); + //console.log(sendret); res.status(sendret.status).send(sendret); } );