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