- fix: se inserisco un username telegram di chi mi ha invitato, non facevo il controllo.
This commit is contained in:
@@ -876,6 +876,57 @@ UserSchema.statics.findByUsername = async function (idapp, username, alsoemail,
|
||||
return rec;
|
||||
});
|
||||
};
|
||||
/**
|
||||
* Find a user by their Telegram username.
|
||||
*
|
||||
* @param {string} idapp - The application ID
|
||||
* @param {string} username - The Telegram username
|
||||
* @param {boolean} alsoemail - Flag to indicate if email should also be considered
|
||||
* @param {boolean} onlyifVerifiedByAportador - Flag to indicate if only verified users should be returned
|
||||
* @return {Promise} A Promise that resolves to the found user or null
|
||||
**/
|
||||
|
||||
UserSchema.statics.findByUsernameTelegram = async function (idapp, username, alsoemail, onlyifVerifiedByAportador) {
|
||||
const User = this;
|
||||
|
||||
if (username && username[0] === '@') {
|
||||
username = username.substring(1);
|
||||
}
|
||||
|
||||
const myreg = ['^', username, '$'].join('');
|
||||
let regexusername = new RegExp(myreg, 'i');
|
||||
|
||||
//++TODO: Set only the necessary fields to get in memory
|
||||
|
||||
return await User.findOne({
|
||||
idapp: idapp,
|
||||
'profile.username_telegram': { $regex: regexusername },
|
||||
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }],
|
||||
}).then(async (ris) => {
|
||||
if ((!ris) && (alsoemail)) {
|
||||
regexemail = new RegExp(['^', username.toLowerCase(), '$'].join(''), 'i');
|
||||
|
||||
return await User.findOne({
|
||||
'idapp': idapp,
|
||||
'email': { $regex: regexemail },
|
||||
$or: [
|
||||
{ deleted: { $exists: false } },
|
||||
{ deleted: { $exists: true, $eq: false } }],
|
||||
});
|
||||
}
|
||||
return ris;
|
||||
}).then((rec) => {
|
||||
if (rec && onlyifVerifiedByAportador) {
|
||||
if (tools.getAskToVerifyReg(idapp)) {
|
||||
if (!rec.verified_by_aportador)
|
||||
return null;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return rec;
|
||||
});
|
||||
};
|
||||
|
||||
UserSchema.statics.getProjectUser = function () {
|
||||
return {
|
||||
@@ -1728,6 +1779,20 @@ UserSchema.statics.getUserByUsername = function (idapp, username) {
|
||||
});
|
||||
};
|
||||
|
||||
UserSchema.statics.getUserByUsernameTelegram = function (idapp, username_telegram) {
|
||||
const User = this;
|
||||
|
||||
if (username_telegram[0] === '@'){
|
||||
username_telegram = username_telegram.substring(1);
|
||||
}
|
||||
|
||||
return User.findOne({
|
||||
idapp,
|
||||
'profile.username_telegram': username_telegram,
|
||||
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }],
|
||||
});
|
||||
};
|
||||
|
||||
UserSchema.statics.isMyFriend = async function (idapp, username, myusername) {
|
||||
|
||||
const myfriends = await User.getUsernameFriendsByUsername(idapp, myusername);
|
||||
|
||||
@@ -266,8 +266,16 @@ router.post('/', async (req, res) => {
|
||||
|
||||
user.aportador_solidario = user.aportador_solidario.trim();
|
||||
|
||||
const id_aportador = await User.getIdByUsername(user.idapp, user.aportador_solidario);
|
||||
let id_aportador = await User.getIdByUsername(user.idapp, user.aportador_solidario);
|
||||
if (!id_aportador) {
|
||||
// Cerca se esiste l'aportador solidario con l'username Telegram
|
||||
const useraportador = await User.getUserByUsernameTelegram(user.idapp, user.aportador_solidario);
|
||||
id_aportador = useraportador._id;
|
||||
user.aportador_solidario = useraportador.username;
|
||||
}
|
||||
|
||||
if (id_aportador) {
|
||||
// Ottiene l'username "corretto" (senza maiuscole o minuscole)
|
||||
user.aportador_solidario = await User.getRealUsernameByUsername(user.idapp, user.aportador_solidario);
|
||||
}
|
||||
|
||||
@@ -381,15 +389,18 @@ router.get('/:idapp/:username', async (req, res) => {
|
||||
// return res.status(200).send();
|
||||
// }
|
||||
|
||||
await User.findByUsername(idapp, username, false, true).then((user) => {
|
||||
await User.findByUsername(idapp, username, false, true).then(async (user) => {
|
||||
if (!user) {
|
||||
return res.status(404).send();
|
||||
user = await User.findByUsernameTelegram(idapp, username, false, true);
|
||||
if (!user)
|
||||
return res.status(404).send();
|
||||
}
|
||||
// console.log('TROVATO!')
|
||||
return res.status(200).send();
|
||||
}).catch((e) => {
|
||||
return res.status(400).send();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
router.patch('/:id', authenticate, (req, res) => {
|
||||
|
||||
Reference in New Issue
Block a user