1
0
forked from apxtri/apxtri
apxtri/routes/notifications.js

246 lines
8.4 KiB
JavaScript
Raw Permalink Normal View History

2023-12-07 12:04:19 +01:00
const express = require("express");
// Classes
const Notifications = require("../models/Notifications.js");
// Middlewares
const checkHeaders = require("../middlewares/checkHeaders");
const isAuthenticated = require("../middlewares/isAuthenticated");
const router = express.Router();
2023-12-29 13:38:47 +01:00
/**
* wait Sagar feedback for language and label description
* @ api {post} /api/notifications/backend - Notification Backend post
* @apiName notifBackend
* @apiDescription Send an api result {status,ref,msg,data} to get personnalize notification by header.xlang abd by data
* @apiGroup Notification
2024-07-11 13:29:54 +02:00
*
* @apiBody {integer} status an http status
2023-12-29 13:38:47 +01:00
* @apiBody {string} ref an existing model name
2024-07-11 13:29:54 +02:00
* @apiBody {string} msg a key word existing in referentiual
2023-12-29 13:38:47 +01:00
* */
2024-07-11 13:29:54 +02:00
router.post("/backend", (req, res) => {});
2023-12-29 13:38:47 +01:00
2023-12-07 12:04:19 +01:00
/**
2024-07-11 13:29:54 +02:00
* @api {get} adminapi/notifications/messages/:alias/:tribeId -Get message list for alias in tribe
2023-12-07 12:04:19 +01:00
* @apiName notiflist
2024-07-11 13:29:54 +02:00
* @apiDescription Get list of notifications for an alias and a tribe
2023-12-29 13:38:47 +01:00
* @apiGroup Notifications
2023-12-07 12:04:19 +01:00
*
* @apiParam {string} alias
* @apiParam {string} tribeId
* @apiSuccess {object} notif content
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {status:200,ref:"Notification",msg:"Messagelist",data:{notif:[{tribeId,msg:{from(email or uuid or alias):[{dt,msg}]}}]}
2023-12-29 13:38:47 +01:00
* bouture
2023-12-07 12:04:19 +01:00
**/
2024-07-11 13:29:54 +02:00
router.get("/messages/:alias/:tribeId", (req, res) => {
2023-12-29 13:38:47 +01:00
const getnot = Notification.get(req.params.alias, req.params.tribeId);
2023-12-07 12:04:19 +01:00
res.status(getalias.status).send(getalias);
});
2024-07-11 13:29:54 +02:00
/**
* @api {POST} adminapi/notifications/sendmail/:tribe/:template -Send personnalize emails
* @apiName Sendmail
2024-07-18 09:34:26 +02:00
* @apiDescription Send personnalize email with data from template store in ../../{tribe}/template/{template}.json and smtp in conf global or in /itm/{tribe}.json that must have valid parameter emailcontact must be authorized by the smtp
* "emailcontact": "noreply@smatchit.io",
* "smtp": {
* "host": "smtp-relay.brevo.com",
* "port": 587,
* "secure": false,
* "auth": {
* "user": "xx",
* "pass": "yy"
* }
* }
2024-07-11 13:29:54 +02:00
* @apiGroup Notifications
*
* @apiParam {string} template
* @apiParam {string} tribe
* @apiBody {array} emails to send (array of valid email)
2024-07-18 09:34:26 +02:00
* @apiBody {object} data to personnalize template
2024-07-11 13:29:54 +02:00
*
* @apiSuccess {object} notif content
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {status:200,ref:"Notification",msg:"Messagelist",data:{notif:[{tribeId,msg:{from(email or uuid or alias):[{dt,msg}]}}]}
* bouture
*/
router.post(
2024-07-18 09:34:26 +02:00
"/sendmail/:tribe/:template",
2024-07-11 13:29:54 +02:00
checkHeaders,
isAuthenticated,
2024-07-18 09:34:26 +02:00
async (req, res) => {
const data = req.body.data;
data.emailsto = req.body.emails;
const pathtpl = `../../${req.params.tribe}/template/${req.params.template}_${req.session.header.xlang}.js`;
const sendemail = await Notifications.manageemail(
data,
pathtpl,
req.params.tribe
);
res.status(sendemail.status).send(sendemail);
2024-07-11 13:29:54 +02:00
}
);
2023-12-29 13:38:47 +01:00
/**
2024-03-15 08:49:23 +01:00
* @api {POST} adminapi/notifications/registeranonymous/:tribe/:mlist/:typekey/:data -Register email||phone to mlist
* @apiName register
* @apiGroup Notifications
2024-03-15 08:49:23 +01:00
* @apiDescription Register an email or phone into a mailinglist mlist
* @apiBody {string} tribe an existing tribe
2024-07-11 13:29:54 +02:00
* @apiBody {string} mlist a mailing list name
2024-03-15 08:49:23 +01:00
* @apiBody {string} key email or phone keyword
2024-07-11 13:29:54 +02:00
* @apiBod {string} srckey must exist in tribes/schema/lg/enumtrk_xx.json
2024-03-15 08:49:23 +01:00
* @apiParams {string} data the email or phone value
* @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}}}
2024-07-11 13:29:54 +02:00
*
*/
2024-07-11 13:29:54 +02:00
router.post("/registeranonymous", checkHeaders, (req, res) => {
//console.log("list registration ",req.body)
2024-07-11 13:29:54 +02:00
if (!req.body.typekey || !["email", "telephone"].includes(req.body.typekey)) {
2024-07-18 09:34:26 +02:00
return res.status(406).json({
status: 406,
ref: "Notifications",
msg: "typekeyunknown",
data: { typekey: req.body.typekey },
});
}
2024-07-11 13:29:54 +02:00
const key = req.body.contactpoint
? req.body.contactpoint
: req.body[req.body.typekey];
result = Notifications.registertolist(
key,
req.body.typekey,
req.body.tribe,
req.body.mlist,
req.body.srckey,
req.session.header.xuuid
);
res.status(result.status).json(result);
});
/**
2024-07-11 13:29:54 +02:00
* @api {GET} adminapi/notifications/unregister/:tribe/:mlist/:typekey/:data/:validation -Unregister email or phone from a mlist or all mlist
2024-03-15 08:49:23 +01:00
* @apiName unregister
* @apiGroup Notifications
* @apiDescription Register an email into a mailinglist mlist
* @apiParams {string} tribe an existing tribe
2024-07-11 13:29:54 +02:00
* @apiParams {string} mlist a mailing list name
* @apiParams {string} key email or phone
2024-07-11 13:29:54 +02:00
* @apiParams {string} srckey must exist in tribes/schema/lg/enumtrk_xx.json
* @apiParams {string} data the email or phone
2024-07-11 13:29:54 +02:00
* @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}}}
2024-07-11 13:29:54 +02:00
*
*/
2023-12-29 13:38:47 +01:00
2024-07-11 13:29:54 +02:00
router.get(
"/unregister/: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);
}
);
2023-12-29 13:38:47 +01:00
2024-04-03 17:32:37 +02:00
/**
* @api {GET} adminapi/notifications/stat/maillinglst/:tribe --Statistic maillinglst
* @apiName statmaillinglst
* @apiGroup Notifications
* @apiDescription Mailling list statistique
* @apiParams {string} tribe an existing tribe
2024-07-11 13:29:54 +02:00
*
2024-04-03 17:32:37 +02:00
* @apiSuccess {object} with data results
* @apiSuccessExample {json} successfullmessage
* HTTP/1.1 200 OK
* {
* "status": 200,
* "ref": "Notifications",
* "msg": "statistics",
* "data": {
* "teasingwebpage": {
* "email_stayinformboth": 1,
* "email_stayinformseeker": 6
* },
* "gorillamkt": {
* "email_trouvemonjobrecruiter": 7,
* "email_trouvemonjobseeker": 142,
* "telephonefr_trouvemonjobrecruiter": 4,
* "telephonefr_trouvemonjobseeker": 103
* },
* "forumantony": {
* "email_trouvemonjobseeker": 18,
* "telephonefr_trouvemonjobseeker": 151
* }
* }
*}
2024-07-11 13:29:54 +02:00
*
2024-04-03 17:32:37 +02:00
*/
router.get("/stat/maillinglst/:tribe", checkHeaders, async (req, res) => {
2024-07-11 13:29:54 +02:00
console.log("passe là");
const resstat = Notifications.statmaillist(req.params.tribe);
2024-04-03 17:32:37 +02:00
res.status(resstat.status).json(resstat);
});
2023-12-29 13:38:47 +01:00
/**
2024-03-15 08:49:23 +01:00
* @api {POST} adminapi/notifications/contactanonymous/:tribe -Contact anonymous
2023-12-29 13:38:47 +01:00
* @apiName contactanonymous
* @apiGroup Notifications
* @apiDescription Register a contact in tribe and send a mail to admin of tribe
* @apiParams {string} tribe an existing tribe
2023-12-29 13:38:47 +01:00
* @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
2024-07-11 13:29:54 +02:00
*
* @apiSuccess {object} create/update tribe/contacts/{}.json successfull
2023-12-29 13:38:47 +01:00
* @apiSuccessExample {json} successfullmessage
* HTTP/1.1 200 OK
* {"status":200, "ref":"Contact", "msg":"success", "data":{"indexlist":[]}}
2024-07-11 13:29:54 +02:00
*
2023-12-29 13:38:47 +01:00
*/
router.post("/contactanonymous", checkHeaders, async (req, res) => {
const done = Actions[req.body.order]
? await Actions[req.body.order](req.body, req.session.header)
: { status: 406, ref: "Actions", msg: "bodyerror", data: req.body };
//console.log('routes contactanonymous ', req.body);
res.status(done.status).json(done);
});
/**
* Same as /copntactanonymous but for authenticated user => data are updated in persons/itm/alias.json
*/
2024-07-11 13:29:54 +02:00
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 };
console.log(req.body);
res.status(done.status).json(done);
}
);
2023-12-29 13:38:47 +01:00
router.get("/contact", checkHeaders, isAuthenticated, (req, res) => {
res.status(200).json({ data: {} });
});
2024-07-11 13:29:54 +02:00
module.exports = router;