diff --git a/src/server/models/myhosp.js b/src/server/models/myhosp.js new file mode 100755 index 0000000..57e3447 --- /dev/null +++ b/src/server/models/myhosp.js @@ -0,0 +1,498 @@ +const mongoose = require('mongoose').set('debug', false); +const Schema = mongoose.Schema; + +mongoose.Promise = global.Promise; +mongoose.level = 'F'; + +const tools = require('../tools/general'); + +const {ObjectID} = require('mongodb'); + +// Resolving error Unknown modifier: $pushAll +mongoose.plugin(schema => { + schema.options.usePushEach = true; +}); + +const MyHospSchema = new Schema({ + _id: { + type: Number, + }, + idapp: { + type: String, + required: true, + }, + userId: {type: Schema.Types.ObjectId, ref: 'User'}, + idSector: { + type: Number, + }, + idSkill: { + type: Number, + default: 0, + }, + idStatusSkill: [ + { + type: Number, + }], + idContribType: [ + { + type: String, + }], + idCity: [ + { + type: Number, + }], + dateTimeStart: { + type: Date, + }, + dateTimeEnd: { + type: Date, + }, + numLevel: { + type: Number, + default: 0, + }, + adType: { + type: Number, + }, + photos: [ + { + imagefile: { + type: String, + }, + alt: { + type: String, + }, + description: { + type: String, + }, + }], + note: { + type: String, + default: '', + }, + descr: { + type: String, + }, + //**ADDFIELD_MyHospS + website: { + type: String, + }, + date_created: { + type: Date, + default: Date.now, + }, + date_updated: { + type: Date, + default: Date.now, + }, +}); + +MyHospSchema.pre('save', async function(next) { + if (this.isNew) { + const myrec = await MyHosp.findOne().limit(1).sort({_id: -1}); + if (!!myrec) { + if (myrec._doc._id === 0) + this._id = 1; + else + this._id = myrec._doc._id + 1; + + } else { + this._id = 1; + } + + this.date_created = new Date(); + } + + next(); +}); + +MyHospSchema.statics.findAllIdApp = async function(idapp) { + const MyHosp = this; + + const query = [ + {$match: {idapp}}, + {$sort: {descr: 1}}, + ]; + + return MyHosp.aggregate(query).then((arrrec) => { + return arrrec; + }); + +}; + +MyHospSchema.statics.getFieldsForSearch = function() { + return []; +}; + +MyHospSchema.statics.getFieldsLastForSearch = function() { + return [ + {field: 'note', type: tools.FieldType.string}, + {field: 'descr', type: tools.FieldType.string}, + {field: 'recSkill.descr', type: tools.FieldType.string}, + {field: 'MyHosp.descr', type: tools.FieldType.string}, + ]; +}; + + +MyHospSchema.statics.executeQueryTable = function(idapp, params) { + params.fieldsearch = this.getFieldsForSearch(); + params.fieldsearch_last = this.getFieldsLastForSearch(); + + const otherparams = { + lookup1: { + lk_tab: 'users', + lk_LF: 'userId', + lk_FF: '_id', + lk_as: 'user', + af_objId_tab: 'myId', + lk_proj: { + idSkill: 1, + idSubSkill: 1, + MyHosp: 1, + idStatusSkill: 1, + idContribType: 1, + dateTimeStart: 1, + dateTimeEnd: 1, + idCity: 1, + numLevel: 1, + adType: 1, + photos: 1, + note: 1, + //**ADDFIELD_MyHospS + website: 1, + descr: 1, + date_created: 1, + date_updated: 1, + userId: 1, + username: 1, + name: 1, + surname: 1, + 'profile.img': 1, + 'profile.qualifica': 1, + }, + }, + }; + + params = {...params, ...otherparams}; + + return tools.executeQueryTable(this, idapp, params); +}; + +MyHospSchema.statics.getMyRecById = function(idapp, id) { + const MyHosp = this; + + const query = [ + { + '$match': { + '$and': [ + { + '_id': parseInt(id), + }, + ], + }, + }, + { + '$match': { + 'idapp': idapp, + }, + }, + { + '$sort': { + 'desc': 1, + }, + }, + { + '$addFields': { + 'myId1': { + '$toObjectId': '$userId', + }, + }, + }, + { + '$lookup': { + 'from': 'users', + 'localField': 'myId1', + 'foreignField': '_id', + 'as': 'user', + }, + }, + { + '$replaceRoot': { + 'newRoot': { + '$mergeObjects': [ + { + '$arrayElemAt': [ + '$user', + 0, + ], + }, + '$$ROOT', + ], + }, + }, + }, + { + '$project': { + 'recSkill': 1, + 'sector': 1, + 'idSector': 1, + 'idSkill': 1, + 'idSubSkill': 1, + 'idStatusSkill': 1, + 'idContribType': 1, + dateTimeStart: 1, + dateTimeEnd: 1, + 'idCity': 1, + 'numLevel': 1, + adType: 1, + 'photos': 1, + 'note': 1, + website: 1, + //**ADDFIELD_MyHospS + 'descr': 1, + 'date_created': 1, + 'date_updated': 1, + 'userId': 1, + 'username': 1, + 'name': 1, + 'surname': 1, + 'comune': 1, + 'mycities': 1, + 'profile.img': 1, + 'profile.qualifica': 1, + }, + }, + { + '$lookup': { + 'from': 'skills', + 'localField': 'idSkill', + 'foreignField': '_id', + 'as': 'recSkill', + }, + }, + { + '$replaceRoot': { + 'newRoot': { + '$mergeObjects': [ + { + '$arrayElemAt': [ + '$recSkill', + 0, + ], + }, + '$$ROOT', + ], + }, + }, + }, + { + '$project': { + 'recSkill': 1, + 'sector': 1, + 'idSector': 1, + 'idSkill': 1, + 'idSubSkill': 1, + 'idStatusSkill': 1, + 'idContribType': 1, + dateTimeStart: 1, + dateTimeEnd: 1, + 'idCity': 1, + 'numLevel': 1, + adType: 1, + 'photos': 1, + 'note': 1, + website: 1, + //**ADDFIELD_MyHospS + 'descr': 1, + 'date_created': 1, + 'date_updated': 1, + 'userId': 1, + 'username': 1, + 'name': 1, + 'surname': 1, + 'comune': 1, + 'mycities': 1, + 'profile.img': 1, + 'profile.qualifica': 1, + }, + }, + { + '$lookup': { + 'from': 'sectors', + 'localField': 'recSkill.idSector', + 'foreignField': '_id', + 'as': 'sector', + }, + }, + { + '$replaceRoot': { + 'newRoot': { + '$mergeObjects': [ + { + '$arrayElemAt': [ + '$sector', + 0, + ], + }, + '$$ROOT', + ], + }, + }, + }, + { + '$project': { + 'recSkill': 1, + 'sector': 1, + 'idSector': 1, + 'idSkill': 1, + 'idSubSkill': 1, + 'idStatusSkill': 1, + 'idContribType': 1, + dateTimeStart: 1, + dateTimeEnd: 1, + 'idCity': 1, + 'numLevel': 1, + adType: 1, + 'photos': 1, + 'note': 1, + website: 1, + //**ADDFIELD_MyHospS + 'descr': 1, + 'date_created': 1, + 'date_updated': 1, + 'userId': 1, + 'username': 1, + 'name': 1, + 'surname': 1, + 'comune': 1, + 'mycities': 1, + 'profile.img': 1, + 'profile.qualifica': 1, + }, + }, + /*{ + '$lookup': { + 'from': 'subskills', + 'localField': 'idSubSkill', + 'foreignField': '_id', + 'as': 'MyHosp', + }, + },*/ + { + '$replaceRoot': { + 'newRoot': { + '$mergeObjects': [ + { + '$arrayElemAt': [ + '$MyHosp', + 0, + ], + }, + '$$ROOT', + ], + }, + }, + }, + { + '$project': { + 'recSkill': 1, + 'sector': 1, + 'idSector': 1, + 'idSkill': 1, + // 'idSubSkill': 1, + 'idStatusSkill': 1, + 'idContribType': 1, + dateTimeStart: 1, + dateTimeEnd: 1, + 'idCity': 1, + 'numLevel': 1, + adType: 1, + 'photos': 1, + 'note': 1, + website: 1, + //**ADDFIELD_MyHospS + 'descr': 1, + 'date_created': 1, + 'date_updated': 1, + 'userId': 1, + 'username': 1, + 'name': 1, + 'surname': 1, + 'comune': 1, + 'mycities': 1, + 'profile.img': 1, + 'profile.qualifica': 1, + }, + }, + { + '$lookup': { + 'from': 'cities', + 'localField': 'idCity', + 'foreignField': '_id', + 'as': 'mycities', + }, + }, + { + '$replaceRoot': { + 'newRoot': { + '$mergeObjects': [ + { + '$arrayElemAt': [ + '$mycities', + 0, + ], + }, + '$$ROOT', + ], + }, + }, + }, + { + '$project': { + 'recSkill': 1, + 'sector': 1, + 'idSector': 1, + 'idSkill': 1, + // 'idSubSkill': 1, + 'idStatusSkill': 1, + 'idContribType': 1, + dateTimeStart: 1, + dateTimeEnd: 1, + 'idCity': 1, + 'numLevel': 1, + adType: 1, + 'photos': 1, + 'note': 1, + website: 1, + //**ADDFIELD_MyHospS + 'descr': 1, + 'date_created': 1, + 'date_updated': 1, + 'userId': 1, + 'username': 1, + 'name': 1, + 'surname': 1, + 'comune': 1, + 'mycities': 1, + 'profile.img': 1, + 'profile.qualifica': 1, + }, + }, + ]; + + return MyHosp.aggregate(query).then((rec) => { + return rec ? rec[0] : null; + }); +}; + +MyHospSchema.statics.getCompleteRecord = function(idapp, id) { + const MyHosp = this; + + return MyHosp.getMyRecById(idapp, id); + +}; + + +const MyHosp = mongoose.model('MyHosp', MyHospSchema); + +module.exports = {MyHosp}; diff --git a/src/server/models/producer.js b/src/server/models/producer.js index a1e2e72..c6469e1 100755 --- a/src/server/models/producer.js +++ b/src/server/models/producer.js @@ -76,19 +76,19 @@ module.exports.getProducerByTitle = function (query,sort, callback) { } module.exports.filterProducerByDepartment = function (department, callback) { - let regexp = new RegExp(`${department}`, 'i') + let regexp = new RegExp(`^${department}$`, 'i') var query = { department: { $regex: regexp } }; Producer.find(query, callback) } module.exports.filterProducerByCategory = function (category, callback) { - let regexp = new RegExp(`${category}`, 'i') + let regexp = new RegExp(`^${category}$`, 'i') var query = { category: { $regex: regexp } }; Producer.find(query, callback); } module.exports.filterProducerByTitle = function (title, callback) { - let regexp = new RegExp(`${title}`, 'i') + let regexp = new RegExp(`^${title}$`, 'i') var query = { title: { $regex: regexp } }; Producer.find(query, callback); } diff --git a/src/server/models/product.js b/src/server/models/product.js index 9373a16..ec5e47f 100755 --- a/src/server/models/product.js +++ b/src/server/models/product.js @@ -162,19 +162,19 @@ module.exports.getProductByCode = function (idapp, code) { } module.exports.filterProductByDepartment = function (department, callback) { - let regexp = new RegExp(`${department}`, 'i') + let regexp = new RegExp(`^${department}$`, 'i') var query = { department: { $regex: regexp } }; Product.find(query, callback) } module.exports.filterProductByCategory = function (category, callback) { - let regexp = new RegExp(`${category}`, 'i') + let regexp = new RegExp(`^${category}$`, 'i') var query = { category: { $regex: regexp } }; Product.find(query, callback); } module.exports.filterProductByTitle = function (title, callback) { - let regexp = new RegExp(`${title}`, 'i') + let regexp = new RegExp(`^${title}$`, 'i') var query = { title: { $regex: regexp } }; Product.find(query, callback); } diff --git a/src/server/models/user.js b/src/server/models/user.js index 670556a..81d229a 100755 --- a/src/server/models/user.js +++ b/src/server/models/user.js @@ -549,7 +549,7 @@ UserSchema.statics.findByCredentials = function(idapp, username, password, pwdcr const User = this; let pwd = ''; - let regexp = new RegExp(`${username}`, 'i'); + let regexp = new RegExp(`^${username}$`, 'i'); return User.findOne({ idapp, @@ -647,7 +647,7 @@ 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'); + let regexp = new RegExp(`^${username}$`, 'i'); const myrec = await User.findOne({ 'idapp': idapp, @@ -2196,7 +2196,7 @@ UserSchema.statics.getNameSurnameByUsername = async function( UserSchema.statics.getIdByUsername = async function(idapp, username) { const User = this; - let regexp = new RegExp(`${username}`, 'i'); + let regexp = new RegExp(`^${username}$`, 'i'); return User.findOne({ idapp, @@ -2212,7 +2212,7 @@ UserSchema.statics.getIdByUsername = async function(idapp, username) { UserSchema.statics.getRealUsernameByUsername = async function(idapp, username) { const User = this; - let regexp = new RegExp(`${username}`, 'i'); + let regexp = new RegExp(`^${username}$`, 'i'); return User.findOne({ idapp, diff --git a/src/server/router/users_router.js b/src/server/router/users_router.js index a80b79c..0af57e6 100755 --- a/src/server/router/users_router.js +++ b/src/server/router/users_router.js @@ -171,8 +171,7 @@ router.post('/', async (req, res) => { if (useralreadyexist) { if (tools.getAskToVerifyReg(useralreadyexist.idapp)) { - if (!useralreadyexist.verified_by_aportador && - useralreadyexist.profile.teleg_id > 0) { + if (!useralreadyexist.verified_by_aportador && useralreadyexist.profile.teleg_id > 0) { if (trovarec) { utentenonancoraVerificato = true; } @@ -316,6 +315,9 @@ router.post('/', async (req, res) => { // Invia un'email all'utente // tools.mylog('process.env.TESTING_ON', process.env.TESTING_ON); console.log('res.locale', res.locale); + + await telegrambot.askConfirmationUser(user.idapp, shared_consts.CallFunz.REGISTRATION, user); + // if (!tools.testing()) { await sendemail.sendEmail_Registration(user.lang, user.email, user, user.idapp, user.linkreg); diff --git a/src/server/telegram/telegrambot.js b/src/server/telegram/telegrambot.js index a7e5816..85a9066 100755 --- a/src/server/telegram/telegrambot.js +++ b/src/server/telegram/telegrambot.js @@ -408,8 +408,9 @@ const MenuYesNo = { const MenuAdmin = { it: { - menu: [[Menu.RESTART_SRV + ' 6711', Menu.LOG_SRV + ' 6711'], - [Menu.REBOOT_SRV+ ' 6711', Menu.it.INDIETRO]], + menu: [ + [Menu.RESTART_SRV + ' 6711', Menu.LOG_SRV + ' 6711'], + [Menu.REBOOT_SRV + ' 6711', Menu.it.INDIETRO]], }, }; @@ -439,8 +440,10 @@ const Status = { EXIT: -5, NONE: 0, VERIFIED: 1, - WAITFOR_USERNAME_BO: 102, - WAITFOR_VERIFY_CODE: 103, + WAITFOR_USERNAME_TELEGRAM: 101, + WAITFOR_USERNAME_INVITANTE: 104, + SET_USERNAME_INVITANTE: 105, + CLICK_LINK_REG: 106, WAITFOR_RISPOSTA: 110, }; @@ -473,6 +476,8 @@ const txt = { MSG_SCEGLI_MENU: emoji.get('dizzy') + ' Scegli una voce di menu:' + emoji.get('dizzy'), MSG_ASK_USERNAME_BO: 'Scrivete nel messaggio l\'username (SENZA SPAZI) o la email con cui vi siete registrati sul sito di %s:', + MSG_ASK_USERNAME_INVITANTE: 'Scrivi l\'username di chi ti ha invitato su %s (non inserire spazi)', + MSG_ERRORE_INVITANTE_NOT_FOUND: 'L\'username dell\'invitante appena digitato non sembra essere corretto!', MSG_ERRORE_USERNAME: 'Attenzione! Devi inserire solo lo username (40 caratteri massimo)', MSG_ERRORE_USERNAME_NOT_FOUND: 'Per Completare la Verifica Telegram BOT, dovete ora scrivere qui sotto nel messaggio l\'Username (senza spazi) OPPURE la email con cui vi siete registrati sul sito', MSG_ERRORE_USERNAME_ANNULLA: 'Inserimento Annullato. Riprovare', @@ -496,13 +501,26 @@ const txt = { MSG_APORTADOR_DEST_CONFIRMED: '✅ Sei stato Abilitato correttamente da %s!\n' + 'Vai sulla App oppure clicca qui per entrare\n👉🏻 %s', MSG_GROUP_CONFIRMED: '✅ Sei stato Aggiunto sul Gruppo %s!', - MSG_APORTADOR_DEST_NOT_CONFIRMED: emo.EXCLAMATION_MARK + '🚫 Ci dispiace ma non sei stato Verificato correttamente dal tuo invitante %s.
Contattalo per farti abilitare !', + MSG_APORTADOR_DEST_NOT_CONFIRMED: emo.EXCLAMATION_MARK + + '🚫 Ci dispiace ma non sei stato Verificato correttamente dal tuo invitante %s.
Contattalo per farti abilitare !', MSG_APORTADOR_NOT_CONFIRMED: emo.EXCLAMATION_MARK + '🚫 %s Non è stato Abilitato !', MSG_ISCRITTO_CONACREIS: emo.FIRE + ' Si è appena Iscritto al Conacreis "%s"', MSG_MSG_SENT: emoji.get('envelope') + ' Messaggi Inviati !', MSG_MSG_TOSENT: emoji.get('envelope') + ' Messaggi da Inviare', - MSG_MSG_INCORSO: emoji.get('envelope') + - ' messaggi in corso... Inviati attualmente', + MSG_MSG_INCORSO: emoji.get('envelope') + ' messaggi in corso... Inviati attualmente', + MSG_SET_USERNAME_TELEGRAM: '[Prima di continuare con la registrazione in RISO.APP]\n' + + 'Segui questi passi per IMPOSTARE UN USERNAME IN TELEGRAM: \n\n' + + '1. premi sull\'icona con 3 linee orizzontali, in alto a sinistra (sull\'iPhone si chiama "Impostazioni").\n' + + 'Per vederlo premi INDIETRO (si chiuderà questa chat).\n\n' + + '2. Seleziona la voce IMPOSTAZIONI dalla lista.\n\n' + + '3.Premi su USERNAME nella sezione account.\n\n' + + '4. Digita un username (o nome utente) qualsiasi a tua scelta, se è già usato aggiungi dei numeri.\n\n' + + '5. Per confermare premi sull\'icona con la spunta (V) in alto a destra.\n\n' + + '🏁 6. Infine ritorna su questa chat BOT e scrivi OK nel messaggio. \n\n' + + ' Se non riesci a fare questi passaggi, clicca qui: \n' + + '👉🏻 https://www.nigiara.it/telegram/come-impostare-uno-username-su-telegram.htm ', + MSG_SIGNUP: 'Per registrarti su %s:', + }; const txt_es = { @@ -1345,7 +1363,7 @@ class Telegram { } } - async start(msg) { + async settotheDatabase(msg) { // Check if Present to the DB const riscreate = await this.createIfNotExist(msg); @@ -1357,12 +1375,21 @@ class Telegram { if (rec.user) rec.status = Status.VERIFIED; - } else { - // await this.sendMsg(msg.chat.id, getstr(this.getlang(msg), 'MSG_ASK_USERNAME_BO', tools.getNomeAppByIdApp(this.idapp)), MenuNoLogin); } } + async start(msg) { + + // Check if Username Telegram Present + if (!msg.from.username) { + return false; + } + + await this.settotheDatabase(msg); + + } + geturlfaq() { return tools.getHostByIdApp(this.idapp) + '/faq'; } @@ -1463,10 +1490,10 @@ class Telegram { } } - } else { - if (!rec.user.profile.username_telegram) { - return this.checkIfUsernameTelegramSet(msg, rec.user); - } + /*} else { + if (!rec.user.profile.username_telegram) { + return this.checkIfUsernameTelegramSet(msg, rec.user); + }*/ } } } @@ -1875,10 +1902,6 @@ class Telegram { async menuAccedi(msg) { let status = this.getstatusInMemory(msg); - if (status === Status.WAITFOR_USERNAME_BO) { - await this.sendMsg(msg.from.id, - getstr(this.getlang(msg), 'MSG_ERRORE_USERNAME_NOT_FOUND')); - } } getlang(msg) { @@ -2216,7 +2239,8 @@ class Telegram { } nonAbilitato(msg) { - MyTelegramBot.local_sendMsgTelegramByIdTelegram(this.idapp, msg.chat.id, 'Utente Non abilitato o codice non valido ad eseguire questa funzione'); + MyTelegramBot.local_sendMsgTelegramByIdTelegram(this.idapp, msg.chat.id, + 'Utente Non abilitato o codice non valido ad eseguire questa funzione'); } chisono(rec) { @@ -2228,9 +2252,10 @@ class Telegram { async menuRestartSrv(rec, msg, cmd2) { if (cmd2 === '6711') { await MyTelegramBot.sendMsgTelegramToTheAdminAllSites(this.chisono(rec) + ' ha rilanciato il Server NODE.JS...'); - const ris = await tools.execScript(this.idapp, msg, '~/batch/production_restart_server.sh', this.chisono(rec) + ' Restart il Server (Node.Js)'); + const ris = await tools.execScript(this.idapp, msg, '~/batch/production_restart_server.sh', + this.chisono(rec) + ' Restart il Server (Node.Js)'); } else { - this.nonAbilitato(msg) + this.nonAbilitato(msg); } } @@ -2238,16 +2263,17 @@ class Telegram { if (cmd2 === '6711') { const ris = await tools.execScript(this.idapp, msg, '~/batch/logserver.sh', this.chisono(rec) + ' Visualizzo il Log del Server...'); } else { - this.nonAbilitato(msg) + this.nonAbilitato(msg); } } async menuRebootSrv(rec, msg, cmd2) { if (cmd2 === '6711') { await MyTelegramBot.sendMsgTelegramToTheAdminAllSites(this.chisono(rec) + ' ha effettuato il Reboot del Server! ...'); - const ris = await tools.execScript(this.idapp, msg, '~/batch/esegui_reboot.sh', this.chisono(rec) + ' Eseguo il Reboot del Server !...'); + const ris = await tools.execScript(this.idapp, msg, '~/batch/esegui_reboot.sh', + this.chisono(rec) + ' Eseguo il Reboot del Server !...'); } else { - this.nonAbilitato(msg) + this.nonAbilitato(msg); } } @@ -2432,41 +2458,19 @@ class Telegram { } async setUsernameBo(msg) { + + } + + async askUsernameInvitante(msg) { try { let rec = this.getRecInMem(msg); if (msg.text === undefined) return false; - console.log('msg.text', msg.text); - const text = msg.text; - if (text.length < 40) { - if (rec) { - let ris = await this.getUser(msg, rec, true); - rec = ris.rec; + if (rec) { + rec.status = Status.SET_USERNAME_INVITANTE; + await this.sendMsg(msg.chat.id, getstr(this.getlang(msg), 'MSG_ASK_USERNAME_INVITANTE', tools.getNomeAppByIdApp(this.idapp), null)); - if (ris.ok) { - rec.user = ris.user; - await User.SetTelegramCheckCode(this.idapp, ris.myid.toString(), - rec.code); - rec.status = Status.WAITFOR_VERIFY_CODE; - await this.sendMsg(msg.from.id, - getstr(this.getlang(msg), 'MSG_VERIFY_CODE', - tools.getHostByIdApp(this.idapp)), null); - } else { - await this.sendMsg(msg.chat.id, - getstr(this.getlang(msg), 'MSG_ASK_USERNAME_BO', - tools.getNomeAppByIdApp(this.idapp)), null); - } - } - } else if (text.length === 0) { - if (rec) - rec.status = Status.NONE; - await this.sendMsg(msg.from.id, - getstr(this.getlang(msg), 'MSG_ERRORE_USERNAME_ANNULLA')); - this.deleteRecInMem(msg); - } else { - await this.sendMsg(msg.from.id, - getstr(this.getlang(msg), 'MSG_ERRORE_USERNAME')); } } catch (e) { console.error('Error setUsernameBo:', e); @@ -2484,13 +2488,85 @@ class Telegram { } } - async checkIfUsernameTelegramSet(msg, myuser) { + async sendMsgByTemplateByMsgStrutt(title, msg) { + + try { + let langcode = 'it'; + let id = 0; + try { + langcode = msg.from.language_code; + id = msg.from.id; + } catch (e) { + } + const mymsg = await MsgTemplate.getMsgByTitleAndLang(tools.RISO, title, langcode); + const mytext = tools.convertSpecialTags(null, mymsg.body); + if (id > 0) + return this.sendMsg(id, mytext); + } catch (e) { + console.error('ERR sendMsgByTemplateByMsgStrutt', e); + } + } + + async sendMsgByTextByMsgStrutt(keytext, msg) { + + try { + let langcode = 'it'; + let id = 0; + try { + id = msg.from.id; + langcode = msg.from.language_code; + } catch (e) { + } + const text = getstr(langcode, keytext); + const mytext = tools.convertSpecialTags(null, text); + if (id > 0) + return this.sendMsg(id, mytext); + } catch (e) { + console.error('ERR sendMsgByTextByMsgStrutt', e); + } + } + + async checkIfUsernameTelegramSet(msg) { try { if (!msg.from.username) { - await this.sendMsgByTemplate('MSG_SET_USERNAME', myuser); + //await this.sendMsgByTemplateByMsgStrutt('MSG_SET_USERNAME', msg); + await this.sendMsgByTextByMsgStrutt('MSG_SET_USERNAME_TELEGRAM', msg); + return false; + //await this.sendMsgByTemplate('MSG_SET_USERNAME', myuser); } + return true; } catch (e) { console.error('e'); + return false; + } + } + + async setUsernameInvitante(msg) { + try { + if (!!msg.text) { + const rec = this.getRecInMem(msg); + let aportador_solidario = rec.aportador_solidario; + if (!aportador_solidario) { + aportador_solidario = msg.text.toString().trim().toLowerCase(); + const user = await User.getUserShortDataByUsername(this.idapp, aportador_solidario); + if (user) + rec.aportador_solidario = user.username; + } + if (rec.aportador_solidario) { + + // Link per Registrarsi + this.asktoRegistrationByLink(msg); + + } else { + rec.status = Status.WAITFOR_USERNAME_INVITANTE; + + const str2 = '\n' + getstr(this.getlang(msg), 'MSG_ASK_USERNAME_INVITANTE', tools.getNomeAppByIdApp(this.idapp)); + + await this.sendMsg(msg.from.id, getstr(this.getlang(msg), 'MSG_ERRORE_INVITANTE_NOT_FOUND') + str2, null); + } + } + } catch (e) { + console.error('Error setUsernameInvitante', e); } } @@ -2535,7 +2611,7 @@ class Telegram { console.log(' ... MA GLI MANCA L\'USERNAME TELEGRAM !! '); } - this.checkIfUsernameTelegramSet(msg, recuser); + // this.checkIfUsernameTelegramSet(msg, recuser); // local_sendMsgTelegramToTheManagers(this.idapp, recuser.name + ' ' + recuser.surname + ' si è Verificato a Telegram BOT! (lang=' + recuser.lang + ')' + emo.STARS, msg); } else { this.sendMsgLog(msg.from.id, getstr(this.getlang(msg), 'MSG_ERR_UNKNOWN_VERIFY_CODE')); @@ -2830,6 +2906,11 @@ class Telegram { async receiveMsg(msg) { + // Check if Username Telegram Present + if (!this.checkIfUsernameTelegramSet(msg)) { + return; + } + let status = await this.setInit(msg); if (status === Status.EXIT) { @@ -2839,11 +2920,14 @@ class Telegram { const rec = this.getRecInMem(msg); status = this.getstatus(rec); - if (status === Status.WAITFOR_USERNAME_BO && !this.selectMenuHelp(msg) && - !this.selectMenuLang(msg)) { - await this.setUsernameBo(msg); - } else if (status === Status.WAITFOR_VERIFY_CODE) { - await this.setVerifyCode(msg); + if (status === Status.WAITFOR_USERNAME_TELEGRAM && !this.selectMenuHelp(msg) && !this.selectMenuLang(msg)) { + // await this.setUsernameBo(msg); + await this.settotheDatabase(msg); + } else if (status === Status.WAITFOR_USERNAME_INVITANTE && !this.selectMenuHelp(msg) && !this.selectMenuLang(msg)) { + await this.askUsernameInvitante(msg); + // await this.settotheDatabase(msg); + } else if (status === Status.SET_USERNAME_INVITANTE) { + await this.setUsernameInvitante(msg); } else if (status === Status.NONE) { await this.start(msg); } else if (status === Status.VERIFIED) { @@ -2924,18 +3008,47 @@ class Telegram { } } - async askVerifiedCode(msg) { + async asktoRegistrationByLink(msg) { const rec = this.getRecInMem(msg); + let visu = false; + if (!rec) { await this.addUser(msg); - // await this.sendMsg(msg.chat.id, getstr(this.getlang(msg), printf(txt.MSG_ASK_USERNAME_BO, tools.getHostByIdApp(this.idapp)))); - await this.sendMsg(msg.chat.id, getstr(this.getlang(msg), 'MSG_ASK_USERNAME_BO', tools.getNomeAppByIdApp(this.idapp)), null); + visu = true; + + } + + if (rec && !rec.username_bo) + visu = true; + + if (visu) { + + const status = this.getstatus(rec); + if (status === Status.SET_USERNAME_INVITANTE && rec.aportador_solidario) { + let linkreg = tools.getLinkRegByIdAppAndMsgStrutt(this.idapp, msg); + + if (process.env.LOCALE === '1') { + linkreg = 'http://192.168.1.115:8084/signup/' + rec.aportador_solidario + '/' + msg.from.username + '/' + msg.from.id; + } + + const keyb = this.getInlineKeyboard(this.getlang(msg), [ + { + text: '✅ CLICCA QUI ', + url: linkreg, + }, + ]); + + // await this.sendMsg(msg.chat.id, getstr(this.getlang(msg), printf(txt.MSG_ASK_USERNAME_BO, tools.getHostByIdApp(this.idapp)))); + await this.sendMsg(msg.chat.id, getstr(this.getlang(msg), 'MSG_SIGNUP', tools.getNomeAppByIdApp(this.idapp)), null, keyb); + + } return false; } + return true; } @@ -3003,7 +3116,6 @@ class Telegram { id: this.getchatid(msg), code, lang, - status: Status.WAITFOR_USERNAME_BO, statusmsg: 0, msgcodeprec: '', sex: 0, @@ -3029,8 +3141,15 @@ class Telegram { menuSaved: {}, pagenow: 1, menuDb: null, + aportador_solidario: '', }; + if (!msg.from.username) { + rec.status = Status.WAITFOR_USERNAME_TELEGRAM; + } else { + rec.status = Status.WAITFOR_USERNAME_INVITANTE; + } + this.arrUsers.push(rec); return rec; @@ -3039,7 +3158,7 @@ class Telegram { async insertIntoDB(msg) { - return this.askVerifiedCode(msg); + return this.asktoRegistrationByLink(msg); } @@ -3071,7 +3190,7 @@ class Telegram { // Check if you are Admin const user = await User.UserByIdTelegram(idapp, id); // let isAdmin = user ? user.profile.manage_telegram && user.username === 'paoloar77' : false; - let isAdmin = user ? user.profile.manage_telegram: false; + let isAdmin = user ? user.profile.manage_telegram : false; const isManager = user ? user.profile.manage_telegram : false; const isVerified = user ? user.profile.teleg_id > 0 && user.verified_by_aportador : false; @@ -3280,7 +3399,6 @@ class Telegram { // text = text.replace(/<\/div>/g, ''); text = text.replace(/ /g, ' '); - if (!form) { form = { 'parse_mode': 'HTML', diff --git a/src/server/tools/general.js b/src/server/tools/general.js index b5ef2c9..dd3d52e 100755 --- a/src/server/tools/general.js +++ b/src/server/tools/general.js @@ -1114,6 +1114,16 @@ module.exports = { return ''; }, + getLinkRegByIdAppAndMsgStrutt: function(idapp, msg) { + + let myapp = this.getHostByIdApp(idapp); + if (myapp) { + myapp += '/' + msg.from.username + '/' + msg.from.id; + } + + return myapp; + }, + getHostByIdApp: function(idapp) { const myapp = this.getApps().find(item => item.idapp === idapp); @@ -1222,8 +1232,7 @@ module.exports = { }, getAskToVerifyReg: function(idapp) { - return this.getConfSiteOptionEnabledByIdApp(idapp, - shared_consts.ConfSite.Need_Aportador_On_DataReg_To_Verify_Reg); + return this.getConfSiteOptionEnabledByIdApp(idapp, shared_consts.ConfSite.Need_Aportador_On_DataReg_To_Verify_Reg); }, isManagAndAdminDifferent(idapp) { diff --git a/src/server/tools/globalTables.js b/src/server/tools/globalTables.js index af12f7e..0d33e42 100755 --- a/src/server/tools/globalTables.js +++ b/src/server/tools/globalTables.js @@ -25,6 +25,7 @@ const {SubSkill} = require('../models/subskill'); const {MySkill} = require('../models/myskill'); const {MyGood} = require('../models/mygood'); const {MyBacheca} = require('../models/mybacheca'); +const {MyHosp} = require('../models/myhosp'); const {StatusSkill} = require('../models/statusSkill'); const {City} = require('../models/city'); const {Province} = require('../models/province'); @@ -167,6 +168,8 @@ module.exports = { mytable = MySkill; else if (tablename === shared_consts.TABLES_MYBACHECAS) mytable = MyBacheca; + else if (tablename === shared_consts.TABLES_MYHOSPS) + mytable = MyHosp; else if (tablename === shared_consts.TABLES_MYGOODS) mytable = MyGood; else if (tablename === 'statusSkills') diff --git a/src/server/tools/shared_nodejs.js b/src/server/tools/shared_nodejs.js index 6c166cf..eede103 100755 --- a/src/server/tools/shared_nodejs.js +++ b/src/server/tools/shared_nodejs.js @@ -90,16 +90,17 @@ module.exports = { TABLES_MYSKILLS: 'myskills', TABLES_MYBACHECAS: 'mybachecas', + TABLES_MYHOSPS: 'myhosps', TABLES_MYGOODS: 'mygoods', - TABLES_ENABLE_GETREC_BYID: ['mybachecas', 'myskills', 'mygoods'], + TABLES_ENABLE_GETREC_BYID: ['mybachecas', 'myhosps', 'myskills', 'mygoods'], TABLES_USER_INCLUDE_MY: ['mygroups'], - TABLES_GETCOMPLETEREC: ['myskills', 'mybachecas', 'mygoods'], + TABLES_GETCOMPLETEREC: ['myskills', 'mybachecas', 'myhosps', 'mygoods'], TABLES_PERM_NEWREC: ['skills', 'goods', 'subskills', 'mygroups'], TABLES_REC_ID: ['skills', 'goods', 'subskills'], - TABLES_VISU_STAT_IN_HOME: ['myskills', 'mybachecas', 'mygoods', 'mygroups'], + TABLES_VISU_STAT_IN_HOME: ['myskills', 'mybachecas', 'myhosps', 'mygoods', 'mygroups'], TABLES_ID_NUMBER: [ 'permissions', @@ -116,15 +117,13 @@ module.exports = { 'provinces', 'myskills', 'mybachecas', + 'myhosps', 'mygoods', 'mygroups'], - TABLES_USER_ID: ['mygroups', 'myskills', 'mybachecas', 'mygoods'], - TABLES_UPDATE_LASTMODIFIED: ['myskills', 'mybachecas', 'mygoods', 'mybots'], - TABLES_FINDER: ['myskills', 'mybachecas', 'mygoods', 'mygroups'], - TABLES_VISU_CMYSRECCARD: ['myskills', 'mybachecas', 'mygoods'], + TABLES_USER_ID: ['mygroups', 'myskills', 'mybachecas', 'myhosps', 'mygoods'], + TABLES_UPDATE_LASTMODIFIED: ['myskills', 'mybachecas', 'myhosps', 'mygoods', 'mybots'], - TABLES_PERM_CHANGE_FOR_USERS: ['myskills', 'mybachecas', 'mygoods'], - TABLES_VISU_LISTA_USER: ['myskills', 'mybachecas', 'mygoods', 'users'], + TABLES_PERM_CHANGE_FOR_USERS: ['myskills', 'mybachecas', 'myhosps', 'mygoods'], TABLES_POPULATE_DATA: [ {