From 403645a99003b05e2b9841d72076908f37c10908 Mon Sep 17 00:00:00 2001 From: Surya Paolo Date: Wed, 30 Nov 2022 15:00:52 +0100 Subject: [PATCH] =?UTF-8?q?Quando=20aggiungi=20e=20rimuovi=20poi=20quando?= =?UTF-8?q?=20lo=20riaggiungi=20controlla=20che=20gi=C3=A0=20non=20ci=20si?= =?UTF-8?q?a=20un=20ACCOUNTS=20!=20(ora=20crea=20un'altro=20ACCOUNTS=20!)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 8196 -> 8196 bytes src/server/models/account.js | 92 +++++++++++++++++----------------- src/server/models/circuit.js | 4 +- src/server/models/movement.js | 2 +- 4 files changed, 50 insertions(+), 48 deletions(-) diff --git a/.DS_Store b/.DS_Store index 1e9ae4eaf8cc7b8de9d6890099e00f37e30acb78..c659ec0d03a8844f7505c7def0704646910d5e3d 100644 GIT binary patch delta 34 pcmZp1XmQxEPejz*%uGkY*w~_0N1@u#z{pfb!PLTX^JNi3ZUC_)34s6r delta 34 pcmZp1XmQxEPejz%(nv?a$jrD_N1@u#)WT3l!Nk~X^JNi3ZUC^433mVh diff --git a/src/server/models/account.js b/src/server/models/account.js index 04d2c51..78f3c1c 100755 --- a/src/server/models/account.js +++ b/src/server/models/account.js @@ -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 }; diff --git a/src/server/models/circuit.js b/src/server/models/circuit.js index 7c94f80..9958b94 100755 --- a/src/server/models/circuit.js +++ b/src/server/models/circuit.js @@ -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); diff --git a/src/server/models/movement.js b/src/server/models/movement.js index 2dac90e..3b9ce3a 100755 --- a/src/server/models/movement.js +++ b/src/server/models/movement.js @@ -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) {