const glob = require("glob"); const path = require("path"); const fs = require("fs-extra"); const axios = require("axios"); //const smtp = require("smtp-client"); const nodemailer = require("nodemailer"); const conf = require(`../../conf/townconf.json`); /** * To manage any communication between Pagan * mayor druid emailing/sms/paper from tribe register smtp, simcard, mail api to Person(s) / Pagan(s) * volatile notification message from tribe activities to Pagans / person () * */ const Notifications = {}; Notifications.get = (alias, tribeId) => { const notiffile = `../nationchains/tribes/${req.params.tribeId}/notifications/${req.params.alias}.json`; const msg = fs.existsSync(notiffile) ? fs.readJSONSync(notiffile) : {}; return { status: 200, ref: "Notification", msg: "Messagelist", data: { notif: [{ tribeId, msg }] }, }; }; Notifications.sendsms = async (data, tribeId) => { /** * Never use need wallet in mailjet to test * To set up with mailjet see https://dev.mailjet.com/sms/guides/send-sms-api/#authentication * * @param {string} data.To a phone number with international +3360101010101 * @param {string} data.Text text to send * * a conf.sms with {url:"smsurl", Token:"", From:""} * * */ if (!conf.sms) { return { status: 412, ref: "Notifications", msg: "missingconf", data: { tribe: tribeId }, }; } let missingk = [][("To", "Text")].forEach((k) => { if (!data[k]) { missingk.push(k); } }); if (missingk.lenght > 0) { return { status: 428, ref: "Notifications", msg: "missingdata", data: { missingk: missingk }, }; } let confsms = conf.sms; if ( fs.existsSync( `../nationchains/tribes/itm/${req.session.header.xtribe}.json` ) ) { const conftrib = fs.readJSONSync( `../nationchains/tribes/itm/${req.session.header.xtribe}.json` ); if (conftrib.sms) confsms = conftrib.sms; } data.From = confsms.From; const sendsms = await axios.post(confsms.url, { headers: { Authorization: `Bearer ${confsms.MJ_TOKEN}`, "Content-Type": "application/json", }, body: JSON.stringify(data), }); if (sendsms.status == 200) { return { status: 200, ref: "Notifications", msg: "successfullsentsms", data: {}, }; } else { return { status: sendsms.status, ref: "Notifications", msg: "errsendsms", data: { err: sendsms.data }, }; } /* si tout se passe bien: { "From": "MJPilot", "To": "+33600000000", "Text": "Have a nice SMS flight with Mailjet !", "MessageId": "2034075536371630429", "SmsCount": 1, "CreationTS": 1521626400, "SentTS": 1521626402, "Cost": { "Value": 0.0012, "Currency": "EUR" }, "Status": { "Code": 2, "Name": "sent", "Description": "Message sent" } } } */ }; Notifications.sendmail = async (data, tribe) => { /** * See https://nodemailer.com/message/ for available fields to add * @param {string} [data.from] an email authorized by smtp used priority from header xtribe * @param {string} data.to list of email separate by , * @param {string} data.subject * @param {string} data.html * @param {string} data.text * @param {string} [data.Cc] list of email in copy * @param {string} [data.Bcc] list of email in hidden copy * @param {string} [data.attachments] array of * {filename:'filename.txt',content:'txt'}, * {filename:'img.svg',path:"https://....svg", contentType:'image/svg'} * {filename:'img.svg',path:"https://....svg", contentType :'text/plain'} * {filename:'img.png',path:"data:text/svg;base64.aGVsbG8gd29ybGQ="} * * @example data * {"to":"wall-ants.ndda.fr", * "subject":"Test", * "html":"