Files
freeplanet_serverside/src/server/models/account.js

596 lines
14 KiB
JavaScript
Raw Normal View History

2022-08-26 03:33:13 +02:00
/*
Account is a User's single Circuit
*/
2022-09-14 11:32:04 +02:00
const mongoose = require('mongoose').set('debug', false);
2022-04-07 08:19:40 +02:00
const Schema = mongoose.Schema;
mongoose.Promise = global.Promise;
mongoose.level = 'F';
const tools = require('../tools/general');
const { ObjectID } = require('mongodb');
2022-04-07 08:19:40 +02:00
const shared_consts = require('../tools/shared_nodejs');
2022-04-07 08:19:40 +02:00
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
schema.options.usePushEach = true;
});
const AccountSchema = new Schema({
_id: {
2022-09-14 11:32:04 +02:00
type: String,
default: function () {
return new ObjectID().toString();
},
2022-04-07 08:19:40 +02:00
},
2022-08-26 03:33:13 +02:00
idapp: {
type: String,
2022-04-07 08:19:40 +02:00
},
2022-08-26 03:33:13 +02:00
username: {
2022-04-07 08:19:40 +02:00
type: String,
},
2023-01-03 16:51:32 +01:00
groupname: { // For the Groups
type: String,
},
2023-02-01 16:36:10 +01:00
contocom: { // For the Conto Comunitario dei Circuiti
type: String,
},
2022-08-26 03:33:13 +02:00
circuitId: { // ----- REF TO Circuit
2022-09-14 11:32:04 +02:00
type: String,
2022-08-26 03:33:13 +02:00
},
name: {
2022-04-07 08:19:40 +02:00
type: String,
},
deperibile: {
type: Boolean,
},
fidoConcesso: {
type: Number,
},
qta_maxConcessa: {
type: Number,
},
2022-04-07 08:19:40 +02:00
importo_iniziale: {
type: Number,
default: 0,
2022-04-07 08:19:40 +02:00
},
saldo: {
type: Number,
default: 0,
2022-04-07 08:19:40 +02:00
},
totTransato: {
type: Number,
default: 0,
},
regulation_ok: {
type: Boolean,
},
deleted: {
type: Boolean,
default: false,
},
2022-09-03 13:06:58 +02:00
date_created: {
type: Date,
default: Date.now,
},
date_updated: {
type: Date,
},
2022-04-07 08:19:40 +02:00
});
AccountSchema.statics.findAllIdApp = async function (idapp) {
2022-08-26 03:33:13 +02:00
const Account = this;
2022-04-07 08:19:40 +02:00
const myfind = { idapp, deleted: false };
2022-04-07 08:19:40 +02:00
2022-08-26 03:33:13 +02:00
return await Account.find(myfind, (err, arrrec) => {
2022-04-07 08:19:40 +02:00
return arrrec;
});
};
AccountSchema.pre('save', async function (next) {
2022-04-07 08:19:40 +02:00
if (this.isNew) {
this._id = new ObjectID().toString();
2022-04-07 08:19:40 +02:00
}
next();
});
AccountSchema.statics.getFieldsForSearch = function () {
2022-04-07 08:19:40 +02:00
return [
{ field: 'name', type: tools.FieldType.string },
{ field: 'username', type: tools.FieldType.string },
{ field: 'groupname', type: tools.FieldType.string },
{ field: 'contocom', type: tools.FieldType.string },
2022-04-07 08:19:40 +02:00
];
};
AccountSchema.statics.executeQueryTable = function (idapp, params) {
2022-04-07 08:19:40 +02:00
params.fieldsearch = this.getFieldsForSearch();
return tools.executeQueryTable(this, idapp, params);
2022-04-07 08:19:40 +02:00
};
AccountSchema.statics.getAccountsByUsername = async function (idapp, username) {
2022-08-26 03:33:13 +02:00
const Account = this;
if (username === undefined)
return false;
const myquery = {
'idapp': idapp,
'username': username,
};
return await Account.find(myquery).lean();
2022-08-26 03:33:13 +02:00
2022-09-03 13:06:58 +02:00
};
AccountSchema.statics.calcTotCircolante = async function (idapp, circuitId) {
2022-09-19 15:10:23 +02:00
const Account = this;
try {
let aggr1 = [
{
$match: { idapp, circuitId, saldo: { $gt: 0 } },
2022-09-19 15:10:23 +02:00
},
{ $group: { _id: null, count: { $sum: '$saldo' } } }
];
ris = await Account.aggregate(aggr1);
return ris ? ris[0].count : 0;
} catch (e) {
console.error('err', e);
}
};
AccountSchema.methods.addtoSaldoSave = async function (amount) {
const account = this;
2022-09-03 13:06:58 +02:00
if (account) {
account.saldo = account.saldo + amount;
if (!account.totTransato) {
account.totTransato = 0;
}
account.totTransato += Math.abs(amount);
account.date_updated = new Date();
return await account.save();
2022-09-03 13:06:58 +02:00
}
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) {
2022-09-14 11:32:04 +02:00
if (this.isNew) {
if (!this.date_created)
this.date_created = new Date();
2022-09-14 11:32:04 +02:00
}
next();
});
AccountSchema.statics.getAccountByUsernameAndCircuitId = async function (idapp, username, circuitId, createifnotexist, confido, groupname = '', contocom = "") {
2022-09-03 13:06:58 +02:00
const Account = this;
2022-09-12 18:37:08 +02:00
try {
const { Circuit } = require('../models/circuit');
2022-09-03 13:06:58 +02:00
2022-09-12 18:37:08 +02:00
if (username === undefined)
return false;
2022-09-03 13:06:58 +02:00
2022-09-12 18:37:08 +02:00
let myquery = {
2023-01-03 16:51:32 +01:00
idapp,
circuitId,
2022-09-12 18:37:08 +02:00
};
2023-01-03 16:51:32 +01:00
if (groupname) {
myquery.groupname = groupname;
2023-02-01 16:36:10 +01:00
} else if (contocom) {
myquery.contocom = contocom;
2023-01-03 16:51:32 +01:00
} else {
myquery.username = username;
}
let mycircuit = await Circuit.getCircuitById(circuitId);
2022-09-14 11:32:04 +02:00
if (mycircuit) {
let myaccount = await Account.findOne(myquery);
2022-09-14 11:32:04 +02:00
if (!myaccount && createifnotexist) {
myaccount = new Account({
_id: new ObjectID().toString(),
idapp,
username,
2023-01-03 16:51:32 +01:00
groupname,
2023-02-01 16:36:10 +01:00
contocom,
2022-09-14 11:32:04 +02:00
circuitId: mycircuit._id,
deperibile: false,
importo_iniziale: 0,
saldo: 0,
2023-01-03 16:51:32 +01:00
fidoConcesso: 0,
qta_maxConcessa: 0,
2022-09-14 11:32:04 +02:00
});
2023-02-01 16:36:10 +01:00
if (contocom) {
2023-02-01 01:19:50 +01:00
myaccount.fidoConcesso = shared_consts.CIRCUIT_PARAMS.SCOPERTO_MIN_CONTO_COMUNITARIO;
myaccount.qta_maxConcessa = shared_consts.CIRCUIT_PARAMS.SCOPERTO_MAX_CONTO_COMUNITARIO;
2023-01-03 16:51:32 +01:00
} else {
2023-02-01 01:19:50 +01:00
if (!mycircuit.fido_scoperto_default_grp)
mycircuit.fido_scoperto_default_grp = shared_consts.CIRCUIT_PARAMS.SCOPERTO_MIN_GRP;
if (!mycircuit.qta_max_default_grp)
mycircuit.qta_max_default_grp = shared_consts.CIRCUIT_PARAMS.SCOPERTO_MAX_GRP;
if (groupname) {
myaccount.fidoConcesso = mycircuit.fido_scoperto_default_grp;
myaccount.qta_maxConcessa = mycircuit.qta_max_default_grp;
} else {
myaccount.fidoConcesso = mycircuit.fido_scoperto_default;
myaccount.qta_maxConcessa = mycircuit.qta_max_default;
}
2023-01-03 16:51:32 +01:00
}
if (!confido) {
myaccount.fidoConcesso = 0;
}
2023-02-01 01:19:50 +01:00
2022-09-14 11:32:04 +02:00
return await myaccount.save();
}
return myaccount;
2022-09-12 18:37:08 +02:00
}
2022-09-14 11:32:04 +02:00
return null;
2022-09-12 18:37:08 +02:00
} catch (e) {
console.error('error', e);
2022-09-03 13:06:58 +02:00
}
2022-09-12 18:37:08 +02:00
};
AccountSchema.statics.isExistAccountByUsernameAndCircuitId = async function (idapp, username, circuitId, groupname = '', contocom = "") {
const Account = this;
try {
const { Circuit } = require('../models/circuit');
if (username === undefined)
return false;
let myquery = {
idapp,
circuitId,
};
if (groupname) {
myquery.groupname = groupname;
} else if (contocom) {
myquery.contocom = contocom;
} else {
myquery.username = username;
}
let mycircuit = await Circuit.getCircuitById(circuitId);
if (mycircuit) {
let myaccount = await Account.findOne(myquery);
return !!myaccount
}
return false;
} catch (e) {
console.error('error', e);
}
};
AccountSchema.statics.createAccount = async function (idapp, username, circuitName, confido, groupname = '', contocom = '') {
const { Circuit } = require('../models/circuit');
2022-09-12 18:37:08 +02:00
try {
2023-02-01 01:19:50 +01:00
mycircuit = await Circuit.findOne({ name: circuitName }, { _id: 1 });
if (mycircuit) {
return await Account.getAccountByUsernameAndCircuitId(idapp, username, mycircuit._id, true, confido, groupname, contocom);
} else {
return null;
}
} catch (e) {
console.error('error', e);
return null;
}
2022-09-03 13:06:58 +02:00
};
2022-08-26 03:33:13 +02:00
AccountSchema.statics.getUserAccounts = async function (idapp, username) {
2022-09-12 18:37:08 +02:00
try {
let aggr1 = [
{
$match: { idapp, username },
2022-09-12 18:37:08 +02:00
},
{
$lookup: {
from: 'circuits',
localField: 'circuitId',
foreignField: '_id',
as: 'circuit',
},
},
{ $unwind: '$circuit' },
{
$lookup: {
from: 'sendnotifs',
as: 'notifspending',
let: {
circuitname: '$circuit.name',
username: '$username',
idapp: '$idapp',
typedir: shared_consts.TypeNotifs.TYPEDIR_CIRCUITS,
typeid: shared_consts.TypeNotifs.ID_CIRCUIT_SENDCOINSREQ,
},
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: ['$typedir', '$$typedir'] },
{ $eq: ['$typeid', '$$typeid'] },
{ $eq: ['$status', 0] },
{ $eq: ['$sender', '$$username'] },
{ $eq: ['$idapp', '$$idapp'] },
{ $eq: ['$extrarec.circuitname', '$$circuitname'] },
],
},
},
},
{
$group:
{ _id: "$extrarec.notifIdToUpdate", result: { $first: "$$ROOT" } }
},
],
},
},
2022-09-12 18:37:08 +02:00
];
ris = await this.aggregate(aggr1);
const { SendNotif } = require('../models/sendnotif');
if (ris) {
for (const account of ris) {
try {
const pendingtransactions = await SendNotif.getSumPendingTransactions(idapp, username, account.circuit.name);
const saldopending = pendingtransactions.reduce((sum, rec) => sum + rec.extrarec.qty, 0);
account.saldo -= saldopending;
account.totTransato = account.totTransato || 0;
} catch (e) {
console.error('getUserAccounts 1) ', e);
}
}
}
2022-09-12 18:37:08 +02:00
return ris;
} catch (e) {
console.error('getUserAccounts', e);
2022-09-12 18:37:08 +02:00
}
};
2022-08-26 03:33:13 +02:00
2023-01-03 16:51:32 +01:00
AccountSchema.statics.getGroupAccounts = async function (idapp, groupname) {
try {
let aggr1 = [
{
$match: { idapp, groupname },
},
{
$lookup: {
from: 'circuits',
localField: 'circuitId',
foreignField: '_id',
as: 'circuit',
},
},
{ $unwind: '$circuit' },
{
$lookup: {
from: 'sendnotifs',
as: 'notifspending',
let: {
circuitname: '$circuit.name',
groupname: '$groupname',
idapp: '$idapp',
typedir: shared_consts.TypeNotifs.TYPEDIR_CIRCUITS,
typeid: shared_consts.TypeNotifs.ID_CIRCUIT_SENDCOINSREQ,
},
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: ['$typedir', '$$typedir'] },
{ $eq: ['$typeid', '$$typeid'] },
{ $eq: ['$status', 0] },
2023-01-12 01:03:10 +01:00
{ $eq: ['$sendergroup', '$$groupname'] },
2023-01-03 16:51:32 +01:00
{ $eq: ['$idapp', '$$idapp'] },
{ $eq: ['$extrarec.circuitname', '$$circuitname'] },
],
},
},
},
{
$group:
{ _id: "$extrarec.notifIdToUpdate", result: { $first: "$$ROOT" } }
},
2023-01-03 16:51:32 +01:00
],
},
},
];
ris = await this.aggregate(aggr1);
const { SendNotif } = require('../models/sendnotif');
if (ris) {
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);
2023-01-03 16:51:32 +01:00
account.saldo -= saldopending;
}
}
return ris;
} catch (e) {
console.error('e', e);
}
};
// Imposta a tutti i Conti Collettivi, i seguenti minimi e massimi
AccountSchema.statics.SetMinMaxCollettivi = async function (idapp, valmin, valmax) {
const Account = this;
ris = await Account.updateMany({ idapp, groupname: { "$nin": [null, ""] } },
{
$set:
{
fidoConcesso: valmin,
qta_maxConcessa: valmax,
}
});
};
// Imposta a tutti i Conti Comunitari, i seguenti minimi e massimi
AccountSchema.statics.SetMinMaxComunitari = async function (idapp, valmin, valmax) {
const Account = this;
ris = await Account.updateMany({ idapp, contocom: { "$nin": [null, ""] } },
{
$set:
{
fidoConcesso: valmin,
qta_maxConcessa: valmax,
}
});
};
// Imposta a tutti i Conti Personali, i seguenti minimi e massimi
AccountSchema.statics.SetMinMaxPersonali = async function (idapp, valmin, valmax, circuitId) {
const Account = this;
if (circuitId) {
ris = await Account.updateMany({ idapp, circuitId, fidoConcesso: { $gt: 0 }, username: { "$nin": [null, ""] } },
{
$set:
{
fidoConcesso: valmin,
qta_maxConcessa: valmax,
}
});
} else {
ris = await Account.updateMany({ idapp, fidoConcesso: { $gt: 0 }, username: { "$nin": [null, ""] } },
{
$set:
{
fidoConcesso: valmin,
qta_maxConcessa: valmax,
}
});
}
};
AccountSchema.statics.updateFido = async function (idapp, username, circuitId, fido) {
let paramstoupdate = {
fidoConcesso: fido,
};
const risult = await Account.updateOne({ idapp, username, circuitId }, { $set: paramstoupdate });
return risult;
};
AccountSchema.statics.canEditAccountAdmins = async function (username, id) {
const account = await Account.findOne({_id: id}).lean();
const { Circuit } = require('../models/circuit');
if (account) {
const circuit = await Circuit.findOne({_id: account.circuitId}).lean();
if (circuit) {
return circuit.admins.findIndex((admin) => admin.username === username) >= 0;
}
}
return false;
};
2023-01-03 16:51:32 +01:00
AccountSchema.statics.addToPeopleOfMyAccount = async function (idapp, username, circuitId, person_username, perm) {
return await Account.updateOne({ idapp, username, circuitId },
{
$push:
{
people: {
username: person_username,
perm,
date: new Date(),
},
},
});
};
// Rimuovi dagli Admin del Account
AccountSchema.statics.removeAdminOfAccount = async function (idapp, username, circuitId, person_username, perm) {
return await Circuit.updateOne({ idapp, username, circuitId },
{ $pull: { people: { username: { $in: [person_username] } } } });
};
2022-04-07 08:19:40 +02:00
const Account = mongoose.model('Account', AccountSchema);
module.exports = { Account };