force subscribe in brevo from notification

This commit is contained in:
2026-04-10 10:55:57 +02:00
parent e54a4ad16d
commit 1af6a5e308

View File

@@ -9,7 +9,9 @@ const Checkjson = require(`./Checkjson.js`);
const nodemailer = require("nodemailer");
const conf = require(`../../../adminapi/objects/tribes/itm/adminapi.json`);
const currentmod = "Notifications";
const log = process.env.ACTIVELOG ? process.env.ACTIVELOG.split(',').includes(currentmod) : false;
const log = process.env.ACTIVELOG
? process.env.ACTIVELOG.split(",").includes(currentmod)
: false;
/**
* To manage any communication between Pagan
* mayor druid emailing/sms/paper from tribe register smtp, simcard, mail api to Person(s) / Pagan(s)
@@ -76,7 +78,7 @@ Notifications.registertolist = (key, typekey, tribe, mlist, srckey, uuid) => {
const destin = `../${tribe}/objects/maillinglists/${typekey}_${mlist}.json`;
if (!fs.existsSync(destin)) {
console.log(
`######## Attention tentative d'ecriture non autorisé,le fichier n'existe pas ${destin} créer le à la main vide {}`
`######## Attention tentative d'ecriture non autorisé,le fichier n'existe pas ${destin} créer le à la main vide {}`,
);
return {
status: 406,
@@ -156,11 +158,11 @@ Notifications.sendsms = async (data, tribeId) => {
let confsms = conf.sms;
if (
fs.existsSync(
`../adminapi/objects/tribes/itm/${req.session.header.xtribe}.json`
`../adminapi/objects/tribes/itm/${req.session.header.xtribe}.json`,
)
) {
const conftrib = fs.readJSONSync(
`../adminapi/objects/tribes/itm/${req.session.header.xtribe}.json`
`../adminapi/objects/tribes/itm/${req.session.header.xtribe}.json`,
);
if (conftrib.sms) confsms = conftrib.sms;
}
@@ -216,7 +218,7 @@ Notifications.manageemail = async (
tribe,
lg,
numberpersecond = 10,
profils = ["anonymous"]
profils = ["anonymous"],
) => {
const sleep = (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms));
@@ -277,9 +279,9 @@ Notifications.manageemail = async (
data: { template: tplpath },
};
}
if (!profils.includes('druid')){
const setallow = new Set(tplemail.allowedprofils)
if (profils.filter(v=> setallow.has(v)).length==0){
if (!profils.includes("druid")) {
const setallow = new Set(tplemail.allowedprofils);
if (profils.filter((v) => setallow.has(v)).length == 0) {
return {
status: 403,
ref: "Notification",
@@ -340,7 +342,7 @@ Notifications.manageemail = async (
}
fs.outputJSONSync(
`../${tribe}/logs/campains/${templatename}_${lg}_${Date.now()}`,
results
results,
);
return {
status: 200,
@@ -394,6 +396,26 @@ Notifications.sendmail = async (data, tribe) => {
*
*
*/
async function ensureContact(email,BREVO_APIKEY) {
try {
await axios.post(
"https://api.brevo.com/v3/contacts",
{
email: email,
emailBlacklisted: false,
updateEnabled: true,
},
{
headers: {
"api-key": BREVO_APIKEY,
"Content-Type": "application/json",
},
},
);
} catch (err) {
console.log("Brevo contact error:", err.response?.data || err.message);
}
}
if (!conf.smtp || !conf.emailcontact) {
return {
status: 412,
@@ -437,6 +459,10 @@ Notifications.sendmail = async (data, tribe) => {
if (!data.from || data.from == conf.emailcontact)
data.from = conftrib.emailcontact;
}
const toEmails = data.to.split(",");
for (const email of toEmails) {
await ensureContact(email,conftrib.BREVO.APIKEY);
}
//console.log(confsmtp);
const transporter = await nodemailer.createTransport(confsmtp);
if (data.filelist) {
@@ -447,7 +473,7 @@ Notifications.sendmail = async (data, tribe) => {
data.attachments.push({
filename: fo.filename || path.basename(fo.pathfile),
path: fo.pathfile,
contentType: fo.contenttype || 'application/octet-stream'
contentType: fo.contenttype || "application/octet-stream",
});
} else {
missingfile.push(fo.pathfile);
@@ -482,7 +508,7 @@ Notifications.sendmail = async (data, tribe) => {
*/
fs.outputFile(
`../${tribe}/logs/template/${data.templatename}_${data.lg}.html`,
data.html
data.html,
);
try {
@@ -498,10 +524,21 @@ Notifications.sendmail = async (data, tribe) => {
console.log("sendmail result:", res);
if (res && res.accepted) {
// Normalize emails for comparison
const normalizedAccepted = res.accepted.map(email => email.trim().toLowerCase());
const toEmails = data.to.split(",").map(email => email.trim().toLowerCase());
console.log("normalizedAccepted:", normalizedAccepted, "toEmails:", toEmails);
const allAccepted = toEmails.every(email => normalizedAccepted.includes(email));
const normalizedAccepted = res.accepted.map((email) =>
email.trim().toLowerCase(),
);
const toEmails = data.to
.split(",")
.map((email) => email.trim().toLowerCase());
console.log(
"normalizedAccepted:",
normalizedAccepted,
"toEmails:",
toEmails,
);
const allAccepted = toEmails.every((email) =>
normalizedAccepted.includes(email),
);
if (allAccepted && (!res.rejected || res.rejected.length === 0)) {
data.accepted = res.accepted;