Conto Comunitario all'interno di un Circuito
This commit is contained in:
@@ -867,13 +867,14 @@ module.exports = {
|
||||
|
||||
},
|
||||
|
||||
getReqByPar(idapp, usernameOrig) {
|
||||
getReqByPar(idapp, usernameOrig, groupOrig) {
|
||||
return {
|
||||
body: {
|
||||
idapp: idapp,
|
||||
},
|
||||
user: {
|
||||
username: usernameOrig,
|
||||
groupOrig,
|
||||
},
|
||||
};
|
||||
},
|
||||
@@ -1034,7 +1035,7 @@ module.exports = {
|
||||
},
|
||||
|
||||
sendNotifCircuitByUsername: async function (
|
||||
cmd, idapp, usernameOrig, usernameDest, username_action,
|
||||
cmd, idapp, usernameOrig, groupOrig, usernameDest, groupDest, username_action,
|
||||
circuitname, path, myreccircuit, isAdmin, username_worked, extrarec) {
|
||||
|
||||
const { SendNotif } = require('../models/sendnotif');
|
||||
@@ -1042,7 +1043,9 @@ module.exports = {
|
||||
const telegrambot = require('../telegram/telegrambot');
|
||||
const { MyGroup } = require('../models/mygroup');
|
||||
|
||||
const req = this.getReqByPar(idapp, usernameOrig);
|
||||
const req = this.getReqByPar(idapp, usernameOrig, groupOrig);
|
||||
|
||||
let arrris = [];
|
||||
|
||||
const user = await User.findOne({ idapp, username: usernameDest },
|
||||
{ _id: 1, lang: 1 });
|
||||
@@ -1051,6 +1054,7 @@ module.exports = {
|
||||
if (user) {
|
||||
lang = user.lang;
|
||||
}
|
||||
let ris = null;
|
||||
|
||||
try {
|
||||
let paramsObj = {
|
||||
@@ -1114,25 +1118,99 @@ module.exports = {
|
||||
|
||||
if (sendnotif && typeid > 0) {
|
||||
// Check if is group:
|
||||
if (extrarec.groupdest) {
|
||||
if (groupDest && (
|
||||
(cmd !== shared_consts.CIRCUITCMD.SENDCOINS_ACCEPT_SENT) &&
|
||||
(cmd !== shared_consts.CIRCUITCMD.SENDCOINS_REFUSE_SENT)
|
||||
)) {
|
||||
|
||||
let arrusers = await MyGroup.getListAdminsByGroupName(idapp, extrarec.groupdest);
|
||||
let arrusers = await MyGroup.getListAdminsByGroupName(idapp, groupDest);
|
||||
|
||||
let ris = null;
|
||||
let notiftoUpdate = '';
|
||||
let findrecnotif = await SendNotif.findOne({ _id: paramsObj.extrarec.notifId }).lean();
|
||||
notiftoUpdate = findrecnotif ? findrecnotif.extrarec.notifIdToUpdate : '';
|
||||
// arrnotiftoUpdate = findrecnotif ? findrecnotif.extrarec.arrnotifIdToUpdate : '';
|
||||
if (findrecnotif) {
|
||||
let arrrec = [];
|
||||
|
||||
if (notiftoUpdate)
|
||||
arrrec = await SendNotif.find({ 'extrarec.notifIdToUpdate': notiftoUpdate });
|
||||
// if (arrnotiftoUpdate)
|
||||
// arrrec = await SendNotif.find({'extrarec.notifIdToUpdate': notiftoUpdate});
|
||||
|
||||
|
||||
for (let rec of arrrec) {
|
||||
paramsObj.usernameDest = rec.dest;
|
||||
if ((cmd === shared_consts.CIRCUITCMD.SENDCOINS_ACCEPT) || (cmd === shared_consts.CIRCUITCMD.SENDCOINS_REFUSE)) {
|
||||
try {
|
||||
paramsObj.extrarec.notifId = rec._id;
|
||||
} catch (e) {
|
||||
}
|
||||
}
|
||||
|
||||
ris = await SendNotif.createNewNotifToSingleUser(req, null, paramsObj, onlysave, typedir, typeid);
|
||||
if (!ris) {
|
||||
console.error('Errore createNewNotifToSingleUser! ', paramsObj.usernameDest, "dest ->", groupDest)
|
||||
} else {
|
||||
arrris.push(ris._id);
|
||||
}
|
||||
|
||||
for (let i = 0; i < arrusers.length; i++) {
|
||||
paramsObj.usernameDest = arrusers[i].username;
|
||||
ris = await SendNotif.createNewNotifToSingleUser(req, null, paramsObj, onlysave, typedir, typeid);
|
||||
if (!ris) {
|
||||
console.error('Errore createNewNotifToSingleUser! ', paramsObj.usernameDest, "dest ->", extrarec.groupdest)
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
for (let i = 0; i < arrusers.length; i++) {
|
||||
paramsObj.usernameDest = arrusers[i].username;
|
||||
|
||||
ris = await SendNotif.createNewNotifToSingleUser(req, null, paramsObj, onlysave, typedir, typeid);
|
||||
if (!ris) {
|
||||
console.error('Errore createNewNotifToSingleUser! ', paramsObj.usernameDest, "dest ->", groupDest)
|
||||
} else {
|
||||
arrris.push(ris._id);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
// CREATE NOTIFICATION IN TABLE SENDNOTIF
|
||||
return await SendNotif.createNewNotifToSingleUser(req, null, paramsObj, onlysave, typedir, typeid);
|
||||
ris = await SendNotif.createNewNotifToSingleUser(req, null, paramsObj, onlysave, typedir, typeid);
|
||||
|
||||
if ((cmd === shared_consts.CIRCUITCMD.SENDCOINS_ACCEPT) ||
|
||||
(cmd === shared_consts.CIRCUITCMD.SENDCOINS_REFUSE)) {
|
||||
|
||||
if (cmd === shared_consts.CIRCUITCMD.SENDCOINS_ACCEPT) {
|
||||
typeid = shared_consts.TypeNotifs.ID_CIRCUIT_COINS_ACCEPTED_SENT;
|
||||
} else if (cmd === shared_consts.CIRCUITCMD.SENDCOINS_REFUSE) {
|
||||
typeid = shared_consts.TypeNotifs.ID_CIRCUIT_COINS_REFUSED_SENT;
|
||||
}
|
||||
|
||||
let notiftoUpdate = '';
|
||||
let findrecnotif = await SendNotif.findOne({ _id: paramsObj.extrarec.notifId }).lean();
|
||||
notiftoUpdate = findrecnotif ? findrecnotif.extrarec.notifIdToUpdate : '';
|
||||
arrnotiftoUpdate = findrecnotif ? findrecnotif.extrarec.arrnotifIdToUpdate : '';
|
||||
if (findrecnotif && notiftoUpdate && arrnotiftoUpdate) {
|
||||
|
||||
for (let myid of arrnotiftoUpdate) {
|
||||
let rec = await SendNotif.findOne({ _id: myid }).lean();
|
||||
if (rec && rec.status === 0) {
|
||||
paramsObj.usernameDest = rec.dest;
|
||||
paramsObj.extrarec.idStatusToSent = rec._id;
|
||||
|
||||
|
||||
ris = await SendNotif.createNewNotifToSingleUser(req, null, paramsObj, onlysave, typedir, typeid);
|
||||
if (!ris) {
|
||||
console.error('Errore createNewNotifToSingleUser! ', paramsObj.usernameDest, "dest ->", groupDest)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { recnotif: ris, arrris };
|
||||
} catch (e) {
|
||||
console.log(e.message);
|
||||
}
|
||||
@@ -1179,6 +1257,7 @@ module.exports = {
|
||||
sendNotificationByCircuit: async function (idapp, usernameOrig, circuitname, cmd, value, telegram, username_action, extrarec) {
|
||||
|
||||
const { Circuit } = require('../models/circuit');
|
||||
const { SendNotif } = require('../models/sendnotif');
|
||||
|
||||
const circuit = await Circuit.findOne({ idapp, name: circuitname }, { _id: 1, admins: 1, createdBy: 1, path: 1 }).lean();
|
||||
if (!circuit)
|
||||
@@ -1195,41 +1274,75 @@ module.exports = {
|
||||
if (cmd === shared_consts.CIRCUITCMD.SENDCOINS_REQ || cmd === shared_consts.CIRCUITCMD.SENDCOINS_ACCEPT || cmd ===
|
||||
shared_consts.CIRCUITCMD.SENDCOINS_REFUSE) {
|
||||
|
||||
let extrarecorig = { ...extrarec };
|
||||
let temp = ''
|
||||
temp = extrarecorig.groupdest
|
||||
extrarecorig.groupdest = extrarecorig.grouporig
|
||||
groupOrig = extrarec.grouporig
|
||||
// extrarecorig.grouporig = temp
|
||||
|
||||
let arrris = [];
|
||||
let recnotif = null;
|
||||
|
||||
if (cmd === shared_consts.CIRCUITCMD.SENDCOINS_REQ) {
|
||||
recnotif = await this.sendNotifCircuitByUsername(shared_consts.CIRCUITCMD.SENDCOINS_REQ_SENT, idapp, extrarec.dest, usernameOrig,
|
||||
// msg al Mittente (o mittenti del 'groupOrig') "Stai inviando X RIS"
|
||||
let res = await this.sendNotifCircuitByUsername(shared_consts.CIRCUITCMD.SENDCOINS_REQ_SENT, idapp, extrarec.dest, extrarec.groupdest, usernameOrig, groupOrig,
|
||||
username_action, circuitname, circuit.path, null,
|
||||
false, '', extrarec);
|
||||
|
||||
if (res) {
|
||||
recnotif = res.recnotif;
|
||||
arrris = res.arrris;
|
||||
}
|
||||
} else if (cmd === shared_consts.CIRCUITCMD.SENDCOINS_ACCEPT) {
|
||||
await this.sendNotifCircuitByUsername(shared_consts.CIRCUITCMD.SENDCOINS_ACCEPT_SENT, idapp, extrarec.dest, usernameOrig,
|
||||
// msg ai MITTENTI: "X RIS sono stati accettati da Destinatario"
|
||||
await this.sendNotifCircuitByUsername(shared_consts.CIRCUITCMD.SENDCOINS_ACCEPT_SENT, idapp, extrarec.dest, extrarec.groupdest, usernameOrig, groupOrig,
|
||||
username_action, circuitname, circuit.path, null,
|
||||
false, '', extrarec);
|
||||
} else if (cmd === shared_consts.CIRCUITCMD.SENDCOINS_REFUSE) {
|
||||
await this.sendNotifCircuitByUsername(shared_consts.CIRCUITCMD.SENDCOINS_REFUSE_SENT, idapp, extrarec.dest, usernameOrig,
|
||||
await this.sendNotifCircuitByUsername(shared_consts.CIRCUITCMD.SENDCOINS_REFUSE_SENT, idapp, extrarec.dest, extrarec.groupdest, usernameOrig, groupOrig,
|
||||
username_action, circuitname, circuit.path, null,
|
||||
false, '', extrarec);
|
||||
}
|
||||
|
||||
if (recnotif)
|
||||
extrarec.notifIdToUpdate = recnotif._id;
|
||||
if (arrris)
|
||||
extrarec.arrnotifIdToUpdate = arrris;
|
||||
|
||||
ris = await this.sendNotifCircuitByUsername(cmd, idapp, usernameOrig, extrarec.dest, username_action, circuitname, circuit.path,
|
||||
let usernametoOrigUse = usernameOrig
|
||||
if (extrarec.groupname) {
|
||||
usernametoOrigUse = ''
|
||||
}
|
||||
ris = await this.sendNotifCircuitByUsername(cmd, idapp, usernametoOrigUse, groupOrig, extrarec.dest, extrarec.groupdest, username_action, circuitname, circuit.path,
|
||||
null, false, '', extrarec);
|
||||
|
||||
if (arrris && ris && ris.recnotif) {
|
||||
for (const rec of arrris) {
|
||||
let obj = {
|
||||
'extrarec.notifIdToUpdate': ris.recnotif._id
|
||||
};
|
||||
|
||||
await SendNotif.findOneAndUpdate({ _id: rec._id }, { $set: obj }, { new: false });
|
||||
}
|
||||
}
|
||||
|
||||
extrarec.notifIdToUpdate = '';
|
||||
|
||||
if (ris)
|
||||
if (ris.recnotif)
|
||||
inviato = true;
|
||||
// ++Todo: Inviare anche agli Admin ?!
|
||||
} else {
|
||||
|
||||
const groupOrig = '' //++Todo: extrarec.groupOrig
|
||||
|
||||
for (const singleadmin of circuit.admins) {
|
||||
try {
|
||||
if (singleadmin.username) {
|
||||
if (usernameOrig === singleadmin.username)
|
||||
giainviato = true;
|
||||
|
||||
await this.sendNotifCircuitByUsername(cmd, idapp, usernameOrig, singleadmin.username, username_action, circuitname,
|
||||
await this.sendNotifCircuitByUsername(cmd, idapp, usernameOrig, groupOrig, singleadmin.username, '', username_action, circuitname,
|
||||
circuit.path, circuit, true, '', extrarec);
|
||||
inviato = true;
|
||||
}
|
||||
@@ -1240,15 +1353,15 @@ module.exports = {
|
||||
|
||||
if (!giainviato && cmd !== shared_consts.CIRCUITCMD.REQ) {
|
||||
// SEND TO THE USER DEST THE NOTIFICATION
|
||||
ris = await this.sendNotifCircuitByUsername(cmd, idapp, usernameOrig, usernameOrig, username_action, circuitname, circuit.path,
|
||||
ris = await this.sendNotifCircuitByUsername(cmd, idapp, usernameOrig, groupOrig, usernameOrig, '', username_action, circuitname, circuit.path,
|
||||
null,
|
||||
false, '', extrarec);
|
||||
if (ris)
|
||||
if (ris.recnotif)
|
||||
inviato = true;
|
||||
}
|
||||
}
|
||||
|
||||
return { ris, inviato };
|
||||
return { ris: ris.recnotif, inviato };
|
||||
|
||||
} catch (e) {
|
||||
console.error('sendNotificationByCircuit: ', e);
|
||||
|
||||
Reference in New Issue
Block a user