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,38 +75,38 @@ 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
amount = Math.abs(amount); amount = Math.abs(amount);
let mymov = await Movement.create( let mymov = await Movement.create(
{ {
_id: new ObjectID().toString(), _id: new ObjectID().toString(),
idapp, idapp,
transactionDate: new Date(), transactionDate: new Date(),
accountFromId: accountFromIdTable._id, accountFromId: accountFromIdTable._id,
accountToId: accountToIdTable._id, accountToId: accountToIdTable._id,
amount, amount,
causal, causal,
residual: 0, residual: 0,
notifId, notifId,
// expiringDate: // expiringDate:
}, },
); );
if (mymov) { 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 { 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,76 +149,91 @@ 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:
{ {
$expr: $expr:
{ {
$and: $and:
[ [
{$eq: ['$$username', '$username']}, { $eq: ['$$username', '$username'] },
{$eq: ['$$idapp', '$idapp']}, { $eq: ['$$idapp', '$idapp'] },
], ],
}, },
}, },
}, },
], ],
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:
{ {
$expr: $expr:
{ {
$and: $and:
[ [
{$eq: ['$$groupname', '$groupname']}, { $eq: ['$$groupname', '$groupname'] },
{$eq: ['$$idapp', '$idapp']}, { $eq: ['$$idapp', '$idapp'] },
], ],
}, },
}, },
}, },
], ],
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:
{ {
$expr: $expr:
{ {
$and: $and:
[ [
{$eq: ['$$contocom', '$path']}, { $eq: ['$$contocom', '$path'] },
{$eq: ['$$idapp', '$idapp']}, { $eq: ['$$idapp', '$idapp'] },
], ],
}, },
}, },
}, },
], ],
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,90 +268,111 @@ 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:
{ {
$expr: $expr:
{ {
$and: $and:
[ [
{$eq: ['$$username', '$username']}, { $eq: ['$$username', '$username'] },
{$eq: ['$$idapp', '$idapp']}, { $eq: ['$$idapp', '$idapp'] },
], ],
}, },
}, },
}, },
], ],
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:
{ {
$expr: $expr:
{ {
$and: $and:
[ [
{$eq: ['$$groupname', '$groupname']}, { $eq: ['$$groupname', '$groupname'] },
{$eq: ['$$idapp', '$idapp']}, { $eq: ['$$idapp', '$idapp'] },
], ],
}, },
}, },
}, },
], ],
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:
{ {
$expr: $expr:
{ {
$and: $and:
[ [
{$eq: ['$$contocom', '$path']}, { $eq: ['$$contocom', '$path'] },
{$eq: ['$$idapp', '$idapp']}, { $eq: ['$$idapp', '$idapp'] },
], ],
}, },
}, },
}, },
], ],
as: 'contocomto', as: 'contocomto',
}, },
}, },
{$unwind: '$contocomto'}, {
$unwind: {
path: '$contocomto',
preserveNullAndEmptyArrays: true,
},
},
{ {
$project: $project:
{ {
transactionDate: 1, transactionDate: 1,
amount: 1, amount: 1,
causal: 1, causal: 1,
notifId: 1, notifId: 1,
'circuitfrom.symbol': 1, 'circuitfrom.symbol': 1,
'circuitto.symbol': 1, 'circuitto.symbol': 1,
'userfrom.username': 1, 'userfrom.username': 1,
'userfrom.profile.img': 1, 'userfrom.profile.img': 1,
'userto.username': 1, 'userto.username': 1,
'userto.profile.img': 1, 'userto.profile.img': 1,
'groupfrom.groupname': 1, 'groupfrom.groupname': 1,
'groupto.groupname': 1, 'groupfrom.descr': 1,
'contocomfrom.path': 1, 'groupfrom.photos': 1,
'contocomto.path': 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 []; return [];
}; };
MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, circuitId) { MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function (idapp, circuitId) {
try { try {
if (!circuitId) { if (!circuitId) {
@@ -371,24 +407,24 @@ 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:
{ {
$expr: $expr:
{ {
$and: $and:
[ [
{$eq: ['$$username', '$username']}, { $eq: ['$$username', '$username'] },
{$eq: ['$$idapp', '$idapp']}, { $eq: ['$$idapp', '$idapp'] },
], ],
}, },
}, },
}, },
], ],
as: 'userfrom', as: 'userfrom',
@@ -403,20 +439,20 @@ 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:
{ {
$expr: $expr:
{ {
$and: $and:
[ [
{$eq: ['$$groupname', '$groupname']}, { $eq: ['$$groupname', '$groupname'] },
{$eq: ['$$idapp', '$idapp']}, { $eq: ['$$idapp', '$idapp'] },
], ],
}, },
}, },
}, },
], ],
as: 'groupfrom', as: 'groupfrom',
@@ -431,20 +467,20 @@ 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:
{ {
$expr: $expr:
{ {
$and: $and:
[ [
{$eq: ['$$contocom', '$path']}, { $eq: ['$$contocom', '$path'] },
{$eq: ['$$idapp', '$idapp']}, { $eq: ['$$idapp', '$idapp'] },
], ],
}, },
}, },
}, },
], ],
as: 'contocomfrom', as: 'contocomfrom',
@@ -471,7 +507,7 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, c
}, },
}, },
{ {
$match: {'accto.circuitId': circuitId}, $match: { 'accto.circuitId': circuitId },
}, },
{ {
'$lookup': { '$lookup': {
@@ -504,20 +540,20 @@ 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:
{ {
$expr: $expr:
{ {
$and: $and:
[ [
{$eq: ['$$username', '$username']}, { $eq: ['$$username', '$username'] },
{$eq: ['$$idapp', '$idapp']}, { $eq: ['$$idapp', '$idapp'] },
], ],
}, },
}, },
}, },
], ],
as: 'userto', as: 'userto',
@@ -532,20 +568,20 @@ 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:
{ {
$expr: $expr:
{ {
$and: $and:
[ [
{$eq: ['$$groupname', '$groupname']}, { $eq: ['$$groupname', '$groupname'] },
{$eq: ['$$idapp', '$idapp']}, { $eq: ['$$idapp', '$idapp'] },
], ],
}, },
}, },
}, },
], ],
as: 'groupto', as: 'groupto',
@@ -560,20 +596,20 @@ 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:
{ {
$expr: $expr:
{ {
$and: $and:
[ [
{$eq: ['$$contocom', '$path']}, { $eq: ['$$contocom', '$path'] },
{$eq: ['$$idapp', '$idapp']}, { $eq: ['$$idapp', '$idapp'] },
], ],
}, },
}, },
}, },
], ],
as: 'contocomto', as: 'contocomto',
@@ -587,22 +623,26 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, c
}, },
{ {
$project: $project:
{ {
transactionDate: 1, transactionDate: 1,
amount: 1, amount: 1,
causal: 1, causal: 1,
notifId: 1, notifId: 1,
'circuitfrom.symbol': 1, 'circuitfrom.symbol': 1,
'circuitto.symbol': 1, 'circuitto.symbol': 1,
'userfrom.username': 1, 'userfrom.username': 1,
'userfrom.profile.img': 1, 'userfrom.profile.img': 1,
'userto.username': 1, 'userto.username': 1,
'userto.profile.img': 1, 'userto.profile.img': 1,
'groupfrom.groupname': 1, 'groupfrom.groupname': 1,
'groupto.groupname': 1, 'groupfrom.descr': 1,
'contocomfrom.path': 1, 'groupto.groupname': 1,
'contocomto.path': 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 []; 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 };