Files
freeplanet_serverside/src/server/models/movement.js

115 lines
2.1 KiB
JavaScript
Raw Normal View History

2022-04-07 08:19:40 +02:00
const mongoose = require('mongoose').set('debug', false);
const Schema = mongoose.Schema;
mongoose.Promise = global.Promise;
mongoose.level = 'F';
const tools = require('../tools/general');
const {ObjectID} = require('mongodb');
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
schema.options.usePushEach = true;
});
const MovementSchema = new Schema({
_id: {
type: Number,
},
transactionDate: {
type: Date
},
accountFromId: {
type: Number,
},
accountToId: {
type: Number,
},
causal_table: {
type: String,
},
causal_IdRec: {
type: String,
},
2022-04-07 08:19:40 +02:00
amount: {
type: Number,
},
causal: {
type: String,
},
residual: {
type: Number,
},
expiringDate: {
type: Date
},
});
MovementSchema.statics.findAllIdApp = async function(idapp) {
const MyMovement = this;
const myfind = {idapp};
return MyMovement.find(myfind, (err, arrrec) => {
return arrrec;
});
};
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;
}
}
next();
});
MovementSchema.statics.getFieldsForSearch = function() {
return [
{field: 'nome_conto', type: tools.FieldType.string},
];
};
MovementSchema.statics.executeQueryTable = function(idapp, params) {
params.fieldsearch = this.getFieldsForSearch();
return tools.executeQueryTable(this, 0, params);
};
2022-09-03 13:06:58 +02:00
MovementSchema.statics.addMov = async function(accountFromId, accountToId, amount, causal) {
const {Account} = require('../models/account');
let mymov = Movement(
{
transactionDate: new Date(),
accountFromId,
accountToId,
amount,
causal,
residual: 0,
// expiringDate:
}
);
// Update saldo dell'Account
Account.addtoSaldo(accountToId, amount);
Account.addtoSaldo(accountFromId, -amount);
return mymov.save();
};
2022-04-07 08:19:40 +02:00
const Movement = mongoose.model('Movement', MovementSchema);
module.exports = {Movement};