fixed double send coins... if user clicked twice.
This commit is contained in:
@@ -38,7 +38,7 @@
|
||||
"jsonwebtoken": "^8.5.1",
|
||||
"lodash": "^4.17.21",
|
||||
"mongodb": "^4.4.1",
|
||||
"mongoose": "^5.10.19",
|
||||
"mongoose": "^5.13.15",
|
||||
"mysql": "^2.18.1",
|
||||
"node-cron": "^3.0.2",
|
||||
"node-emoji": "^1.11.0",
|
||||
|
||||
@@ -12,6 +12,7 @@ const tools = require('../tools/general');
|
||||
|
||||
const {ObjectID} = require('mongodb');
|
||||
|
||||
|
||||
const shared_consts = require('../tools/shared_nodejs');
|
||||
|
||||
// Resolving error Unknown modifier: $pushAll
|
||||
@@ -56,6 +57,9 @@ const AccountSchema = new Schema({
|
||||
totTransato: {
|
||||
type: Number,
|
||||
},
|
||||
regulation_ok: {
|
||||
type: Boolean,
|
||||
},
|
||||
deleted: {
|
||||
type: Boolean,
|
||||
default: false,
|
||||
@@ -134,7 +138,7 @@ AccountSchema.statics.calcTotCircolante = async function(idapp, circuitId) {
|
||||
}
|
||||
|
||||
};
|
||||
AccountSchema.methods.addtoSaldo = async function(amount) {
|
||||
AccountSchema.methods.addtoSaldoSave = async function(amount) {
|
||||
const account = this;
|
||||
|
||||
if (account) {
|
||||
@@ -150,6 +154,36 @@ AccountSchema.methods.addtoSaldo = async function(amount) {
|
||||
return null;
|
||||
};
|
||||
|
||||
AccountSchema.statics.addtoSaldo = async function(myaccount, amount) {
|
||||
const Account = this;
|
||||
|
||||
try {
|
||||
let myaccountupdate = {};
|
||||
|
||||
if (myaccount) {
|
||||
myaccount.saldo = myaccount.saldo + amount;
|
||||
if (!myaccount.totTransato) {
|
||||
myaccount.totTransato = 0;
|
||||
}
|
||||
myaccount.totTransato += Math.abs(amount);
|
||||
myaccount.date_updated = new Date();
|
||||
|
||||
myaccountupdate.saldo = myaccount.saldo;
|
||||
myaccountupdate.totTransato = myaccount.totTransato;
|
||||
myaccountupdate.date_updated = myaccount.date_updated;
|
||||
|
||||
return await Account.updateOne({_id: myaccount.id},
|
||||
{
|
||||
$set: myaccountupdate
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('error', e);
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
AccountSchema.pre('save', async function(next) {
|
||||
if (this.isNew) {
|
||||
this.date_created = new Date();
|
||||
|
||||
@@ -8,6 +8,9 @@ const tools = require('../tools/general');
|
||||
|
||||
const {ObjectID} = require('mongodb');
|
||||
|
||||
const {Movement} = require('../models/movement');
|
||||
const {Account} = require('../models/account');
|
||||
|
||||
const i18n = require('i18n');
|
||||
|
||||
// Resolving error Unknown modifier: $pushAll
|
||||
@@ -507,8 +510,6 @@ CircuitSchema.statics.getCircolanteSingolaTransaz = function(accountorigTable, a
|
||||
|
||||
CircuitSchema.statics.sendCoins = async function(onlycheck, idapp, usernameOrig, extrarec) {
|
||||
|
||||
const {Movement} = require('../models/movement');
|
||||
const {Account} = require('../models/account');
|
||||
|
||||
let ris = {
|
||||
result: false,
|
||||
@@ -547,7 +548,7 @@ CircuitSchema.statics.sendCoins = async function(onlycheck, idapp, usernameOrig,
|
||||
if (!onlycheck) {
|
||||
// Add a Transaction !
|
||||
if (ris.cansend) {
|
||||
ris.rec = await Movement.addMov(idapp, accountorigTable, accountdestTable, myqty, extrarec.causal);
|
||||
ris.rec = await Movement.addMov(idapp, accountorigTable, accountdestTable, myqty, extrarec.causal, extrarec.notifId);
|
||||
}
|
||||
|
||||
if (ris.cansend && ris.rec) {
|
||||
@@ -558,7 +559,12 @@ CircuitSchema.statics.sendCoins = async function(onlycheck, idapp, usernameOrig,
|
||||
circuittable.totTransato += myqty;
|
||||
// circuittable.totCircolante = circuittable.totCircolante + (circolanteAtt - circolantePrec);
|
||||
circuittable.totCircolante = await Account.calcTotCircolante(idapp, circuittable._id);
|
||||
await circuittable.save();
|
||||
// await circuittable.save();
|
||||
paramstoupdate = {
|
||||
totTransato: circuittable.totTransato,
|
||||
totCircolante: circuittable.totCircolante,
|
||||
};
|
||||
await Circuit.updateOne({_id: circuittable}, { $set: paramstoupdate } );
|
||||
|
||||
ris.result = true;
|
||||
console.log('Inviate Monete da', usernameOrig, extrarec.dest, myqty, extrarec.causal);
|
||||
|
||||
@@ -24,6 +24,9 @@ const MovementSchema = new Schema({
|
||||
idapp: {
|
||||
type: String,
|
||||
},
|
||||
notifId: {
|
||||
type: String,
|
||||
},
|
||||
transactionDate: {
|
||||
type: Date,
|
||||
},
|
||||
@@ -85,13 +88,13 @@ MovementSchema.statics.executeQueryTable = function(idapp, params) {
|
||||
return tools.executeQueryTable(this, 0, params);
|
||||
};
|
||||
|
||||
MovementSchema.statics.addMov = async function(idapp, accountFromIdTable, accountToIdTable, amount, causal) {
|
||||
MovementSchema.statics.addMov = async function(idapp, accountFromIdTable, accountToIdTable, amount, causal, notifId) {
|
||||
|
||||
try {
|
||||
// Only positive values
|
||||
amount = Math.abs(amount);
|
||||
|
||||
let mymov = Movement(
|
||||
let mymov = await Movement.create(
|
||||
{
|
||||
_id: new ObjectID().toString(),
|
||||
idapp,
|
||||
@@ -101,18 +104,18 @@ MovementSchema.statics.addMov = async function(idapp, accountFromIdTable, accoun
|
||||
amount,
|
||||
causal,
|
||||
residual: 0,
|
||||
notifId,
|
||||
// expiringDate:
|
||||
},
|
||||
);
|
||||
|
||||
const rismov = await mymov.save();
|
||||
if (rismov) {
|
||||
if (mymov) {
|
||||
// Update saldo dell'Account
|
||||
await accountToIdTable.addtoSaldo(amount);
|
||||
await Account.addtoSaldo(accountToIdTable, amount);
|
||||
|
||||
await accountFromIdTable.addtoSaldo(-amount);
|
||||
await Account.addtoSaldo(accountFromIdTable, amount);
|
||||
|
||||
return rismov;
|
||||
return mymov;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Error in addMov', e.message);
|
||||
@@ -230,6 +233,7 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username,
|
||||
transactionDate: 1,
|
||||
amount: 1,
|
||||
causal: 1,
|
||||
notifId: 1,
|
||||
'circuitfrom.symbol': 1,
|
||||
'circuitto.symbol': 1,
|
||||
'userfrom.username': 1,
|
||||
@@ -376,6 +380,7 @@ MovementSchema.statics.getQueryAllUsersMovsByCircuitId = async function(idapp, c
|
||||
transactionDate: 1,
|
||||
amount: 1,
|
||||
causal: 1,
|
||||
notifId: 1,
|
||||
'circuitfrom.symbol': 1,
|
||||
'circuitto.symbol': 1,
|
||||
'userfrom.username': 1,
|
||||
@@ -410,6 +415,22 @@ MovementSchema.statics.getMovsByCircuitId = async function(idapp, username, circ
|
||||
return [];
|
||||
};
|
||||
|
||||
MovementSchema.statics.checkIfCoinsAlreadySent = async function(notifId) {
|
||||
const MyMovement = this;
|
||||
|
||||
try {
|
||||
const rec = await MyMovement.findOne({notifId}, {_id: 1});
|
||||
|
||||
return !!rec;
|
||||
|
||||
} catch (e) {
|
||||
// If Error, don't send the coins
|
||||
return true;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
const Movement = mongoose.model('Movement', MovementSchema);
|
||||
|
||||
module.exports = {Movement};
|
||||
|
||||
@@ -521,10 +521,12 @@ sendNotifSchema.statics.updateStatusAndDescr = async function(myrecnotif, onlysa
|
||||
typeidsearch = shared_consts.TypeNotifs.ID_CIRCUIT_SENDCOINSREQ;
|
||||
newstatus = shared_consts.CircuitsNotif.STATUS_COINS_REFUSED;
|
||||
} else if (myrecnotif.typeid === shared_consts.TypeNotifs.ID_CIRCUIT_COINS_ACCEPTED_SENT) {
|
||||
typeidsearch = shared_consts.TypeNotifs.ID_CIRCUIT_SENDCOINSREQ_SENT;
|
||||
if (myrecnotif.paramsObj.extrarec.hasOwnProperty('notifIdToUpdate'))
|
||||
idnotiftosearch = myrecnotif.paramsObj.extrarec.notifIdToUpdate;
|
||||
newstatus = shared_consts.CircuitsNotif.STATUS_COINS_ACCEPTED_SENT;
|
||||
} else if (myrecnotif.typeid === shared_consts.TypeNotifs.ID_CIRCUIT_COINS_REFUSED_SENT) {
|
||||
typeidsearch = shared_consts.TypeNotifs.ID_CIRCUIT_SENDCOINSREQ_SENT;
|
||||
if (myrecnotif.paramsObj.extrarec.hasOwnProperty('notifIdToUpdate'))
|
||||
idnotiftosearch = myrecnotif.paramsObj.extrarec.notifIdToUpdate;
|
||||
newstatus = shared_consts.CircuitsNotif.STATUS_COINS_REFUSED_SENT;
|
||||
@@ -598,8 +600,8 @@ sendNotifSchema.statics.checkIfCoinsAlreadySent = async function(notifId) {
|
||||
const status = await SendNotif.getStatus(notifId);
|
||||
|
||||
if (status !== null) {
|
||||
return (status === shared_consts.CircuitsNotif.STATUS_ACCEPTED) ||
|
||||
(status === shared_consts.CircuitsNotif.STATUS_REFUSED);
|
||||
return (status === shared_consts.CircuitsNotif.STATUS_COINS_ACCEPTED) ||
|
||||
(status === shared_consts.CircuitsNotif.STATUS_COINS_REFUSED);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
@@ -17,6 +17,7 @@ const {MyGroup} = require('../models/mygroup');
|
||||
const {Circuit} = require('../models/circuit');
|
||||
|
||||
const {Account} = require('../models/account');
|
||||
const {Movement} = require('../models/movement');
|
||||
|
||||
const {ObjectID} = require('mongodb');
|
||||
|
||||
@@ -2131,6 +2132,8 @@ UserSchema.statics.setGroupsCmd = async function(idapp, usernameOrig, groupnameD
|
||||
|
||||
UserSchema.statics.setCircuitCmd = async function(idapp, usernameOrig, circuitname, cmd, value, username_action, extrarec) {
|
||||
|
||||
// console.log('setCircuitCmd', cmd);
|
||||
|
||||
const {SendNotif} = require('../models/sendnotif');
|
||||
|
||||
let ris = null;
|
||||
@@ -2262,8 +2265,10 @@ UserSchema.statics.setCircuitCmd = async function(idapp, usernameOrig, circuitna
|
||||
|
||||
if (outres.cansend) {
|
||||
// Invia una notifica di moneta alla persona
|
||||
outres.result = await tools.sendNotificationByCircuit(idapp, usernameOrig, circuitname, cmd, false, true, username_action,
|
||||
const out = await tools.sendNotificationByCircuit(idapp, usernameOrig, circuitname, cmd, false, true, username_action,
|
||||
extrarec);
|
||||
if (out)
|
||||
outres.result = out.ris;
|
||||
} else {
|
||||
outres.cansend = false;
|
||||
}
|
||||
@@ -2277,6 +2282,8 @@ UserSchema.statics.setCircuitCmd = async function(idapp, usernameOrig, circuitna
|
||||
errormsg: '',
|
||||
};
|
||||
|
||||
let outcheck = outres;
|
||||
|
||||
let risStatus = '';
|
||||
const status = await SendNotif.getStatus(extrarec.notifId);
|
||||
if (status === shared_consts.CircuitsNotif.STATUS_ACCEPTED) {
|
||||
@@ -2284,15 +2291,27 @@ UserSchema.statics.setCircuitCmd = async function(idapp, usernameOrig, circuitna
|
||||
} else if (status === shared_consts.CircuitsNotif.STATUS_REFUSED) {
|
||||
risStatus = i18n.__('STATUS_REFUSED');
|
||||
}
|
||||
if (!await SendNotif.checkIfCoinsAlreadySent(extrarec.notifId)) {
|
||||
// if (!await SendNotif.checkIfCoinsAlreadySent(extrarec.notifId)) {
|
||||
if (!await Movement.checkIfCoinsAlreadySent(extrarec.notifId)) {
|
||||
if (cmd === shared_consts.CIRCUITCMD.SENDCOINS_ACCEPT) {
|
||||
outres = await Circuit.sendCoins(false, idapp, usernameOrig, extrarec);
|
||||
outcheck = await Circuit.sendCoins(true, idapp, usernameOrig, extrarec);
|
||||
} else {
|
||||
outcheck.cansend = true;
|
||||
outres.cansend = true;
|
||||
}
|
||||
|
||||
// Invia una notifica di moneta (accettata o rifiutata) alla persona
|
||||
await tools.sendNotificationByCircuit(idapp, usernameOrig, circuitname, cmd, false, true, username_action, extrarec);
|
||||
if (cmd === shared_consts.CIRCUITCMD.SENDCOINS_ACCEPT && outcheck.cansend) {
|
||||
if (!await Movement.checkIfCoinsAlreadySent(extrarec.notifId)) {
|
||||
outres = await Circuit.sendCoins(false, idapp, usernameOrig, extrarec);
|
||||
} else {
|
||||
outcheck.cansend = false; //GIA INVIATO
|
||||
}
|
||||
}
|
||||
|
||||
if (outcheck.cansend) {
|
||||
// Invia una notifica di moneta (accettata o rifiutata) alla persona
|
||||
const out = await tools.sendNotificationByCircuit(idapp, usernameOrig, circuitname, cmd, false, true, username_action, extrarec);
|
||||
}
|
||||
|
||||
outres.recnotif = await SendNotif.getRecNotif(extrarec.notifId);
|
||||
|
||||
|
||||
@@ -1138,7 +1138,6 @@ module.exports = {
|
||||
sendNotificationByCircuit: async function(idapp, usernameOrig, circuitname, cmd, value, telegram, username_action, extrarec) {
|
||||
|
||||
const {Circuit} = require('../models/circuit');
|
||||
const {User} = require('../models/user');
|
||||
|
||||
const circuit = await Circuit.findOne({idapp, name: circuitname}, {_id: 1, admins: 1, createdBy: 1, path: 1}).lean();
|
||||
if (!circuit)
|
||||
@@ -1208,11 +1207,11 @@ module.exports = {
|
||||
}
|
||||
}
|
||||
|
||||
return inviato;
|
||||
return {ris, inviato};
|
||||
|
||||
} catch (e) {
|
||||
console.error('sendNotificationByCircuit: ', e);
|
||||
return false;
|
||||
return {ris: null, inviato: false} ;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
@@ -6984,7 +6984,7 @@ mongoose-legacy-pluralize@1.0.2:
|
||||
resolved "https://registry.yarnpkg.com/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.2.tgz#3ba9f91fa507b5186d399fb40854bff18fb563e4"
|
||||
integrity sha512-Yo/7qQU4/EyIS8YDFSeenIvXxZN+ld7YdV9LqFVQJzTLye8unujAWPZ4NWKfFA+RNjh+wvTWKY9Z3E5XM6ZZiQ==
|
||||
|
||||
mongoose@^5.10.19:
|
||||
mongoose@^5.13.15:
|
||||
version "5.13.15"
|
||||
resolved "https://registry.yarnpkg.com/mongoose/-/mongoose-5.13.15.tgz#ba2cd0f22c1a5dd9ae15aaf6b10f03c59a4202dd"
|
||||
integrity sha512-cxp1Gbb8yUWkaEbajdhspSaKzAvsIvOtRlYD87GN/P2QEUhpd6bIvebi36T6M0tIVAMauNaK9SPA055N3PwF8Q==
|
||||
|
||||
Reference in New Issue
Block a user