2022-09-11 11:45:33 +02:00
|
|
|
const mongoose = require('mongoose').set('debug', process.env.DEBUG);
|
2022-04-07 08:19:40 +02:00
|
|
|
const Schema = mongoose.Schema;
|
|
|
|
|
|
|
|
|
|
mongoose.Promise = global.Promise;
|
|
|
|
|
mongoose.level = 'F';
|
|
|
|
|
|
|
|
|
|
const tools = require('../tools/general');
|
|
|
|
|
|
|
|
|
|
const {ObjectID} = require('mongodb');
|
2022-09-11 11:45:33 +02:00
|
|
|
const {Account} = require('../models/account');
|
2022-04-07 08:19:40 +02:00
|
|
|
|
|
|
|
|
// Resolving error Unknown modifier: $pushAll
|
|
|
|
|
mongoose.plugin(schema => {
|
|
|
|
|
schema.options.usePushEach = true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const MovementSchema = new Schema({
|
|
|
|
|
_id: {
|
|
|
|
|
type: Number,
|
|
|
|
|
},
|
2022-09-11 11:45:33 +02:00
|
|
|
idapp: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2022-04-07 08:19:40 +02:00
|
|
|
transactionDate: {
|
2022-09-11 11:45:33 +02:00
|
|
|
type: Date,
|
2022-04-07 08:19:40 +02:00
|
|
|
},
|
|
|
|
|
accountFromId: {
|
|
|
|
|
type: Number,
|
|
|
|
|
},
|
|
|
|
|
accountToId: {
|
|
|
|
|
type: Number,
|
|
|
|
|
},
|
2022-05-08 00:09:59 +02:00
|
|
|
causal_table: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
causal_IdRec: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2022-04-07 08:19:40 +02:00
|
|
|
amount: {
|
|
|
|
|
type: Number,
|
|
|
|
|
},
|
|
|
|
|
causal: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
residual: {
|
|
|
|
|
type: Number,
|
|
|
|
|
},
|
|
|
|
|
expiringDate: {
|
2022-09-11 11:45:33 +02:00
|
|
|
type: Date,
|
2022-04-07 08:19:40 +02:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
MovementSchema.statics.findAllIdApp = async function(idapp) {
|
|
|
|
|
const MyMovement = this;
|
|
|
|
|
|
|
|
|
|
const myfind = {idapp};
|
|
|
|
|
|
2022-09-11 11:45:33 +02:00
|
|
|
return await MyMovement.find(myfind, (err, arrrec) => {
|
2022-04-07 08:19:40 +02:00
|
|
|
return arrrec;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
MovementSchema.pre('save', async function(next) {
|
|
|
|
|
if (this.isNew) {
|
|
|
|
|
const myrec = await Movement.findOne().limit(1).sort({_id: -1});
|
|
|
|
|
if (!!myrec) {
|
|
|
|
|
if (myrec._doc._id === 0)
|
|
|
|
|
this._id = 1;
|
|
|
|
|
else
|
|
|
|
|
this._id = myrec._doc._id + 1;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
this._id = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
MovementSchema.statics.getFieldsForSearch = function() {
|
|
|
|
|
return [
|
|
|
|
|
{field: 'nome_conto', type: tools.FieldType.string},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
MovementSchema.statics.executeQueryTable = function(idapp, params) {
|
|
|
|
|
params.fieldsearch = this.getFieldsForSearch();
|
|
|
|
|
return tools.executeQueryTable(this, 0, params);
|
|
|
|
|
};
|
|
|
|
|
|
2022-09-11 11:45:33 +02:00
|
|
|
MovementSchema.statics.addMov = async function(idapp, accountFromId, accountToId, amount, causal) {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
let mymov = Movement(
|
|
|
|
|
{
|
|
|
|
|
idapp,
|
|
|
|
|
transactionDate: new Date(),
|
|
|
|
|
accountFromId: accountFromId._id,
|
|
|
|
|
accountToId: accountToId._id,
|
|
|
|
|
amount,
|
|
|
|
|
causal,
|
|
|
|
|
residual: 0,
|
|
|
|
|
// expiringDate:
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Update saldo dell'Account
|
|
|
|
|
Account.addtoSaldo(accountToId, amount);
|
|
|
|
|
|
|
|
|
|
Account.addtoSaldo(accountFromId, -amount);
|
|
|
|
|
|
|
|
|
|
return await mymov.save();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('Error in addMov', e.message);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username, circuitId) {
|
|
|
|
|
|
2022-09-12 18:37:08 +02:00
|
|
|
try {
|
|
|
|
|
if (!circuitId) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
const myaccount = await Account.getAccountByUsernameAndCircuitId(idapp, username, {circuitId}, false);
|
2022-09-11 11:45:33 +02:00
|
|
|
|
2022-09-12 18:37:08 +02:00
|
|
|
if (myaccount) {
|
2022-09-11 11:45:33 +02:00
|
|
|
|
|
|
|
|
let aggr1 = [
|
|
|
|
|
{
|
|
|
|
|
$match: {
|
|
|
|
|
idapp,
|
|
|
|
|
$or: [
|
|
|
|
|
{accountFromId: myaccount._id},
|
|
|
|
|
{accountToId: myaccount._id}],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$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: '$userfrom'},
|
|
|
|
|
{
|
|
|
|
|
$lookup: {
|
|
|
|
|
from: 'accounts',
|
|
|
|
|
localField: 'accountToId',
|
|
|
|
|
foreignField: '_id',
|
|
|
|
|
as: 'accto',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{$unwind: '$accto'},
|
2022-09-12 18:37:08 +02:00
|
|
|
{
|
|
|
|
|
"$lookup": {
|
|
|
|
|
"from": "circuits",
|
|
|
|
|
"localField": "accfrom.circuitId",
|
|
|
|
|
"foreignField": "_id",
|
|
|
|
|
"as": "circuitfrom"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"$unwind": "$circuitfrom"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"$lookup": {
|
|
|
|
|
"from": "circuits",
|
|
|
|
|
"localField": "accto.circuitId",
|
|
|
|
|
"foreignField": "_id",
|
|
|
|
|
"as": "circuitto"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"$unwind": "$circuitto"
|
|
|
|
|
},
|
2022-09-11 11:45:33 +02:00
|
|
|
{
|
|
|
|
|
$lookup: {
|
|
|
|
|
from: 'users',
|
|
|
|
|
let: {username: '$accto.username', idapp: '$accto.idapp'},
|
|
|
|
|
pipeline: [
|
|
|
|
|
{
|
|
|
|
|
$match:
|
|
|
|
|
{
|
|
|
|
|
$expr:
|
|
|
|
|
{
|
|
|
|
|
$and:
|
|
|
|
|
[
|
|
|
|
|
{$eq: ['$$username', '$username']},
|
|
|
|
|
{$eq: ['$$idapp', '$idapp']},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
as: 'userto',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{$unwind: '$userto'},
|
|
|
|
|
{
|
|
|
|
|
$project:
|
|
|
|
|
{
|
|
|
|
|
transactionDate: 1,
|
|
|
|
|
amount: 1,
|
|
|
|
|
causal: 1,
|
2022-09-12 18:37:08 +02:00
|
|
|
'circuitfrom.symbol': 1,
|
|
|
|
|
'circuitto.symbol': 1,
|
2022-09-11 11:45:33 +02:00
|
|
|
'userfrom.username': 1,
|
|
|
|
|
'userfrom.profile.img': 1,
|
|
|
|
|
'userto.username': 1,
|
|
|
|
|
'userto.profile.img': 1,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return aggr1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-12 18:37:08 +02:00
|
|
|
} catch (e) {
|
|
|
|
|
return [];
|
2022-09-11 11:45:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [];
|
|
|
|
|
};
|
2022-09-03 13:06:58 +02:00
|
|
|
|
2022-09-11 11:45:33 +02:00
|
|
|
MovementSchema.statics.getMovsByCircuitId = async function(idapp, username, circuitId) {
|
|
|
|
|
const MyMovement = this;
|
2022-09-03 13:06:58 +02:00
|
|
|
|
2022-09-11 11:45:33 +02:00
|
|
|
const myquery = await MyMovement.getQueryMovsByCircuitId(idapp, username, circuitId);
|
2022-09-03 13:06:58 +02:00
|
|
|
|
2022-09-12 18:37:08 +02:00
|
|
|
if (myquery && myquery.length > 0) {
|
2022-09-11 11:45:33 +02:00
|
|
|
ris = await MyMovement.aggregate(myquery);
|
2022-09-03 13:06:58 +02:00
|
|
|
|
2022-09-11 11:45:33 +02:00
|
|
|
return ris;
|
|
|
|
|
}
|
2022-09-03 13:06:58 +02:00
|
|
|
|
2022-09-11 11:45:33 +02:00
|
|
|
return [];
|
2022-09-03 13:06:58 +02:00
|
|
|
};
|
|
|
|
|
|
2022-04-07 08:19:40 +02:00
|
|
|
const Movement = mongoose.model('Movement', MovementSchema);
|
|
|
|
|
|
|
|
|
|
module.exports = {Movement};
|