This commit is contained in:
Paolo Arena
2021-01-18 00:48:17 +01:00
parent 142380e54b
commit 5493953b58
22 changed files with 9749 additions and 231 deletions

View File

@@ -83,6 +83,58 @@ router.post('/', (req, res) => {
});
});
async function SendMsgToAll(idapp, params) {
const arrusers = await User.find({ idapp },
{
username: 1,
name: 1,
surname: 1,
}
);
let msgsent = 0;
for (const user of arrusers) {
await SendMsgTo(idapp, user.username, params);
msgsent++;
}
return msgsent;
}
async function SendMsgToParam(idapp, params) {
const arrusers = await User.find({ idapp },
{
username: 1,
name: 1,
surname: 1,
'profile.socio': 1,
'profile.socioresidente': 1,
}
);
let msgsent = 0;
for (const user of arrusers) {
let invia = false;
if (params.typemsg === shared_consts.TypeMsg.SEND_TO_SOCI) {
invia = user.profile.socio;
}else if (params.typemsg === shared_consts.TypeMsg.SEND_TO_SOCIO_RESIDENTE) {
invia = user.profile.socioresidente;
}else if (params.typemsg === shared_consts.TypeMsg.SEND_TO_NON_SOCI) {
invia = !user.profile.socio;
} else {
invia = true;
}
if (invia) {
await SendMsgTo(idapp, user.username, params);
msgsent++;
}
}
return msgsent;
}
async function SendMsgTo(idapp, username, params) {
return await User.find({ idapp, username }).then((arrusers) => {
@@ -105,25 +157,6 @@ async function SendMsgTo(idapp, username, params) {
}
async function SendMsgToAll(idapp, params) {
const arrusers = await User.find({ idapp },
{
username: 1,
name: 1,
surname: 1,
}
);
let msgsent = 0;
for (const user of arrusers) {
await SendMsgTo(idapp, user.username, params);
msgsent++;
}
return msgsent;
}
router.post('/send', authenticate, async (req, res) => {
const idapp = req.body.idapp;
const params = req.body.params;
@@ -138,9 +171,11 @@ router.post('/send', authenticate, async (req, res) => {
try {
if (params.typemsg === shared_consts.TypeMsg.SEND_TO_ALL) {
nummsg = await SendMsgToAll(idapp, params);
} else {
nummsg = await SendMsgToParam(idapp, params);
}
return res.send({ code: server_constants.RIS_CODE_OK, msg: nummsg + ' Msg Inviati !' });
return res.send({ code: server_constants.RIS_CODE_OK, msg: nummsg + ' Msg Inviati !', nummsg });
}catch (e) {
return res.send({ code: server_constants.RIS_CODE_ERR, msg: nummsg + ' Msg Inviati !' });
}