diff --git a/src/server/models/user.js b/src/server/models/user.js index 42aa8f9..18030ab 100755 --- a/src/server/models/user.js +++ b/src/server/models/user.js @@ -693,16 +693,8 @@ UserSchema.statics.findByUsername = async function(idapp, username, alsoemail, o }); }; -UserSchema.statics.getUserShortDataByUsername = async function(idapp, username) { - const User = this; - - let regexp = new RegExp(`^${username}$`, 'i'); - - const myrec = await User.findOne({ - 'idapp': idapp, - username: {$regex: regexp}, - $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], - }, { +UserSchema.statics.getProjectUser = function() { + return { idapp: 1, lang: 1, index: 1, @@ -740,15 +732,45 @@ UserSchema.statics.getUserShortDataByUsername = async function(idapp, username) email: 1, date_reg: 1, img: 1, - }).then((ris) => { - if (!!ris) { - // console.log('ris', ris); - if (!!ris._doc) - return ris._doc; - else - return null; - } - }); + }; + +}; + +UserSchema.statics.getUserShortDataByUsername = async function(idapp, username) { + const User = this; + + let regexp = new RegExp(`^${username}$`, 'i'); + + const myrec = await User.findOne({ + 'idapp': idapp, + username: {$regex: regexp}, + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], + }, + this.getProjectUser(), + ).lean(); + + if (myrec) { + myrec.qualified = await User.isUserQualified7(idapp, myrec.username); + myrec.numNaviEntrato = 0; + // myrec.numinvitati = await ListaIngresso.getnumInvitati(idapp, myrec.username); + // myrec.numinvitatiattivi = await ListaIngresso.getnumInvitatiAttivi(idapp, myrec.username); + } + + return myrec; +}; + +UserSchema.statics.getUserShortDataByUsernameTelegram = async function(idapp, username_telegram) { + const User = this; + + let regexp = new RegExp(`^${username_telegram}$`, 'i'); + + const myrec = await User.findOne({ + 'idapp': idapp, + 'profile.username_telegram': {$regex: regexp}, + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], + }, + this.getProjectUser(), + ).lean(); if (myrec) { myrec.qualified = await User.isUserQualified7(idapp, myrec.username); @@ -2643,13 +2665,13 @@ UserSchema.statics.SetTelegramIdSuccess = async function(idapp, id, teleg_id) { }; UserSchema.statics.getUsernameTelegram = async function(idapp, username) { - const User = this; - return await User.findOne({idapp, username}, {'profile.username_telegram': 1}).lean().then((ris) => { - if (ris) - return ris.profile.username_telegram - else - return ''; - }); + const User = this; + return await User.findOne({idapp, username}, {'profile.username_telegram': 1}).lean().then((ris) => { + if (ris) + return ris.profile.username_telegram; + else + return ''; + }); }; UserSchema.statics.setUsernameTelegram = async function( diff --git a/src/server/telegram/telegrambot.js b/src/server/telegram/telegrambot.js index 60485e1..7fe7740 100755 --- a/src/server/telegram/telegrambot.js +++ b/src/server/telegram/telegrambot.js @@ -2587,8 +2587,12 @@ class Telegram { let aportador_solidario = rec.aportador_solidario; if (!aportador_solidario) { aportador_solidario = msg.text.toString().trim().toLowerCase(); - tools.removeAtChar(aportador_solidario); - const user = await User.getUserShortDataByUsername(this.idapp, aportador_solidario); + aportador_solidario = tools.removeAtChar(aportador_solidario); + let user = await User.getUserShortDataByUsername(this.idapp, aportador_solidario); + if (!user) { + // check if is username telegram inserted + user = await User.getUserShortDataByUsernameTelegram(this.idapp, aportador_solidario); + } if (user) rec.aportador_solidario = user.username; }