From e6fd8e1fededd211d9997cd696948dad8c4a03ad Mon Sep 17 00:00:00 2001 From: Surya Paolo Date: Fri, 16 Feb 2024 21:12:53 +0100 Subject: [PATCH] - inizio check transazioni RISO --- .env.development | 6 +- src/server/models/account.js | 1 + src/server/models/circuit.js | 151 ++++++++++++++++++++++++++++++ src/server/router/users_router.js | 2 + 4 files changed, 157 insertions(+), 3 deletions(-) diff --git a/.env.development b/.env.development index 61bd7e9..3136acd 100644 --- a/.env.development +++ b/.env.development @@ -1,11 +1,11 @@ -DATABASE=test_PiuCheBuono +DATABASE=test_FreePlanet UDB=paofreeplanet PDB=mypassword@1A SEND_EMAIL=0 SEND_EMAIL_ORDERS=1 PORT=3000 -appTelegram_TEST=["1","17"] -appTelegram=["1","17"] +appTelegram_TEST=["1","13"] +appTelegram=["1","13"] DOMAIN=mongodb://localhost:27017/ AUTH_MONGODB=true MONGODB_USER=admin diff --git a/src/server/models/account.js b/src/server/models/account.js index e2d304d..ef26a2b 100755 --- a/src/server/models/account.js +++ b/src/server/models/account.js @@ -244,6 +244,7 @@ AccountSchema.statics.addtoSaldo = async function (myaccount, amount, mitt) { myaccountupdate.saldo = myaccount.saldo; myaccountupdate.totTransato = myaccount.totTransato; + myaccountupdate.numtransactions = myaccount.numtransactions; myaccountupdate.date_updated = myaccount.date_updated; const ris = await Account.updateOne({ _id: myaccount.id }, diff --git a/src/server/models/circuit.js b/src/server/models/circuit.js index b4f20f7..4698578 100755 --- a/src/server/models/circuit.js +++ b/src/server/models/circuit.js @@ -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'); diff --git a/src/server/router/users_router.js b/src/server/router/users_router.js index b642bee..d983f2d 100755 --- a/src/server/router/users_router.js +++ b/src/server/router/users_router.js @@ -1249,6 +1249,8 @@ async function eseguiDbOp(idapp, mydata, locale, req, res) { await Order.GeneraCSVOrdineProdotti(); } else if (mydata.dbop === 'RemoveDeletedOrdersInOrderscart') { await Order.RemoveDeletedOrdersInOrderscart(); + } else if (mydata.dbop === 'CheckTransazioniCircuiti') { + await Circuit.CheckTransazioniCircuiti(); } else if (mydata.dbop === 'AbilitaNewsletterALL') { await User.updateMany({ $or: [