From 37403e9014e3440d2a4437e9202c8d096ffaca15 Mon Sep 17 00:00:00 2001 From: philc Date: Thu, 15 Feb 2024 12:32:51 +0100 Subject: [PATCH] modif notification to register email phone --- models/Notifications.js | 13 ++++-- routes/notifications.js | 98 ++++++++++++++++++++++++++++------------- 2 files changed, 77 insertions(+), 34 deletions(-) diff --git a/models/Notifications.js b/models/Notifications.js index 145b0c8..91edb27 100644 --- a/models/Notifications.js +++ b/models/Notifications.js @@ -2,9 +2,13 @@ const glob = require("glob"); const path = require("path"); const fs = require("fs-extra"); const axios = require("axios"); +const dayjs = require("dayjs"); +const Checkjson = require(`./Checkjson.js`); //const smtp = require("smtp-client"); const nodemailer = require("nodemailer"); const conf = require(`../../conf/townconf.json`); +const currentmod = "Notifications"; +const log = conf.api.activelog.includes(currentmod); /** * To manage any communication between Pagan * mayor druid emailing/sms/paper from tribe register smtp, simcard, mail api to Person(s) / Pagan(s) @@ -35,11 +39,13 @@ Notifications.registertolist = (key, typekey, tribe, mlist, srckey, uuid) => { return { status: 400, ref: "Notifications", - msg: "formaterreur", + msg: "formaterror", data: { fielderr: typekey, format: typekey }, }; - const destin = `../../../nationchains/tribes/${tribe}/contacts/${typecontact}_${mlist}.json`; + const destin = `../nationchains/tribes/${tribe}/objects/maillinglists/${typekey}_${mlist}.json`; + console.log(destin) const filestorage = fs.existsSync(destin) ? fs.readJsonSync(destin) : {}; + //if (log) console.log(currentmod,`filestorage`,filestorage, key, (filestorage[key])); if (filestorage[key]) { filestorage[key].dt_update = dayjs().toISOString(); if (!filestorage[key].srckeys.includes(srckey)) @@ -47,12 +53,13 @@ Notifications.registertolist = (key, typekey, tribe, mlist, srckey, uuid) => { if (!filestorage[key].uuids.includes(uuid)) filestorage[key].uuids.push(uuid); }else{ + filestorage[key]={} filestorage[key].dt_create=dayjs().toISOString(); filestorage[key].srckeys=[srckey]; filestorage[key].uuids=[uuid] } fs.outputJSONSync(destin, filestorage); - return {status:200,ref:"Notifications",msg:"register",data:{key, typekey, tribe, mlist, srckey, uuid}} + return {status:200,ref:"Notifications",msg:"registersuccess",data:{key, typekey, tribe, mlist, srckey, uuid}} } /** * Unsubscribe an eamil or phone from a mailinglist for a tribe diff --git a/routes/notifications.js b/routes/notifications.js index 7a8bf2d..825306c 100644 --- a/routes/notifications.js +++ b/routes/notifications.js @@ -44,41 +44,77 @@ router.get("/:alias/:tribeId", (req, res) => { res.status(getalias.status).send(getalias); }); -router.post("/registeranonymous", checkHeaders, (req, res) => { - - ['uuid','srckey','mlist'].forEach(k=>{ - if (!req.body[k]){} - }); - res.status().json({status:410, ref:"", msg:""}) - let result; - if (req.body.email) { - result= Notifications.registertolist( - req.body.email, - "email", - req.session.header.xtribe, - req.body.mlist, - req.body.srckey, - req.body.uuid); - } - res(200).json({status:200}) -}) - - /** - * @api {POST} /actions/contactanonymous -Contact anonymous + * @api {GET} /notifications/registeranonymous/:tribe/:mlist/:typekey/:data -Contact anonymous + * @apiName register + * @apiGroup Notifications + * @apiDescription Register an email into a mailinglist mlist + * @apiParams {string} tribe an existing tribe + * @apiParams {string} mlist a mailing list name + * @apiParams {string} key email or phone + * @apiParams {string} srckey must exist in tribes/schema/lg/enumtrk_xx.json + * @apiParams {string} data the email or phone + * @apiSuccess {object} update mailinglist/{mlist}.json successfull + * @apiSuccessExample {json} successfullmessage + * HTTP/1.1 200 OK + * {"status":200, "ref":"Notifications", "msg":"registersuccess", "data":{data, typekey, tribe, mlist, srckey, uuid}}} + * + */ + +router.get("/registeranonymous/:tribe/:mlist/:typekey/:srckey/:key", checkHeaders, (req, res) => { + console.log("pass ici") + //Notifications.registertolist = (typekey, tribe, mlist, srckey, uuid) + result= Notifications.registertolist( + req.params.key, + req.params.typekey, + req.params.tribe, + req.params.mlist, + req.params.srckey, + req.session.header.xuuid); + res.status(result.status).json(result) +}) + +/** + * @api {GET} /notifications/unregister/:tribe/:mlist/:typekey/:data/:validation -Contact anonymous * @apiName contactanonymous * @apiGroup Notifications - * @apiDescription Run action store in body.order and update mailinglist or create contact message - * - * @apiBody {string} order name of function for action in Actions.js example:registercontact , - * @apiBody {string} srckey: where it come from and eventualy email template name to use to send email , - * @apiBody {string} email to use - * @apiBody {string} route /actions/contactanonymous - * @apiBody {string} [mlist] filename to store email registration /contacts/mlist.json if not => filename is quest_/contacts/email.json with {email:{dt_create,dt_update, src:[list of source]}} or add message in /contacts/quest_{email}.json {timestamp:{message,name,email,dt_create,emailcontact}} - * @apiBody {string} others any other usefull key:value relevant for order action + * @apiDescription Register an email into a mailinglist mlist + * @apiParams {string} tribe an existing tribe + * @apiParams {string} mlist a mailing list name + * @apiParams {string} key email or phone + * @apiParams {string} srckey must exist in tribes/schema/lg/enumtrk_xx.json + * @apiParams {string} data the email or phone + * @apiParams {string} validation a key store in /tmp waiting to be approved, if "request" then send an email for confirmation, else if exist then remove email from list. + * @apiSuccess {object} update mailinglist/{mlist}.json successfull + * @apiSuccessExample {json} successfullmessage + * HTTP/1.1 200 OK + * {"status":200, "ref":"Notifications", "msg":"registersuccess", "data":{data, typekey, tribe, mlist, srckey, uuid}}} * - * @apiSuccess {object} update/contacts/{mlist}.json successfull + */ + +router.get("/unregisteranonymous/:tribe/:mlist/:typekey/:srckey/:data/:validation", checkHeaders, (req, res) => { + Notifications.registertolist = (typekey, tribe, mlist, srckey, uuid) + result= Notifications.registertolist( + req.params.typekey, + req.params.tribe, + req.params.mlist, + req.params.srckey, + req.session.header.xuuid); + res.status(result.status).json(result) +}) + +/** + * @api {POST} /notifications/contactanonymous/:tribe -Contact anonymous + * @apiName contactanonymous + * @apiGroup Notifications + * @apiDescription Register a contact in tribe and send a mail to admin of tribe + * @apiParams {string} tribe an existing tribe + * @apiBody {string} srckey: where it come from and eventualy email template name to use to send email , + * @apiBody {string} email to recontact + * @apiBody {string} others any other usefull key:value + * + * @apiSuccess {object} create/update tribe/contacts/{}.json successfull * @apiSuccessExample {json} successfullmessage * HTTP/1.1 200 OK * {"status":200, "ref":"Contact", "msg":"success", "data":{"indexlist":[]}} @@ -94,7 +130,7 @@ router.post("/contactanonymous", checkHeaders, async (req, res) => { /** * Same as /copntactanonymous but for authenticated user => data are updated in persons/itm/alias.json */ -router.post("/contact", checkHeaders, isAuthenticated, (req, res) => { +router.post("/contact/:tribe/:alias", checkHeaders, isAuthenticated, (req, res) => { const done = Actions[req.body.order] ? Actions[req.body.order](req.body, req.session.header) : { status: 406, ref: "Actions", msg: "bodyerror", data: req.body };