++ diffusori

fixed user chip
This commit is contained in:
Surya Paolo
2022-12-11 02:57:35 +01:00
parent 234fd8b00d
commit 3eef70f3f3
5 changed files with 663 additions and 420 deletions

100
mongodb/Diffusori.mongodb Normal file
View File

@@ -0,0 +1,100 @@
// MongoDB Playground
// To disable this template go to Settings | MongoDB | Use Default Template For Playground.
// Make sure you are connected to enable completions and to be able to run a playground.
// Use Ctrl+Space inside a snippet or a string literal to trigger completions.
// Select the database to use.
use('test_FreePlanet');
let aggregation = [
{
$match: {
idapp: "13",
$or: [
{
deleted: {
$exists: false,
},
},
{
deleted: {
$exists: true,
$eq: false,
},
},
],
},
},
{
$group: {
_id: "$aportador_solidario",
count: {
$sum: 1,
},
},
},
{
$match: { "count": { $gte: 2 } }
},
{
$sort: {
count: -1,
},
},
{
$lookup: {
from: "users",
let: {
username: "$_id",
idapp: "13",
},
pipeline: [
{
$match: {
$expr: {
$and: [
{
$eq: [
"$$username",
"$username",
],
},
{
$eq: [
"$$idapp",
"$idapp",
],
},
],
},
},
},
],
as: "user",
},
},
{ $unwind: "$user" },
{
$replaceRoot: {
newRoot: {
$mergeObjects: [ "$user", "$$ROOT" ],
},
},
},
{
$project: {
_id: 0,
count: 1,
aportador_solidario: 1,
username: 1,
name: 1,
surname: 1,
lasttimeonline: 1,
idapp: 1,
"profile.img": 1,
},
},
];
db.users.aggregate(aggregation);

View File

@@ -136,6 +136,9 @@ const UserSchema = new mongoose.Schema({
tokenforgot: {
type: String,
},
tokenforgot_code: {
type: String,
},
date_tokenreg: {
type: Date,
},
@@ -1164,6 +1167,17 @@ UserSchema.statics.findByLinkTokenforgot = function (idapp, email, tokenforgot)
});
};
UserSchema.statics.findByLinkTokenforgotCode = function (idapp, email, tokenforgot_code) {
const User = this;
return User.findOne({
email,
tokenforgot_code,
date_tokenforgot: { $gte: tools.IncDateNow(-1000 * 60 * 60 * 4) }, // 4 ore fa!
idapp,
});
};
UserSchema.statics.createNewRequestPwd = function (idapp, email) {
const User = this;
@@ -1177,9 +1191,10 @@ UserSchema.statics.createNewRequestPwd = function (idapp, email) {
user.tokenforgot = jwt.sign(user._id.toHexString(), process.env.SIGNCODE).
toString();
user.date_tokenforgot = new Date();
user.tokenforgot_code = 100000 + Math.round(Math.random() * 899999);
user.lasttimeonline = new Date();
return await user.save().then(async () => {
await sendemail.sendEmail_RequestNewPassword(user.lang, user, user.email, user.idapp, user.tokenforgot);
await sendemail.sendEmail_RequestNewPassword(user.lang, user, user.email, user.idapp, user.tokenforgot, user.tokenforgot_code);
return true;
});
@@ -1202,10 +1217,12 @@ UserSchema.statics.createNewRequestPwdByUsernameAndGetLink = async function (ida
user.tokenforgot = jwt.sign(user._id.toHexString(), process.env.SIGNCODE).
toString();
user.date_tokenforgot = new Date();
user.tokenforgot_code = 100000 + Math.round(Math.random() * 899999);
user.lasttimeonline = new Date();
user.code_pwd_reset = 0;
return await user.save().then(() => {
return tools.getlinkRequestNewPassword(idapp, user.email, user.tokenforgot);
return tools.getlinkRequestNewPassword(idapp, user.email, user.tokenforgot, user.tokenforgot_code);
});
}
@@ -3501,6 +3518,18 @@ UserSchema.statics.getLastOnlineUsers = async function (idapp) {
};
UserSchema.statics.getDiffusoriUsers = async function (idapp) {
const User = this;
const lastn = 10;
return await User.aggregate(User.getQueryUsersDiffusori(idapp)).then(ris => {
// console.table(ris);
return ris;
});
};
UserSchema.statics.checkUser = async function (idapp, username) {
const User = this;
@@ -3580,6 +3609,7 @@ UserSchema.statics.findAllDistinctNationality = async function (idapp) {
});
};
UserSchema.statics.getUsersRegDaily = function (idapp, nrec) {
const query = [
@@ -3611,6 +3641,101 @@ UserSchema.statics.getUsersRegDaily = function (idapp, nrec) {
return query;
};
UserSchema.statics.getQueryUsersDiffusori = function (idapp) {
const query = [
{
$match: {
idapp,
$or: [
{
deleted: {
$exists: false,
},
},
{
deleted: {
$exists: true,
$eq: false,
},
},
],
},
},
{
$group: {
_id: "$aportador_solidario",
count: {
$sum: 1,
},
},
},
{
$match: { "count": { $gte: 2 } }
},
{
$sort: {
count: -1,
},
},
{ $limit: 20 },
{
$lookup: {
from: "users",
let: {
username: "$_id",
idapp,
},
pipeline: [
{
$match: {
$expr: {
$and: [
{
$eq: [
"$$username",
"$username",
],
},
{
$eq: [
"$$idapp",
"$idapp",
],
},
],
},
},
},
],
as: "user",
},
},
{ $unwind: "$user" },
{
$replaceRoot: {
newRoot: {
$mergeObjects: [ "$user", "$$ROOT" ],
},
},
},
{
$project: {
_id: 0,
count: 1,
aportador_solidario: 1,
username: 1,
name: 1,
surname: 1,
lasttimeonline: 1,
idapp: 1,
"profile.img": 1,
},
},
];
return query;
};
UserSchema.statics.getUsersRegWeekly = function (idapp, nrec) {
const query = [

View File

@@ -161,18 +161,38 @@ router.post(process.env.LINK_REQUEST_NEWPASSWORD, async (req, res) => {
// Invio la Nuova Password richiesta dal reset!
// Ritorna il token per poter effettuare le chiamate...
router.post(process.env.LINK_UPDATE_PWD, (req, res) => {
const body = _.pick(req.body, ['idapp', 'email', 'tokenforgot', 'password']);
router.post(process.env.LINK_UPDATE_PWD, async (req, res) => {
const body = _.pick(req.body, ['idapp', 'email', 'tokenforgot', 'tokenforgot_code', 'password']);
const idapp = body.idapp;
const email = body.email.toLowerCase().trim();
const tokenforgot = body.tokenforgot;
const tokenforgot_code = body.tokenforgot_code;
const password = body.password;
const msg = 'Richiesta Nuova Password: idapp= ' + idapp + ' email = ' + email;
console.log(msg);
// telegrambot.sendMsgTelegramToTheManagers(body.idapp, msg);
User.findByLinkTokenforgot(idapp, email, tokenforgot).then((user) => {
let user = null;
user = await User.findByLinkTokenforgot(idapp, email, tokenforgot)
.then((user) => {
return user;
}).catch((e) => {
console.log(process.env.LINK_UPDATE_PWD, e.message);
res.status(400).send();
});
if (!user) {
user = await User.findByLinkTokenforgotCode(idapp, email, tokenforgot_code)
.then((user) => {
return user;
}).catch((e) => {
console.log(process.env.LINK_UPDATE_PWD, e.message);
res.status(400).send();
});
}
if (!user) {
return res.send(
{ code: server_constants.RIS_CODE_TOKEN_RESETPASSWORD_NOT_FOUND });
@@ -184,6 +204,7 @@ router.post(process.env.LINK_UPDATE_PWD, (req, res) => {
// Crea token
user.generateAuthToken(req).then(token => {
user.tokenforgot = ''; // Svuota il tokenforgot perché non ti servirà più...
user.tokenforgot_code = ''; // Svuota il tokenforgot perché non ti servirà più...
// Salva lo User
user.save().then(() => {
@@ -192,11 +213,6 @@ router.post(process.env.LINK_UPDATE_PWD, (req, res) => {
});
});
}
}).catch((e) => {
console.log(process.env.LINK_UPDATE_PWD, e.message);
res.status(400).send();
});
});
router.post('/testServer', authenticate_noerror, async (req, res) => {

View File

@@ -48,6 +48,7 @@ router.post('/load', async (req, res) => {
reg_weekly: await User.calcRegWeekly(idapp),
lastsreg: await User.getLastUsers(idapp),
lastsonline: await User.getLastOnlineUsers(idapp),
diffusorilist: await User.getDiffusoriUsers(idapp),
checkuser: await User.checkUser(idapp, username),
// navi_partite: await Nave.getNaviPartite(idapp),
// navi_in_partenza: await Nave.getNaviInPartenza(idapp),

View File

@@ -278,7 +278,7 @@ module.exports = {
}
},
sendEmail_RequestNewPassword: async function(lang, user, emailto, idapp, tokenforgot) {
sendEmail_RequestNewPassword: async function(lang, user, emailto, idapp, tokenforgot, tokenforgot_code) {
let mylocalsconf = {
idapp,
@@ -286,6 +286,7 @@ module.exports = {
locale: lang,
nomeapp: tools.getNomeAppByIdApp(idapp),
strlinksetpassword: tools.getlinkRequestNewPassword(idapp, emailto, tokenforgot),
tokenforgot_code,
emailto: emailto,
};