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 tools = require('../tools/general');
const {ObjectID} = require('mongodb'); const { ObjectID } = require('mongodb');
const shared_consts = require('../tools/shared_nodejs'); const shared_consts = require('../tools/shared_nodejs');
@@ -23,7 +23,7 @@ mongoose.plugin(schema => {
const AccountSchema = new Schema({ const AccountSchema = new Schema({
_id: { _id: {
type: String, type: String,
default: function() { default: function () {
return new ObjectID().toString(); 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 Account = this;
const myfind = {idapp, deleted: false}; const myfind = { idapp, deleted: false };
return await Account.find(myfind, (err, arrrec) => { return await Account.find(myfind, (err, arrrec) => {
return arrrec; return arrrec;
}); });
}; };
AccountSchema.pre('save', async function(next) { AccountSchema.pre('save', async function (next) {
if (this.isNew) { if (this.isNew) {
this._id = new ObjectID().toString(); this._id = new ObjectID().toString();
} }
@@ -91,19 +91,19 @@ AccountSchema.pre('save', async function(next) {
next(); next();
}); });
AccountSchema.statics.getFieldsForSearch = function() { AccountSchema.statics.getFieldsForSearch = function () {
return [ 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(); params.fieldsearch = this.getFieldsForSearch();
return tools.executeQueryTable(this, idapp, params); return tools.executeQueryTable(this, idapp, params);
}; };
AccountSchema.statics.getAccountsByUsername = async function(idapp, username) { AccountSchema.statics.getAccountsByUsername = async function (idapp, username) {
const Account = this; const Account = this;
if (username === undefined) 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; const Account = this;
try { try {
let aggr1 = [ let aggr1 = [
{ {
$match: {idapp, circuitId, saldo: {$gt: 0}}, $match: { idapp, circuitId, saldo: { $gt: 0 } },
}, },
{ $group: { _id: null, count: { $sum: '$saldo' } } } { $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; const account = this;
if (account) { if (account) {
@@ -154,7 +154,7 @@ AccountSchema.methods.addtoSaldoSave = async function(amount) {
return null; return null;
}; };
AccountSchema.statics.addtoSaldo = async function(myaccount, amount) { AccountSchema.statics.addtoSaldo = async function (myaccount, amount) {
const Account = this; const Account = this;
try { try {
@@ -172,10 +172,10 @@ AccountSchema.statics.addtoSaldo = async function(myaccount, amount) {
myaccountupdate.totTransato = myaccount.totTransato; myaccountupdate.totTransato = myaccount.totTransato;
myaccountupdate.date_updated = myaccount.date_updated; myaccountupdate.date_updated = myaccount.date_updated;
return await Account.updateOne({_id: myaccount.id}, return await Account.updateOne({ _id: myaccount.id },
{ {
$set: myaccountupdate $set: myaccountupdate
}); });
} }
} catch (e) { } catch (e) {
console.error('error', e); console.error('error', e);
@@ -184,7 +184,7 @@ AccountSchema.statics.addtoSaldo = async function(myaccount, amount) {
return null; return null;
}; };
AccountSchema.pre('save', async function(next) { AccountSchema.pre('save', async function (next) {
if (this.isNew) { if (this.isNew) {
this.date_created = new Date(); this.date_created = new Date();
} }
@@ -192,12 +192,12 @@ AccountSchema.pre('save', async function(next) {
next(); next();
}); });
AccountSchema.statics.getAccountByUsernameAndCircuitId = async function(idapp, username, {circuitId, circuitName}, createifnotexist) { AccountSchema.statics.getAccountByUsernameAndCircuitId = async function (idapp, username, circuitId, createifnotexist) {
const Account = this; const Account = this;
try { try {
const {Circuit} = require('../models/circuit'); const { Circuit } = require('../models/circuit');
if (username === undefined) if (username === undefined)
return false; return false;
@@ -205,20 +205,10 @@ AccountSchema.statics.getAccountByUsernameAndCircuitId = async function(idapp, u
let myquery = { let myquery = {
'idapp': idapp, 'idapp': idapp,
'username': username, 'username': username,
circuitId,
}; };
if (circuitId) { let mycircuit = await Circuit.getCircuitById(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});
if (mycircuit) { if (mycircuit) {
let myaccount = await Account.findOne(myquery); 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 { try {
let aggr1 = [ let aggr1 = [
{ {
$match: {idapp, username}, $match: { idapp, username },
}, },
{ {
$lookup: { $lookup: {
@@ -270,7 +272,7 @@ AccountSchema.statics.getUserAccounts = async function(idapp, username) {
as: 'circuit', as: 'circuit',
}, },
}, },
{$unwind: '$circuit'}, { $unwind: '$circuit' },
{ {
$lookup: { $lookup: {
from: 'sendnotifs', from: 'sendnotifs',
@@ -287,12 +289,12 @@ AccountSchema.statics.getUserAccounts = async function(idapp, username) {
$match: { $match: {
$expr: { $expr: {
$and: [ $and: [
{$eq: ['$typedir', '$$typedir']}, { $eq: ['$typedir', '$$typedir'] },
{$eq: ['$typeid', '$$typeid']}, { $eq: ['$typeid', '$$typeid'] },
{$eq: ['$status', 0]}, { $eq: ['$status', 0] },
{$eq: ['$sender', '$$username']}, { $eq: ['$sender', '$$username'] },
{$eq: ['$idapp', '$$idapp']}, { $eq: ['$idapp', '$$idapp'] },
{$eq: ['$extrarec.circuitname', '$$circuitname']}, { $eq: ['$extrarec.circuitname', '$$circuitname'] },
], ],
}, },
}, },
@@ -304,7 +306,7 @@ AccountSchema.statics.getUserAccounts = async function(idapp, username) {
ris = await this.aggregate(aggr1); ris = await this.aggregate(aggr1);
const {SendNotif} = require('../models/sendnotif'); const { SendNotif } = require('../models/sendnotif');
if (ris) { if (ris) {
for (const account of ris) { for (const account of ris) {
@@ -323,4 +325,4 @@ AccountSchema.statics.getUserAccounts = async function(idapp, username) {
const Account = mongoose.model('Account', AccountSchema); 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) { if (circuittable) {
const myqty = Math.abs(extrarec.qty); const myqty = Math.abs(extrarec.qty);
const accountdestTable = await Account.getAccountByUsernameAndCircuitId(idapp, extrarec.dest, {circuitId: circuittable._id}, true); const accountdestTable = await Account.getAccountByUsernameAndCircuitId(idapp, extrarec.dest, circuittable._id, true);
const accountorigTable = await Account.getAccountByUsernameAndCircuitId(idapp, usernameOrig, {circuitId: circuittable._id}, true); const accountorigTable = await Account.getAccountByUsernameAndCircuitId(idapp, usernameOrig, circuittable._id, true);
const circolantePrec = this.getCircolanteSingolaTransaz(accountorigTable, accountdestTable); const circolantePrec = this.getCircolanteSingolaTransaz(accountorigTable, accountdestTable);

View File

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