Add Movement !
This commit is contained in:
@@ -14,6 +14,9 @@ mongoose.plugin(schema => {
|
||||
});
|
||||
|
||||
const CircuitSchema = new Schema({
|
||||
_id: {
|
||||
type: String,
|
||||
},
|
||||
Num: {
|
||||
type: Number,
|
||||
unique: true,
|
||||
@@ -60,11 +63,11 @@ const CircuitSchema = new Schema({
|
||||
},
|
||||
symbol: {
|
||||
type: String,
|
||||
maxlength: 3,
|
||||
maxlength: 7,
|
||||
},
|
||||
abbrev: {
|
||||
type: String,
|
||||
maxlength: 3,
|
||||
maxlength: 7,
|
||||
},
|
||||
compara_valuta: {
|
||||
type: Number,
|
||||
@@ -142,11 +145,22 @@ const CircuitSchema = new Schema({
|
||||
}], // username
|
||||
deleted: {
|
||||
type: Boolean,
|
||||
default: false,
|
||||
},
|
||||
});
|
||||
}, );
|
||||
|
||||
CircuitSchema.pre('save', async function(next) {
|
||||
if (this.isNew) {
|
||||
const myrec = await Circuit.findOne().limit(1).sort({_id: -1});
|
||||
if (!!myrec) {
|
||||
if (myrec._doc.Num === 0)
|
||||
this.Num = 1;
|
||||
else
|
||||
this.Num = myrec._doc.Num + 1;
|
||||
|
||||
} else {
|
||||
this.Num = 1;
|
||||
}
|
||||
|
||||
this.date_created = new Date();
|
||||
}
|
||||
@@ -158,9 +172,11 @@ CircuitSchema.pre('save', async function(next) {
|
||||
CircuitSchema.statics.findAllIdApp = async function(idapp) {
|
||||
const Circuit = this;
|
||||
|
||||
const myfind = {idapp};
|
||||
const myfind = {idapp, deleted: false};
|
||||
|
||||
return await Circuit.find(myfind, (err, arrrec) => {
|
||||
const whatToShow = this.getWhatToShow(idapp, '');
|
||||
|
||||
return await Circuit.find(myfind, whatToShow, (err, arrrec) => {
|
||||
return arrrec;
|
||||
});
|
||||
};
|
||||
@@ -347,11 +363,50 @@ CircuitSchema.statics.getInfoCircuitByName = async function(idapp, name) {
|
||||
|
||||
};
|
||||
|
||||
CircuitSchema.statics.getCircuitByName = async function(idapp, name) {
|
||||
|
||||
const myfind = {
|
||||
idapp,
|
||||
name,
|
||||
};
|
||||
|
||||
try {
|
||||
return await Circuit.findOne(myfind).lean();
|
||||
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
CircuitSchema.statics.deleteCircuit = async function(idapp, usernameOrig, name) {
|
||||
console.log('Circuito ' + name + ' rimosso da ' + usernameOrig);
|
||||
return Circuit.findOneAndRemove({idapp, name});
|
||||
};
|
||||
|
||||
CircuitSchema.statics.sendCoins = async function(idapp, usernameOrig, extrarec) {
|
||||
console.log('Aggiungi Monete');
|
||||
|
||||
const {Movement} = require('../models/movement');
|
||||
const {Account} = require('../models/account');
|
||||
|
||||
try {
|
||||
const reccircuit = Circuit.getCircuitByName(extrarec.circuitname);
|
||||
|
||||
const myqty = extrarec.qty;
|
||||
|
||||
const accountdest = await Account.getAccountByUsernameAndCircuitId(idapp, extrarec.dest, reccircuit._id, true);
|
||||
const accountorig = await Account.getAccountByUsernameAndCircuitId(idapp, usernameOrig, reccircuit._id, true);
|
||||
|
||||
// Add a Transaction !
|
||||
Movement.addMov(accountorig, accountdest, myqty, extrarec.causal);
|
||||
|
||||
}catch (e) {
|
||||
console.error('Err sendCoins', e);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// Rimuovo la Richiesta del Circuito
|
||||
CircuitSchema.statics.removeReqCircuit = async function(idapp, username, name) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user