From 4bfcf28de9695eebc0914f76c84d67cf7b7b05bc Mon Sep 17 00:00:00 2001 From: philc Date: Sat, 7 Nov 2026 15:18:46 +0100 Subject: [PATCH] fix envoie email de check --- apxtri/models/Notifications.js | 93 ++++++++++++++++++++++------------ apxtri/models/Pagans.js | 21 ++++++-- apxtri/routes/notifications.js | 2 +- apxtri/routes/pagans.js | 23 +++++++-- schema/tribes.json | 2 +- template/checkemail_en.js | 1 + template/checkemail_fr.js | 1 + template/createidentity_en.js | 1 + template/createidentity_fr.js | 1 + 9 files changed, 102 insertions(+), 43 deletions(-) diff --git a/apxtri/models/Notifications.js b/apxtri/models/Notifications.js index 7846a1b..ab18aeb 100644 --- a/apxtri/models/Notifications.js +++ b/apxtri/models/Notifications.js @@ -396,14 +396,18 @@ Notifications.manageemail = async ( }; } //if (typeof data.emailsto === "string") data.emailsto = [data.emailsto]; - const tplpath = `../${tribe}/template/${templatename}_${lg}.js`; + let tplpath = `../${tribe}/template/${templatename}_${lg}.js`; if (!fs.existsSync(tplpath)) { - return { - status: 404, - ref: "Notification", - msg: "templatenotfound", - data: { template: tplpath }, - }; + const fallbacktplpath = `../adminapi/template/${templatename}_${lg}.js`; + if (!fs.existsSync(fallbacktplpath)) { + return { + status: 404, + ref: "Notification", + msg: "templatenotfound", + data: { template: tplpath }, + }; + } + tplpath = fallbacktplpath; } const results = { fail: [], success: [], simulated: [] }; const emailtosend = {}; @@ -574,7 +578,48 @@ Notifications.sendmail = async (data, tribe) => { console.log("Brevo contact error:", err.response?.data || err.message); } } - if (!conf.smtp || !conf.emailcontact) { + // Resolve the mail configuration key by key: the tribe config wins, and any + // key it does not define ("" / {} / [] treated as unset) falls back to the + // global adminapi config. + const isunset = (v) => + v === undefined || + v === null || + (typeof v === "string" && v.trim() === "") || + (Array.isArray(v) && v.length === 0) || + (typeof v === "object" && !Array.isArray(v) && Object.keys(v).length === 0); + + let conftrib = {}; + const conftribfile = `../adminapi/objects/tribes/itm/${tribe}.json`; + if (fs.existsSync(conftribfile)) { + conftrib = fs.readJSONSync(conftribfile); + } else if (log) { + console.log( + currentmod, + "Warning: this conf does not exist:", + path.resolve(conftribfile), + ); + } + + const resolveconf = (key) => + !isunset(conftrib[key]) + ? conftrib[key] + : !isunset(conf[key]) + ? conf[key] + : undefined; + + const confemailcontact = resolveconf("emailcontact"); + const confsmtp = resolveconf("smtp"); + const confbrevo = resolveconf("BREVO"); + + if (!confemailcontact) { + return { + status: 428, + ref: "Notifications", + msg: "missingemailcontactinconf", + data: { tribe }, + }; + } + if (!confsmtp) { return { status: 412, ref: "Notifications", @@ -584,7 +629,7 @@ Notifications.sendmail = async (data, tribe) => { } if (!data.from) { - data.from = conf.emailcontact; + data.from = confemailcontact; } let missingk = []; @@ -601,32 +646,14 @@ Notifications.sendmail = async (data, tribe) => { data: { missingk: missingk }, }; } - let confsmtp = conf.smtp; - let confbrevoapikey = conf.BREVO.APIKEY; - const conftribfile = `../adminapi/objects/tribes/itm/${tribe}.json`; - if (fs.existsSync(conftribfile)) { - const conftrib = fs.readJSONSync(conftribfile); - if (!conftrib.emailcontact) { - return { - status: 428, - ref: "Notifications", - msg: "missingemailcontactinconf", - data: { tribe }, - }; + + if (confbrevo && confbrevo.APIKEY) { + const toEmails = data.to.split(","); + for (const email of toEmails) { + await ensureContact(email, confbrevo.APIKEY); } - confsmtp = conftrib.smtp; - if (!data.from || data.from == conf.emailcontact) - data.from = conftrib.emailcontact; - if (conftrib.BREVO){ - const toEmails = data.to.split(","); - for (const email of toEmails) { - await ensureContact(email,conftrib.BREVO.APIKEY); - } - } - } else { - if (log) console.log(currentmod,"Warning: this conf does not exist:",path.resolve(conftribfile)); } - + //console.log(confsmtp); const transporter = await nodemailer.createTransport(confsmtp); if (data.filelist) { diff --git a/apxtri/models/Pagans.js b/apxtri/models/Pagans.js index 4f1e5b3..bee72ca 100644 --- a/apxtri/models/Pagans.js +++ b/apxtri/models/Pagans.js @@ -50,7 +50,7 @@ Pagans.logout = (alias, tribe, xdays, xhash) => { * @Param lg language to send email */ -Pagans.keyrecovery = (emailalias, tribe, search, lg) => { +Pagans.keyrecovery = async (emailalias, tribe, search, lg) => { if (log) console.log( currentmod, @@ -108,16 +108,29 @@ Pagans.keyrecovery = (emailalias, tribe, search, lg) => { /* emaillist.forEach((e) => { const ret = Pagans.sendmailkey(e); });*/ + let numberemailsent = 0; + let emailfail = 0; for (let i = 0; i < emaillist.length && i < 3; i++) { const e = emaillist[i]; - const ret = Pagans.sendmailkey(e); + try { + const ret = await Pagans.sendmailkey(e); + if (ret && ret.status == 200) { + numberemailsent++; + } else { + emailfail++; + if (log) console.log(currentmod, "keyrecovery sendmailkey failed:", ret); + } + } catch (err) { + emailfail++; + console.log(currentmod, "keyrecovery sendmailkey error:", err.message); + } } if (emaillist.length > 0) { return { status: 200, ref: "Pagans", msg: "recoveryemailsent", - data: { numberemailsent: emaillist.length }, + data: { numberemailsent, emailfail }, }; } return { @@ -241,7 +254,7 @@ Pagans.sendmailkey = (data) => { person.avecpassphrase = person.passphrase != ""; let tplfile = `../${data.tribe}/template/createidentity_${data.lg}.js`; if (!fs.existsSync(tplfile)) { - tplfile = `../${data.tribe}/template/createidentity_${data.lg}.js`; + tplfile = `../adminapi/template/createidentity_${data.lg}.js`; if (!fs.existsSync(tplfile)) { return { status: 406, diff --git a/apxtri/routes/notifications.js b/apxtri/routes/notifications.js index fdf82c1..f279c16 100644 --- a/apxtri/routes/notifications.js +++ b/apxtri/routes/notifications.js @@ -283,7 +283,7 @@ router.get("/checkemail/:email/:digit6", checkHeaders, async (req, res) => { emailsto: [{ to: email, code }] }, "checkemail", - "adminapi", + req.session.header.xtribe, Notifications.recipientlang(null, req.session.header.xlang), 1, ["anonymous"] diff --git a/apxtri/routes/pagans.js b/apxtri/routes/pagans.js index 2bf8610..62b52bf 100755 --- a/apxtri/routes/pagans.js +++ b/apxtri/routes/pagans.js @@ -157,7 +157,7 @@ router.post("/", checkHeaders, isAuthenticated, async (req, res) => { alias: req.body.alias, privatekey: req.body.privatekey, tribe: req.session.header.xtribe, - passprhase: req.body.passphrase, + passphrase: req.body.passphrase, publickey: req.body.publickey, email: req.body.email, lg: req.session.header.xlang, @@ -414,9 +414,24 @@ router.put("/person/:tribe", checkHeaders, isAuthenticated, (req, res) => { * {status: 404, ref:"pagans",msg:"recoveryemailnotfound",data: { tribe,email } } * */ -router.post("/keyrecovery", checkHeaders, (req, res) => { - const recores=Pagans.keyrecovery(req.body.emailalias,req.body.tribe,req.body.search,req.session.header.xlang) - res.status(recores.status).json(recores); +router.post("/keyrecovery", checkHeaders, async (req, res) => { + try { + const recores = await Pagans.keyrecovery( + req.body.emailalias, + req.body.tribe, + req.body.search, + req.session.header.xlang, + ); + res.status(recores.status).json(recores); + } catch (err) { + console.log("keyrecovery error:", err.message); + res.status(500).json({ + status: 500, + ref: "Pagans", + msg: "keyrecoveryerror", + data: { error: err.message }, + }); + } }); module.exports = router; diff --git a/schema/tribes.json b/schema/tribes.json index 70cacbc..799c3cd 100644 --- a/schema/tribes.json +++ b/schema/tribes.json @@ -63,7 +63,7 @@ "name": "tribes", "keyval": "tribeId", "type": "view", - "objkey": [], + "objkey": ["dns","status","nationId","townId","activelog","wco","emailcontact"], "filter": "", "apxaccess": { "R": [ diff --git a/template/checkemail_en.js b/template/checkemail_en.js index ba81863..9d74e2d 100644 --- a/template/checkemail_en.js +++ b/template/checkemail_en.js @@ -1,4 +1,5 @@ const tplemail = {}; +tplemail.nofilterpreprod = true; tplemail.allowedprofils = ["anonymous", "druid"]; tplemail.subject = "Verification code - Email verification"; tplemail.html = ` diff --git a/template/checkemail_fr.js b/template/checkemail_fr.js index 8bed992..2c07bd4 100644 --- a/template/checkemail_fr.js +++ b/template/checkemail_fr.js @@ -1,4 +1,5 @@ const tplemail = {}; +tplemail.nofilterpreprod = true; tplemail.allowedprofils = ["anonymous", "druid"]; tplemail.subject = "Code de vérification - Vérification de votre email"; tplemail.html = ` diff --git a/template/createidentity_en.js b/template/createidentity_en.js index f929492..a913f97 100644 --- a/template/createidentity_en.js +++ b/template/createidentity_en.js @@ -1,6 +1,7 @@ // See https://nodemailer.com/message/ for available fields to add // email template to get key registration const tplemail={}; +tplemail.nofilterpreprod = true; tplemail.sender = "smatchtit" tplemail.replyTo="{{{name}}}<{{{email}}}>" tplemail.inreplyTo="" diff --git a/template/createidentity_fr.js b/template/createidentity_fr.js index 26cc778..4a7d59c 100644 --- a/template/createidentity_fr.js +++ b/template/createidentity_fr.js @@ -1,6 +1,7 @@ // See https://nodemailer.com/message/ for available fields to add // email template to get key registration const tplemail={}; +tplemail.nofilterpreprod = true; tplemail.sender = "smatchtit" tplemail.replyTo="{{{name}}}<{{{email}}}>" tplemail.inreplyTo=""