|
|
|
|
@@ -1230,6 +1230,157 @@ CircuitSchema.statics.setFido = async function (idapp, username, circuitName, gr
|
|
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
CircuitSchema.statics.CheckTransazioniCircuiti = async function () {
|
|
|
|
|
const { User } = require('../models/user');
|
|
|
|
|
const { MyGroup } = require('../models/mygroup');
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
console.log('--------- INIZIO CONTROLLO CheckTransazioniCircuiti -----------')
|
|
|
|
|
|
|
|
|
|
const arrcircuits = await Circuit.find({ idapp }).lean();
|
|
|
|
|
for (const circuit of arrcircuits) {
|
|
|
|
|
let strusersnotinaCircuit = '';
|
|
|
|
|
let strusersnotExist = '';
|
|
|
|
|
|
|
|
|
|
let numtransazionitot = 0;
|
|
|
|
|
let qta = 0;
|
|
|
|
|
|
|
|
|
|
let mystr = ''
|
|
|
|
|
|
|
|
|
|
// 1. Calcola la somma di tutti i Saldi
|
|
|
|
|
|
|
|
|
|
// prendo la lista di tutti gli account del circuito
|
|
|
|
|
|
|
|
|
|
let circuitId = circuit._id;
|
|
|
|
|
|
|
|
|
|
const accounts = await Account.find({ idapp, circuitId }).lean();
|
|
|
|
|
|
|
|
|
|
// CONTROLLA DUPLICATI !
|
|
|
|
|
|
|
|
|
|
const usernameCounts = accounts.reduce((acc, curr) => {
|
|
|
|
|
if (curr.username !== '') {
|
|
|
|
|
acc[curr.username] = (acc[curr.username] || 0) + 1;
|
|
|
|
|
}
|
|
|
|
|
return acc;
|
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
|
|
const duplicatedUsernames = Object.keys(usernameCounts).filter(username => usernameCounts[username] > 1);
|
|
|
|
|
|
|
|
|
|
if (duplicatedUsernames.length > 0) {
|
|
|
|
|
mystr += ' Esistono username duplicati (escludendo quelli vuoti):' + duplicatedUsernames;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let saldotot = 0;
|
|
|
|
|
|
|
|
|
|
let ris = await User.find({
|
|
|
|
|
"profile.mycircuits": { $elemMatch: { circuitname: circuit.name } }
|
|
|
|
|
}, "username").lean()
|
|
|
|
|
|
|
|
|
|
let arrusers_byprofile = ris ? ris.map(user => user.username) : [];
|
|
|
|
|
|
|
|
|
|
let risgroups = await MyGroup.find({
|
|
|
|
|
"mycircuits": { $elemMatch: { circuitname: circuit.name } }
|
|
|
|
|
}, "groupname").lean()
|
|
|
|
|
|
|
|
|
|
let arrgroups_byprofile = risgroups ? risgroups.map(group => group.groupname) : [];
|
|
|
|
|
|
|
|
|
|
let arrusers_byaccounts = [];
|
|
|
|
|
|
|
|
|
|
for (const account of accounts) {
|
|
|
|
|
|
|
|
|
|
if (account.username && !arrusers_byaccounts.includes(account.username)) {
|
|
|
|
|
arrusers_byaccounts.push(account.username);
|
|
|
|
|
} else if (account.groupname && !arrusers_byaccounts.includes(account.groupname)) {
|
|
|
|
|
arrusers_byaccounts.push(account.groupname);
|
|
|
|
|
} else if (account.contocom && !arrusers_byaccounts.includes(account.contocom)) {
|
|
|
|
|
arrusers_byaccounts.push(account.contocom);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// per ogni Account ricalcolo il numero di transazioni avvenute in Entrata/uscita
|
|
|
|
|
let result = await Movement.aggregate([
|
|
|
|
|
{
|
|
|
|
|
$match: {
|
|
|
|
|
$or: [
|
|
|
|
|
{ accountFromId: account._id },
|
|
|
|
|
{ accountToId: account._id },
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$group: {
|
|
|
|
|
_id: null,
|
|
|
|
|
numtransactions: { $sum: 1 },
|
|
|
|
|
totTransato: { $sum: { $abs: "$saldo" } }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
let numtransactions = result && result.length > 0 ? result[0].numtransactions : 0;
|
|
|
|
|
let totTransato = result && result.length > 0 ? result[0].totTransato : 0;
|
|
|
|
|
|
|
|
|
|
if (numtransactions > 0) {
|
|
|
|
|
await Account.findOneAndUpdate({ _id: account._id }, { $set: { numtransactions } })
|
|
|
|
|
}
|
|
|
|
|
if (!account.totTransato || (totTransato !== account.totTransato)) {
|
|
|
|
|
await Account.findOneAndUpdate({ _id: account._id }, { $set: { totTransato } })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
saldotot += account.saldo;
|
|
|
|
|
|
|
|
|
|
if (!account.totTransato)
|
|
|
|
|
mystr += 'TOTTRANSATO => ' + account.totTransato;
|
|
|
|
|
|
|
|
|
|
if (account.totTransato)
|
|
|
|
|
qta += account.totTransato;
|
|
|
|
|
if (account.numtransactions)
|
|
|
|
|
numtransazionitot += account.numtransactions;
|
|
|
|
|
|
|
|
|
|
// await account.calcPending();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let numaccounts = accounts.length;
|
|
|
|
|
let esistecontocom = accounts.find((rec) => (rec.hasOwnProperty('contocom') && rec.contocom !== ''));
|
|
|
|
|
|
|
|
|
|
let numacc_profile = arrusers_byprofile.length + arrgroups_byprofile.length;
|
|
|
|
|
if (esistecontocom && esistecontocom.contocom === circuit.path) {
|
|
|
|
|
numacc_profile++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (numacc_profile !== numaccounts) {
|
|
|
|
|
mystr += ' IL NUMERO DI UTENTI NON COINCIDONO ! \n';
|
|
|
|
|
mystr += 'Utenti Profilo = ' + numacc_profile + '\n';
|
|
|
|
|
mystr += 'Utenti Accounts = ' + arrusers_byaccounts.length + '\n';
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
saldotot = saldotot.toFixed(2);
|
|
|
|
|
|
|
|
|
|
mystr += ' numaccounts=' + numaccounts;
|
|
|
|
|
if (strusersnotinaCircuit)
|
|
|
|
|
mystr += ' Utenti non presenti nel Circuito ! => ' + strusersnotinaCircuit;
|
|
|
|
|
if (strusersnotExist)
|
|
|
|
|
mystr += ' Utenti non più esistenti ! => ' + strusersnotExist;
|
|
|
|
|
|
|
|
|
|
// Verifica se saldotot è uguale a ZERO
|
|
|
|
|
if (saldotot != 0) {
|
|
|
|
|
console.log('*** ATTENZIONE! ' + circuit.name + ' ha come somma un saldo di ' + saldotot, 'qta=' + qta, 'numtransazionitot', numtransazionitot, mystr);
|
|
|
|
|
} else {
|
|
|
|
|
if (numtransazionitot)
|
|
|
|
|
console.log(circuit.name + ' qta=', qta, 'numtransazionitot', numtransazionitot, mystr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('--------- FINE CONTROLLO CheckTransazioniCircuiti -----------', 'Transazioni = ', numtransazionitot)
|
|
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('Err', e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
CircuitSchema.statics.addMovementByOrdersCart = async function (ordersCart, usernameDest, groupDest) {
|
|
|
|
|
|
|
|
|
|
const { User } = require('../models/user');
|
|
|
|
|
|