force subscribe in brevo from notification
This commit is contained in:
@@ -9,7 +9,9 @@ const Checkjson = require(`./Checkjson.js`);
|
|||||||
const nodemailer = require("nodemailer");
|
const nodemailer = require("nodemailer");
|
||||||
const conf = require(`../../../adminapi/objects/tribes/itm/adminapi.json`);
|
const conf = require(`../../../adminapi/objects/tribes/itm/adminapi.json`);
|
||||||
const currentmod = "Notifications";
|
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
|
* To manage any communication between Pagan
|
||||||
* mayor druid emailing/sms/paper from tribe register smtp, simcard, mail api to Person(s) / Pagan(s)
|
* 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`;
|
const destin = `../${tribe}/objects/maillinglists/${typekey}_${mlist}.json`;
|
||||||
if (!fs.existsSync(destin)) {
|
if (!fs.existsSync(destin)) {
|
||||||
console.log(
|
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 {
|
return {
|
||||||
status: 406,
|
status: 406,
|
||||||
@@ -140,7 +142,7 @@ Notifications.sendsms = async (data, tribeId) => {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
let missingk = [];
|
let missingk = [];
|
||||||
["To", "Text"].forEach((k) => {
|
["To", "Text"].forEach((k) => {
|
||||||
if (!data[k]) {
|
if (!data[k]) {
|
||||||
missingk.push(k);
|
missingk.push(k);
|
||||||
}
|
}
|
||||||
@@ -156,11 +158,11 @@ Notifications.sendsms = async (data, tribeId) => {
|
|||||||
let confsms = conf.sms;
|
let confsms = conf.sms;
|
||||||
if (
|
if (
|
||||||
fs.existsSync(
|
fs.existsSync(
|
||||||
`../adminapi/objects/tribes/itm/${req.session.header.xtribe}.json`
|
`../adminapi/objects/tribes/itm/${req.session.header.xtribe}.json`,
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
const conftrib = fs.readJSONSync(
|
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;
|
if (conftrib.sms) confsms = conftrib.sms;
|
||||||
}
|
}
|
||||||
@@ -216,7 +218,7 @@ Notifications.manageemail = async (
|
|||||||
tribe,
|
tribe,
|
||||||
lg,
|
lg,
|
||||||
numberpersecond = 10,
|
numberpersecond = 10,
|
||||||
profils = ["anonymous"]
|
profils = ["anonymous"],
|
||||||
) => {
|
) => {
|
||||||
const sleep = (ms) => {
|
const sleep = (ms) => {
|
||||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
@@ -269,7 +271,7 @@ Notifications.manageemail = async (
|
|||||||
});
|
});
|
||||||
const tplemail = require(path.resolve(tplpath));
|
const tplemail = require(path.resolve(tplpath));
|
||||||
// check allowed profil
|
// check allowed profil
|
||||||
if (!tplemail.allowedprofils){
|
if (!tplemail.allowedprofils) {
|
||||||
return {
|
return {
|
||||||
status: 406,
|
status: 406,
|
||||||
ref: "Notification",
|
ref: "Notification",
|
||||||
@@ -277,14 +279,14 @@ Notifications.manageemail = async (
|
|||||||
data: { template: tplpath },
|
data: { template: tplpath },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if (!profils.includes('druid')){
|
if (!profils.includes("druid")) {
|
||||||
const setallow = new Set(tplemail.allowedprofils)
|
const setallow = new Set(tplemail.allowedprofils);
|
||||||
if (profils.filter(v=> setallow.has(v)).length==0){
|
if (profils.filter((v) => setallow.has(v)).length == 0) {
|
||||||
return {
|
return {
|
||||||
status: 403,
|
status: 403,
|
||||||
ref: "Notification",
|
ref: "Notification",
|
||||||
msg: "profilsnotallowed",
|
msg: "profilsnotallowed",
|
||||||
data: { template: tplpath,profils },
|
data: { template: tplpath, profils },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -340,7 +342,7 @@ Notifications.manageemail = async (
|
|||||||
}
|
}
|
||||||
fs.outputJSONSync(
|
fs.outputJSONSync(
|
||||||
`../${tribe}/logs/campains/${templatename}_${lg}_${Date.now()}`,
|
`../${tribe}/logs/campains/${templatename}_${lg}_${Date.now()}`,
|
||||||
results
|
results,
|
||||||
);
|
);
|
||||||
return {
|
return {
|
||||||
status: 200,
|
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) {
|
if (!conf.smtp || !conf.emailcontact) {
|
||||||
return {
|
return {
|
||||||
status: 412,
|
status: 412,
|
||||||
@@ -408,7 +430,7 @@ Notifications.sendmail = async (data, tribe) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let missingk = [];
|
let missingk = [];
|
||||||
["from", "to", "subject", "html", "text"].forEach((k) => {
|
["from", "to", "subject", "html", "text"].forEach((k) => {
|
||||||
if (!data[k]) {
|
if (!data[k]) {
|
||||||
missingk.push(k);
|
missingk.push(k);
|
||||||
}
|
}
|
||||||
@@ -437,6 +459,10 @@ Notifications.sendmail = async (data, tribe) => {
|
|||||||
if (!data.from || data.from == conf.emailcontact)
|
if (!data.from || data.from == conf.emailcontact)
|
||||||
data.from = conftrib.emailcontact;
|
data.from = conftrib.emailcontact;
|
||||||
}
|
}
|
||||||
|
const toEmails = data.to.split(",");
|
||||||
|
for (const email of toEmails) {
|
||||||
|
await ensureContact(email,conftrib.BREVO.APIKEY);
|
||||||
|
}
|
||||||
//console.log(confsmtp);
|
//console.log(confsmtp);
|
||||||
const transporter = await nodemailer.createTransport(confsmtp);
|
const transporter = await nodemailer.createTransport(confsmtp);
|
||||||
if (data.filelist) {
|
if (data.filelist) {
|
||||||
@@ -447,7 +473,7 @@ Notifications.sendmail = async (data, tribe) => {
|
|||||||
data.attachments.push({
|
data.attachments.push({
|
||||||
filename: fo.filename || path.basename(fo.pathfile),
|
filename: fo.filename || path.basename(fo.pathfile),
|
||||||
path: fo.pathfile,
|
path: fo.pathfile,
|
||||||
contentType: fo.contenttype || 'application/octet-stream'
|
contentType: fo.contenttype || "application/octet-stream",
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
missingfile.push(fo.pathfile);
|
missingfile.push(fo.pathfile);
|
||||||
@@ -482,7 +508,7 @@ Notifications.sendmail = async (data, tribe) => {
|
|||||||
*/
|
*/
|
||||||
fs.outputFile(
|
fs.outputFile(
|
||||||
`../${tribe}/logs/template/${data.templatename}_${data.lg}.html`,
|
`../${tribe}/logs/template/${data.templatename}_${data.lg}.html`,
|
||||||
data.html
|
data.html,
|
||||||
);
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -494,14 +520,25 @@ Notifications.sendmail = async (data, tribe) => {
|
|||||||
} else {
|
} else {
|
||||||
error = err;
|
error = err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log("sendmail result:", res);
|
console.log("sendmail result:", res);
|
||||||
if (res && res.accepted) {
|
if (res && res.accepted) {
|
||||||
// Normalize emails for comparison
|
// Normalize emails for comparison
|
||||||
const normalizedAccepted = res.accepted.map(email => email.trim().toLowerCase());
|
const normalizedAccepted = res.accepted.map((email) =>
|
||||||
const toEmails = data.to.split(",").map(email => email.trim().toLowerCase());
|
email.trim().toLowerCase(),
|
||||||
console.log("normalizedAccepted:", normalizedAccepted, "toEmails:", toEmails);
|
);
|
||||||
const allAccepted = toEmails.every(email => normalizedAccepted.includes(email));
|
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)) {
|
if (allAccepted && (!res.rejected || res.rejected.length === 0)) {
|
||||||
data.accepted = res.accepted;
|
data.accepted = res.accepted;
|
||||||
|
|||||||
Reference in New Issue
Block a user