From 26a42b1f30f531fcc03f9ca8cbc28009339a5045 Mon Sep 17 00:00:00 2001 From: Surya Paolo Date: Mon, 3 Nov 2025 14:24:33 +0100 Subject: [PATCH] - versione: 1.2.77 - aggiustamenti e miglioramenti estetici --- src/server/models/circuit.js | 4 +- src/server/models/movement.js | 907 +++++++++++++++--------------- src/server/models/stat.js | 2 +- src/server/router/users_router.js | 3 +- src/server/version.txt | 2 +- 5 files changed, 466 insertions(+), 452 deletions(-) diff --git a/src/server/models/circuit.js b/src/server/models/circuit.js index 08cde3d..5334575 100755 --- a/src/server/models/circuit.js +++ b/src/server/models/circuit.js @@ -488,7 +488,8 @@ CircuitSchema.statics.getCircuitsByUsername = async function (idapp, username, u { deleted: { $exists: true, $eq: false } }], }, whatToShow_Unknown).sort({ status: -1 }).lean(); - const last_my_transactions = await Movement.getLastN_Transactions(idapp, nummovTodownload, username, ''); + const last_my_transactions = await Movement.getLastN_Transactions(idapp, nummovTodownload, nummovTodownload, username, ''); + const total_transactions = await Movement.getNumTotalTransactions(idapp, username, ''); return { listcircuits, @@ -497,6 +498,7 @@ CircuitSchema.statics.getCircuitsByUsername = async function (idapp, username, u manage_mycircuits, mycircuits: user.profile.mycircuits, last_my_transactions, + total_transactions }; } catch (e) { diff --git a/src/server/models/movement.js b/src/server/models/movement.js index 3a863df..02747a4 100755 --- a/src/server/models/movement.js +++ b/src/server/models/movement.js @@ -10,7 +10,7 @@ const { ObjectId } = require('mongodb'); const { Account } = require('../models/account'); // Resolving error Unknown modifier: $pushAll -mongoose.plugin(schema => { +mongoose.plugin((schema) => { schema.options.usePushEach = true; }); @@ -73,12 +73,10 @@ MovementSchema.statics.findAllIdApp = async function (idapp) { const myfind = { idapp }; return await tools.findAllQueryIdApp(MyMovement, myfind); - }; MovementSchema.pre('save', async function (next) { if (this.isNew) { - this.transactionDate = new Date(); } @@ -90,7 +88,6 @@ MovementSchema.statics.getFieldsForSearch = function () { { field: 'causal', type: tools.FieldType.string }, { field: 'amount', type: tools.FieldType.number }, ]; - }; MovementSchema.statics.executeQueryTable = function (idapp, params) { @@ -98,27 +95,33 @@ MovementSchema.statics.executeQueryTable = function (idapp, params) { return tools.executeQueryTable(this, 0, params); }; -MovementSchema.statics.addMov = async function (idapp, accountFromIdTable, accountToIdTable, amount, causal, causalDest, notifId, idOrdersCart) { - +MovementSchema.statics.addMov = async function ( + idapp, + accountFromIdTable, + accountToIdTable, + amount, + causal, + causalDest, + notifId, + idOrdersCart +) { try { // Only positive values amount = Math.abs(amount); - let mymov = await Movement.create( - { - idapp, - transactionDate: new Date(), - accountFromId: accountFromIdTable._id, - accountToId: accountToIdTable._id, - idOrdersCart, - amount, - causal, - causalDest, - residual: 0, - notifId, - // expiringDate: - }, - ); + let mymov = await Movement.create({ + idapp, + transactionDate: new Date(), + accountFromId: accountFromIdTable._id, + accountToId: accountToIdTable._id, + idOrdersCart, + amount, + causal, + causalDest, + residual: 0, + notifId, + // expiringDate: + }); if (mymov) { // Update saldo dell'Account @@ -134,24 +137,26 @@ MovementSchema.statics.addMov = async function (idapp, accountFromIdTable, accou }; MovementSchema.statics.getQueryMovsByCircuitId = async function (idapp, username, groupname, contocom, circuitId) { - try { if (!circuitId) { return []; } - const myaccount = await Account.getAccountByUsernameAndCircuitId(idapp, username, circuitId, false, true, groupname, contocom); - - + const myaccount = await Account.getAccountByUsernameAndCircuitId( + idapp, + username, + circuitId, + false, + true, + groupname, + contocom + ); if (myaccount) { - let aggr1 = [ { $match: { idapp, - $or: [ - { accountFromId: myaccount._id }, - { accountToId: myaccount._id }], + $or: [{ accountFromId: myaccount._id }, { accountToId: myaccount._id }], }, }, { @@ -169,15 +174,9 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function (idapp, username let: { username: '$accfrom.username', idapp: '$accfrom.idapp' }, pipeline: [ { - $match: - { - $expr: - { - $and: - [ - { $eq: ['$$username', '$username'] }, - { $eq: ['$$idapp', '$idapp'] }, - ], + $match: { + $expr: { + $and: [{ $eq: ['$$username', '$username'] }, { $eq: ['$$idapp', '$idapp'] }], }, }, }, @@ -197,15 +196,9 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function (idapp, username let: { groupname: '$accfrom.groupname', idapp: '$accfrom.idapp' }, pipeline: [ { - $match: - { - $expr: - { - $and: - [ - { $eq: ['$$groupname', '$groupname'] }, - { $eq: ['$$idapp', '$idapp'] }, - ], + $match: { + $expr: { + $and: [{ $eq: ['$$groupname', '$groupname'] }, { $eq: ['$$idapp', '$idapp'] }], }, }, }, @@ -226,15 +219,9 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function (idapp, username let: { contocom: '$accfrom.contocom', idapp: '$accfrom.idapp' }, pipeline: [ { - $match: - { - $expr: - { - $and: - [ - { $eq: ['$$contocom', '$path'] }, - { $eq: ['$$idapp', '$idapp'] }, - ], + $match: { + $expr: { + $and: [{ $eq: ['$$contocom', '$path'] }, { $eq: ['$$idapp', '$idapp'] }], }, }, }, @@ -285,15 +272,9 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function (idapp, username let: { username: '$accto.username', idapp: '$accto.idapp' }, pipeline: [ { - $match: - { - $expr: - { - $and: - [ - { $eq: ['$$username', '$username'] }, - { $eq: ['$$idapp', '$idapp'] }, - ], + $match: { + $expr: { + $and: [{ $eq: ['$$username', '$username'] }, { $eq: ['$$idapp', '$idapp'] }], }, }, }, @@ -313,15 +294,9 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function (idapp, username let: { groupname: '$accto.groupname', idapp: '$accto.idapp' }, pipeline: [ { - $match: - { - $expr: - { - $and: - [ - { $eq: ['$$groupname', '$groupname'] }, - { $eq: ['$$idapp', '$idapp'] }, - ], + $match: { + $expr: { + $and: [{ $eq: ['$$groupname', '$groupname'] }, { $eq: ['$$idapp', '$idapp'] }], }, }, }, @@ -341,15 +316,9 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function (idapp, username let: { contocom: '$accto.contocom', idapp: '$accto.idapp' }, pipeline: [ { - $match: - { - $expr: - { - $and: - [ - { $eq: ['$$contocom', '$path'] }, - { $eq: ['$$idapp', '$idapp'] }, - ], + $match: { + $expr: { + $and: [{ $eq: ['$$contocom', '$path'] }, { $eq: ['$$idapp', '$idapp'] }], }, }, }, @@ -364,8 +333,7 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function (idapp, username }, }, { - $project: - { + $project: { transactionDate: 1, amount: 1, causal: 1, @@ -393,12 +361,10 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function (idapp, username 'contocomto.name': 1, }, }, - ]; return aggr1; } - } catch (e) { return []; } @@ -406,9 +372,7 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function (idapp, username return []; }; - MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function (idapp, circuitId) { - try { if (!circuitId) { return []; @@ -434,15 +398,9 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function (idapp, let: { username: '$accfrom.username', idapp: '$accfrom.idapp' }, pipeline: [ { - $match: - { - $expr: - { - $and: - [ - { $eq: ['$$username', '$username'] }, - { $eq: ['$$idapp', '$idapp'] }, - ], + $match: { + $expr: { + $and: [{ $eq: ['$$username', '$username'] }, { $eq: ['$$idapp', '$idapp'] }], }, }, }, @@ -462,15 +420,9 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function (idapp, let: { groupname: '$accfrom.groupname', idapp: '$accfrom.idapp' }, pipeline: [ { - $match: - { - $expr: - { - $and: - [ - { $eq: ['$$groupname', '$groupname'] }, - { $eq: ['$$idapp', '$idapp'] }, - ], + $match: { + $expr: { + $and: [{ $eq: ['$$groupname', '$groupname'] }, { $eq: ['$$idapp', '$idapp'] }], }, }, }, @@ -490,15 +442,9 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function (idapp, let: { contocom: '$accfrom.contocom', idapp: '$accfrom.idapp' }, pipeline: [ { - $match: - { - $expr: - { - $and: - [ - { $eq: ['$$contocom', '$path'] }, - { $eq: ['$$idapp', '$idapp'] }, - ], + $match: { + $expr: { + $and: [{ $eq: ['$$contocom', '$path'] }, { $eq: ['$$idapp', '$idapp'] }], }, }, }, @@ -530,11 +476,11 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function (idapp, $match: { 'accto.circuitId': circuitId }, }, { - '$lookup': { - 'from': 'circuits', - 'localField': 'accfrom.circuitId', - 'foreignField': '_id', - 'as': 'circuitfrom', + $lookup: { + from: 'circuits', + localField: 'accfrom.circuitId', + foreignField: '_id', + as: 'circuitfrom', }, }, { @@ -544,11 +490,11 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function (idapp, }, }, { - '$lookup': { - 'from': 'circuits', - 'localField': 'accto.circuitId', - 'foreignField': '_id', - 'as': 'circuitto', + $lookup: { + from: 'circuits', + localField: 'accto.circuitId', + foreignField: '_id', + as: 'circuitto', }, }, { @@ -563,15 +509,9 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function (idapp, let: { username: '$accto.username', idapp: '$accto.idapp' }, pipeline: [ { - $match: - { - $expr: - { - $and: - [ - { $eq: ['$$username', '$username'] }, - { $eq: ['$$idapp', '$idapp'] }, - ], + $match: { + $expr: { + $and: [{ $eq: ['$$username', '$username'] }, { $eq: ['$$idapp', '$idapp'] }], }, }, }, @@ -591,15 +531,9 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function (idapp, let: { groupname: '$accto.groupname', idapp: '$accto.idapp' }, pipeline: [ { - $match: - { - $expr: - { - $and: - [ - { $eq: ['$$groupname', '$groupname'] }, - { $eq: ['$$idapp', '$idapp'] }, - ], + $match: { + $expr: { + $and: [{ $eq: ['$$groupname', '$groupname'] }, { $eq: ['$$idapp', '$idapp'] }], }, }, }, @@ -619,15 +553,9 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function (idapp, let: { contocom: '$accto.contocom', idapp: '$accto.idapp' }, pipeline: [ { - $match: - { - $expr: - { - $and: - [ - { $eq: ['$$contocom', '$path'] }, - { $eq: ['$$idapp', '$idapp'] }, - ], + $match: { + $expr: { + $and: [{ $eq: ['$$contocom', '$path'] }, { $eq: ['$$idapp', '$idapp'] }], }, }, }, @@ -642,8 +570,7 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function (idapp, }, }, { - $project: - { + $project: { transactionDate: 1, amount: 1, causal: 1, @@ -667,11 +594,9 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function (idapp, 'contocomto.title': 1, }, }, - ]; return aggr1; - } catch (e) { return []; } @@ -682,13 +607,11 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function (idapp, MovementSchema.statics.getTotal_Transactions = async function (idapp) { const MyMovement = this; - const numtransac = await MyMovement.countDocuments( - { - idapp, - }); + const numtransac = await MyMovement.countDocuments({ + idapp, + }); return numtransac; - }; MovementSchema.statics.getTot_RIS_Transati = async function (idapp) { @@ -697,19 +620,19 @@ MovementSchema.statics.getTot_RIS_Transati = async function (idapp) { // Utilizza l'aggregazione per sommare tutti gli 'amount' per un dato 'idapp' const numtot = await MyMovement.aggregate([ { - $match: { idapp } // Filtra i documenti per 'idapp' + $match: { idapp }, // Filtra i documenti per 'idapp' }, { $sort: { transactionDate: -1, - } + }, }, { $group: { _id: null, - totalAmount: { $sum: "$amount" } // Somma tutti gli 'amount' - } - } + totalAmount: { $sum: '$amount' }, // Somma tutti gli 'amount' + }, + }, ]); try { @@ -717,8 +640,6 @@ MovementSchema.statics.getTot_RIS_Transati = async function (idapp) { } catch (e) { return 0; } - - }; MovementSchema.statics.getMovsByCircuitId = async function (idapp, username, circuitId) { @@ -741,56 +662,28 @@ MovementSchema.statics.checkIfCoinsAlreadySent = async function (notifId) { try { const rec = await MyMovement.findOne({ notifId }, { _id: 1 }); return !!rec; - } catch (e) { // If Error, don't send the coins return true; } - }; -MovementSchema.statics.getLastN_Transactions = async function (idapp, numtransaz = 10, username = '', groupname = '', contocom = '') { +MovementSchema.statics.getNumTotalTransactions = async function (idapp, username = '', groupname = '', contocom = '') { const MyMovement = this; - // get last "numtransaz" transactions - + // Determina se filtrare per un account specifico let abilitaquerypersingolo = (username || groupname || contocom); - let querym2 = null; + if (!abilitaquerypersingolo) { + // Se non c'è filtro, conta tutte le transazioni dell'app + return await MyMovement.countDocuments({ idapp }); + } - if (abilitaquerypersingolo) { - let v1 = []; - - if (username) { - v1.push({ 'userfrom.username': username }, - { 'userto.username': username }); - } - - if (groupname) { - v1.push({ 'groupfrom.groupname': groupname }, - { 'groupto.groupname': groupname }); - } - if (contocom) { - v1.push({ 'contocomfrom.name': contocom }, - { 'contocomto.name': contocom }); - } - - querym2 = { - $match: - { $or: v1 } - }; - }; - - - let aggr1 = [ + // Pipeline per contare le transazioni + let pipeline = [ { $match: { idapp }, }, - { - $sort: { - transactionDate: -1, - } - }, { $lookup: { from: 'accounts', @@ -802,89 +695,96 @@ MovementSchema.statics.getLastN_Transactions = async function (idapp, numtransaz { $unwind: '$accfrom' }, { $lookup: { - from: 'users', - let: { username: '$accfrom.username', idapp: '$accfrom.idapp' }, - pipeline: [ - { - $match: - { - $expr: - { - $and: - [ - { $eq: ['$$username', '$username'] }, - { $eq: ['$$idapp', '$idapp'] }, - ], - }, - }, - }, - ], - as: 'userfrom', - }, - }, - { - $unwind: { - path: '$userfrom', - preserveNullAndEmptyArrays: true, - }, - }, - { - $lookup: { - from: 'mygroups', - let: { groupname: '$accfrom.groupname', idapp: '$accfrom.idapp' }, - pipeline: [ - { - $match: - { - $expr: - { - $and: - [ - { $eq: ['$$groupname', '$groupname'] }, - { $eq: ['$$idapp', '$idapp'] }, - ], - }, - }, - }, - ], - as: 'groupfrom', - }, - }, - { - $unwind: { - path: '$groupfrom', - preserveNullAndEmptyArrays: true, + from: 'accounts', + localField: 'accountToId', + foreignField: '_id', + as: 'accto', }, }, + { $unwind: '$accto' }, + ]; + // Costruisci il filtro OR per entrata o uscita + let orConditions = []; + + if (username) { + orConditions.push( + { 'accfrom.username': username }, + { 'accto.username': username } + ); + } + + if (groupname) { + orConditions.push( + { 'accfrom.groupname': groupname }, + { 'accto.groupname': groupname } + ); + } + + if (contocom) { + orConditions.push( + { 'accfrom.contocom': contocom }, + { 'accto.contocom': contocom } + ); + } + + // Aggiungi il match con OR + pipeline.push({ + $match: { + $or: orConditions + } + }); + + // Conta i documenti + pipeline.push({ + $count: 'total' + }); + + const result = await MyMovement.aggregate(pipeline); + + // Se non ci sono risultati, ritorna 0 + return result.length > 0 ? result[0].total : 0; +}; + +MovementSchema.statics.getLastN_Transactions = async function ( + idapp, + numtransaz = 10, + numtransaz_uscita = 10, + username = '', + groupname = '', + contocom = '' +) { + const MyMovement = this; + + // Determina se filtrare per un account specifico + let abilitaquerypersingolo = username || groupname || contocom; + + // Costruisce i criteri di filtro base per identificare l'account + let matchCriteria = {}; + if (username) { + matchCriteria['accfrom.username'] = username; + matchCriteria['accto.username'] = username; + } + if (groupname) { + matchCriteria['accfrom.groupname'] = groupname; + matchCriteria['accto.groupname'] = groupname; + } + if (contocom) { + matchCriteria['accfrom.contocom'] = contocom; + matchCriteria['accto.contocom'] = contocom; + } + + // Pipeline comune per i lookup + const commonLookups = [ { $lookup: { - from: 'circuits', - let: { contocom: '$accfrom.contocom', idapp: '$accfrom.idapp' }, - pipeline: [ - { - $match: - { - $expr: - { - $and: - [ - { $eq: ['$$contocom', '$path'] }, - { $eq: ['$$idapp', '$idapp'] }, - ], - }, - }, - }, - ], - as: 'contocomfrom', - }, - }, - { - $unwind: { - path: '$contocomfrom', - preserveNullAndEmptyArrays: true, + from: 'accounts', + localField: 'accountFromId', + foreignField: '_id', + as: 'accfrom', }, }, + { $unwind: '$accfrom' }, { $lookup: { from: 'accounts', @@ -894,183 +794,294 @@ MovementSchema.statics.getLastN_Transactions = async function (idapp, numtransaz }, }, { $unwind: '$accto' }, - { - $lookup: { - from: 'circuits', - localField: 'accfrom.circuitId', - foreignField: '_id', - as: 'circuitfrom', - }, - }, - { - $unwind: '$circuitfrom', - }, - { - $lookup: { - from: 'circuits', - localField: 'accto.circuitId', - foreignField: '_id', - as: 'circuitto', - }, - }, - { - $unwind: '$circuitto', - }, - { - $lookup: { - from: 'users', - let: { username: '$accto.username', idapp: '$accto.idapp' }, - pipeline: [ - { - $match: - { - $expr: - { - $and: - [ - { $eq: ['$$username', '$username'] }, - { $eq: ['$$idapp', '$idapp'] }, - ], - }, - }, - }, - ], - as: 'userto', - }, - }, - { - $unwind: { - path: '$userto', - preserveNullAndEmptyArrays: true, - }, - }, - { - $lookup: { - from: 'mygroups', - let: { groupname: '$accto.groupname', idapp: '$accto.idapp' }, - pipeline: [ - { - $match: - { - $expr: - { - $and: - [ - { $eq: ['$$groupname', '$groupname'] }, - { $eq: ['$$idapp', '$idapp'] }, - ], - }, - }, - }, - ], - as: 'groupto', - }, - }, - { - $unwind: { - path: '$groupto', - preserveNullAndEmptyArrays: true, - }, - }, - { - $lookup: { - from: 'circuits', - let: { contocom: '$accto.contocom', idapp: '$accto.idapp' }, - pipeline: [ - { - $match: - { - $expr: - { - $and: - [ - { $eq: ['$$contocom', '$path'] }, - { $eq: ['$$idapp', '$idapp'] }, - ], - }, - }, - }, - ], - as: 'contocomto', - }, - }, - { - $unwind: { - path: '$contocomto', - preserveNullAndEmptyArrays: true, - }, - }, ]; - if (querym2) - aggr1.push(querym2); + // Funzione helper per costruire la pipeline completa + const buildPipeline = (isEntrata) => { + let pipeline = [ + { + $match: { idapp }, + }, + { + $sort: { + transactionDate: -1, + }, + }, + ...commonLookups, + ]; - if (numtransaz) - aggr1.push({ $limit: numtransaz }); + // Filtro per entrata/uscita + if (abilitaquerypersingolo) { + let matchCondition = {}; - aggr1.push({ - $project: - { - transactionDate: 1, - amount: 1, - causal: 1, - causalDest: 1, - notifId: 1, - 'circuitfrom.symbol': 1, - 'circuitfrom.name': 1, - 'circuitto.symbol': 1, - 'circuitto.name': 1, - 'userfrom.verified_by_aportador': 1, - 'userfrom.username': 1, - 'userfrom.profile.img': 1, - 'userto.username': 1, - 'userto.profile.img': 1, - 'userto.verified_by_aportador': 1, - 'groupfrom.groupname': 1, - 'groupfrom.verified_by_aportador': 1, - 'groupfrom.title': 1, - 'groupfrom.photos': 1, - 'groupto.groupname': 1, - 'groupto.title': 1, - 'groupto.photos': 1, - 'groupto.verified_by_aportador': 1, - 'contocomfrom.path': 1, - 'contocomfrom.name': 1, - 'contocomto.path': 1, - 'contocomto.name': 1, - }, + if (username) { + matchCondition['username'] = isEntrata ? 'accto.username' : 'accfrom.username'; + pipeline.push({ + $match: { + [matchCondition['username']]: username, + }, + }); + } else if (groupname) { + matchCondition['groupname'] = isEntrata ? 'accto.groupname' : 'accfrom.groupname'; + pipeline.push({ + $match: { + [matchCondition['groupname']]: groupname, + }, + }); + } else if (contocom) { + matchCondition['contocom'] = isEntrata ? 'accto.contocom' : 'accfrom.contocom'; + pipeline.push({ + $match: { + [matchCondition['contocom']]: contocom, + }, + }); + } + } + + // Limit appropriato + const limit = isEntrata ? numtransaz : numtransaz_uscita; + if (limit) { + pipeline.push({ $limit: limit }); + } + + // Aggiungi tutti i lookup rimanenti + pipeline.push( + { + $lookup: { + from: 'users', + let: { username: '$accfrom.username', idapp: '$accfrom.idapp' }, + pipeline: [ + { + $match: { + $expr: { + $and: [{ $eq: ['$$username', '$username'] }, { $eq: ['$$idapp', '$idapp'] }], + }, + }, + }, + ], + as: 'userfrom', + }, + }, + { + $unwind: { + path: '$userfrom', + preserveNullAndEmptyArrays: true, + }, + }, + { + $lookup: { + from: 'mygroups', + let: { groupname: '$accfrom.groupname', idapp: '$accfrom.idapp' }, + pipeline: [ + { + $match: { + $expr: { + $and: [{ $eq: ['$$groupname', '$groupname'] }, { $eq: ['$$idapp', '$idapp'] }], + }, + }, + }, + ], + as: 'groupfrom', + }, + }, + { + $unwind: { + path: '$groupfrom', + preserveNullAndEmptyArrays: true, + }, + }, + { + $lookup: { + from: 'circuits', + let: { contocom: '$accfrom.contocom', idapp: '$accfrom.idapp' }, + pipeline: [ + { + $match: { + $expr: { + $and: [{ $eq: ['$$contocom', '$path'] }, { $eq: ['$$idapp', '$idapp'] }], + }, + }, + }, + ], + as: 'contocomfrom', + }, + }, + { + $unwind: { + path: '$contocomfrom', + preserveNullAndEmptyArrays: true, + }, + }, + { + $lookup: { + from: 'circuits', + localField: 'accfrom.circuitId', + foreignField: '_id', + as: 'circuitfrom', + }, + }, + { + $unwind: '$circuitfrom', + }, + { + $lookup: { + from: 'circuits', + localField: 'accto.circuitId', + foreignField: '_id', + as: 'circuitto', + }, + }, + { + $unwind: '$circuitto', + }, + { + $lookup: { + from: 'users', + let: { username: '$accto.username', idapp: '$accto.idapp' }, + pipeline: [ + { + $match: { + $expr: { + $and: [{ $eq: ['$$username', '$username'] }, { $eq: ['$$idapp', '$idapp'] }], + }, + }, + }, + ], + as: 'userto', + }, + }, + { + $unwind: { + path: '$userto', + preserveNullAndEmptyArrays: true, + }, + }, + { + $lookup: { + from: 'mygroups', + let: { groupname: '$accto.groupname', idapp: '$accto.idapp' }, + pipeline: [ + { + $match: { + $expr: { + $and: [{ $eq: ['$$groupname', '$groupname'] }, { $eq: ['$$idapp', '$idapp'] }], + }, + }, + }, + ], + as: 'groupto', + }, + }, + { + $unwind: { + path: '$groupto', + preserveNullAndEmptyArrays: true, + }, + }, + { + $lookup: { + from: 'circuits', + let: { contocom: '$accto.contocom', idapp: '$accto.idapp' }, + pipeline: [ + { + $match: { + $expr: { + $and: [{ $eq: ['$$contocom', '$path'] }, { $eq: ['$$idapp', '$idapp'] }], + }, + }, + }, + ], + as: 'contocomto', + }, + }, + { + $unwind: { + path: '$contocomto', + preserveNullAndEmptyArrays: true, + }, + }, + { + $project: { + transactionDate: 1, + amount: 1, + causal: 1, + causalDest: 1, + notifId: 1, + 'circuitfrom.symbol': 1, + 'circuitfrom.name': 1, + 'circuitto.symbol': 1, + 'circuitto.name': 1, + 'userfrom.verified_by_aportador': 1, + 'userfrom.username': 1, + 'userfrom.profile.img': 1, + 'userto.username': 1, + 'userto.profile.img': 1, + 'userto.verified_by_aportador': 1, + 'groupfrom.groupname': 1, + 'groupfrom.verified_by_aportador': 1, + 'groupfrom.title': 1, + 'groupfrom.photos': 1, + 'groupto.groupname': 1, + 'groupto.title': 1, + 'groupto.photos': 1, + 'groupto.verified_by_aportador': 1, + 'contocomfrom.path': 1, + 'contocomfrom.name': 1, + 'contocomto.path': 1, + 'contocomto.name': 1, + }, + } + ); + + return pipeline; + }; + + // Esegui entrambe le query in parallelo se necessario + let transazioniEntrata = []; + let transazioniUscita = []; + + if (abilitaquerypersingolo) { + // Esegui le query separate per entrata e uscita + [transazioniEntrata, transazioniUscita] = await Promise.all([ + numtransaz ? MyMovement.aggregate(buildPipeline(true)) : Promise.resolve([]), + numtransaz_uscita ? MyMovement.aggregate(buildPipeline(false)) : Promise.resolve([]), + ]); + } else { + // Se non c'è filtro specifico, prendi solo le ultime N transazioni totali + const aggr1 = buildPipeline(false); // usa la pipeline base + if (numtransaz) { + // Rimuovi l'ultimo limit e aggiungine uno nuovo + aggr1.splice( + aggr1.findIndex((stage) => stage.$limit), + 1 + ); + aggr1.splice(3, 0, { $limit: numtransaz }); + } + transazioniEntrata = await MyMovement.aggregate(aggr1); + } + + // Combina e ordina i risultati + const allTransactions = [...transazioniEntrata, ...transazioniUscita]; + allTransactions.sort((a, b) => b.transactionDate - a.transactionDate); + + // Processa i risultati + allTransactions.forEach(function (mov) { + let ris = tools.getStringaConto(mov); + mov.userfrom = ris.userfrom; + mov.userto = ris.userto; + mov.tipocontofrom = ris.tipocontofrom; + mov.tipocontoto = ris.tipocontoto; }); - const lastNtransac = await MyMovement.aggregate(aggr1); - - /* - transacDate: Date - mitt_username: string - mitt_group: string - dest_username: string - dest_group: string - circuito: string - amount: number - causale: string - - */ - - lastNtransac.forEach(function (mov) { - let ris = tools.getStringaConto(mov) - mov.userfrom = ris.userfrom - mov.userto = ris.userto - mov.tipocontofrom = ris.tipocontofrom - mov.tipocontoto = ris.tipocontoto - }); - - return lastNtransac; + return allTransactions; }; const Movement = mongoose.model('Movement', MovementSchema); Movement.createIndexes() - .then(() => { }) - .catch((err) => { throw err; }); - + .then(() => {}) + .catch((err) => { + throw err; + }); module.exports = { Movement }; diff --git a/src/server/models/stat.js b/src/server/models/stat.js index ffcabb1..095b74d 100755 --- a/src/server/models/stat.js +++ b/src/server/models/stat.js @@ -68,7 +68,7 @@ const StatSchema = new Schema({ StatSchema.statics.updateStats = async function (idapp, datastat) { - datastat.last_transactions = await Movement.getLastN_Transactions(idapp, 5); + datastat.last_transactions = await Movement.getLastN_Transactions(idapp, 5, 5); return datastat; }; diff --git a/src/server/router/users_router.js b/src/server/router/users_router.js index 33a2160..417ed7f 100755 --- a/src/server/router/users_router.js +++ b/src/server/router/users_router.js @@ -427,13 +427,14 @@ router.patch('/:id', authenticate, (req, res) => { router.post('/lastmovs', authenticate, async (req, res) => { const nummov = req.body.nummov; + const nummov_uscita = req.body.nummov_uscita; const idapp = req.body.idapp; try { const { Movement } = require('../models/movement'); if (nummov) { - last_transactions = await Movement.getLastN_Transactions(idapp, nummov); + last_transactions = await Movement.getLastN_Transactions(idapp, nummov, nummov_uscita); } res.send({ code: server_constants.RIS_CODE_OK, last_transactions }); diff --git a/src/server/version.txt b/src/server/version.txt index 22bccd4..2ce74da 100644 --- a/src/server/version.txt +++ b/src/server/version.txt @@ -1 +1 @@ -1.2.76 \ No newline at end of file +1.2.77 \ No newline at end of file