- MyProfile

- Immagine profilo e small
This commit is contained in:
paoloar77
2022-01-03 21:53:50 +01:00
parent b587b5e0a7
commit 181af3c895
7 changed files with 447 additions and 326 deletions

View File

@@ -93,6 +93,13 @@ CitySchema.statics.getFieldsForSearch = function () {
CitySchema.statics.executeQueryTable = function (idapp, params) {
params.fieldsearch = this.getFieldsForSearch();
const strfind = params.search;
if (strfind === '') {
return [];
}
return tools.executeQueryTable(this, 0, params);
};

View File

@@ -865,7 +865,8 @@ UserSchema.statics.setUserQualified = async function(idapp, username) {
return !!myrec;
};
UserSchema.statics.setVerifiedByAportador = async function(idapp, username, valuebool) {
UserSchema.statics.setVerifiedByAportador = async function(
idapp, username, valuebool) {
const User = this;
if (username === undefined)
@@ -884,11 +885,11 @@ UserSchema.statics.setVerifiedByAportador = async function(idapp, username, valu
UserSchema.statics.setVerifiedByAportadorToALL = async function() {
return User.updateMany({}, {$set: {'verified_by_aportador': true}}, {new: false});
return User.updateMany({}, {$set: {'verified_by_aportador': true}},
{new: false});
};
UserSchema.statics.setUserQualified_2Invitati = async function(
idapp, username) {
const User = this;
@@ -1147,6 +1148,51 @@ UserSchema.statics.getUserById = function(idapp, id) {
});
};
UserSchema.statics.getUserProfileByUsername = async function(idapp, username) {
let perm = tools.Perm.PERM_ALL; //++Todo: sistemare
let whatToShow = {};
if (perm === tools.Perm.PERM_ALL) {
whatToShow = {
lang: 1,
index: 1,
username: 1,
aportador_solidario: 1,
name: 1,
surname: 1,
deleted: 1,
sospeso: 1,
verified_email: 1,
verified_by_aportador: 1,
'profile.nationality': 1,
'profile.biografia': 1,
'profile.teleg_id': 1,
'profile.username_telegram': 1,
'profile.img': 1,
'profile.sex': 1,
'profile.dateofbirth': 1,
'profile.born_city': 1,
'profile.born_province': 1,
'profile.born_country': 1,
email: 1,
date_reg: 1,
img: 1,
};
}
return User.findOne({
idapp, username,
$or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}],
}, whatToShow).then((rec) => {
return (rec._doc);
}).catch((e) => {
return null;
});
};
UserSchema.statics.getAportadorSolidarioByUsername = async function(
idapp, username) {
const User = this;

View File

@@ -319,6 +319,8 @@ function getTableByTableName(tablename) {
mytable = Level;
else if (shared_consts.TablePickup.includes(tablename))
mytable = Pickup;
else if (shared_consts.TableCities.includes(tablename))
mytable = City;
return mytable;
}

View File

@@ -28,22 +28,20 @@ const reg = require('../reg/registration');
const {authenticate} = require('../middleware/authenticate');
const Cart = require('../models/cart');
const CartClass = require('../modules/Cart')
const Product = require('../models/product')
const Variant = require('../models/variant')
const TypedError = require('../modules/ErrorHandler')
const CartClass = require('../modules/Cart');
const Product = require('../models/product');
const Variant = require('../models/variant');
const TypedError = require('../modules/ErrorHandler');
const mongoose = require('mongoose').set('debug', false)
const mongoose = require('mongoose').set('debug', false);
const Subscription = mongoose.model('subscribers');
function existSubScribe(userId, access, browser) {
return Subscription.findOne({ userId, access, browser })
.then(itemsub => {
return itemsub
})
.catch(err => {
return null
})
return Subscription.findOne({userId, access, browser}).then(itemsub => {
return itemsub;
}).catch(err => {
return null;
});
}
@@ -53,8 +51,9 @@ function getMobileComplete(user) {
// str = str.replace(/.+/g, '');
// str = str.replace(/-+/g, '');
return str
return str;
}
router.post('/test1', async (req, res) => {
const user = await User.findOne({
@@ -65,13 +64,22 @@ router.post('/test1', async (req, res) => {
await sendemail.sendEmail_Registration(user.lang, user.email, user,
user.idapp, user.linkreg);
})
});
// POST /users
router.post('/', async (req, res) => {
tools.mylog("POST /users");
const body = _.pick(req.body, ['email', 'password', 'username', 'name', 'surname', 'idapp', 'keyappid', 'lang', 'profile', 'aportador_solidario']);
tools.mylog('POST /users');
const body = _.pick(req.body, [
'email',
'password',
'username',
'name',
'surname',
'idapp',
'keyappid',
'lang',
'profile',
'aportador_solidario']);
body.email = body.email.toLowerCase();
const user = new User(body);
@@ -79,19 +87,21 @@ router.post('/', async (req, res) => {
// tools.mylog("LANG PASSATO = " + user.lang, "IDAPP", user.idapp);
if (!tools.isAlphaNumeric(body.username) || body.email.length < 6 || body.username.length < 6 || body.password.length < 6) {
if (!tools.isAlphaNumeric(body.username) || body.email.length < 6 ||
body.username.length < 6 || body.password.length < 6) {
await tools.snooze(5000);
res.status(400).send({ code: server_constants.RIS_CODE_USERNAME_NOT_VALID, msg: '' });
res.status(400).
send({code: server_constants.RIS_CODE_USERNAME_NOT_VALID, msg: ''});
return 1;
}
if (tools.blockwords(body.username) || tools.blockwords(body.name) || tools.blockwords(body.surname)) {
if (tools.blockwords(body.username) || tools.blockwords(body.name) ||
tools.blockwords(body.surname)) {
// tools.writeIPToBan(user.ipaddr + ': [' + user.username + '] ' + user.name + ' ' + user.surname);
await tools.snooze(5000);
return res.status(404).send();
}
user.linkreg = reg.getlinkregByEmail(body.idapp, body.email, body.username);
user.verified_email = false;
user.lasttimeonline = new Date();
@@ -104,15 +114,18 @@ router.post('/', async (req, res) => {
// Controlla se anche l'ultimo record era dallo stesso IP:
const lastrec = await User.getLastRec(body.idapp);
if (!!lastrec) {
if (process.env.LOCALE !== "1") {
if (process.env.LOCALE !== '1') {
if (lastrec.ipaddr === user.ipaddr) {
// Se l'ha fatto troppo ravvicinato
if (lastrec.date_reg) {
let ris = tools.isdiffSecDateLess(lastrec.date_reg, 120);
if (ris) {
tools.writeIPToBan(user.ipaddr + ': [' + user.username + '] ' + user.name + ' ' + user.surname);
tools.writeIPToBan(
user.ipaddr + ': [' + user.username + '] ' + user.name + ' ' +
user.surname);
await tools.snooze(10000);
res.status(400).send({ code: server_constants.RIS_CODE_BANIP, msg: '' });
res.status(400).
send({code: server_constants.RIS_CODE_BANIP, msg: ''});
return 1;
}
}
@@ -133,9 +146,14 @@ router.post('/', async (req, res) => {
let exit;
// Check if already esist email or username
exit = await User.findByUsername(user.idapp, user.username).then((useralreadyexist) => {
exit = await User.findByUsername(user.idapp, user.username).
then((useralreadyexist) => {
if (useralreadyexist) {
res.status(400).send({ code: server_constants.RIS_CODE_USERNAME_ALREADY_EXIST, msg: '' });
res.status(400).
send({
code: server_constants.RIS_CODE_USERNAME_ALREADY_EXIST,
msg: '',
});
return 1;
}
@@ -144,10 +162,14 @@ router.post('/', async (req, res) => {
if (exit === 1)
return;
exit = await User.findByEmail(user.idapp, user.email)
.then((useralreadyexist) => {
exit = await User.findByEmail(user.idapp, user.email).
then((useralreadyexist) => {
if (useralreadyexist) {
res.status(400).send({ code: server_constants.RIS_CODE_EMAIL_ALREADY_EXIST, msg: '' });
res.status(400).
send({
code: server_constants.RIS_CODE_EMAIL_ALREADY_EXIST,
msg: '',
});
return 1;
}
@@ -158,12 +180,14 @@ router.post('/', async (req, res) => {
let recuser = null;
recuser = await User.findByCellAndNameSurname(user.idapp, user.profile.cell, user.name, user.surname);
recuser = await User.findByCellAndNameSurname(user.idapp, user.profile.cell,
user.name, user.surname);
if (recuser) {
console.log('UTENTE GIA ESISTENTE:\n');
console.log(user);
// User already registered!
res.status(400).send({ code: server_constants.RIS_CODE_USER_ALREADY_EXIST, msg: '' });
res.status(400).
send({code: server_constants.RIS_CODE_USER_ALREADY_EXIST, msg: ''});
return 1;
}
@@ -190,8 +214,8 @@ router.post('/', async (req, res) => {
return 1;
}*/
let already_registered = (recextra || user.aportador_solidario === tools.APORTADOR_NONE) && (user.idapp === tools.AYNI);
let already_registered = (recextra || user.aportador_solidario ===
tools.APORTADOR_NONE) && (user.idapp === tools.AYNI);
// Check if is an other people aportador_solidario
@@ -207,10 +231,9 @@ router.post('/', async (req, res) => {
return 1;
} */
return user.save()
.then(async () => {
return User.findByUsername(user.idapp, user.username, false)
.then((usertrovato) => {
return user.save().then(async () => {
return User.findByUsername(user.idapp, user.username, false).
then((usertrovato) => {
// tools.mylog("TROVATO USERNAME ? ", user.username, usertrovato);
if (usertrovato !== null) {
@@ -219,8 +242,8 @@ router.post('/', async (req, res) => {
res.status(400).send();
return 0;
}
})
.then(async (token) => {
}).
then(async (token) => {
// tools.mylog("passo il TOKEN: ", token);
if (recextra) {
@@ -231,15 +254,16 @@ router.post('/', async (req, res) => {
// await User.fixUsername(user.idapp, user.ind_order, user.username);
}
return token;
})
.then(async (token) => {
}).
then(async (token) => {
// tools.mylog("LINKREG = " + user.linkreg);
// Invia un'email all'utente
// tools.mylog('process.env.TESTING_ON', process.env.TESTING_ON);
console.log('res.locale', res.locale);
// if (!tools.testing()) {
await sendemail.sendEmail_Registration(user.lang, user.email, user, user.idapp, user.linkreg);
await sendemail.sendEmail_Registration(user.lang, user.email, user,
user.idapp, user.linkreg);
// }
res.header('x-auth', token).send(user);
return true;
@@ -247,7 +271,7 @@ router.post('/', async (req, res) => {
}).catch((e) => {
console.error(e.message);
res.status(400).send(e);
})
});
});
router.get('/:idapp/:username', async (req, res) => {
@@ -277,7 +301,8 @@ router.patch('/:id', authenticate, (req, res) => {
if (!User.isAdmin(req.user.perm)) {
// If without permissions, exit
return res.status(404).send({ code: server_constants.RIS_CODE_ERR_UNAUTHORIZED, msg: '' });
return res.status(404).
send({code: server_constants.RIS_CODE_ERR_UNAUTHORIZED, msg: ''});
}
User.findByIdAndUpdate(id, {$set: body}).then((user) => {
@@ -291,12 +316,28 @@ router.patch('/:id', authenticate, (req, res) => {
}).catch((e) => {
tools.mylogserr('Error patch USER: ', e);
res.status(400).send();
})
});
});
router.post('/profile', (req, res) => {
const username = req.body['username'];
idapp = req.body.idapp;
locale = req.body.locale;
//++Todo: controlla che tipo di dati ha il permesso di leggere
return User.getUserProfileByUsername(idapp, username).then((ris) => {
res.send(ris);
}).catch((e) => {
tools.mylog('ERRORE IN Profile: ' + e.message);
res.status(400).send();
});
});
router.post('/login', (req, res) => {
var body = _.pick(req.body, ['username', 'password', 'idapp', 'keyappid', 'lang']);
var body = _.pick(req.body,
['username', 'password', 'idapp', 'keyappid', 'lang']);
var user = new User(body);
// const subs = _.pick(req.body, ['subs']);
@@ -309,12 +350,14 @@ router.post('/login', (req, res) => {
let resalreadysent = false;
User.findByCredentials(user.idapp, user.username, user.password)
.then(async (user) => {
User.findByCredentials(user.idapp, user.username, user.password).
then(async (user) => {
// tools.mylog("CREDENZIALI ! ");
if (!user) {
await tools.snooze(3000);
const msg = "Tentativo di Login ERRATO [" + body.username + ' , ' + body.password + ']\n' + '[IP: ' + tools.getiPAddressUser(req) + ']';
const msg = 'Tentativo di Login ERRATO [' + body.username + ' , ' +
body.password + ']\n' + '[IP: ' + tools.getiPAddressUser(req) +
']';
tools.mylogshow(msg);
// telegrambot.sendMsgTelegramToTheManagers(body.idapp, msg);
res.status(404).send({code: server_constants.RIS_CODE_LOGIN_ERR});
@@ -323,18 +366,19 @@ router.post('/login', (req, res) => {
// const msg = "Tentativo di Login ERRATO [" + body.username + ' , ' + body.password + ']\n' + '[IP: ' + tools.getiPAddressUser(req) + ']';
// tools.mylogshow(msg);
// telegrambot.sendMsgTelegramToTheManagers(body.idapp, msg);
res.status(404).send({ code: server_constants.RIS_CODE_LOGIN_ERR_SUBACCOUNT });
res.status(404).
send({code: server_constants.RIS_CODE_LOGIN_ERR_SUBACCOUNT});
return null;
}
return user
})
.then(user => {
return user;
}).
then(user => {
if (user) {
return user.generateAuthToken(req).then((token) => {
var usertosend = new User();
shared_consts.fieldsUserToChange().forEach((field) => {
usertosend[field] = user[field]
usertosend[field] = user[field];
});
// usertosend._id = user._id.toHexString();
@@ -345,21 +389,31 @@ router.post('/login', (req, res) => {
// tools.mylog("user.verified_email:" + user.verified_email);
// tools.mylog("usertosend.userId", usertosend.userId);
return { usertosend, token }
return {usertosend, token};
})
.then((myris) => {
}).then((myris) => {
const access = 'auth';
const browser = req.get('User-Agent');
// Check if already exist Subscribe
return existSubScribe(myris.usertosend._id, access, browser).then(subscribe => {
return (subscribe !== null)
}).then(subsExistonDb => {
return { usertosend: myris.usertosend, token: myris.token, subsExistonDb }
}).catch(err => {
return { usertosend: myris.usertosend, token: myris.token, subsExistonDb: false }
})
return existSubScribe(myris.usertosend._id, access, browser).
then(subscribe => {
return (subscribe !== null);
}).
then(subsExistonDb => {
return {
usertosend: myris.usertosend,
token: myris.token,
subsExistonDb,
};
}).
catch(err => {
return {
usertosend: myris.usertosend,
token: myris.token,
subsExistonDb: false,
};
});
}).then(myris => {
// console.log('res', myris.token, myris.usertosend);
@@ -367,7 +421,7 @@ router.post('/login', (req, res) => {
res.header('x-auth', myris.token).send({
usertosend: myris.usertosend,
code: server_constants.RIS_CODE_OK,
subsExistonDb: myris.subsExistonDb
subsExistonDb: myris.subsExistonDb,
});
// tools.mylog("TROVATOOO!");
@@ -375,11 +429,12 @@ router.post('/login', (req, res) => {
// tools.mylog('FINE LOGIN')
});
}
})
.catch((e) => {
tools.mylog("ERRORE IN LOGIN: " + e.message);
}).
catch((e) => {
tools.mylog('ERRORE IN LOGIN: ' + e.message);
if (!resalreadysent)
res.status(400).send({ code: server_constants.RIS_CODE_LOGIN_ERR_GENERIC });
res.status(400).
send({code: server_constants.RIS_CODE_LOGIN_ERR_GENERIC});
});
});
@@ -394,7 +449,7 @@ router.delete('/me/token', authenticate, (req, res) => {
router.post('/setperm', authenticate, (req, res) => {
const body = _.pick(req.body, ['idapp', 'username', 'perm']);
tools.mylog("SETPERM = " + req.token);
tools.mylog('SETPERM = ' + req.token);
User.setPermissionsById(res.user._id, body).then(() => {
res.status(200).send();
@@ -657,7 +712,8 @@ async function eseguiDbOp(idapp, mydata, locale) {
}
ris = { num };
*/} else if (mydata.dbop === 'creaUtentiTest') {
*/
} else if (mydata.dbop === 'creaUtentiTest') {
let num = 0;
lastrec = await User.find({idapp}).sort({_id: -1}).limit(1);
@@ -674,16 +730,16 @@ async function eseguiDbOp(idapp, mydata, locale) {
myuser._id = new ObjectID();
myuser.index = last + ind + 1;
myuser.idapp = idapp;
myuser.password = "$2a$12$DEaX1h5saTUVC43f7kubyOAlah1xHDgqQTfSIux0.RFDT9WGbyCaG";
myuser.password = '$2a$12$DEaX1h5saTUVC43f7kubyOAlah1xHDgqQTfSIux0.RFDT9WGbyCaG';
myuser.lang = 'it';
myuser.email = "miaemail@email.it";
myuser.email = 'miaemail@email.it';
myuser.name = 'U' + myuser.index;
myuser.surname = 'Ar' + myuser.index;
myuser.verified_email = true;
myuser.verified_by_aportador = true;
if (myuser.index < 2)
myuser.perm = "3";
myuser.username = "Userna_" + myuser.name;
myuser.perm = '3';
myuser.username = 'Userna_' + myuser.name;
myuser.profile.special_req = true;
myuser.profile.nationality = 'IT';
await myuser.save();
@@ -749,7 +805,6 @@ async function eseguiDbOp(idapp, mydata, locale) {
*/
}
// console.log('ris', ris);
return ris;
@@ -771,5 +826,4 @@ router.post('/dbop', authenticate, async (req, res) => {
});
module.exports = router;

View File

@@ -408,6 +408,12 @@ module.exports = {
SEND_MSG_DONO_NON_RICEVUTO: 1050,
},
Perm: {
PERM_NONE: 0,
PERM_FRIEND: 1,
PERM_ALL: 10,
},
Placca: {
DONATORI: 1,
TUTTI: 2,
@@ -1218,7 +1224,12 @@ module.exports = {
}
if (params.filtercustom) {
filtriadded.push(...params.filtercustom);
for (const myfilter of params.filtercustom) {
if (myfilter["userId"]) {
myfilter["userId"] = ObjectID(myfilter["userId"]);
}
filtriadded.push(myfilter);
}
}
if (params.filtersearch) {

View File

@@ -92,7 +92,7 @@ module.exports = Object.freeze({
LIST_START: null,
PREFIX_IMG: 'm_',
PREFIX_IMG_SMALL: 'small_',
PREFIX_IMG_SMALL: 'sm_',
Privacy: {
all: 'all',

View File

@@ -43,6 +43,7 @@ module.exports = {
KEY_TO_CRYPTED: ['PWD_FROM'],
TablePickup: ['countries', 'phones'],
TableCities: ['citta', 'province'],
PaymentTypes: [
'Nessuno',