const mongoose = require('mongoose').set('debug', false); const Schema = mongoose.Schema; mongoose.Promise = global.Promise; mongoose.level = 'F'; const tools = require('../tools/general'); const {ObjectID} = require('mongodb'); const {Account} = require('../models/account'); // Resolving error Unknown modifier: $pushAll mongoose.plugin(schema => { schema.options.usePushEach = true; }); const MovementSchema = new Schema({ _id: { type: String, default: function() { return new ObjectID().toString(); }, }, idapp: { type: String, }, transactionDate: { type: Date, }, accountFromId: { type: String, }, accountToId: { type: String, }, causal_table: { type: String, }, causal_IdRec: { type: String, }, amount: { type: Number, }, causal: { type: String, }, residual: { type: Number, }, expiringDate: { type: Date, }, }); MovementSchema.statics.findAllIdApp = async function(idapp) { const MyMovement = this; const myfind = {idapp}; return await MyMovement.find(myfind, (err, arrrec) => { return arrrec; }); }; MovementSchema.pre('save', async function(next) { if (this.isNew) { this.transactionDate = new Date(); } next(); }); MovementSchema.statics.getFieldsForSearch = function() { return [ {field: 'causal', type: tools.FieldType.string}, {field: 'amount', type: tools.FieldType.number}, ]; }; 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) { try { let mymov = Movement( { _id: new ObjectID().toString(), idapp, transactionDate: new Date(), accountFromId: accountFromIdTable._id, accountToId: accountToIdTable._id, amount, causal, residual: 0, // expiringDate: }, ); const rismov = await mymov.save(); if (rismov) { // Update saldo dell'Account await accountToIdTable.addtoSaldo(amount); await accountFromIdTable.addtoSaldo(-amount); return rismov; } } catch (e) { console.error('Error in addMov', e.message); } }; MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username, circuitId) { try { if (!circuitId) { return []; } const myaccount = await Account.getAccountByUsernameAndCircuitId(idapp, username, {circuitId}, false); if (myaccount) { 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'}, { '$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: '$userto'}, { $project: { transactionDate: 1, amount: 1, causal: 1, 'circuitfrom.symbol': 1, 'circuitto.symbol': 1, 'userfrom.username': 1, 'userfrom.profile.img': 1, 'userto.username': 1, 'userto.profile.img': 1, }, }, ]; return aggr1; } } catch (e) { return []; } return []; }; MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, circuitId) { try { if (!circuitId) { return []; } let aggr1 = [ { $match: { idapp, }, }, { $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: 'accounts', localField: 'accountToId', foreignField: '_id', as: 'accto', }, }, { $unwind: { path: '$accto', preserveNullAndEmptyArrays: true, }, }, { '$lookup': { 'from': 'circuits', 'localField': 'accfrom.circuitId', 'foreignField': '_id', 'as': 'circuitfrom', }, }, { $unwind: { path: '$circuitfrom', preserveNullAndEmptyArrays: true, }, }, { '$lookup': { 'from': 'circuits', 'localField': 'accto.circuitId', 'foreignField': '_id', 'as': 'circuitto', }, }, { $unwind: { path: '$circuitto', preserveNullAndEmptyArrays: true, }, }, { $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, }, }, { $project: { transactionDate: 1, amount: 1, causal: 1, 'circuitfrom.symbol': 1, 'circuitto.symbol': 1, 'userfrom.username': 1, 'userfrom.profile.img': 1, 'userto.username': 1, 'userto.profile.img': 1, }, }, ]; return aggr1; } catch (e) { return []; } return []; }; MovementSchema.statics.getMovsByCircuitId = async function(idapp, username, circuitId) { const MyMovement = this; const myquery = await MyMovement.getQueryMovsByCircuitId(idapp, username, circuitId); if (myquery && myquery.length > 0) { ris = await MyMovement.aggregate(myquery); return ris; } return []; }; const Movement = mongoose.model('Movement', MovementSchema); module.exports = {Movement};