Invio RIS da User a Conto Comunitario

This commit is contained in:
Surya Paolo
2023-02-01 23:50:39 +01:00
parent aa9d27c8fd
commit 2d213b7020

View File

@@ -6,8 +6,8 @@ mongoose.level = 'F';
const tools = require('../tools/general'); const tools = require('../tools/general');
const {ObjectID} = require('mongodb'); const { ObjectID } = require('mongodb');
const {Account} = require('../models/account'); const { Account } = require('../models/account');
// Resolving error Unknown modifier: $pushAll // Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => { mongoose.plugin(schema => {
@@ -17,7 +17,7 @@ mongoose.plugin(schema => {
const MovementSchema = new Schema({ const MovementSchema = new Schema({
_id: { _id: {
type: String, type: String,
default: function() { default: function () {
return new ObjectID().toString(); 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 MyMovement = this;
const myfind = {idapp}; const myfind = { idapp };
return await MyMovement.find(myfind, (err, arrrec) => { return await MyMovement.find(myfind, (err, arrrec) => {
return arrrec; return arrrec;
}); });
}; };
MovementSchema.pre('save', async function(next) { MovementSchema.pre('save', async function (next) {
if (this.isNew) { if (this.isNew) {
this.transactionDate = new Date(); this.transactionDate = new Date();
@@ -75,20 +75,20 @@ MovementSchema.pre('save', async function(next) {
next(); next();
}); });
MovementSchema.statics.getFieldsForSearch = function() { MovementSchema.statics.getFieldsForSearch = function () {
return [ return [
{field: 'causal', type: tools.FieldType.string}, { field: 'causal', type: tools.FieldType.string },
{field: 'amount', type: tools.FieldType.number}, { field: 'amount', type: tools.FieldType.number },
]; ];
}; };
MovementSchema.statics.executeQueryTable = function(idapp, params) { MovementSchema.statics.executeQueryTable = function (idapp, params) {
params.fieldsearch = this.getFieldsForSearch(); params.fieldsearch = this.getFieldsForSearch();
return tools.executeQueryTable(this, 0, params); 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 { try {
// Only positive values // Only positive values
@@ -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 { try {
if (!circuitId) { if (!circuitId) {
@@ -137,8 +137,8 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username,
$match: { $match: {
idapp, idapp,
$or: [ $or: [
{accountFromId: myaccount._id}, { accountFromId: myaccount._id },
{accountToId: myaccount._id}], { accountToId: myaccount._id }],
}, },
}, },
{ {
@@ -149,11 +149,11 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username,
as: 'accfrom', as: 'accfrom',
}, },
}, },
{$unwind: '$accfrom'}, { $unwind: '$accfrom' },
{ {
$lookup: { $lookup: {
from: 'users', from: 'users',
let: {username: '$accfrom.username', idapp: '$accfrom.idapp'}, let: { username: '$accfrom.username', idapp: '$accfrom.idapp' },
pipeline: [ pipeline: [
{ {
$match: $match:
@@ -162,8 +162,8 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username,
{ {
$and: $and:
[ [
{$eq: ['$$username', '$username']}, { $eq: ['$$username', '$username'] },
{$eq: ['$$idapp', '$idapp']}, { $eq: ['$$idapp', '$idapp'] },
], ],
}, },
}, },
@@ -172,11 +172,16 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username,
as: 'userfrom', as: 'userfrom',
}, },
}, },
{$unwind: '$userfrom'}, {
$unwind: {
path: '$userfrom',
preserveNullAndEmptyArrays: true,
},
},
{ {
$lookup: { $lookup: {
from: 'mygroups', from: 'mygroups',
let: {groupname: '$accfrom.groupname', idapp: '$accfrom.idapp'}, let: { groupname: '$accfrom.groupname', idapp: '$accfrom.idapp' },
pipeline: [ pipeline: [
{ {
$match: $match:
@@ -185,8 +190,8 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username,
{ {
$and: $and:
[ [
{$eq: ['$$groupname', '$groupname']}, { $eq: ['$$groupname', '$groupname'] },
{$eq: ['$$idapp', '$idapp']}, { $eq: ['$$idapp', '$idapp'] },
], ],
}, },
}, },
@@ -195,11 +200,16 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username,
as: 'groupfrom', as: 'groupfrom',
}, },
}, },
{$unwind: '$groupfrom'}, {
$unwind: {
path: '$groupfrom',
preserveNullAndEmptyArrays: true,
},
},
{ {
$lookup: { $lookup: {
from: 'circuits', from: 'circuits',
let: {contocom: '$accfrom.contocom', idapp: '$accfrom.idapp'}, let: { contocom: '$accfrom.contocom', idapp: '$accfrom.idapp' },
pipeline: [ pipeline: [
{ {
$match: $match:
@@ -208,8 +218,8 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username,
{ {
$and: $and:
[ [
{$eq: ['$$contocom', '$path']}, { $eq: ['$$contocom', '$path'] },
{$eq: ['$$idapp', '$idapp']}, { $eq: ['$$idapp', '$idapp'] },
], ],
}, },
}, },
@@ -218,7 +228,12 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username,
as: 'contocomfrom', as: 'contocomfrom',
}, },
}, },
{$unwind: '$contocomfrom'}, {
$unwind: {
path: '$contocomfrom',
preserveNullAndEmptyArrays: true,
},
},
{ {
$lookup: { $lookup: {
from: 'accounts', from: 'accounts',
@@ -227,7 +242,7 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username,
as: 'accto', as: 'accto',
}, },
}, },
{$unwind: '$accto'}, { $unwind: '$accto' },
{ {
$lookup: { $lookup: {
from: 'circuits', from: 'circuits',
@@ -253,7 +268,7 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username,
{ {
$lookup: { $lookup: {
from: 'users', from: 'users',
let: {username: '$accto.username', idapp: '$accto.idapp'}, let: { username: '$accto.username', idapp: '$accto.idapp' },
pipeline: [ pipeline: [
{ {
$match: $match:
@@ -262,8 +277,8 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username,
{ {
$and: $and:
[ [
{$eq: ['$$username', '$username']}, { $eq: ['$$username', '$username'] },
{$eq: ['$$idapp', '$idapp']}, { $eq: ['$$idapp', '$idapp'] },
], ],
}, },
}, },
@@ -272,11 +287,16 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username,
as: 'userto', as: 'userto',
}, },
}, },
{$unwind: '$userto'}, {
$unwind: {
path: '$userto',
preserveNullAndEmptyArrays: true,
},
},
{ {
$lookup: { $lookup: {
from: 'mygroups', from: 'mygroups',
let: {groupname: '$accto.groupname', idapp: '$accto.idapp'}, let: { groupname: '$accto.groupname', idapp: '$accto.idapp' },
pipeline: [ pipeline: [
{ {
$match: $match:
@@ -285,8 +305,8 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username,
{ {
$and: $and:
[ [
{$eq: ['$$groupname', '$groupname']}, { $eq: ['$$groupname', '$groupname'] },
{$eq: ['$$idapp', '$idapp']}, { $eq: ['$$idapp', '$idapp'] },
], ],
}, },
}, },
@@ -295,11 +315,16 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username,
as: 'groupto', as: 'groupto',
}, },
}, },
{$unwind: '$groupto'}, {
$unwind: {
path: '$groupto',
preserveNullAndEmptyArrays: true,
},
},
{ {
$lookup: { $lookup: {
from: 'circuits', from: 'circuits',
let: {contocom: '$accto.contocom', idapp: '$accto.idapp'}, let: { contocom: '$accto.contocom', idapp: '$accto.idapp' },
pipeline: [ pipeline: [
{ {
$match: $match:
@@ -308,8 +333,8 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username,
{ {
$and: $and:
[ [
{$eq: ['$$contocom', '$path']}, { $eq: ['$$contocom', '$path'] },
{$eq: ['$$idapp', '$idapp']}, { $eq: ['$$idapp', '$idapp'] },
], ],
}, },
}, },
@@ -318,7 +343,12 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username,
as: 'contocomto', as: 'contocomto',
}, },
}, },
{$unwind: '$contocomto'}, {
$unwind: {
path: '$contocomto',
preserveNullAndEmptyArrays: true,
},
},
{ {
$project: $project:
{ {
@@ -333,9 +363,15 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username,
'userto.username': 1, 'userto.username': 1,
'userto.profile.img': 1, 'userto.profile.img': 1,
'groupfrom.groupname': 1, 'groupfrom.groupname': 1,
'groupfrom.descr': 1,
'groupfrom.photos': 1,
'groupto.groupname': 1, 'groupto.groupname': 1,
'groupto.descr': 1,
'groupto.photos': 1,
'contocomfrom.path': 1, 'contocomfrom.path': 1,
'contocomfrom.name': 1,
'contocomto.path': 1, 'contocomto.path': 1,
'contocomto.name': 1,
}, },
}, },
@@ -351,7 +387,7 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username,
return []; return [];
}; };
MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, circuitId) { MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function (idapp, circuitId) {
try { try {
if (!circuitId) { if (!circuitId) {
@@ -371,11 +407,11 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, c
as: 'accfrom', as: 'accfrom',
}, },
}, },
{$unwind: '$accfrom'}, { $unwind: '$accfrom' },
{ {
$lookup: { $lookup: {
from: 'users', from: 'users',
let: {username: '$accfrom.username', idapp: '$accfrom.idapp'}, let: { username: '$accfrom.username', idapp: '$accfrom.idapp' },
pipeline: [ pipeline: [
{ {
$match: $match:
@@ -384,8 +420,8 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, c
{ {
$and: $and:
[ [
{$eq: ['$$username', '$username']}, { $eq: ['$$username', '$username'] },
{$eq: ['$$idapp', '$idapp']}, { $eq: ['$$idapp', '$idapp'] },
], ],
}, },
}, },
@@ -403,7 +439,7 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, c
{ {
$lookup: { $lookup: {
from: 'mygroups', from: 'mygroups',
let: {groupname: '$accfrom.groupname', idapp: '$accfrom.idapp'}, let: { groupname: '$accfrom.groupname', idapp: '$accfrom.idapp' },
pipeline: [ pipeline: [
{ {
$match: $match:
@@ -412,8 +448,8 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, c
{ {
$and: $and:
[ [
{$eq: ['$$groupname', '$groupname']}, { $eq: ['$$groupname', '$groupname'] },
{$eq: ['$$idapp', '$idapp']}, { $eq: ['$$idapp', '$idapp'] },
], ],
}, },
}, },
@@ -431,7 +467,7 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, c
{ {
$lookup: { $lookup: {
from: 'circuits', from: 'circuits',
let: {contocom: '$accfrom.contocom', idapp: '$accfrom.idapp'}, let: { contocom: '$accfrom.contocom', idapp: '$accfrom.idapp' },
pipeline: [ pipeline: [
{ {
$match: $match:
@@ -440,8 +476,8 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, c
{ {
$and: $and:
[ [
{$eq: ['$$contocom', '$path']}, { $eq: ['$$contocom', '$path'] },
{$eq: ['$$idapp', '$idapp']}, { $eq: ['$$idapp', '$idapp'] },
], ],
}, },
}, },
@@ -471,7 +507,7 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, c
}, },
}, },
{ {
$match: {'accto.circuitId': circuitId}, $match: { 'accto.circuitId': circuitId },
}, },
{ {
'$lookup': { '$lookup': {
@@ -504,7 +540,7 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, c
{ {
$lookup: { $lookup: {
from: 'users', from: 'users',
let: {username: '$accto.username', idapp: '$accto.idapp'}, let: { username: '$accto.username', idapp: '$accto.idapp' },
pipeline: [ pipeline: [
{ {
$match: $match:
@@ -513,8 +549,8 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, c
{ {
$and: $and:
[ [
{$eq: ['$$username', '$username']}, { $eq: ['$$username', '$username'] },
{$eq: ['$$idapp', '$idapp']}, { $eq: ['$$idapp', '$idapp'] },
], ],
}, },
}, },
@@ -532,7 +568,7 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, c
{ {
$lookup: { $lookup: {
from: 'mygroups', from: 'mygroups',
let: {groupname: '$accto.groupname', idapp: '$accto.idapp'}, let: { groupname: '$accto.groupname', idapp: '$accto.idapp' },
pipeline: [ pipeline: [
{ {
$match: $match:
@@ -541,8 +577,8 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, c
{ {
$and: $and:
[ [
{$eq: ['$$groupname', '$groupname']}, { $eq: ['$$groupname', '$groupname'] },
{$eq: ['$$idapp', '$idapp']}, { $eq: ['$$idapp', '$idapp'] },
], ],
}, },
}, },
@@ -560,7 +596,7 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, c
{ {
$lookup: { $lookup: {
from: 'circuits', from: 'circuits',
let: {contocom: '$accto.contocom', idapp: '$accto.idapp'}, let: { contocom: '$accto.contocom', idapp: '$accto.idapp' },
pipeline: [ pipeline: [
{ {
$match: $match:
@@ -569,8 +605,8 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, c
{ {
$and: $and:
[ [
{$eq: ['$$contocom', '$path']}, { $eq: ['$$contocom', '$path'] },
{$eq: ['$$idapp', '$idapp']}, { $eq: ['$$idapp', '$idapp'] },
], ],
}, },
}, },
@@ -599,9 +635,13 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, c
'userto.username': 1, 'userto.username': 1,
'userto.profile.img': 1, 'userto.profile.img': 1,
'groupfrom.groupname': 1, 'groupfrom.groupname': 1,
'groupfrom.descr': 1,
'groupto.groupname': 1, 'groupto.groupname': 1,
'groupto.descr': 1,
'contocomfrom.path': 1, 'contocomfrom.path': 1,
'contocomfrom.name': 1,
'contocomto.path': 1, 'contocomto.path': 1,
'contocomto.name': 1,
}, },
}, },
@@ -616,7 +656,7 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, c
return []; return [];
}; };
MovementSchema.statics.getMovsByCircuitId = async function(idapp, username, circuitId) { MovementSchema.statics.getMovsByCircuitId = async function (idapp, username, circuitId) {
const MyMovement = this; const MyMovement = this;
const myquery = await MyMovement.getQueryMovsByCircuitId(idapp, username, circuitId); const myquery = await MyMovement.getQueryMovsByCircuitId(idapp, username, circuitId);
@@ -630,11 +670,11 @@ MovementSchema.statics.getMovsByCircuitId = async function(idapp, username, circ
return []; return [];
}; };
MovementSchema.statics.checkIfCoinsAlreadySent = async function(notifId) { MovementSchema.statics.checkIfCoinsAlreadySent = async function (notifId) {
const MyMovement = this; const MyMovement = this;
try { try {
const rec = await MyMovement.findOne({notifId}, {_id: 1}); const rec = await MyMovement.findOne({ notifId }, { _id: 1 });
return !!rec; return !!rec;
@@ -648,4 +688,4 @@ MovementSchema.statics.checkIfCoinsAlreadySent = async function(notifId) {
const Movement = mongoose.model('Movement', MovementSchema); const Movement = mongoose.model('Movement', MovementSchema);
module.exports = {Movement}; module.exports = { Movement };