Add Movement !

This commit is contained in:
Paolo Arena
2022-09-03 13:06:58 +02:00
parent 6bee366547
commit d262f94315
18 changed files with 599 additions and 323 deletions

View File

@@ -45,6 +45,13 @@ const AccountSchema = new Schema({
type: Boolean,
default: false,
},
date_created: {
type: Date,
default: Date.now,
},
date_updated: {
type: Date,
},
});
AccountSchema.statics.findAllIdApp = async function(idapp) {
@@ -86,6 +93,7 @@ AccountSchema.statics.executeQueryTable = function(idapp, params) {
return tools.executeQueryTable(this, idapp, params);
};
AccountSchema.statics.getAccountsByUsername = async function(idapp, username) {
const Account = this;
@@ -100,6 +108,99 @@ AccountSchema.statics.getAccountsByUsername = async function(idapp, username) {
return await Account.find(myquery).lean();
};
AccountSchema.statics.getUserAccounts = async function(idapp, username) {
try {
let aggr1 = [
{
$match: {idapp, username},
},
{
$lookup: {
from: 'circuits',
localField: 'circuitId',
foreignField: '_id',
as: 'circuit',
},
},
{
'$replaceRoot': {
'newRoot': {
'$mergeObjects': [
{
'$arrayElemAt': [
'$circuit',
0,
],
},
'$$ROOT',
],
},
},
},
{
$project: {
"circuit.name": 1,
},
},
];
ris = await this.aggregate(aggr1);
return ris;
}catch (e) {
console.error('e', e);
}
};
AccountSchema.statics.addtoSaldo = async function(id, amount) {
const Account = this;
if (!id)
return false;
const myaccount = await Account.findById(id);
if (myaccount) {
myaccount.saldo = myaccount.saldo + amount;
myaccount.date_updated = new Date();
return myaccount.save();
}
return null;
};
AccountSchema.statics.getAccountByUsernameAndCircuitId = async function(idapp, username, circuitId, createifnotexist) {
const Account = this;
if (username === undefined)
return false;
const myquery = {
'idapp': idapp,
'username': username,
circuitId,
};
let myaccount = await Account.findOne(myquery).lean();
if (!myaccount && createifnotexist) {
myaccount = new Account({
idapp,
username,
circuitId,
deperibile: false,
importo_iniziale: 0,
saldo: 0,
});
return myaccount.save();
}
return myaccount;
};
const Account = mongoose.model('Account', AccountSchema);