Aggiornamenti 2

This commit is contained in:
Paolo Arena
2020-05-04 19:34:41 +02:00
parent 2549d43447
commit 7d0538f7ae
29 changed files with 698 additions and 221 deletions

View File

@@ -279,6 +279,14 @@ UserSchema.statics.isManager = function (perm) {
}
};
UserSchema.statics.isTraduttrici = function (perm) {
try {
return ((perm & shared_consts.Permissions.Traduttrici) === shared_consts.Permissions.Traduttrici);
} catch (e) {
return false
}
};
UserSchema.statics.isTutor = function (perm) {
try {
return ((perm & shared_consts.Permissions.Tutor) === shared_consts.Permissions.Tutor);
@@ -324,10 +332,16 @@ UserSchema.statics.findByCredentials = function (idapp, username, password) {
const User = this;
let pwd = "";
return User.findOne({ idapp, username: username }).then((user) => {
return User.findOne({
idapp, username: username,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }]
}).then((user) => {
if (!user) {
// Check if with email:
return User.findOne({ idapp, email: username.toLowerCase() })
return User.findOne({
idapp, email: username.toLowerCase(),
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }]
})
} else {
return !user.deleted ? user : null
}
@@ -361,6 +375,7 @@ UserSchema.statics.findByUsername = async function (idapp, username, alsoemail)
return await User.findOne({
'idapp': idapp,
'username': regexusername,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }],
}).then(async (ris) => {
if ((!ris) && (alsoemail)) {
regexemail = new RegExp(["^", username.toLowerCase(), "$"].join(""), "i");
@@ -368,6 +383,7 @@ UserSchema.statics.findByUsername = async function (idapp, username, alsoemail)
return User.findOne({
'idapp': idapp,
'email': regexemail,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }],
});
}
return ris;
@@ -380,6 +396,7 @@ UserSchema.statics.getUserShortDataByUsername = async function (idapp, username)
const myrec = await User.findOne({
'idapp': idapp,
'username': username,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }],
}, {
lang: 1,
ind_order: 1,
@@ -427,6 +444,7 @@ UserSchema.statics.getDownlineByUsername = async function (idapp, username) {
const arrrec = await User.find({
'idapp': idapp,
'aportador_solidario': username,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }]
}, {
lang: 1,
aportador_solidario: 1,
@@ -473,24 +491,38 @@ UserSchema.statics.getnumInvitatiAttivi = function (idapp, username) {
return User.countDocuments({
idapp,
aportador_solidario: username,
$or: [
$and: [
{
'profile.special_req': true
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }],
},
{
$or: [
{
'profile.special_req': true
},
{
verified_email: true,
'profile.teleg_id': { $gt: 1 },
'profile.saw_and_accepted': shared_consts.ALL_SAW_AND_ACCEPTED,
'profile.saw_zoom_presentation': true,
'profile.my_dream': { $exists: true },
'profile.paymenttypes': { "$in": ['paypal'] },
$and: [
{ "$expr": { "$gt": [{ "$strLenCP": "$profile.my_dream" }, 10] } },
{ "$expr": { "$gt": [{ "$strLenCP": "$profile.email_paypal" }, 6] } }
],
}]
});
verified_email: true,
'profile.teleg_id': { $gt: 1 },
'profile.saw_and_accepted': shared_consts.ALL_SAW_AND_ACCEPTED,
'profile.saw_zoom_presentation': true,
'profile.my_dream': { $exists: true },
'profile.paymenttypes': { "$in": ['paypal'] },
$and: [
{ 'profile.my_dream': { $exists: true } },
{ "$expr": { "$gt": [{ "$strLenCP": "$profile.my_dream" }, 10] } },
{
$and: [
{ 'profile.email_paypal': { $exists: true } },
{ "$expr": { "$gt": [{ "$strLenCP": "$profile.email_paypal" }, 6] } }
],
}
],
}]
}
]
})
;
};
UserSchema.statics.isUserQualified7 = async function (idapp, username) {
@@ -502,23 +534,29 @@ UserSchema.statics.isUserQualified7 = async function (idapp, username) {
const myquery = {
'idapp': idapp,
'username': username,
$or: [
$and: [
{
'profile.special_req': true
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }],
},
{
verified_email: true,
'profile.teleg_id': { $gt: 1 },
'profile.saw_and_accepted': shared_consts.ALL_SAW_AND_ACCEPTED,
'profile.saw_zoom_presentation': true,
'profile.my_dream': { $exists: true },
'profile.email_paypal': { $exists: true },
'profile.paymenttypes': { "$in": ['paypal'] },
$and: [
{ "$expr": { "$gt": [{ "$strLenCP": "$profile.my_dream" }, 10] } },
{ "$expr": { "$gt": [{ "$strLenCP": "$profile.email_paypal" }, 6] } }
],
$where: "this.profile.paymenttypes.length >= 1",
$or: [
{
'profile.special_req': true
},
{
verified_email: true,
'profile.teleg_id': { $gt: 1 },
'profile.saw_and_accepted': shared_consts.ALL_SAW_AND_ACCEPTED,
'profile.saw_zoom_presentation': true,
'profile.my_dream': { $exists: true },
'profile.email_paypal': { $exists: true },
'profile.paymenttypes': { "$in": ['paypal'] },
$and: [
{ "$expr": { "$gt": [{ "$strLenCP": "$profile.my_dream" }, 10] } },
{ "$expr": { "$gt": [{ "$strLenCP": "$profile.email_paypal" }, 6] } }
],
$where: "this.profile.paymenttypes.length >= 1",
}]
}]
};
@@ -545,6 +583,7 @@ UserSchema.statics.getnumPaymentOk = function (idapp) {
return User.count({
idapp,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }],
'profile.paymenttypes': { "$in": ['paypal'] },
$where: "this.profile.paymenttypes.length >= 1",
'profile.email_paypal': { $exists: true },
@@ -554,7 +593,7 @@ UserSchema.statics.getnumPaymentOk = function (idapp) {
UserSchema.statics.getUsersNationalityQuery = function (idapp) {
const query = [
{
$match: { idapp }
$match: { idapp, $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }], }
},
{
$group: { _id: "$profile.nationality", count: { $sum: 1 } }
@@ -583,6 +622,7 @@ UserSchema.statics.getnumInvitati = function (idapp, username) {
return User.count({
idapp,
aportador_solidario: username,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }],
});
};
@@ -628,13 +668,17 @@ UserSchema.statics.findByEmail = function (idapp, email) {
return User.findOne({
'idapp': idapp,
'email': email,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }],
});
};
UserSchema.statics.getLastUser = function (idapp) {
const User = this;
return User.findOne({ idapp }).sort({ ind_order: -1 })
return User.findOne({
idapp,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }],
}).sort({ ind_order: -1 })
};
UserSchema.statics.findByIndOrder = function (idapp, ind_order) {
@@ -645,9 +689,10 @@ UserSchema.statics.findByIndOrder = function (idapp, ind_order) {
return User.findOne({
idapp,
ind_order,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }],
});
} catch (e) {
console.error(e);
console.error(e.message);
}
};
@@ -684,7 +729,10 @@ UserSchema.methods.removeToken = function (token) {
UserSchema.statics.getEmailByUsername = async function (idapp, username) {
const User = this;
return await User.findOne({ idapp, username })
return await User.findOne({
idapp, username,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }]
})
.then((arrrec) => {
return ((arrrec) ? arrrec.email : '');
}).catch((e) => {
@@ -695,7 +743,10 @@ UserSchema.statics.getEmailByUsername = async function (idapp, username) {
UserSchema.statics.getUsernameById = async function (idapp, id) {
const User = this;
return await User.findOne({ idapp, _id: id }, { username: 1 })
return await User.findOne({
idapp, _id: id,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }]
}, { username: 1 })
.then((myuser) => {
return ((myuser) ? myuser.username : '');
}).catch((e) => {
@@ -706,19 +757,28 @@ UserSchema.statics.getUsernameById = async function (idapp, id) {
UserSchema.statics.getUserById = function (idapp, id) {
const User = this;
return User.findOne({ idapp, _id: id })
return User.findOne({
idapp, _id: id,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }]
})
};
UserSchema.statics.getUserByAportador = function (idapp, aportador_solidario) {
const User = this;
return User.findOne({ idapp, aportador_solidario })
return User.findOne({
idapp, aportador_solidario,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }],
})
};
UserSchema.statics.getAportadorSolidarioByUsername = async function (idapp, username) {
const User = this;
return await User.findOne({ idapp, username })
return await User.findOne({
idapp, username,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }]
})
.then((rec) => {
return ((rec) ? rec.aportador_solidario : '');
}).catch((e) => {
@@ -729,7 +789,10 @@ UserSchema.statics.getAportadorSolidarioByUsername = async function (idapp, user
UserSchema.statics.UserByIdTelegram = async function (idapp, teleg_id) {
const User = this;
return await User.findOne({ idapp, 'profile.teleg_id': teleg_id })
return await User.findOne({
idapp, 'profile.teleg_id': teleg_id,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }],
})
.then((rec) => {
return (!!rec) ? rec._doc : null;
}).catch((e) => {
@@ -740,7 +803,10 @@ UserSchema.statics.UserByIdTelegram = async function (idapp, teleg_id) {
UserSchema.statics.UsersByIdTelegram = async function (idapp, teleg_id) {
const User = this;
return await User.find({ idapp, 'profile.teleg_id': teleg_id })
return await User.find({
idapp, 'profile.teleg_id': teleg_id,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }]
})
.then((rec) => {
return (!!rec) ? rec._doc : null;
}).catch((e) => {
@@ -751,7 +817,10 @@ UserSchema.statics.UsersByIdTelegram = async function (idapp, teleg_id) {
UserSchema.statics.TelegIdByUsername = async function (idapp, username) {
const User = this;
return await User.findOne({ idapp, username }, { 'profile.teleg_id': 1 })
return await User.findOne({
idapp, username,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }]
}, { 'profile.teleg_id': 1 })
.then((rec) => {
return (!!rec) ? rec.profile.teleg_id : null;
}).catch((e) => {
@@ -839,7 +908,10 @@ UserSchema.statics.SetTelegramWasBlocked = async function (idapp, teleg_id) {
UserSchema.statics.getNameSurnameByUsername = async function (idapp, username) {
const User = this;
return await User.findOne({ idapp, username }, { name: 1, surname: 1 })
return await User.findOne({
idapp, username,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }]
}, { name: 1, surname: 1 })
.then((rec) => {
return (!!rec) ? `${rec.name} ${rec.surname}` : '';
}).catch((e) => {
@@ -850,7 +922,10 @@ UserSchema.statics.getNameSurnameByUsername = async function (idapp, username) {
UserSchema.statics.getSmallRecByIndOrder = async function (idapp, ind_order) {
const User = this;
return await User.findOne({ idapp, ind_order },
return await User.findOne({
idapp, ind_order,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }]
},
{
idapp: 1,
ind_order: 1,
@@ -1005,7 +1080,10 @@ UserSchema.statics.executeQueryTable = function (idapp, params) {
UserSchema.statics.findAllIdApp = function (idapp) {
const User = this;
const myfind = { idapp };
const myfind = {
idapp,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }]
};
return User.find(myfind, (err, arrrec) => {
return arrrec
@@ -1054,23 +1132,76 @@ UserSchema.statics.getDashboard = async function (idapp, aportador_solidario, us
}
}
dashboard.arrposizioni = await Nave.getArrPosizioniByIndOrder(idapp, dashboard.myself.ind_order);
if (!!dashboard.myself.ind_order)
dashboard.arrposizioni = await Nave.getArrPosizioniByIndOrder(idapp, dashboard.myself.ind_order);
else
dashboard.arrposizioni = [];
if (!!dashboard.myself.ind_order)
dashboard.arrimbarchi = await ListaIngresso.findAllByIndOrder(idapp, dashboard.myself.ind_order);
else
dashboard.arrimbarchi = [];
for (let myimbarco of dashboard.arrimbarchi) {
myimbarco._doc.posiz = await ListaIngresso.getPosizioneInLista(idapp, myimbarco.ind_order, myimbarco.num_tess);
}
dashboard.navi_partenza = await NavePersistente.getListaNavi(idapp);
dashboard.lastnave = await NavePersistente.getLastNave(idapp);
//for (let indriga = 0; indriga < 10; indriga++) {
// dashboard.navi_partenza.push(await Nave.getPrimaNaveByRiga(idapp, indriga));
//}
for (let mypos of dashboard.arrposizioni) {
mypos._doc.rec = await Nave.getNaveByRigaCol(idapp, mypos.riga, mypos.col);
mypos._doc.nave_partenza = await NavePersistente.findByRigaColByDonatore(idapp, mypos.riga, mypos.col, 0);
}
// console.table(dashboard.arrnavi);
const arrnew = [];
for (let mypos of dashboard.arrposizioni) {
// Controlla se è presente la Nave con il num_tess pari
let trovato = false;
if (mypos.num_tess % 2 !== 0) {
for (let myrec of dashboard.arrposizioni) {
if (myrec.num_tess === mypos.num_tess + 1) {
trovato = true
}
}
} else {
trovato = true;
}
if (!trovato) {
const mymediatore = mypos._doc.rec.mediatore.arrdonatori[7];
if (!!mymediatore) {
const myrec = {
riga: mymediatore.riga,
col: mymediatore.col,
name: mypos._doc.rec.mediatore.recmediatore.name,
surname: mypos._doc.rec.mediatore.recmediatore.surname,
username: mypos._doc.rec.mediatore.recmediatore.username,
num_tess: mypos._doc.rec.mediatore.recmediatore.num_tess + 1,
rec: await Nave.getNaveByRigaCol(idapp, mymediatore.riga, mymediatore.col),
nave_partenza: {},
};
myrec.nave_partenza = await NavePersistente.findByRigaColByDonatore(idapp, myrec.riga, myrec.col, 0);
arrnew.push(myrec);
}
}
}
dashboard.arrposizioni = [...dashboard.arrposizioni, ...arrnew];
// console.table(dashboard.arrnavi);
return dashboard;
} catch (e) {
console.error(e);
console.error(e.message);
return false;
}
};
@@ -1101,6 +1232,7 @@ UserSchema.statics.findByCellAndNameSurname = function (idapp, cell, name, surna
'profile.cell': cell,
'name': name,
'surname': surname,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }]
});
};
@@ -1121,21 +1253,25 @@ UserSchema.statics.getUsersQualified = async function (idapp, numinvitati) {
const arrusers = await User.find({
idapp,
$or: [
$and: [
{ $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }] },
{
'profile.special_req': true
},
{
verified_email: true,
'profile.teleg_id': { $gt: 0 },
'profile.paymenttypes': { "$in": ['paypal'] },
'profile.saw_and_accepted': shared_consts.ALL_SAW_AND_ACCEPTED,
'profile.saw_zoom_presentation': true,
'profile.my_dream': { $exists: true },
$and: [
{ "$expr": { "$gt": [{ "$strLenCP": "$profile.my_dream" }, 10] } },
{ "$expr": { "$gt": [{ "$strLenCP": "$profile.email_paypal" }, 6] } }
],
$or: [
{
'profile.special_req': true
},
{
verified_email: true,
'profile.teleg_id': { $gt: 0 },
'profile.paymenttypes': { "$in": ['paypal'] },
'profile.saw_and_accepted': shared_consts.ALL_SAW_AND_ACCEPTED,
'profile.saw_zoom_presentation': true,
'profile.my_dream': { $exists: true },
$and: [
{ "$expr": { "$gt": [{ "$strLenCP": "$profile.my_dream" }, 10] } },
{ "$expr": { "$gt": [{ "$strLenCP": "$profile.email_paypal" }, 6] } }
],
}]
}]
}, {
@@ -1164,21 +1300,25 @@ UserSchema.statics.visuUtentiNonInNavi = async function (idapp) {
const arrusers = await User.find({
idapp,
$or: [
$and: [
{ $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }] },
{
'profile.special_req': true
},
{
verified_email: true,
'profile.teleg_id': { $gt: 0 },
'profile.paymenttypes': { "$in": ['paypal'] },
'profile.saw_and_accepted': shared_consts.ALL_SAW_AND_ACCEPTED,
'profile.saw_zoom_presentation': true,
'profile.my_dream': { $exists: true },
$and: [
{ "$expr": { "$gt": [{ "$strLenCP": "$profile.my_dream" }, 10] } },
{ "$expr": { "$gt": [{ "$strLenCP": "$profile.email_paypal" }, 6] } }
],
$or: [
{
'profile.special_req': true
},
{
verified_email: true,
'profile.teleg_id': { $gt: 0 },
'profile.paymenttypes': { "$in": ['paypal'] },
'profile.saw_and_accepted': shared_consts.ALL_SAW_AND_ACCEPTED,
'profile.saw_zoom_presentation': true,
'profile.my_dream': { $exists: true },
$and: [
{ "$expr": { "$gt": [{ "$strLenCP": "$profile.my_dream" }, 10] } },
{ "$expr": { "$gt": [{ "$strLenCP": "$profile.email_paypal" }, 6] } }
],
}]
}]
}, {
@@ -1202,24 +1342,60 @@ UserSchema.statics.visuUtentiNonInNavi = async function (idapp) {
let numnoinlista = 0;
let numeliminati = 0;
let numsospesi = 0;
let strnavidoppie = '';
let esiste = false;
for (const user of arrusers) {
if (user.deleted) {
numeliminati++;
} else {
esiste = true;
if (!!user.deleted) {
if (user.deleted) {
numeliminati++;
esiste = false;
}
}
if (esiste) {
let visualizza = false;
// Controlla se ho un doppione nelle Navi !
let mienavi = await Nave.find({ idapp, ind_order: user.ind_order }, { num_tess: 1 });
let strnavidoppie = [];
if (!!mienavi) {
strnavidoppie = mienavi.reduce((acc, currentValue, index, array) => {
if (array.indexOf(currentValue.num_tess) > -1 && !acc.includes(currentValue.num_tess))
acc.push(currentValue.num_tess);
return acc;
}, []);
}
if (strnavidoppie.length > 1) {
visualizza = true;
}
user.numinvitati = await User.getnumInvitati(idapp, user.username);
reg++;
let mianave = await Nave.findOne({ idapp, ind_order: user.ind_order });
let mialistaingresso = await ListaIngresso.findOne({ idapp, ind_order: user.ind_order });
let trovato = false;
if (!mianave) {
if (!mianave)
visualizza = true;
if (visualizza) {
mystr += user.username + ' ' + user.name + ' ' + user.surname + ' [' + user.ind_order + '] [inv=' + user.numinvitati + ']'
noninnave++;
trovato = true;
} else {
innave++;
}
if (strnavidoppie.length > 1) {
mystr += ' NAVI DUPLICATE! ' + strnavidoppie.join(',');
}
if (!mianave)
noninnave++;
else
innave++;
if (user.sospeso) {
numsospesi++;
}
@@ -1422,7 +1598,10 @@ UserSchema.statics.getUsersRegDaily = function (idapp, nrec) {
const query = [
{
$match: { idapp, date_reg: { $gte: tools.IncDateNow(-(1000 * 60 * 60 * 24 * nrec)) } }
$match: {
idapp, date_reg: { $gte: tools.IncDateNow(-(1000 * 60 * 60 * 24 * nrec)) },
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }]
}
},
{
$group: { _id: { $dateToString: { format: "%Y-%m-%d", date: "$date_reg" } }, count: { $sum: 1 } }
@@ -1434,12 +1613,35 @@ UserSchema.statics.getUsersRegDaily = function (idapp, nrec) {
return query
};
UserSchema.statics.getUsersRegWeekly = function (idapp, nrec) {
const query = [
{
$match: {
idapp, date_reg: { $gte: tools.IncDateNow(-(1000 * 60 * 60 * 24 * nrec)) },
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }]
}
},
{
$group: { _id: { $dateToString: { format: "%Y-%U", date: "$date_reg" } }, count: { $sum: 1 } }
},
{
$sort: { _id: 1 }
}
];
return query
};
UserSchema.statics.getnumRegNDays = function (idapp, nrec) {
const query = [
{
$match: { idapp, date_reg: { $lt: tools.IncDateNow(-(1000 * 60 * 60 * 24 * nrec)) } }
$match: {
idapp,
date_reg: { $lt: tools.IncDateNow(-(1000 * 60 * 60 * 24 * nrec)) },
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }]
}
},
{
$group: { _id: { $dateToString: { format: "%Y-%m-%d", date: "$date_reg" } }, count: { $sum: 1 } }
@@ -1471,6 +1673,16 @@ UserSchema.statics.calcRegDaily = async function (idapp) {
});
};
UserSchema.statics.calcRegWeekly = async function (idapp) {
const User = this;
return User.aggregate(User.getUsersRegWeekly(idapp, 20 * 7))
.then(ris => {
// console.table(ris);
return JSON.stringify(ris.slice(0, -1));
});
};
if (tools.INITDB_FIRSTIME) {
console.log(' createIndex User Index...');
@@ -1573,7 +1785,10 @@ UserSchema.statics.DbOp = async function (idapp, mydata) {
await ListaIngresso.deleteMany({ idapp, added: false });
arrusers = await User.find({ 'idapp': idapp }).sort({ ind_order: 1 });
arrusers = await User.find({
'idapp': idapp,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }]
}).sort({ ind_order: 1 });
let num = 0;
num += await addUtentiInLista(idapp, 1, arrusers);
@@ -1588,7 +1803,7 @@ UserSchema.statics.DbOp = async function (idapp, mydata) {
}
} catch (e) {
console.error(e);
console.error(e.message);
}