Circuits OK

Accounts Ok
Movements OK
This commit is contained in:
Paolo Arena
2022-09-14 11:32:04 +02:00
parent 2f24a02a63
commit 845e244470
90 changed files with 362 additions and 258 deletions

View File

@@ -1,7 +1,7 @@
/*
Account is a User's single Circuit
*/
const mongoose = require('mongoose').set('debug', process.env.DEBUG);
const mongoose = require('mongoose').set('debug', false);
const Schema = mongoose.Schema;
mongoose.Promise = global.Promise;
@@ -18,7 +18,10 @@ mongoose.plugin(schema => {
const AccountSchema = new Schema({
_id: {
type: Number,
type: String,
default: function () {
return new ObjectID().toString()
}
},
idapp: {
type: String,
@@ -27,7 +30,7 @@ const AccountSchema = new Schema({
type: String,
},
circuitId: { // ----- REF TO Circuit
type: Number,
type: String,
},
name: {
type: String,
@@ -72,16 +75,7 @@ AccountSchema.statics.findAllIdApp = async function(idapp) {
AccountSchema.pre('save', async function(next) {
if (this.isNew) {
const myrec = await Account.findOne().limit(1).sort({_id: -1});
if (!!myrec) {
if (myrec._doc._id === 0)
this._id = 1;
else
this._id = myrec._doc._id + 1;
} else {
this._id = 1;
}
this._id = new ObjectID().toString()
}
next();
@@ -130,6 +124,14 @@ AccountSchema.statics.addtoSaldo = async function(id, amount) {
return null;
};
AccountSchema.pre('save', async function(next) {
if (this.isNew) {
this.date_created = new Date();
}
next();
});
AccountSchema.statics.getAccountByUsernameAndCircuitId = async function(idapp, username, {circuitId, circuitName}, createifnotexist) {
const Account = this;
@@ -152,26 +154,35 @@ AccountSchema.statics.getAccountByUsernameAndCircuitId = async function(idapp, u
myquery.circuitName = circuitName;
}
const mycircuit = await Circuit.getCircuitById(circuitId);
let mycircuit;
if (circuitId)
mycircuit = await Circuit.getCircuitById(circuitId);
else
mycircuit = await Circuit.findOne({name: circuitName}).lean();
let myaccount = await Account.findOne(myquery).lean();
if (!myaccount && createifnotexist) {
myaccount = new Account({
idapp,
username,
circuitId: mycircuit._id,
deperibile: false,
fidoConcesso: mycircuit.fido_scoperto_default,
qta_maxConcessa: mycircuit.qta_max_default,
importo_iniziale: 0,
saldo: 0,
});
if (mycircuit) {
let myaccount = await Account.findOne(myquery).lean();
return await myaccount.save();
if (!myaccount && createifnotexist) {
myaccount = new Account({
_id: new ObjectID().toString(),
idapp,
username,
circuitId: mycircuit._id,
deperibile: false,
fidoConcesso: mycircuit.fido_scoperto_default,
qta_maxConcessa: mycircuit.qta_max_default,
importo_iniziale: 0,
saldo: 0,
});
return await myaccount.save();
}
return myaccount;
}
return myaccount;
return null;
} catch (e) {
console.error('error', e);