Circuits...

Circuits Fido e Max Qta
Fixed error eslint: 7.0.0 is OK
This commit is contained in:
Paolo Arena
2022-09-11 11:45:33 +02:00
parent d262f94315
commit d62888083b
100 changed files with 3699 additions and 2774 deletions

View File

@@ -1,4 +1,4 @@
const mongoose = require('mongoose').set('debug', false);
const mongoose = require('mongoose').set('debug', process.env.DEBUG);
const Schema = mongoose.Schema;
mongoose.Promise = global.Promise;
@@ -7,6 +7,7 @@ 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 => {
@@ -17,8 +18,11 @@ const MovementSchema = new Schema({
_id: {
type: Number,
},
idapp: {
type: String,
},
transactionDate: {
type: Date
type: Date,
},
accountFromId: {
type: Number,
@@ -42,7 +46,7 @@ const MovementSchema = new Schema({
type: Number,
},
expiringDate: {
type: Date
type: Date,
},
});
@@ -51,7 +55,7 @@ MovementSchema.statics.findAllIdApp = async function(idapp) {
const myfind = {idapp};
return MyMovement.find(myfind, (err, arrrec) => {
return await MyMovement.find(myfind, (err, arrrec) => {
return arrrec;
});
};
@@ -85,28 +89,150 @@ MovementSchema.statics.executeQueryTable = function(idapp, params) {
return tools.executeQueryTable(this, 0, params);
};
MovementSchema.statics.addMov = async function(accountFromId, accountToId, amount, causal) {
MovementSchema.statics.addMov = async function(idapp, accountFromId, accountToId, amount, causal) {
const {Account} = require('../models/account');
try {
let mymov = Movement(
{
idapp,
transactionDate: new Date(),
accountFromId: accountFromId._id,
accountToId: accountToId._id,
amount,
causal,
residual: 0,
// expiringDate:
},
);
let mymov = Movement(
{
transactionDate: new Date(),
accountFromId,
accountToId,
amount,
causal,
residual: 0,
// expiringDate:
}
);
// Update saldo dell'Account
Account.addtoSaldo(accountToId, amount);
// Update saldo dell'Account
Account.addtoSaldo(accountToId, amount);
Account.addtoSaldo(accountFromId, -amount);
Account.addtoSaldo(accountFromId, -amount);
return await mymov.save();
} catch (e) {
console.error('Error in addMov', e.message);
}
};
return mymov.save();
MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username, circuitId) {
const myaccount = await Account.getAccountByUsernameAndCircuitId(idapp, username, circuitId, false);
if (myaccount) {
try {
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: '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,
'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) {
ris = await MyMovement.aggregate(myquery);
return ris;
}
return [];
};
const Movement = mongoose.model('Movement', MovementSchema);