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};
|
||||
|
||||
Reference in New Issue
Block a user