- Statistiche

- Menu e Sottomenu
- Lista ultimi Movimenti
This commit is contained in:
Surya Paolo
2024-09-26 02:14:33 +02:00
parent ca519baad4
commit ce51c87365
13 changed files with 45558 additions and 64 deletions

View File

@@ -213,7 +213,7 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function (idapp, username
preserveNullAndEmptyArrays: true,
},
},
{
$lookup: {
from: 'circuits',
@@ -671,6 +671,48 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function (idapp,
return [];
};
MovementSchema.statics.getTotal_Transactions = async function (idapp) {
const MyMovement = this;
const numtransac = await MyMovement.countDocuments(
{
idapp,
});
return numtransac;
};
MovementSchema.statics.getTot_RIS_Transati = async function (idapp) {
const MyMovement = this;
// Utilizza l'aggregazione per sommare tutti gli 'amount' per un dato 'idapp'
const numtot = await MyMovement.aggregate([
{
$match: { idapp } // Filtra i documenti per 'idapp'
},
{
$sort: {
transactionDate: -1,
}
},
{
$group: {
_id: null,
totalAmount: { $sum: "$amount" } // Somma tutti gli 'amount'
}
}
]);
try {
return numtot.length > 0 ? numtot[0].totalAmount : 0;
} catch (e) {
return 0;
}
};
MovementSchema.statics.getMovsByCircuitId = async function (idapp, username, circuitId) {
const MyMovement = this;
@@ -699,6 +741,284 @@ MovementSchema.statics.checkIfCoinsAlreadySent = async function (notifId) {
};
MovementSchema.statics.getLastN_Transactions = async function (idapp, numtransaz = 10) {
const MyMovement = this;
// get last "numtransaz" transactions
let aggr1 = [
{
$match: {
idapp,
},
},
{
$sort: {
transactionDate: -1,
}
},
{ $limit: numtransaz },
{
$lookup: {
from: 'accounts',
localField: 'accountFromId',
foreignField: '_id',
as: 'accfrom',
},
},
{ $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,
},
},
{
$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: 'accounts',
localField: 'accountToId',
foreignField: '_id',
as: 'accto',
},
},
{ $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,
},
},
{
$project:
{
transactionDate: 1,
amount: 1,
causal: 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,
},
},
];
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
*/
return lastNtransac;
};
const Movement = mongoose.model('Movement', MovementSchema);