notification accessright

This commit is contained in:
philc 2025-03-11 10:32:35 +01:00
parent 87b443d5d0
commit 871f476794
2 changed files with 54 additions and 60 deletions

View File

@ -209,20 +209,14 @@ Notifications.sendsms = async (data, tribeId) => {
} }
*/ */
}; };
Notifications.managepush = async(data,templatename,tribe,lg)=>{ Notifications.manageemail = async (
/** data,
* TODO templatename,
* open templatename get tplemail.notif renderit with data and push it tribe,
* with tribe parameter lg,
*/ numberpersecond = 10,
return { profils = ["anonymous"]
status: 200, ) => {
ref: "Wwws",
msg: "pushmsgTODO",
data: {tribe,templatename},
};
}
Notifications.manageemail = async (data, templatename, tribe, lg,numberpersecond=10) => {
const sleep = (ms) => { const sleep = (ms) => {
return new Promise((resolve) => setTimeout(resolve, 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]; if (!["emailsto"].includes(k)) emailtosend[k] = data[k];
}); });
const tplemail = require(path.resolve(tplpath)); 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" }; let sendit = { status: 200, ref: "Notifications", msg: "successfullsend" };
for (let i = 0; i < data.emailsto.length; i++) { for (let i = 0; i < data.emailsto.length; i++) {
const email = data.emailsto[i]; const email = data.emailsto[i];
if (typeof email.to === "string"){ if (typeof email.to === "string") {
email.to=[email.to] email.to = [email.to];
} }
emailtosend.to="" emailtosend.to = "";
email.to.forEach(em=>{ email.to.forEach((em) => {
em=em.toLowerCase().trim() em = em.toLowerCase().trim();
if (Checkjson.testformat(em, "email")){ if (Checkjson.testformat(em, "email")) {
emailtosend.to+=em+"," emailtosend.to += em + ",";
} } else {
else {
results.fail.push({ to: em, err: ["errorformat"] }); results.fail.push({ to: em, err: ["errorformat"] });
} }
}) });
console.log(email.to,"--",emailtosend.to) if (emailtosend.to.length > 2) {
//emailtosend.to = email.to.toLowerCase().trim(); emailtosend.to = emailtosend.to.slice(0, -1);
if (emailtosend.to.length>2) { emailtosend.templatename = templatename;
emailtosend.to=emailtosend.to.slice(0,-1); emailtosend.lg = lg;
emailtosend.templatename=templatename;
emailtosend.lg=lg
emailtosend.subject = Mustache.render(tplemail.subject, email); emailtosend.subject = Mustache.render(tplemail.subject, email);
emailtosend.html = Mustache.render(tplemail.html, email); emailtosend.html = Mustache.render(tplemail.html, email);
emailtosend.text = Mustache.render(tplemail.text, 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); results.success.push(emailtosend.to);
} else { } else {
results.fail.push({ to: emailtosend.to, status: sendit.status }); results.fail.push({ to: emailtosend.to, status: sendit.status });
console.log(sendit) console.log(sendit);
} }
if ((i + 1) % numberpersecond === 0) { if ((i + 1) % numberpersecond === 0) {
console.log("Prochain lot attend 1 seconde"); console.log("Prochain lot attend 1 seconde");
@ -337,7 +349,7 @@ Notifications.manageemail = async (data, templatename, tribe, lg,numberpersecond
data: { data: {
emailsucces: results.success.length, emailsucces: results.success.length,
emailfail: results.fail.length, emailfail: results.fail.length,
failsdetail:results.fail, failsdetail: results.fail,
timems: Date.now() - start, timems: Date.now() - start,
}, },
}; };
@ -390,11 +402,11 @@ Notifications.sendmail = async (data, tribe) => {
data: { tribe: tribe }, data: { tribe: tribe },
}; };
} }
if (!data.from) { if (!data.from) {
data.from = conf.emailcontact; data.from = conf.emailcontact;
} }
let missingk = []; let missingk = [];
["from", "to", "subject", "html", "text"].forEach((k) => { ["from", "to", "subject", "html", "text"].forEach((k) => {
if (!data[k]) { if (!data[k]) {
@ -462,7 +474,10 @@ Notifications.sendmail = async (data, tribe) => {
data.subject 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 { try {
res = await transporter.sendMail(data); res = await transporter.sendMail(data);

View File

@ -88,7 +88,8 @@ router.post(
req.params.template, req.params.template,
req.params.tribe, req.params.tribe,
req.session.header.xlang, req.session.header.xlang,
1 1,
req.session.header.xprofils
); );
sendret = sendcampain; sendret = sendcampain;
} else { } else {
@ -98,30 +99,8 @@ router.post(
msg: "bodymisformat", msg: "bodymisformat",
data: { body: req.body } 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); res.status(sendret.status).send(sendret);
} }
); );