Quando aggiungi e rimuovi poi quando lo riaggiungi controlla che già non ci sia un ACCOUNTS ! (ora crea un'altro ACCOUNTS !)

This commit is contained in:
Surya Paolo
2022-11-30 15:00:52 +01:00
parent 3f3eda30bf
commit 403645a990
4 changed files with 50 additions and 48 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@@ -10,7 +10,7 @@ mongoose.level = 'F';
const tools = require('../tools/general');
const {ObjectID} = require('mongodb');
const { ObjectID } = require('mongodb');
const shared_consts = require('../tools/shared_nodejs');
@@ -23,7 +23,7 @@ mongoose.plugin(schema => {
const AccountSchema = new Schema({
_id: {
type: String,
default: function() {
default: function () {
return new ObjectID().toString();
},
},
@@ -73,17 +73,17 @@ const AccountSchema = new Schema({
},
});
AccountSchema.statics.findAllIdApp = async function(idapp) {
AccountSchema.statics.findAllIdApp = async function (idapp) {
const Account = this;
const myfind = {idapp, deleted: false};
const myfind = { idapp, deleted: false };
return await Account.find(myfind, (err, arrrec) => {
return arrrec;
});
};
AccountSchema.pre('save', async function(next) {
AccountSchema.pre('save', async function (next) {
if (this.isNew) {
this._id = new ObjectID().toString();
}
@@ -91,19 +91,19 @@ AccountSchema.pre('save', async function(next) {
next();
});
AccountSchema.statics.getFieldsForSearch = function() {
AccountSchema.statics.getFieldsForSearch = function () {
return [
{field: 'name', type: tools.FieldType.string},
{ field: 'name', type: tools.FieldType.string },
];
};
AccountSchema.statics.executeQueryTable = function(idapp, params) {
AccountSchema.statics.executeQueryTable = function (idapp, params) {
params.fieldsearch = this.getFieldsForSearch();
return tools.executeQueryTable(this, idapp, params);
};
AccountSchema.statics.getAccountsByUsername = async function(idapp, username) {
AccountSchema.statics.getAccountsByUsername = async function (idapp, username) {
const Account = this;
if (username === undefined)
@@ -118,13 +118,13 @@ AccountSchema.statics.getAccountsByUsername = async function(idapp, username) {
};
AccountSchema.statics.calcTotCircolante = async function(idapp, circuitId) {
AccountSchema.statics.calcTotCircolante = async function (idapp, circuitId) {
const Account = this;
try {
let aggr1 = [
{
$match: {idapp, circuitId, saldo: {$gt: 0}},
$match: { idapp, circuitId, saldo: { $gt: 0 } },
},
{ $group: { _id: null, count: { $sum: '$saldo' } } }
];
@@ -138,7 +138,7 @@ AccountSchema.statics.calcTotCircolante = async function(idapp, circuitId) {
}
};
AccountSchema.methods.addtoSaldoSave = async function(amount) {
AccountSchema.methods.addtoSaldoSave = async function (amount) {
const account = this;
if (account) {
@@ -154,7 +154,7 @@ AccountSchema.methods.addtoSaldoSave = async function(amount) {
return null;
};
AccountSchema.statics.addtoSaldo = async function(myaccount, amount) {
AccountSchema.statics.addtoSaldo = async function (myaccount, amount) {
const Account = this;
try {
@@ -172,10 +172,10 @@ AccountSchema.statics.addtoSaldo = async function(myaccount, amount) {
myaccountupdate.totTransato = myaccount.totTransato;
myaccountupdate.date_updated = myaccount.date_updated;
return await Account.updateOne({_id: myaccount.id},
{
$set: myaccountupdate
});
return await Account.updateOne({ _id: myaccount.id },
{
$set: myaccountupdate
});
}
} catch (e) {
console.error('error', e);
@@ -184,7 +184,7 @@ AccountSchema.statics.addtoSaldo = async function(myaccount, amount) {
return null;
};
AccountSchema.pre('save', async function(next) {
AccountSchema.pre('save', async function (next) {
if (this.isNew) {
this.date_created = new Date();
}
@@ -192,12 +192,12 @@ AccountSchema.pre('save', async function(next) {
next();
});
AccountSchema.statics.getAccountByUsernameAndCircuitId = async function(idapp, username, {circuitId, circuitName}, createifnotexist) {
AccountSchema.statics.getAccountByUsernameAndCircuitId = async function (idapp, username, circuitId, createifnotexist) {
const Account = this;
try {
const {Circuit} = require('../models/circuit');
const { Circuit } = require('../models/circuit');
if (username === undefined)
return false;
@@ -205,20 +205,10 @@ AccountSchema.statics.getAccountByUsernameAndCircuitId = async function(idapp, u
let myquery = {
'idapp': idapp,
'username': username,
circuitId,
};
if (circuitId) {
myquery.circuitId = circuitId;
}
if (circuitName) {
myquery.circuitName = circuitName;
}
let mycircuit;
if (circuitId)
mycircuit = await Circuit.getCircuitById(circuitId);
else
mycircuit = await Circuit.findOne({name: circuitName});
let mycircuit = await Circuit.getCircuitById(circuitId);
if (mycircuit) {
let myaccount = await Account.findOne(myquery);
@@ -249,18 +239,30 @@ AccountSchema.statics.getAccountByUsernameAndCircuitId = async function(idapp, u
};
AccountSchema.statics.createAccount = async function(idapp, username, circuitName) {
AccountSchema.statics.createAccount = async function (idapp, username, circuitName) {
return await Account.getAccountByUsernameAndCircuitId(idapp, username, {circuitName}, true);
const { Circuit } = require('../models/circuit');
try {
mycircuit = await Circuit.findOne({ name: circuitName }, {_id: 1});
if (mycircuit) {
return await Account.getAccountByUsernameAndCircuitId(idapp, username, mycircuit._id, true);
} else {
return null;
}
} catch (e) {
console.error('error', e);
return null;
}
};
AccountSchema.statics.getUserAccounts = async function(idapp, username) {
AccountSchema.statics.getUserAccounts = async function (idapp, username) {
try {
let aggr1 = [
{
$match: {idapp, username},
$match: { idapp, username },
},
{
$lookup: {
@@ -270,7 +272,7 @@ AccountSchema.statics.getUserAccounts = async function(idapp, username) {
as: 'circuit',
},
},
{$unwind: '$circuit'},
{ $unwind: '$circuit' },
{
$lookup: {
from: 'sendnotifs',
@@ -287,12 +289,12 @@ AccountSchema.statics.getUserAccounts = async function(idapp, username) {
$match: {
$expr: {
$and: [
{$eq: ['$typedir', '$$typedir']},
{$eq: ['$typeid', '$$typeid']},
{$eq: ['$status', 0]},
{$eq: ['$sender', '$$username']},
{$eq: ['$idapp', '$$idapp']},
{$eq: ['$extrarec.circuitname', '$$circuitname']},
{ $eq: ['$typedir', '$$typedir'] },
{ $eq: ['$typeid', '$$typeid'] },
{ $eq: ['$status', 0] },
{ $eq: ['$sender', '$$username'] },
{ $eq: ['$idapp', '$$idapp'] },
{ $eq: ['$extrarec.circuitname', '$$circuitname'] },
],
},
},
@@ -304,7 +306,7 @@ AccountSchema.statics.getUserAccounts = async function(idapp, username) {
ris = await this.aggregate(aggr1);
const {SendNotif} = require('../models/sendnotif');
const { SendNotif } = require('../models/sendnotif');
if (ris) {
for (const account of ris) {
@@ -323,4 +325,4 @@ AccountSchema.statics.getUserAccounts = async function(idapp, username) {
const Account = mongoose.model('Account', AccountSchema);
module.exports = {Account};
module.exports = { Account };

View File

@@ -614,8 +614,8 @@ CircuitSchema.statics.sendCoins = async function(onlycheck, idapp, usernameOrig,
if (circuittable) {
const myqty = Math.abs(extrarec.qty);
const accountdestTable = await Account.getAccountByUsernameAndCircuitId(idapp, extrarec.dest, {circuitId: circuittable._id}, true);
const accountorigTable = await Account.getAccountByUsernameAndCircuitId(idapp, usernameOrig, {circuitId: circuittable._id}, true);
const accountdestTable = await Account.getAccountByUsernameAndCircuitId(idapp, extrarec.dest, circuittable._id, true);
const accountorigTable = await Account.getAccountByUsernameAndCircuitId(idapp, usernameOrig, circuittable._id, true);
const circolantePrec = this.getCircolanteSingolaTransaz(accountorigTable, accountdestTable);

View File

@@ -128,7 +128,7 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username,
if (!circuitId) {
return [];
}
const myaccount = await Account.getAccountByUsernameAndCircuitId(idapp, username, {circuitId}, false);
const myaccount = await Account.getAccountByUsernameAndCircuitId(idapp, username, circuitId, false);
if (myaccount) {