From 2d213b702048c3664da24b4f7e6e344d62e27c40 Mon Sep 17 00:00:00 2001 From: Surya Paolo Date: Wed, 1 Feb 2023 23:50:39 +0100 Subject: [PATCH] Invio RIS da User a Conto Comunitario --- src/server/models/movement.js | 450 ++++++++++++++++++---------------- 1 file changed, 245 insertions(+), 205 deletions(-) diff --git a/src/server/models/movement.js b/src/server/models/movement.js index bf4d7ff..b18e50a 100755 --- a/src/server/models/movement.js +++ b/src/server/models/movement.js @@ -6,8 +6,8 @@ mongoose.level = 'F'; const tools = require('../tools/general'); -const {ObjectID} = require('mongodb'); -const {Account} = require('../models/account'); +const { ObjectID } = require('mongodb'); +const { Account } = require('../models/account'); // Resolving error Unknown modifier: $pushAll mongoose.plugin(schema => { @@ -17,7 +17,7 @@ mongoose.plugin(schema => { const MovementSchema = new Schema({ _id: { type: String, - default: function() { + default: function () { return new ObjectID().toString(); }, }, @@ -56,17 +56,17 @@ const MovementSchema = new Schema({ }, }); -MovementSchema.statics.findAllIdApp = async function(idapp) { +MovementSchema.statics.findAllIdApp = async function (idapp) { const MyMovement = this; - const myfind = {idapp}; + const myfind = { idapp }; return await MyMovement.find(myfind, (err, arrrec) => { return arrrec; }); }; -MovementSchema.pre('save', async function(next) { +MovementSchema.pre('save', async function (next) { if (this.isNew) { this.transactionDate = new Date(); @@ -75,38 +75,38 @@ MovementSchema.pre('save', async function(next) { next(); }); -MovementSchema.statics.getFieldsForSearch = function() { +MovementSchema.statics.getFieldsForSearch = function () { return [ - {field: 'causal', type: tools.FieldType.string}, - {field: 'amount', type: tools.FieldType.number}, + { field: 'causal', type: tools.FieldType.string }, + { field: 'amount', type: tools.FieldType.number }, ]; }; -MovementSchema.statics.executeQueryTable = function(idapp, params) { +MovementSchema.statics.executeQueryTable = function (idapp, params) { params.fieldsearch = this.getFieldsForSearch(); return tools.executeQueryTable(this, 0, params); }; -MovementSchema.statics.addMov = async function(idapp, accountFromIdTable, accountToIdTable, amount, causal, notifId) { +MovementSchema.statics.addMov = async function (idapp, accountFromIdTable, accountToIdTable, amount, causal, notifId) { try { // Only positive values amount = Math.abs(amount); let mymov = await Movement.create( - { - _id: new ObjectID().toString(), - idapp, - transactionDate: new Date(), - accountFromId: accountFromIdTable._id, - accountToId: accountToIdTable._id, - amount, - causal, - residual: 0, - notifId, - // expiringDate: - }, + { + _id: new ObjectID().toString(), + idapp, + transactionDate: new Date(), + accountFromId: accountFromIdTable._id, + accountToId: accountToIdTable._id, + amount, + causal, + residual: 0, + notifId, + // expiringDate: + }, ); if (mymov) { @@ -122,7 +122,7 @@ MovementSchema.statics.addMov = async function(idapp, accountFromIdTable, accoun } }; -MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username, circuitId) { +MovementSchema.statics.getQueryMovsByCircuitId = async function (idapp, username, circuitId) { try { if (!circuitId) { @@ -137,8 +137,8 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username, $match: { idapp, $or: [ - {accountFromId: myaccount._id}, - {accountToId: myaccount._id}], + { accountFromId: myaccount._id }, + { accountToId: myaccount._id }], }, }, { @@ -149,76 +149,91 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username, as: 'accfrom', }, }, - {$unwind: '$accfrom'}, + { $unwind: '$accfrom' }, { $lookup: { from: 'users', - let: {username: '$accfrom.username', idapp: '$accfrom.idapp'}, + let: { username: '$accfrom.username', idapp: '$accfrom.idapp' }, pipeline: [ { $match: - { - $expr: - { - $and: - [ - {$eq: ['$$username', '$username']}, - {$eq: ['$$idapp', '$idapp']}, - ], - }, - }, + { + $expr: + { + $and: + [ + { $eq: ['$$username', '$username'] }, + { $eq: ['$$idapp', '$idapp'] }, + ], + }, + }, }, ], as: 'userfrom', }, }, - {$unwind: '$userfrom'}, + { + $unwind: { + path: '$userfrom', + preserveNullAndEmptyArrays: true, + }, + }, { $lookup: { from: 'mygroups', - let: {groupname: '$accfrom.groupname', idapp: '$accfrom.idapp'}, + let: { groupname: '$accfrom.groupname', idapp: '$accfrom.idapp' }, pipeline: [ { $match: - { - $expr: - { - $and: - [ - {$eq: ['$$groupname', '$groupname']}, - {$eq: ['$$idapp', '$idapp']}, - ], - }, - }, + { + $expr: + { + $and: + [ + { $eq: ['$$groupname', '$groupname'] }, + { $eq: ['$$idapp', '$idapp'] }, + ], + }, + }, }, ], as: 'groupfrom', }, }, - {$unwind: '$groupfrom'}, + { + $unwind: { + path: '$groupfrom', + preserveNullAndEmptyArrays: true, + }, + }, { $lookup: { from: 'circuits', - let: {contocom: '$accfrom.contocom', idapp: '$accfrom.idapp'}, + let: { contocom: '$accfrom.contocom', idapp: '$accfrom.idapp' }, pipeline: [ { $match: - { - $expr: - { - $and: - [ - {$eq: ['$$contocom', '$path']}, - {$eq: ['$$idapp', '$idapp']}, - ], - }, - }, + { + $expr: + { + $and: + [ + { $eq: ['$$contocom', '$path'] }, + { $eq: ['$$idapp', '$idapp'] }, + ], + }, + }, }, ], as: 'contocomfrom', }, }, - {$unwind: '$contocomfrom'}, + { + $unwind: { + path: '$contocomfrom', + preserveNullAndEmptyArrays: true, + }, + }, { $lookup: { from: 'accounts', @@ -227,7 +242,7 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username, as: 'accto', }, }, - {$unwind: '$accto'}, + { $unwind: '$accto' }, { $lookup: { from: 'circuits', @@ -253,90 +268,111 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username, { $lookup: { from: 'users', - let: {username: '$accto.username', idapp: '$accto.idapp'}, + let: { username: '$accto.username', idapp: '$accto.idapp' }, pipeline: [ { $match: - { - $expr: - { - $and: - [ - {$eq: ['$$username', '$username']}, - {$eq: ['$$idapp', '$idapp']}, - ], - }, - }, + { + $expr: + { + $and: + [ + { $eq: ['$$username', '$username'] }, + { $eq: ['$$idapp', '$idapp'] }, + ], + }, + }, }, ], as: 'userto', }, }, - {$unwind: '$userto'}, + { + $unwind: { + path: '$userto', + preserveNullAndEmptyArrays: true, + }, + }, { $lookup: { from: 'mygroups', - let: {groupname: '$accto.groupname', idapp: '$accto.idapp'}, + let: { groupname: '$accto.groupname', idapp: '$accto.idapp' }, pipeline: [ { $match: - { - $expr: - { - $and: - [ - {$eq: ['$$groupname', '$groupname']}, - {$eq: ['$$idapp', '$idapp']}, - ], - }, - }, + { + $expr: + { + $and: + [ + { $eq: ['$$groupname', '$groupname'] }, + { $eq: ['$$idapp', '$idapp'] }, + ], + }, + }, }, ], as: 'groupto', }, }, - {$unwind: '$groupto'}, + { + $unwind: { + path: '$groupto', + preserveNullAndEmptyArrays: true, + }, + }, { $lookup: { from: 'circuits', - let: {contocom: '$accto.contocom', idapp: '$accto.idapp'}, + let: { contocom: '$accto.contocom', idapp: '$accto.idapp' }, pipeline: [ { $match: - { - $expr: - { - $and: - [ - {$eq: ['$$contocom', '$path']}, - {$eq: ['$$idapp', '$idapp']}, - ], - }, - }, + { + $expr: + { + $and: + [ + { $eq: ['$$contocom', '$path'] }, + { $eq: ['$$idapp', '$idapp'] }, + ], + }, + }, }, ], as: 'contocomto', }, }, - {$unwind: '$contocomto'}, + { + $unwind: { + path: '$contocomto', + preserveNullAndEmptyArrays: true, + }, + }, { $project: - { - transactionDate: 1, - amount: 1, - causal: 1, - notifId: 1, - 'circuitfrom.symbol': 1, - 'circuitto.symbol': 1, - 'userfrom.username': 1, - 'userfrom.profile.img': 1, - 'userto.username': 1, - 'userto.profile.img': 1, - 'groupfrom.groupname': 1, - 'groupto.groupname': 1, - 'contocomfrom.path': 1, - 'contocomto.path': 1, - }, + { + transactionDate: 1, + amount: 1, + causal: 1, + notifId: 1, + 'circuitfrom.symbol': 1, + 'circuitto.symbol': 1, + 'userfrom.username': 1, + 'userfrom.profile.img': 1, + 'userto.username': 1, + 'userto.profile.img': 1, + 'groupfrom.groupname': 1, + 'groupfrom.descr': 1, + 'groupfrom.photos': 1, + 'groupto.groupname': 1, + 'groupto.descr': 1, + 'groupto.photos': 1, + 'contocomfrom.path': 1, + 'contocomfrom.name': 1, + 'contocomto.path': 1, + 'contocomto.name': 1, + }, }, ]; @@ -351,7 +387,7 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username, return []; }; -MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, circuitId) { +MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function (idapp, circuitId) { try { if (!circuitId) { @@ -371,24 +407,24 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, c as: 'accfrom', }, }, - {$unwind: '$accfrom'}, + { $unwind: '$accfrom' }, { $lookup: { from: 'users', - let: {username: '$accfrom.username', idapp: '$accfrom.idapp'}, + let: { username: '$accfrom.username', idapp: '$accfrom.idapp' }, pipeline: [ { $match: - { - $expr: - { - $and: - [ - {$eq: ['$$username', '$username']}, - {$eq: ['$$idapp', '$idapp']}, - ], - }, - }, + { + $expr: + { + $and: + [ + { $eq: ['$$username', '$username'] }, + { $eq: ['$$idapp', '$idapp'] }, + ], + }, + }, }, ], as: 'userfrom', @@ -403,20 +439,20 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, c { $lookup: { from: 'mygroups', - let: {groupname: '$accfrom.groupname', idapp: '$accfrom.idapp'}, + let: { groupname: '$accfrom.groupname', idapp: '$accfrom.idapp' }, pipeline: [ { $match: - { - $expr: - { - $and: - [ - {$eq: ['$$groupname', '$groupname']}, - {$eq: ['$$idapp', '$idapp']}, - ], - }, - }, + { + $expr: + { + $and: + [ + { $eq: ['$$groupname', '$groupname'] }, + { $eq: ['$$idapp', '$idapp'] }, + ], + }, + }, }, ], as: 'groupfrom', @@ -431,20 +467,20 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, c { $lookup: { from: 'circuits', - let: {contocom: '$accfrom.contocom', idapp: '$accfrom.idapp'}, + let: { contocom: '$accfrom.contocom', idapp: '$accfrom.idapp' }, pipeline: [ { $match: - { - $expr: - { - $and: - [ - {$eq: ['$$contocom', '$path']}, - {$eq: ['$$idapp', '$idapp']}, - ], - }, - }, + { + $expr: + { + $and: + [ + { $eq: ['$$contocom', '$path'] }, + { $eq: ['$$idapp', '$idapp'] }, + ], + }, + }, }, ], as: 'contocomfrom', @@ -471,7 +507,7 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, c }, }, { - $match: {'accto.circuitId': circuitId}, + $match: { 'accto.circuitId': circuitId }, }, { '$lookup': { @@ -504,20 +540,20 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, c { $lookup: { from: 'users', - let: {username: '$accto.username', idapp: '$accto.idapp'}, + let: { username: '$accto.username', idapp: '$accto.idapp' }, pipeline: [ { $match: - { - $expr: - { - $and: - [ - {$eq: ['$$username', '$username']}, - {$eq: ['$$idapp', '$idapp']}, - ], - }, - }, + { + $expr: + { + $and: + [ + { $eq: ['$$username', '$username'] }, + { $eq: ['$$idapp', '$idapp'] }, + ], + }, + }, }, ], as: 'userto', @@ -532,20 +568,20 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, c { $lookup: { from: 'mygroups', - let: {groupname: '$accto.groupname', idapp: '$accto.idapp'}, + let: { groupname: '$accto.groupname', idapp: '$accto.idapp' }, pipeline: [ { $match: - { - $expr: - { - $and: - [ - {$eq: ['$$groupname', '$groupname']}, - {$eq: ['$$idapp', '$idapp']}, - ], - }, - }, + { + $expr: + { + $and: + [ + { $eq: ['$$groupname', '$groupname'] }, + { $eq: ['$$idapp', '$idapp'] }, + ], + }, + }, }, ], as: 'groupto', @@ -560,20 +596,20 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, c { $lookup: { from: 'circuits', - let: {contocom: '$accto.contocom', idapp: '$accto.idapp'}, + let: { contocom: '$accto.contocom', idapp: '$accto.idapp' }, pipeline: [ { $match: - { - $expr: - { - $and: - [ - {$eq: ['$$contocom', '$path']}, - {$eq: ['$$idapp', '$idapp']}, - ], - }, - }, + { + $expr: + { + $and: + [ + { $eq: ['$$contocom', '$path'] }, + { $eq: ['$$idapp', '$idapp'] }, + ], + }, + }, }, ], as: 'contocomto', @@ -587,22 +623,26 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, c }, { $project: - { - transactionDate: 1, - amount: 1, - causal: 1, - notifId: 1, - 'circuitfrom.symbol': 1, - 'circuitto.symbol': 1, - 'userfrom.username': 1, - 'userfrom.profile.img': 1, - 'userto.username': 1, - 'userto.profile.img': 1, - 'groupfrom.groupname': 1, - 'groupto.groupname': 1, - 'contocomfrom.path': 1, - 'contocomto.path': 1, - }, + { + transactionDate: 1, + amount: 1, + causal: 1, + notifId: 1, + 'circuitfrom.symbol': 1, + 'circuitto.symbol': 1, + 'userfrom.username': 1, + 'userfrom.profile.img': 1, + 'userto.username': 1, + 'userto.profile.img': 1, + 'groupfrom.groupname': 1, + 'groupfrom.descr': 1, + 'groupto.groupname': 1, + 'groupto.descr': 1, + 'contocomfrom.path': 1, + 'contocomfrom.name': 1, + 'contocomto.path': 1, + 'contocomto.name': 1, + }, }, ]; @@ -616,7 +656,7 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, c return []; }; -MovementSchema.statics.getMovsByCircuitId = async function(idapp, username, circuitId) { +MovementSchema.statics.getMovsByCircuitId = async function (idapp, username, circuitId) { const MyMovement = this; const myquery = await MyMovement.getQueryMovsByCircuitId(idapp, username, circuitId); @@ -630,11 +670,11 @@ MovementSchema.statics.getMovsByCircuitId = async function(idapp, username, circ return []; }; -MovementSchema.statics.checkIfCoinsAlreadySent = async function(notifId) { +MovementSchema.statics.checkIfCoinsAlreadySent = async function (notifId) { const MyMovement = this; try { - const rec = await MyMovement.findOne({notifId}, {_id: 1}); + const rec = await MyMovement.findOne({ notifId }, { _id: 1 }); return !!rec; @@ -648,4 +688,4 @@ MovementSchema.statics.checkIfCoinsAlreadySent = async function(notifId) { const Movement = mongoose.model('Movement', MovementSchema); -module.exports = {Movement}; +module.exports = { Movement };