Versione 1.0.17

- Fix saldo pendente
- aggiunto bottone markup Telegram
- aggionta opzione per registrarsi direttamente su Telegram, senza doversi registrare ad un sito
This commit is contained in:
Surya Paolo
2023-10-21 15:27:53 +02:00
parent 536fbd1752
commit c1a9a9a555
17 changed files with 559 additions and 76 deletions

View File

@@ -66,6 +66,14 @@ const AccountSchema = new Schema({
type: Number,
default: 0,
},
saldo_pend: {
type: Number,
default: 0,
},
totTransato_pend: {
type: Number,
default: 0,
},
regulation_ok: {
type: Boolean,
},
@@ -150,23 +158,69 @@ AccountSchema.statics.calcTotCircolante = async function (idapp, circuitId) {
}
};
AccountSchema.methods.addtoSaldoSave = async function (amount) {
const account = this;
AccountSchema.methods.calcPending = async function (mittente) {
try {
const account = this;
if (account) {
account.saldo = account.saldo + amount;
if (!account.totTransato) {
account.totTransato = 0;
const { SendNotif } = require('../models/sendnotif');
const { Circuit } = require('../models/circuit');
// *** Calc Pending Transactions ***
const circuit = await Circuit.getCircuitById(account.circuitId);
if (circuit) {
let prec_saldo_pend = account.saldo_pend;
let prec_totTransato_pend = account.totTransato_pend;
let transatopending = 0;
if (mittente) {
let pendingtransactionsMittente = await SendNotif.getSumPendingTransactionsMittente(account.idapp, account.username, circuit.name, account.groupname);
let saldopendingMitt = pendingtransactionsMittente.reduce((sum, rec) => sum + rec.extrarec.qty, 0);
transatopending = pendingtransactionsMittente.reduce((sum, rec) => sum + Math.abs(rec.extrarec.qty), 0);
account.saldo_pend = account.saldo - saldopendingMitt;
} else {
// let pendingtransactionsDest = await SendNotif.getSumPendingTransactionsMittente(account.idapp, account.username, circuit.name, account.groupname);
// let saldopendingDest = pendingtransactionsDest.reduce((sum, rec) => sum + rec.extrarec.qty, 0);
// transatopending = pendingtransactionsDest.reduce((sum, rec) => sum + Math.abs(rec.extrarec.qty), 0);
// account.saldo_pend = account.saldo + saldopendingDest;
account.saldo_pend = account.saldo;
}
account.totTransato_pend = account.totTransato + transatopending;
if (prec_saldo_pend !== account.saldo_pend || prec_totTransato_pend !== account.totTransato_pend) {
const myaccountupdate = {
saldo_pend: account.saldo_pend,
totTransato_pend: account.totTransato_pend,
};
// Update Record
return await Account.findByIdAndUpdate(account.id,
{
$set: myaccountupdate
}).then((ris) => {
console.log('calcPending', ris);
}).catch((err) => {
console.error('calcPending', err);
});
}
}
account.totTransato += Math.abs(amount);
account.date_updated = new Date();
return await account.save();
// -----
} catch (e) {
console.error(e);
}
return null;
};
}
AccountSchema.statics.addtoSaldo = async function (myaccount, amount) {
AccountSchema.statics.addtoSaldo = async function (myaccount, amount, mitt) {
const Account = this;
try {
@@ -184,10 +238,16 @@ AccountSchema.statics.addtoSaldo = async function (myaccount, amount) {
myaccountupdate.totTransato = myaccount.totTransato;
myaccountupdate.date_updated = myaccount.date_updated;
return await Account.updateOne({ _id: myaccount.id },
const ris = await Account.updateOne({ _id: myaccount.id },
{
$set: myaccountupdate
});
await myaccount.calcPending(mitt);
return ris;
}
} catch (e) {
console.error('error', e);
@@ -244,8 +304,11 @@ AccountSchema.statics.getAccountByUsernameAndCircuitId = async function (idapp,
deperibile: false,
importo_iniziale: 0,
saldo: 0,
saldo_pend: 0,
fidoConcesso: 0,
qta_maxConcessa: 0,
totTransato: 0,
totTransato_pend: 0,
});
if (contocom) {
@@ -372,11 +435,11 @@ AccountSchema.statics.getUserAccounts = async function (idapp, username) {
$match: {
$expr: {
$and: [
{ $eq: ['$idapp', '$$idapp'] },
{ $eq: ['$typedir', '$$typedir'] },
{ $eq: ['$typeid', '$$typeid'] },
{ $eq: ['$status', 0] },
{ $eq: ['$sender', '$$username'] },
{ $eq: ['$idapp', '$$idapp'] },
{ $eq: ['$extrarec.circuitname', '$$circuitname'] },
],
},
@@ -397,7 +460,7 @@ AccountSchema.statics.getUserAccounts = async function (idapp, username) {
// console.log('1 - INIZIA getUserAccounts ')
// console.log(aggr1);
if (ris) {
/* if (ris) {
for (const account of ris) {
try {
//++OTTIMIZZARE QUESTA PARTE ! (CON UNA QUERY...)
@@ -416,9 +479,8 @@ AccountSchema.statics.getUserAccounts = async function (idapp, username) {
}
}
}
} */
// console.log(' 2 - FINE getUserAccounts LEN=', ris.length)
return ris;
} catch (e) {
console.error('getUserAccounts', e);
@@ -482,20 +544,6 @@ AccountSchema.statics.getGroupAccounts = async function (idapp, groupname) {
const ris = await this.aggregate(aggr1);
const { SendNotif } = require('../models/sendnotif');
if (ris) {
// tools.startTimeLog('Query Pending')
for (const account of ris) {
const pendingtransactions = await SendNotif.getSumPendingTransactions(idapp, '', account.circuit.name, groupname);
const saldopending = pendingtransactions.reduce((sum, rec) => sum + rec.result.extrarec.qty, 0);
if (saldopending) {
account.saldo -= saldopending;
}
}
// tools.endTimeLog('Query Pending')
}
return ris;
} catch (e) {
console.error('e', e);
@@ -559,12 +607,16 @@ AccountSchema.statics.SetMinMaxPersonali = async function (idapp, valmin, valmax
};
AccountSchema.statics.updateFido = async function (idapp, username, circuitId, fido) {
AccountSchema.statics.updateFido = async function (idapp, username, groupname, circuitId, fido) {
let paramstoupdate = {
fidoConcesso: fido,
};
const risult = await Account.updateOne({ idapp, username, circuitId }, { $set: paramstoupdate });
let risult = null;
if (groupname)
risult = await Account.updateOne({ idapp, circuitId, groupname }, { $set: paramstoupdate });
else
risult = await Account.updateOne({ idapp, username, circuitId }, { $set: paramstoupdate });
return risult;
};
@@ -606,6 +658,15 @@ AccountSchema.statics.removeAdminOfAccount = async function (idapp, username, ci
{ $pull: { people: { username: { $in: [person_username] } } } });
};
AccountSchema.statics.updateSaldoAndTransato_AllAccounts = async function (idapp) {
const recaccounts = await Account.find({ idapp });
for (const account of recaccounts) {
await account.calcPending();
}
};
const Account = mongoose.model('Account', AccountSchema);

View File

@@ -1101,7 +1101,7 @@ CircuitSchema.statics.setFido = async function (idapp, username, circuitName, gr
else
fido = mycircuit.fido_scoperto_default;
return await Account.updateFido(idapp, username, circuitId, fido);
return await Account.updateFido(idapp, username, groupname, circuitId, fido);
}
}
return false;

View File

@@ -113,9 +113,9 @@ MovementSchema.statics.addMov = async function (idapp, accountFromIdTable, accou
if (mymov) {
// Update saldo dell'Account
await Account.addtoSaldo(accountToIdTable, amount);
await Account.addtoSaldo(accountToIdTable, amount, true);
await Account.addtoSaldo(accountFromIdTable, -amount);
await Account.addtoSaldo(accountFromIdTable, -amount, false);
return mymov;
}
@@ -681,7 +681,6 @@ MovementSchema.statics.checkIfCoinsAlreadySent = async function (notifId) {
try {
const rec = await MyMovement.findOne({ notifId }, { _id: 1 });
return !!rec;
} catch (e) {

View File

@@ -143,6 +143,7 @@ const MyElemSchema = new Schema({
anim: animation,
active: {
type: Boolean,
default: false,
},
class: {
type: String,

View File

@@ -22,18 +22,22 @@ mongoose.plugin(schema => {
const sendNotifSchema = new Schema({
idapp: {
type: String,
index: true,
},
typedir: {
type: Number,
default: 0,
index: true,
},
typeid: {
type: Number,
default: 0,
index: true,
},
sender: { // mittente
type: String,
default: '',
index: true,
},
dest: {
type: String,
@@ -66,6 +70,7 @@ const sendNotifSchema = new Schema({
status: {
type: Number,
default: 0,
index: true,
},
typesend: {
type: Number,
@@ -107,6 +112,14 @@ const sendNotifSchema = new Schema({
}
});
sendNotifSchema.index({ idapp: 1 });
sendNotifSchema.index({ typedir: 1 });
sendNotifSchema.index({ typeid: 1 });
sendNotifSchema.index({ sender: 1 });
sendNotifSchema.index({ idapp: 1, typedir: 1, typeid: 1, status: 1, sender: 1 });
sendNotifSchema.statics.setNotifAsRead = function (idapp, username, idnotif) {
const SendNotif = this;
@@ -368,7 +381,7 @@ sendNotifSchema.statics.getDescrAndLinkByRecNotif = async function (recnotif, us
} else if (recnotif.typeid === shared_consts.TypeNotifs.ID_CIRCUIT_SETFIDO) {
if (recnotif.paramsObj.isAdmin) {
if (recnotif.extrarec.groupname) {
newdescr = i18n.__('FIDO_IMPOSTATO_ADMINS_CIRCUIT_MYGROUP', -recnotif.paramsObj.extrarec.fido_scoperto_default_grp, recnotif.extrarec.groupname, recnotif.paramsObj.circuitnameDest,
newdescr = i18n.__('FIDO_IMPOSTATO_ADMINS_CIRCUIT_MYGROUP', recnotif.extrarec.groupname, -recnotif.paramsObj.extrarec.fido_scoperto_default_grp, recnotif.paramsObj.circuitnameDest,
username_action);
} else {
newdescr = i18n.__('FIDO_IMPOSTATO_ADMINS_CIRCUIT', sender, -recnotif.paramsObj.extrarec.fido_scoperto_default, recnotif.paramsObj.circuitnameDest,
@@ -622,6 +635,8 @@ sendNotifSchema.statics.saveAndSendNotif = async function (myrecnotif, req, res,
// console.log('myrecout._id', myrecout._id.toString());
let risnotif = null;
if (save) {
let res = null;
try {
@@ -633,7 +648,7 @@ sendNotifSchema.statics.saveAndSendNotif = async function (myrecnotif, req, res,
// console.log('myrecread._id', myrecread._id.toString());
if (myrecread)
return await globalTables.sendNotif(myrecread.typedir, myrecread.typeid, res, idapp, user ? user : req.user, myrecread);
risnotif = await globalTables.sendNotif(myrecread.typedir, myrecread.typeid, res, idapp, user ? user : req.user, myrecread);
else
return false;
}
@@ -644,9 +659,12 @@ sendNotifSchema.statics.saveAndSendNotif = async function (myrecnotif, req, res,
}
} else {
return await globalTables.sendNotif(myrecout.typedir, myrecout.typeid, res, idapp, user ? user : req.user, myrecout);
risnotif = await globalTables.sendNotif(myrecout.typedir, myrecout.typeid, res, idapp, user ? user : req.user, myrecout);
}
return risnotif;
};
sendNotifSchema.statics.updateStatusAndDescr = async function (myrecnotif, onlysave, userorig) {
@@ -1031,9 +1049,14 @@ sendNotifSchema.statics.sendToTheDestinations = async function (myrecnotifpass,
send = true;
}
}
} else if (myrecnotifpass.typedir === shared_consts.TypeNotifs.TYPEDIR_BACHECA || myrecnotifpass.typedir === shared_consts.TypeNotifs.TYPEDIR_EVENTS) {
if (shared_consts.TABLES_ADV_NOTIFICATION.includes(myrecnotifpass.tablerec) ||
shared_consts.TABLES_EVENTS_NOTIFICATION.includes(myrecnotifpass.tablerec)) {
} else if (myrecnotifpass.typedir === shared_consts.TypeNotifs.TYPEDIR_BACHECA
|| myrecnotifpass.typedir === shared_consts.TypeNotifs.TYPEDIR_EVENTS
|| myrecnotifpass.typedir === shared_consts.TypeNotifs.TYPEDIR_GROUPS
) {
if (shared_consts.TABLES_ADV_NOTIFICATION.includes(myrecnotifpass.tablerec)
|| shared_consts.TABLES_EVENTS_NOTIFICATION.includes(myrecnotifpass.tablerec)
|| shared_consts.TABLES_GROUPS_NOTIFICATION.includes(myrecnotifpass.tablerec)
) {
// Estrai la Città, la Provincia e la regione.
if (usernotifprofile && tools.isBitActive(usernotifprofile.value, shared_consts.UsersNotif.NEW_ADV_YOUR_PROVINCE) &&
@@ -1067,7 +1090,7 @@ sendNotifSchema.statics.sendToTheDestinations = async function (myrecnotifpass,
}
}
} else if (myrecnotifpass.typedir === shared_consts.TypeNotifs.TYPEDIR_GROUPS) {
/*} else if (myrecnotifpass.typedir === shared_consts.TypeNotifs.TYPEDIR_GROUPS) {
if (shared_consts.TABLES_GROUPS_NOTIFICATION.includes(myrecnotifpass.tablerec)) {
if (usernotifprofile && tools.isBitActive(usernotifprofile.value, shared_consts.GroupsNotifs.STATUS_GROUPS_NEW)) {
send = true;
@@ -1078,7 +1101,7 @@ sendNotifSchema.statics.sendToTheDestinations = async function (myrecnotifpass,
if (usernotifprofile && tools.isBitActive(usernotifprofile.value, shared_consts.CircuitsNotif.STATUS_NEW)) {
send = true;
}
}
}*/
}
if (send) {
@@ -1118,7 +1141,8 @@ sendNotifSchema.statics.sendToSingleUserDest = async function (myrecnotif, req,
};
sendNotifSchema.statics.getSumPendingTransactions = async function (idapp, username, circuitname, groupname) {
sendNotifSchema.statics.getSumPendingTransactionsMittente = async function (idapp, username, circuitname, groupname) {
const SendNotif = this;
try {
@@ -1140,9 +1164,60 @@ sendNotifSchema.statics.getSumPendingTransactions = async function (idapp, usern
};
sendNotifSchema.statics.getSumPendingTransactionsDest = async function (idapp, username, circuitname, groupname) {
const SendNotif = this;
try {
const query = {
idapp,
sender: username,
sendergroup: groupname,
typedir: shared_consts.TypeNotifs.TYPEDIR_CIRCUITS,
typeid: shared_consts.TypeNotifs.ID_CIRCUIT_SENDCOINSREQ,
status: 0,
'extrarec.circuitname': circuitname,
'extrarec.dest': username,
'extrarec.groupdest': groupname,
};
return await SendNotif.find(query).lean();
} catch (e) {
console.error('e', e);
}
};
sendNotifSchema.statics.updatePendingTransactions = async function (recnotif) {
const { Circuit } = require('../models/circuit');
const { Account } = require('../models/account');
try {
if (recnotif && recnotif.extrarec && recnotif.extrarec.circuitname) {
const circuit = await Circuit.getCircuitByName(recnotif.idapp, recnotif.extrarec.circuitname);
const accountdestTable = await Account.getAccountByUsernameAndCircuitId(recnotif.idapp, recnotif.extrarec.dest, circuit._id, true, false, recnotif.extrarec.groupdest, recnotif.extrarec.contoComDest);
const accountorigTable = await Account.getAccountByUsernameAndCircuitId(recnotif.idapp, recnotif.sender, circuit._id, true, true, recnotif.extrarec.grouporig, recnotif.extrarec.contoComOrig);
if (accountdestTable)
await accountdestTable.calcPending(false);
if (accountorigTable)
await accountorigTable.calcPending(true);
}
} catch (e) {
console.error(e);
}
};
sendNotifSchema.statics.checkIfAlreadyExist = async function (idapp, tag, idpost, numtab) {
};
const SendNotif = mongoose.model('SendNotif', sendNotifSchema);
module.exports = { SendNotif: SendNotif };

View File

@@ -62,6 +62,12 @@ const SiteSchema = new Schema({
telegram_key_test: {
type: String,
},
teleg_cfg: {
type: String,
},
teleg_cfg_test: {
type: String,
},
telegram_bot_name_test: {
type: String,
},
@@ -226,6 +232,7 @@ module.exports.findAllIdApp = async function (idapp) {
rec.email_pwd = '';
rec.telegram_key = '';
rec.telegram_key_test = '';
rec.confsite = {};
return rec;
}

View File

@@ -44,7 +44,7 @@ const UserSchema = new mongoose.Schema({
},
email: {
type: String,
required: true,
// required: true,
trim: true,
minlength: 1,
unique: false,
@@ -2791,7 +2791,7 @@ UserSchema.statics.setCircuitCmd = async function (idapp, usernameOrig, circuitn
ris = true;
} else if ((cmd === shared_consts.CIRCUITCMD.SENDCOINS_ACCEPT) || (cmd === shared_consts.CIRCUITCMD.SENDCOINS_REFUSE)) {
// Before to accept, I see if it's already set !
outres = {
cansend: false,
errormsg: '',
@@ -4826,6 +4826,7 @@ UserSchema.statics.getExtraInfoByUsername = async function (idapp, username) {
UserSchema.statics.addExtraInfo = async function (idapp, recUser, recUserSave, version) {
try {
tools.startTimeLog('addExtraInfo')
if (version) {
let versattualeuser = 0;
@@ -4924,6 +4925,8 @@ UserSchema.statics.addExtraInfo = async function (idapp, recUser, recUserSave, v
recUser.profile.calc = await User.calcOtherByUser(idapp, recUser._id);
tools.endTimeLog('addExtraInfo')
return recUser;
} catch (e) {
@@ -5190,6 +5193,7 @@ UserSchema.statics.createNewSubRecord = async function (idapp, req) {
return rec;
};
const User = mongoose.model('User', UserSchema);
class Hero {
@@ -5204,6 +5208,45 @@ class Hero {
}
}
module.exports = { User, Hero };
const FuncUsers = {
createRegistration_withTelegram(userdata) {
const telegrambot = require('../telegram/telegrambot');
try {
// Cerca se esiste già
const user = new User(userdata);
user.verified_email = false;
user.lasttimeonline = new Date();
user.date_reg = new Date();
return user.save().then(async () => {
return User.findByUsername(user.idapp, user.username, false).
then(async (usertrovato) => {
const numutenti = await User.getNumUsers(user.idapp);
let msg = '++ Nuovo Entrato: [' + numutenti + '] ' + user.username + ' ' + user.name + ' ' + user.surname;
await telegrambot.sendMsgTelegramToTheManagers(user.idapp, msg);
return telegrambot.askConfirmationUser(user.idapp, shared_consts.CallFunz.REGISTRATION, user, '', '', '', '');
});
}).catch((e) => {
console.error(e.message);
});
} catch (e) {
console.error(e.message);
}
}
};
module.exports = {
User, Hero, FuncUsers
};