Circuits OK
Accounts Ok Movements OK
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
const mongoose = require('mongoose').set('debug', process.env.DEBUG);
|
||||
const mongoose = require('mongoose').set('debug', false);
|
||||
const Schema = mongoose.Schema;
|
||||
|
||||
mongoose.Promise = global.Promise;
|
||||
@@ -16,7 +16,10 @@ mongoose.plugin(schema => {
|
||||
|
||||
const MovementSchema = new Schema({
|
||||
_id: {
|
||||
type: Number,
|
||||
type: String,
|
||||
default: function () {
|
||||
return new ObjectID().toString()
|
||||
}
|
||||
},
|
||||
idapp: {
|
||||
type: String,
|
||||
@@ -25,10 +28,10 @@ const MovementSchema = new Schema({
|
||||
type: Date,
|
||||
},
|
||||
accountFromId: {
|
||||
type: Number,
|
||||
type: String,
|
||||
},
|
||||
accountToId: {
|
||||
type: Number,
|
||||
type: String,
|
||||
},
|
||||
causal_table: {
|
||||
type: String,
|
||||
@@ -62,16 +65,8 @@ MovementSchema.statics.findAllIdApp = async function(idapp) {
|
||||
|
||||
MovementSchema.pre('save', async function(next) {
|
||||
if (this.isNew) {
|
||||
const myrec = await Movement.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.transactionDate = new Date();
|
||||
}
|
||||
|
||||
next();
|
||||
@@ -94,6 +89,7 @@ MovementSchema.statics.addMov = async function(idapp, accountFromId, accountToId
|
||||
try {
|
||||
let mymov = Movement(
|
||||
{
|
||||
_id: new ObjectID().toString(),
|
||||
idapp,
|
||||
transactionDate: new Date(),
|
||||
accountFromId: accountFromId._id,
|
||||
@@ -105,12 +101,15 @@ MovementSchema.statics.addMov = async function(idapp, accountFromId, accountToId
|
||||
},
|
||||
);
|
||||
|
||||
// Update saldo dell'Account
|
||||
Account.addtoSaldo(accountToId, amount);
|
||||
const rismov = await mymov.save();
|
||||
if (rismov) {
|
||||
// Update saldo dell'Account
|
||||
Account.addtoSaldo(accountToId, amount);
|
||||
|
||||
Account.addtoSaldo(accountFromId, -amount);
|
||||
Account.addtoSaldo(accountFromId, -amount);
|
||||
|
||||
return await mymov.save();
|
||||
return rismov;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Error in addMov', e.message);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user