Send Coins
This commit is contained in:
@@ -99,7 +99,6 @@ AccountSchema.statics.executeQueryTable = function(idapp, params) {
|
||||
return tools.executeQueryTable(this, idapp, params);
|
||||
};
|
||||
|
||||
|
||||
AccountSchema.statics.getAccountsByUsername = async function(idapp, username) {
|
||||
const Account = this;
|
||||
|
||||
@@ -114,6 +113,78 @@ AccountSchema.statics.getAccountsByUsername = async function(idapp, username) {
|
||||
return await Account.find(myquery).lean();
|
||||
|
||||
};
|
||||
|
||||
AccountSchema.statics.addtoSaldo = async function(id, amount) {
|
||||
const Account = this;
|
||||
|
||||
if (!id)
|
||||
return false;
|
||||
|
||||
const myaccount = await Account.findById(id);
|
||||
if (myaccount) {
|
||||
myaccount.saldo = myaccount.saldo + amount;
|
||||
myaccount.date_updated = new Date();
|
||||
return await myaccount.save();
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
AccountSchema.statics.getAccountByUsernameAndCircuitId = async function(idapp, username, {circuitId, circuitName}, createifnotexist) {
|
||||
const Account = this;
|
||||
|
||||
try {
|
||||
|
||||
const {Circuit} = require('../models/circuit');
|
||||
|
||||
if (username === undefined)
|
||||
return false;
|
||||
|
||||
let myquery = {
|
||||
'idapp': idapp,
|
||||
'username': username,
|
||||
};
|
||||
|
||||
if (circuitId) {
|
||||
myquery.circuitId = circuitId;
|
||||
}
|
||||
if (circuitName) {
|
||||
myquery.circuitName = circuitName;
|
||||
}
|
||||
|
||||
const mycircuit = await Circuit.getCircuitById(circuitId);
|
||||
|
||||
let myaccount = await Account.findOne(myquery).lean();
|
||||
|
||||
if (!myaccount && createifnotexist) {
|
||||
myaccount = new Account({
|
||||
idapp,
|
||||
username,
|
||||
circuitId: mycircuit._id,
|
||||
deperibile: false,
|
||||
fidoConcesso: mycircuit.fido_scoperto_default,
|
||||
qta_maxConcessa: mycircuit.qta_max_default,
|
||||
importo_iniziale: 0,
|
||||
saldo: 0,
|
||||
});
|
||||
|
||||
return await myaccount.save();
|
||||
}
|
||||
|
||||
return myaccount;
|
||||
|
||||
} catch (e) {
|
||||
console.error('error', e);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
AccountSchema.statics.createAccount = async function(idapp, username, circuitName) {
|
||||
|
||||
return await Account.getAccountByUsernameAndCircuitId(idapp, username, {circuitName}, true);
|
||||
|
||||
};
|
||||
|
||||
AccountSchema.statics.getUserAccounts = async function(idapp, username) {
|
||||
|
||||
try {
|
||||
@@ -129,95 +200,18 @@ AccountSchema.statics.getUserAccounts = async function(idapp, username) {
|
||||
as: 'circuit',
|
||||
},
|
||||
},
|
||||
{
|
||||
'$replaceRoot': {
|
||||
'newRoot': {
|
||||
'$mergeObjects': [
|
||||
{
|
||||
'$arrayElemAt': [
|
||||
'$circuit',
|
||||
0,
|
||||
],
|
||||
},
|
||||
'$$ROOT',
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
/*
|
||||
{
|
||||
$project: {
|
||||
"circuit.name": 1,
|
||||
},
|
||||
},
|
||||
|
||||
*/
|
||||
{$unwind: '$circuit'},
|
||||
];
|
||||
|
||||
ris = await this.aggregate(aggr1);
|
||||
|
||||
return ris;
|
||||
}catch (e) {
|
||||
} catch (e) {
|
||||
console.error('e', e);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
AccountSchema.statics.addtoSaldo = async function(id, amount) {
|
||||
const Account = this;
|
||||
|
||||
if (!id)
|
||||
return false;
|
||||
|
||||
const myaccount = await Account.findById(id);
|
||||
if (myaccount) {
|
||||
myaccount.saldo = myaccount.saldo + amount;
|
||||
myaccount.date_updated = new Date();
|
||||
return await myaccount.save();
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
AccountSchema.statics.getAccountByUsernameAndCircuitId = async function(idapp, username, circuitId, createifnotexist) {
|
||||
const Account = this;
|
||||
|
||||
const {Circuit} = require('../models/circuit');
|
||||
|
||||
if (username === undefined)
|
||||
return false;
|
||||
|
||||
const myquery = {
|
||||
'idapp': idapp,
|
||||
'username': username,
|
||||
circuitId,
|
||||
};
|
||||
|
||||
const mycircuit = await Circuit.getCircuitById(circuitId);
|
||||
|
||||
let myaccount = await Account.findOne(myquery).lean();
|
||||
|
||||
if (!myaccount && createifnotexist) {
|
||||
myaccount = new Account({
|
||||
idapp,
|
||||
username,
|
||||
circuitId,
|
||||
deperibile: false,
|
||||
fidoConcesso: mycircuit.fido_scoperto_default,
|
||||
qta_maxConcessa: mycircuit.qta_max_default,
|
||||
importo_iniziale: 0,
|
||||
saldo: 0,
|
||||
});
|
||||
|
||||
return await myaccount.save();
|
||||
}
|
||||
|
||||
return myaccount;
|
||||
|
||||
};
|
||||
|
||||
|
||||
const Account = mongoose.model('Account', AccountSchema);
|
||||
|
||||
module.exports = {Account};
|
||||
|
||||
@@ -8,6 +8,8 @@ const tools = require('../tools/general');
|
||||
|
||||
const {ObjectID} = require('mongodb');
|
||||
|
||||
const i18n = require('i18n');
|
||||
|
||||
// Resolving error Unknown modifier: $pushAll
|
||||
mongoose.plugin(schema => {
|
||||
schema.options.usePushEach = true;
|
||||
@@ -60,6 +62,10 @@ const CircuitSchema = new Schema({
|
||||
symbol: {
|
||||
type: String,
|
||||
maxlength: 7,
|
||||
unique: true,
|
||||
},
|
||||
color: {
|
||||
type: String,
|
||||
},
|
||||
abbrev: {
|
||||
type: String,
|
||||
@@ -204,7 +210,6 @@ CircuitSchema.statics.executeQueryTable = function(idapp, params) {
|
||||
CircuitSchema.statics.getWhatToShow = function(idapp, username) {
|
||||
// FOR ME, PERMIT ALL
|
||||
return {
|
||||
Num: 1,
|
||||
circuitId: 1,
|
||||
groupnameId: 1,
|
||||
path: 1,
|
||||
@@ -217,7 +222,10 @@ CircuitSchema.statics.getWhatToShow = function(idapp, username) {
|
||||
date_created: 1,
|
||||
date_updated: 1,
|
||||
nome_valuta: 1,
|
||||
fido_scoperto_default: 1,
|
||||
qta_max_default: 1,
|
||||
symbol: 1,
|
||||
color: 1,
|
||||
abbrev: 1,
|
||||
data_costituz: 1,
|
||||
photos: 1,
|
||||
@@ -237,7 +245,6 @@ CircuitSchema.statics.removeAdminOfMyCircuit = async function(idapp, username, n
|
||||
|
||||
CircuitSchema.statics.getWhatToShow_Unknown = function(idapp, username) {
|
||||
return {
|
||||
Num: 1,
|
||||
circuitId: 1,
|
||||
groupnameId: 1,
|
||||
path: 1,
|
||||
@@ -248,7 +255,10 @@ CircuitSchema.statics.getWhatToShow_Unknown = function(idapp, username) {
|
||||
systemUserId: 1,
|
||||
founderUserId: 1,
|
||||
nome_valuta: 1,
|
||||
fido_scoperto_default: 1,
|
||||
qta_max_default: 1,
|
||||
symbol: 1,
|
||||
color: 1,
|
||||
abbrev: 1,
|
||||
data_costituz: 1,
|
||||
photos: 1,
|
||||
@@ -371,7 +381,7 @@ CircuitSchema.statics.getCircuitByName = async function(idapp, name) {
|
||||
CircuitSchema.statics.getCircuitById = async function(circuitId) {
|
||||
|
||||
const myfind = {
|
||||
circuitId
|
||||
circuitId,
|
||||
};
|
||||
|
||||
try {
|
||||
@@ -388,23 +398,100 @@ CircuitSchema.statics.deleteCircuit = async function(idapp, usernameOrig, name)
|
||||
return await Circuit.findOneAndRemove({idapp, name});
|
||||
};
|
||||
|
||||
CircuitSchema.statics.sendCoins = async function(idapp, usernameOrig, extrarec) {
|
||||
console.log('Aggiungi Monete');
|
||||
CircuitSchema.statics.getUserCircuits = async function(idapp, username) {
|
||||
|
||||
try {
|
||||
let aggr1 = [
|
||||
{
|
||||
$match: {idapp, username},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 'circuits',
|
||||
localField: 'circuitId',
|
||||
foreignField: '_id',
|
||||
as: 'circuit',
|
||||
},
|
||||
},
|
||||
{
|
||||
'$replaceRoot': {
|
||||
'newRoot': {
|
||||
'$mergeObjects': [
|
||||
{
|
||||
'$arrayElemAt': [
|
||||
'$circuit',
|
||||
0,
|
||||
],
|
||||
},
|
||||
'$$ROOT',
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
/*
|
||||
{
|
||||
$project: {
|
||||
"circuit.name": 1,
|
||||
},
|
||||
},
|
||||
|
||||
*/
|
||||
];
|
||||
|
||||
ris = await this.aggregate(aggr1);
|
||||
|
||||
return ris;
|
||||
} catch (e) {
|
||||
console.error('e', e);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
CircuitSchema.statics.sendCoins = async function(onlycheck, idapp, usernameOrig, extrarec) {
|
||||
|
||||
const {Movement} = require('../models/movement');
|
||||
const {Account} = require('../models/account');
|
||||
|
||||
let ris = {
|
||||
cansend: true,
|
||||
errormsg: '',
|
||||
};
|
||||
|
||||
try {
|
||||
const reccircuit = await Circuit.getCircuitByName(idapp, extrarec.circuitname);
|
||||
let reccircuit = null;
|
||||
if (extrarec.circuitname)
|
||||
reccircuit = await Circuit.getCircuitByName(idapp, extrarec.circuitname);
|
||||
if (extrarec.circuitId)
|
||||
reccircuit = await Circuit.getCircuitById(idapp, extrarec.circuitId);
|
||||
|
||||
if (reccircuit) {
|
||||
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);
|
||||
const accountdest = await Account.getAccountByUsernameAndCircuitId(idapp, extrarec.dest, { circuitId: reccircuit._id }, true);
|
||||
const accountorig = await Account.getAccountByUsernameAndCircuitId(idapp, usernameOrig, { circuitId: reccircuit._id }, true);
|
||||
|
||||
// Add a Transaction !
|
||||
return await Movement.addMov(idapp, accountorig, accountdest, myqty, extrarec.causal);
|
||||
// Check if Sender has enough money
|
||||
if (accountorig.saldo - myqty < accountorig.fidoConcesso) {
|
||||
ris.cansend = false;
|
||||
ris.errormsg = i18n.__('CIRCUIT_AMOUNT_EXCEED_FIDO');
|
||||
}
|
||||
if (accountdest.saldo + myqty > accountorig.qta_maxConcessa) {
|
||||
ris.cansend = false;
|
||||
ris.errormsg = i18n.__('CIRCUIT_AMOUNT_EXCEED_QTAMAX');
|
||||
}
|
||||
|
||||
if (!onlycheck) {
|
||||
// Add a Transaction !
|
||||
ris.cansend = await Movement.addMov(idapp, accountorig, accountdest, myqty, extrarec.causal);
|
||||
|
||||
if (ris.cansend) {
|
||||
console.log('Invia Monete da', usernameOrig, extrarec.dest, extrarec.qty, extrarec.causal);
|
||||
} else {
|
||||
console.log('NON Inviate Monete da', usernameOrig, extrarec.dest, extrarec.qty, extrarec.causal);
|
||||
}
|
||||
}
|
||||
|
||||
return ris;
|
||||
|
||||
}
|
||||
} catch (e) {
|
||||
|
||||
@@ -118,11 +118,14 @@ MovementSchema.statics.addMov = async function(idapp, accountFromId, accountToId
|
||||
|
||||
MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username, circuitId) {
|
||||
|
||||
const myaccount = await Account.getAccountByUsernameAndCircuitId(idapp, username, circuitId, false);
|
||||
try {
|
||||
if (!circuitId) {
|
||||
return [];
|
||||
}
|
||||
const myaccount = await Account.getAccountByUsernameAndCircuitId(idapp, username, {circuitId}, false);
|
||||
|
||||
if (myaccount) {
|
||||
if (myaccount) {
|
||||
|
||||
try {
|
||||
let aggr1 = [
|
||||
{
|
||||
$match: {
|
||||
@@ -173,6 +176,28 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username,
|
||||
},
|
||||
},
|
||||
{$unwind: '$accto'},
|
||||
{
|
||||
"$lookup": {
|
||||
"from": "circuits",
|
||||
"localField": "accfrom.circuitId",
|
||||
"foreignField": "_id",
|
||||
"as": "circuitfrom"
|
||||
}
|
||||
},
|
||||
{
|
||||
"$unwind": "$circuitfrom"
|
||||
},
|
||||
{
|
||||
"$lookup": {
|
||||
"from": "circuits",
|
||||
"localField": "accto.circuitId",
|
||||
"foreignField": "_id",
|
||||
"as": "circuitto"
|
||||
}
|
||||
},
|
||||
{
|
||||
"$unwind": "$circuitto"
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 'users',
|
||||
@@ -202,6 +227,8 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username,
|
||||
transactionDate: 1,
|
||||
amount: 1,
|
||||
causal: 1,
|
||||
'circuitfrom.symbol': 1,
|
||||
'circuitto.symbol': 1,
|
||||
'userfrom.username': 1,
|
||||
'userfrom.profile.img': 1,
|
||||
'userto.username': 1,
|
||||
@@ -212,10 +239,10 @@ MovementSchema.statics.getQueryMovsByCircuitId = async function(idapp, username,
|
||||
];
|
||||
|
||||
return aggr1;
|
||||
} catch (e) {
|
||||
return [];
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [];
|
||||
@@ -226,7 +253,7 @@ MovementSchema.statics.getMovsByCircuitId = async function(idapp, username, circ
|
||||
|
||||
const myquery = await MyMovement.getQueryMovsByCircuitId(idapp, username, circuitId);
|
||||
|
||||
if (myquery) {
|
||||
if (myquery && myquery.length > 0) {
|
||||
ris = await MyMovement.aggregate(myquery);
|
||||
|
||||
return ris;
|
||||
|
||||
@@ -279,7 +279,11 @@ sendNotifSchema.statics.getDescrAndLinkByRecNotif = function(recnotif) {
|
||||
|
||||
tag = 'remcircuit';
|
||||
} else if (recnotif.typeid === shared_consts.TypeNotifs.ID_CIRCUIT_REFUSED) {
|
||||
newdescr = i18n.__('CIRCUIT_REFUSED', userorig, recnotif.paramsObj.circuitnameDest, recnotif.paramsObj.username_action);
|
||||
if (recnotif.paramsObj.isAdmin) {
|
||||
newdescr = i18n.__('CIRCUIT_REFUSED_TO_ME', userorig, recnotif.paramsObj.circuitnameDest, recnotif.paramsObj.username_action);
|
||||
} else {
|
||||
newdescr = i18n.__('CIRCUIT_REFUSED', recnotif.paramsObj.username_action, recnotif.paramsObj.circuitnameDest);
|
||||
}
|
||||
tag = 'refcircuit';
|
||||
} else if (recnotif.typeid === shared_consts.TypeNotifs.ID_CIRCUIT_REQUEST_TO_ENTER) {
|
||||
newdescr = i18n.__('CIRCUIT_REQUEST_TO_ENTER', userorig, recnotif.paramsObj.circuitnameDest, recnotif.paramsObj.singleadmin_username);
|
||||
|
||||
@@ -2134,6 +2134,8 @@ UserSchema.statics.setCircuitCmd = async function(idapp, usernameOrig, circuitna
|
||||
},
|
||||
};
|
||||
ris = await Circuit.updateOne({idapp, name: circuitname}, update);
|
||||
|
||||
await Account.createAccount(idapp, usernameOrig, circuitname);
|
||||
}
|
||||
if (ris) {
|
||||
// Invia una notifica alla persona
|
||||
@@ -2178,10 +2180,15 @@ UserSchema.statics.setCircuitCmd = async function(idapp, usernameOrig, circuitna
|
||||
|
||||
} else if (cmd === shared_consts.CIRCUITCMD.REFUSE_REQ) {
|
||||
|
||||
ris = await this.removeFromCircuits(idapp, usernameOrig, circuitname); // Rimuovo l'Amicizia da me
|
||||
|
||||
ris = await Circuit.removeReqCircuit(idapp, usernameOrig, circuitname); // Rimuovo l'Amicizia da me
|
||||
|
||||
ris = await Circuit.refuseReqCircuit(idapp, usernameOrig, circuitname); // Rimuovo l'Amicizia da me
|
||||
|
||||
// Invia una notifica alla persona
|
||||
await tools.sendNotificationByCircuit(idapp, usernameOrig, circuitname, cmd, false, true, username_action, extrarec);
|
||||
|
||||
} else if (cmd === shared_consts.CIRCUITCMD.ADDADMIN) {
|
||||
ris = await Circuit.addToAdminOfCircuit(idapp, usernameOrig, circuitname); // Rimuovo la richiesta di entrare nel gruppo
|
||||
|
||||
@@ -2195,14 +2202,22 @@ w
|
||||
await tools.sendNotificationByCircuit(idapp, usernameOrig, circuitname, cmd, false, true, username_action, extrarec);
|
||||
|
||||
} else if (cmd === shared_consts.CIRCUITCMD.SENDCOINS_REQ) {
|
||||
// Invia una notifica di moneta alla persona
|
||||
ris = await tools.sendNotificationByCircuit(idapp, usernameOrig, circuitname, cmd, false, true, username_action, extrarec);
|
||||
|
||||
let ris = await Circuit.sendCoins(true, idapp, usernameOrig, extrarec);
|
||||
if (ris.cansend) {
|
||||
// Invia una notifica di moneta alla persona
|
||||
await tools.sendNotificationByCircuit(idapp, usernameOrig, circuitname, cmd, false, true, username_action, extrarec);
|
||||
} else {
|
||||
ris.cansend = false;
|
||||
}
|
||||
|
||||
} else if (cmd === shared_consts.CIRCUITCMD.SENDCOINS_ACCEPT) {
|
||||
ris = await Circuit.sendCoins(idapp, usernameOrig, extrarec);
|
||||
ris = await Circuit.sendCoins(false, idapp, usernameOrig, extrarec);
|
||||
|
||||
// Invia una notifica di moneta alla persona
|
||||
await tools.sendNotificationByCircuit(idapp, usernameOrig, circuitname, cmd, false, true, username_action, extrarec);
|
||||
if (ris.cansend) {
|
||||
// Invia una notifica di moneta alla persona
|
||||
await tools.sendNotificationByCircuit(idapp, usernameOrig, circuitname, cmd, false, true, username_action, extrarec);
|
||||
}
|
||||
|
||||
}
|
||||
} catch (e) {
|
||||
|
||||
Reference in New Issue
Block a user