diff --git a/src/server/models/city.js b/src/server/models/city.js new file mode 100755 index 0000000..2cb4732 --- /dev/null +++ b/src/server/models/city.js @@ -0,0 +1,102 @@ +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 CitySchema = new Schema({ + _id: { + type: Number, + }, + istat: { + type: String, + }, + comune: { + type: String + }, + prov: { + type: String, + maxlength: 3, + }, + reg: { + type: String, + maxlength: 3, + }, + pref: { + type: String, + }, + cap: { + type: String, + maxlength: 6, + }, + abitanti: { + type: Number, + }, + country: { + type: String, + maxlength: 2, + }, +}); + +CitySchema.pre('save', async function (next) { + if (this.isNew) { + const myrec = await City.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; + } + } + + next(); +}); + + +CitySchema.statics.findByCity = function (mycity) { + + let myregexp = new RegExp(mycity.trim().replace(' ', '|'), 'ig'); + + const query = [ + { $match: {comune: { $regex: myregexp } } }, + { $sort: { descr: 1 } } + ]; + + return City + .aggregate(query) + .then((arrrec) => { + return arrrec + }) + +}; + +CitySchema.statics.getFieldsForSearch = function () { + return [{ field: 'comune', type: tools.FieldType.string }, + { field: 'prov', type: tools.FieldType.string }, + { field: 'reg', type: tools.FieldType.string }, + { field: 'pref', type: tools.FieldType.number }, + { field: 'cap', type: tools.FieldType.number }, + ] +}; + +CitySchema.statics.executeQueryTable = function (idapp, params) { + params.fieldsearch = this.getFieldsForSearch(); + return tools.executeQueryTable(this, 0, params); +}; + + +const City = mongoose.model('City', CitySchema); + +module.exports = { City }; diff --git a/src/server/models/level.js b/src/server/models/level.js index 7b23bf0..706ed98 100755 --- a/src/server/models/level.js +++ b/src/server/models/level.js @@ -23,7 +23,13 @@ const LevelSchema = new Schema({ years_of_exp: { type: Number, }, -}, {_id: false}); + color: { + type: String, + }, + theme: { + type: String, + }, +}); LevelSchema.pre('save', async function (next) { if (this.isNew) { @@ -33,7 +39,6 @@ LevelSchema.pre('save', async function (next) { this._id = 1; else this._id = myrec._doc._id + 1; - } else { this._id = 1; } @@ -47,7 +52,7 @@ LevelSchema.statics.findAllIdApp = function(idapp) { const Level = this; const query = [ - {$sort: {descr: 1}}, + {$sort: {_id: 1}}, ]; return Level.aggregate(query).then((arrrec) => { @@ -58,7 +63,6 @@ LevelSchema.statics.findAllIdApp = function(idapp) { LevelSchema.statics.getFieldsForSearch = function() { return [ - {field: 'label', type: tools.FieldType.string}, {field: 'descr', type: tools.FieldType.string}]; }; diff --git a/src/server/models/myskill.js b/src/server/models/myskill.js new file mode 100755 index 0000000..e037a28 --- /dev/null +++ b/src/server/models/myskill.js @@ -0,0 +1,91 @@ +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 MySkillSchema = new Schema({ + _id: { + type: Number, + }, + idapp: { + type: String, + required: true, + }, + userId: { type: Schema.Types.ObjectId, ref: 'User' }, + idSkill: { + type: Number, + }, + idStatusSkill: [{ + type: Number, + }], + numLevel: { + type: Number, + }, + note: { + type: String, + }, + date_created: { + type: Date, + default: Date.now, + }, + date_updated: { + type: Date, + default: Date.now, + }, +}); + +MySkillSchema.pre('save', async function (next) { + if (this.isNew) { + const myrec = await MySkill.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(); +}); + + +MySkillSchema.statics.findAllIdApp = function(idapp) { + + const query = [ + { $match: { idapp } }, + {$sort: {descr: 1}}, + ]; + + return MySkill.aggregate(query).then((arrrec) => { + return arrrec; + }); + +}; + +MySkillSchema.statics.getFieldsForSearch = function() { + return [{ field: 'idSkill', type: tools.FieldType.Number }] +}; + +MySkillSchema.statics.executeQueryTable = function(idapp, params) { + params.fieldsearch = this.getFieldsForSearch(); + return tools.executeQueryTable(this, idapp, params); +}; + +const MySkill = mongoose.model('MySkill', MySkillSchema); + +module.exports = {MySkill}; diff --git a/src/server/models/sector.js b/src/server/models/sector.js index c14259c..8c7f9cf 100755 --- a/src/server/models/sector.js +++ b/src/server/models/sector.js @@ -14,15 +14,46 @@ mongoose.plugin(schema => { }); const SectorSchema = new Schema({ + _id: { + type: Number, + }, descr: { type: String, }, + idSector: { + type: Number + }, icon: { type: String, }, img: { type: String, }, + color: { + type: String, + }, + theme: { + type: String, + }, + main: { + type: Boolean, + } +}); + +SectorSchema.pre('save', async function (next) { + if (this.isNew) { + const myrec = await Sector.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; + } + } + + next(); }); SectorSchema.statics.findAllIdApp = function (idapp) { diff --git a/src/server/models/skill.js b/src/server/models/skill.js index 611f9cc..39bede3 100755 --- a/src/server/models/skill.js +++ b/src/server/models/skill.js @@ -14,12 +14,15 @@ mongoose.plugin(schema => { }); const SkillSchema = new Schema({ + _id: { + type: Number, + }, descr: { type: String, }, - idSector: { - type: String - }, + idSector: [{ + type: Number + }], icon: { type: String, }, @@ -43,6 +46,25 @@ SkillSchema.statics.findAllIdApp = function (idapp) { }; +SkillSchema.pre('save', async function (next) { + if (this.isNew) { + const myrec = await Skill.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; + } + } + + next(); +}); + + + SkillSchema.statics.getFieldsForSearch = function () { return [{ field: 'label', type: tools.FieldType.string }, { field: 'descr', type: tools.FieldType.string }] diff --git a/src/server/models/statusSkill.js b/src/server/models/statusSkill.js new file mode 100755 index 0000000..95c7f5e --- /dev/null +++ b/src/server/models/statusSkill.js @@ -0,0 +1,75 @@ +const mongoose = require('mongoose').set('debug', false) +const Schema = mongoose.Schema; + +mongoose.Promise = global.Promise; +mongoose.level = "F"; + +const tools = require('../tools/general'); + +// Resolving error Unknown modifier: $pushAll +mongoose.plugin(schema => { + schema.options.usePushEach = true +}); + +const StatusSkillSchema = new Schema({ + _id: { + type: Number, + }, + descr: { + type: String, + }, + color: { + type: String, + }, + theme: { + type: String, + }, +}); + +StatusSkillSchema.pre('save', async function (next) { + if (this.isNew) { + const myrec = await StatusSkill.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; + } + } + + next(); +}); + + + +StatusSkillSchema.statics.findAllIdApp = function (idapp) { + const StatusSkill = this; + + const query = [ + { $sort: { descr: 1 } } + ]; + + return StatusSkill + .aggregate(query) + .then((arrrec) => { + return arrrec + }) + +}; + +StatusSkillSchema.statics.getFieldsForSearch = function () { + return [{ field: 'descr', type: tools.FieldType.string }] +}; + +StatusSkillSchema.statics.executeQueryTable = function (idapp, params) { + params.fieldsearch = this.getFieldsForSearch(); + return tools.executeQueryTable(this, 0, params); +}; + + +const StatusSkill = mongoose.model('StatusSkill', StatusSkillSchema); + +module.exports = { StatusSkill }; diff --git a/src/server/models/user.js b/src/server/models/user.js index 6a2abc4..1cfdd22 100755 --- a/src/server/models/user.js +++ b/src/server/models/user.js @@ -1,301 +1,303 @@ const bcrypt = require('bcryptjs'); -const mongoose = require('mongoose').set('debug', false) +const mongoose = require('mongoose').set('debug', false); const validator = require('validator'); const jwt = require('jsonwebtoken'); const _ = require('lodash'); const tools = require('../tools/general'); -const { Flotta } = require('../models/flotta'); -const { Settings } = require('../models/settings'); -const { ListaIngresso } = require('../models/listaingresso'); -const { Graduatoria } = require('../models/graduatoria'); -const { Nave } = require('../models/nave'); -const { NavePersistente } = require('../models/navepersistente'); +const {Flotta} = require('../models/flotta'); +const {Settings} = require('../models/settings'); +const {ListaIngresso} = require('../models/listaingresso'); +const {Graduatoria} = require('../models/graduatoria'); +const {Nave} = require('../models/nave'); +const {NavePersistente} = require('../models/navepersistente'); // const { ExtraList } = require('../models/extralist'); -const { ObjectID } = require('mongodb'); +const {ObjectID} = require('mongodb'); const shared_consts = require('../tools/shared_nodejs'); const queryclass = require('../classes/queryclass'); mongoose.Promise = global.Promise; -mongoose.level = "F"; +mongoose.level = 'F'; // Resolving error Unknown modifier: $pushAll mongoose.plugin(schema => { - schema.options.usePushEach = true + schema.options.usePushEach = true; }); mongoose.set('debug', process.env.DEBUG); const UserSchema = new mongoose.Schema({ - userId: { - type: String, - }, - email: { - type: String, - required: true, - trim: true, - minlength: 1, - unique: false, - /*validate: { - validator: validator.isEmail, - message: '{VALUE} is not a valid email' - }*/ - }, - idapp: { - type: String, - required: true, - }, - index: { - type: Number - }, - ind_order: { - type: Number - }, - old_order: { - type: Number - }, - username: { - type: String, - required: true, - trim: true, - minlength: 6, - unique: false, - }, - name: { - type: String, - trim: true, - }, - surname: { - type: String, - trim: true, - }, - password: { - type: String, - require: true, - minlength: 6, - }, - lang: { - type: String, - require: true, - }, - linkreg: { - type: String, - required: false - }, - verified_email: { - type: Boolean, - }, - made_gift: { - type: Boolean, - }, - tokens: [{ - access: { + userId: { type: String, - required: true }, - browser: { + email: { type: String, - required: true + required: true, + trim: true, + minlength: 1, + unique: false, + /*validate: { + validator: validator.isEmail, + message: '{VALUE} is not a valid email' + }*/ }, - token: { + idapp: { type: String, - required: true + required: true, }, - date_login: { - type: Date + index: { + type: Number, }, - }], - perm: { - type: Number - }, - ipaddr: { - type: String, - }, - date_reg: { - type: Date, - }, - date_deleted: { - type: Date, - }, - date_tokenforgot: { - type: Date - }, - tokenforgot: { - type: String, - }, - lasttimeonline: { - type: Date - }, - news_on: { - type: Boolean - }, - aportador_solidario: { // da cancellare - type: String, - }, - aportador_iniziale: { - type: String, - }, - aportador_solidario_nome_completo: { - type: String, - }, - aportador_solidario_ind_order: { - type: Number, - }, - note: { - type: String, - }, - deleted: { - type: Boolean, - default: false - }, - sospeso: { - type: Boolean - }, - non_voglio_imbarcarmi: { - type: Boolean - }, - navinonpresenti: { - type: Boolean - }, - subaccount: { - type: Boolean - }, - cart: { - type: Object - }, - profile: { - img: { - type: String + ind_order: { + type: Number, }, - nationality: { - type: String + old_order: { + type: Number, }, - intcode_cell: { - type: String + username: { + type: String, + required: true, + trim: true, + minlength: 6, + unique: false, }, - iso2_cell: { - type: String + name: { + type: String, + trim: true, }, - cell: { - type: String + surname: { + type: String, + trim: true, }, - country_pay: { - type: String + password: { + type: String, + require: true, + minlength: 6, }, - email_paypal: { - type: String + lang: { + type: String, + require: true, }, - payeer_id: { - type: String + linkreg: { + type: String, + required: false, }, - advcash_id: { - type: String + verified_email: { + type: Boolean, }, - revolut: { - type: String + made_gift: { + type: Boolean, }, - link_payment: { - type: String + tokens: [ + { + access: { + type: String, + required: true, + }, + browser: { + type: String, + required: true, + }, + token: { + type: String, + required: true, + }, + date_login: { + type: Date, + }, + }], + perm: { + type: Number, }, - note_payment: { - type: String + ipaddr: { + type: String, }, - paymenttypes: [], - username_telegram: { - type: String - }, - teleg_id: { - type: Number - }, - teleg_id_old: { - type: Number - }, - teleg_checkcode: { - type: Number - }, - manage_telegram: { - type: Boolean - }, - resplist: { - type: Boolean - }, - workerslist: { - type: Boolean - }, - dateofbirth: { + date_reg: { type: Date, }, - my_dream: { + date_deleted: { + type: Date, + }, + date_tokenforgot: { + type: Date, + }, + tokenforgot: { type: String, }, - saw_and_accepted: { + lasttimeonline: { + type: Date, + }, + news_on: { + type: Boolean, + }, + aportador_solidario: { // da cancellare + type: String, + }, + aportador_iniziale: { + type: String, + }, + aportador_solidario_nome_completo: { + type: String, + }, + aportador_solidario_ind_order: { type: Number, }, - saw_zoom_presentation: { - type: Boolean + note: { + type: String, }, - ask_zoom_partecipato: { - type: Boolean + deleted: { + type: Boolean, + default: false, }, - qualified: { - type: Boolean - }, - qualified_2invitati: { - type: Boolean - }, - special_req: { - type: Boolean - }, - sex: { - type: Number, - }, - motivazioni: { - type: String - }, - competenze_professionalita: { - type: String - }, - cosa_offrire: { - type: String - }, - cosa_ricevere: { - type: String - }, - altre_comunicazioni: { + sospeso: { type: Boolean, }, - come_ci_hai_conosciuto: { + non_voglio_imbarcarmi: { type: Boolean, }, - socio: { + navinonpresenti: { type: Boolean, }, - socioresidente: { + subaccount: { type: Boolean, }, - consiglio: { - type: Boolean, + cart: { + type: Object, + }, + profile: { + img: { + type: String, + }, + nationality: { + type: String, + }, + intcode_cell: { + type: String, + }, + iso2_cell: { + type: String, + }, + cell: { + type: String, + }, + country_pay: { + type: String, + }, + email_paypal: { + type: String, + }, + payeer_id: { + type: String, + }, + advcash_id: { + type: String, + }, + revolut: { + type: String, + }, + link_payment: { + type: String, + }, + note_payment: { + type: String, + }, + paymenttypes: [], + username_telegram: { + type: String, + }, + teleg_id: { + type: Number, + }, + teleg_id_old: { + type: Number, + }, + teleg_checkcode: { + type: Number, + }, + manage_telegram: { + type: Boolean, + }, + resplist: { + type: Boolean, + }, + workerslist: { + type: Boolean, + }, + dateofbirth: { + type: Date, + }, + my_dream: { + type: String, + }, + saw_and_accepted: { + type: Number, + }, + saw_zoom_presentation: { + type: Boolean, + }, + ask_zoom_partecipato: { + type: Boolean, + }, + qualified: { + type: Boolean, + }, + qualified_2invitati: { + type: Boolean, + }, + special_req: { + type: Boolean, + }, + sex: { + type: Number, + }, + motivazioni: { + type: String, + }, + competenze_professionalita: { + type: String, + }, + cosa_offrire: { + type: String, + }, + cosa_ricevere: { + type: String, + }, + altre_comunicazioni: { + type: Boolean, + }, + come_ci_hai_conosciuto: { + type: Boolean, + }, + socio: { + type: Boolean, + }, + socioresidente: { + type: Boolean, + }, + consiglio: { + type: Boolean, + }, + myshares: [ + { + description: {type: String}, + rating: {type: Number}, + }], }, - myshares: [{ - description: { type: String }, - rating: { type: Number }, - }] - }, - }) + }) ; -UserSchema.methods.toJSON = function () { +UserSchema.methods.toJSON = function() { const user = this; const userObject = user.toObject(); return _.pick(userObject, ['_id', ...shared_consts.fieldsUserToChange()]); }; -UserSchema.methods.generateAuthToken = function (req) { +UserSchema.methods.generateAuthToken = function(req) { // console.log("GENERA TOKEN : "); const user = this; @@ -304,40 +306,40 @@ UserSchema.methods.generateAuthToken = function (req) { const access = 'auth'; const browser = useragent; - const token = jwt.sign({ _id: user._id.toHexString(), access }, process.env.SIGNCODE).toString(); + const token = jwt.sign({_id: user._id.toHexString(), access}, + process.env.SIGNCODE).toString(); const date_login = new Date(); // CANCELLA IL PRECEDENTE ! - user.tokens = user.tokens.filter(function (tok) { - return (tok.access !== access) || ((tok.access === access) && (tok.browser !== browser)); + user.tokens = user.tokens.filter(function(tok) { + return (tok.access !== access) || + ((tok.access === access) && (tok.browser !== browser)); }); - user.tokens.push({ access, browser, token, date_login }); + user.tokens.push({access, browser, token, date_login}); user.lasttimeonline = new Date(); - return user.save() - .then(() => { - // console.log("TOKEN CREATO IN LOGIN : " + token); - return token; - }) - .catch(err => { - console.log("Error", err.message); - }); + return user.save().then(() => { + // console.log("TOKEN CREATO IN LOGIN : " + token); + return token; + }).catch(err => { + console.log('Error', err.message); + }); }; -UserSchema.statics.setPermissionsById = function (id, perm) { +UserSchema.statics.setPermissionsById = function(id, perm) { const user = this; - return user.findByIdAndUpdate(id, { $set: { perm } }).then((user) => { + return user.findByIdAndUpdate(id, {$set: {perm}}).then((user) => { if (user) - return res.send({ code: server_constants.RIS_CODE_OK, msg: '' }); + return res.send({code: server_constants.RIS_CODE_OK, msg: ''}); else - return res.send({ code: server_constants.RIS_CODE_ERR, msg: '' }); + return res.send({code: server_constants.RIS_CODE_ERR, msg: ''}); }); }; -UserSchema.statics.setZoomPresenza = async function (idapp, id, presenza) { +UserSchema.statics.setZoomPresenza = async function(idapp, id, presenza) { const User = this; const telegrambot = require('../telegram/telegrambot'); @@ -345,77 +347,85 @@ UserSchema.statics.setZoomPresenza = async function (idapp, id, presenza) { let allData = {}; allData.myuser = await User.getUserById(idapp, id); if (!!allData.myuser) - allData.precDataUser = await User.getInfoUser(idapp, allData.myuser.username); + allData.precDataUser = await User.getInfoUser(idapp, + allData.myuser.username); - return await User.findByIdAndUpdate(id, { $set: { 'profile.saw_zoom_presentation': presenza } }) - .then((rec) => { - if (presenza) { - const messaggio = tools.get__('ZOOM_CONFERMATO'); - telegrambot.sendMsgTelegram(rec.idapp, rec.username, messaggio); - telegrambot.sendMsgTelegramToTheManagersAndZoomeri(idapp, `L\'utente ${rec.name} ${rec.surname} (${rec.username}) è stato confermato per aver visto lo Zoom di Benvenuto`); - } else { - telegrambot.sendMsgTelegramToTheManagersAndZoomeri(idapp, `L\'utente ${rec.name} ${rec.surname} (${rec.username}) è stato annullata la sua richiesta per aver visto lo Zoom di Benvenuto! (Non ci risulta)`); - } + return await User.findByIdAndUpdate(id, + {$set: {'profile.saw_zoom_presentation': presenza}}).then((rec) => { + if (presenza) { + const messaggio = tools.get__('ZOOM_CONFERMATO'); + telegrambot.sendMsgTelegram(rec.idapp, rec.username, messaggio); + telegrambot.sendMsgTelegramToTheManagersAndZoomeri(idapp, + `L\'utente ${rec.name} ${rec.surname} (${rec.username}) è stato confermato per aver visto lo Zoom di Benvenuto`); + } else { + telegrambot.sendMsgTelegramToTheManagersAndZoomeri(idapp, + `L\'utente ${rec.name} ${rec.surname} (${rec.username}) è stato annullata la sua richiesta per aver visto lo Zoom di Benvenuto! (Non ci risulta)`); + } - return User.findByIdAndUpdate(id, { $set: { 'profile.ask_zoom_partecipato': false } }) - .then((user) => { + return User.findByIdAndUpdate(id, + {$set: {'profile.ask_zoom_partecipato': false}}).then((user) => { - - User.checkIfSbloccatiRequisiti(idapp, allData, id); - }); + User.checkIfSbloccatiRequisiti(idapp, allData, id); }); + }); }; -UserSchema.statics.isAdmin = function (perm) { +UserSchema.statics.isAdmin = function(perm) { try { - return ((perm & shared_consts.Permissions.Admin) === shared_consts.Permissions.Admin); + return ((perm & shared_consts.Permissions.Admin) === + shared_consts.Permissions.Admin); } catch (e) { - return false + return false; } }; -UserSchema.statics.isManager = function (perm) { +UserSchema.statics.isManager = function(perm) { try { - return ((perm & shared_consts.Permissions.Manager) === shared_consts.Permissions.Manager); + return ((perm & shared_consts.Permissions.Manager) === + shared_consts.Permissions.Manager); } catch (e) { - return false + return false; } }; -UserSchema.statics.isEditor = function (perm) { +UserSchema.statics.isEditor = function(perm) { try { - return ((perm & shared_consts.Permissions.Editor) === shared_consts.Permissions.Editor); + return ((perm & shared_consts.Permissions.Editor) === + shared_consts.Permissions.Editor); } catch (e) { - return false + return false; } }; -UserSchema.statics.isZoomeri = function (perm) { +UserSchema.statics.isZoomeri = function(perm) { try { - return ((perm & shared_consts.Permissions.Zoomeri) === shared_consts.Permissions.Zoomeri); + return ((perm & shared_consts.Permissions.Zoomeri) === + shared_consts.Permissions.Zoomeri); } catch (e) { - return false + return false; } }; -UserSchema.statics.isDepartment = function (perm) { +UserSchema.statics.isDepartment = function(perm) { try { - return ((perm & shared_consts.Permissions.Zoomeri) === shared_consts.Permissions.Department); + return ((perm & shared_consts.Permissions.Zoomeri) === + shared_consts.Permissions.Department); } catch (e) { - return false + return false; } }; -UserSchema.statics.isTutor = function (perm) { +UserSchema.statics.isTutor = function(perm) { try { - return ((perm & shared_consts.Permissions.Tutor) === shared_consts.Permissions.Tutor); + return ((perm & shared_consts.Permissions.Tutor) === + shared_consts.Permissions.Tutor); } catch (e) { - return false + return false; } }; -UserSchema.statics.findByToken = function (token, typeaccess) { +UserSchema.statics.findByToken = function(token, typeaccess) { const User = this; let decoded; @@ -432,7 +442,7 @@ UserSchema.statics.findByToken = function (token, typeaccess) { }); }; -UserSchema.statics.findByTokenAnyAccess = function (token) { +UserSchema.statics.findByTokenAnyAccess = function(token) { const User = this; let decoded; @@ -448,21 +458,21 @@ UserSchema.statics.findByTokenAnyAccess = function (token) { }); }; -UserSchema.statics.findByCredentials = function (idapp, username, password) { +UserSchema.statics.findByCredentials = function(idapp, username, password) { const User = this; - let pwd = ""; + let pwd = ''; return User.findOne({ idapp, username: username, $or: [ - { deleted: { $exists: false } }, - { deleted: { $exists: true, $eq: false } }, + {deleted: {$exists: false}}, + {deleted: {$exists: true, $eq: false}}, { $and: [ - { deleted: { $exists: true, $eq: true } }, - { subaccount: { $exists: true, $eq: true } } - ] - } + {deleted: {$exists: true, $eq: true}}, + {subaccount: {$exists: true, $eq: true}}, + ], + }, ], }).then((user) => { @@ -470,10 +480,12 @@ UserSchema.statics.findByCredentials = function (idapp, username, password) { // Check if with email: return User.findOne({ idapp, email: username.toLowerCase(), - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }] - }) + $or: [ + {deleted: {$exists: false}}, + {deleted: {$exists: true, $eq: false}}], + }); } else { - return !user.deleted || (user.deleted && user.subaccount) ? user : null + return !user.deleted || (user.deleted && user.subaccount) ? user : null; } }).then(user => { if (!user) @@ -496,38 +508,40 @@ UserSchema.statics.findByCredentials = function (idapp, username, password) { }); }; - -UserSchema.statics.findByUsername = async function (idapp, username, alsoemail) { +UserSchema.statics.findByUsername = async function(idapp, username, alsoemail) { const User = this; - const myreg = ["^", username, "$"].join("") - let regexusername = new RegExp(myreg, "i"); + const myreg = ['^', username, '$'].join(''); + let regexusername = new RegExp(myreg, 'i'); return User.findOne({ idapp: idapp, - username: { $regex: regexusername }, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }], + username: {$regex: regexusername}, + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], }).then((ris) => { if ((!ris) && (alsoemail)) { - regexemail = new RegExp(["^", username.toLowerCase(), "$"].join(""), "i"); + regexemail = new RegExp(['^', username.toLowerCase(), '$'].join(''), 'i'); return User.findOne({ 'idapp': idapp, - 'email': { $regex: regexemail }, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }], + 'email': {$regex: regexemail}, + $or: [ + {deleted: {$exists: false}}, + {deleted: {$exists: true, $eq: false}}], }); } return ris; }); }; -UserSchema.statics.getUserShortDataByUsername = async function (idapp, username) { +UserSchema.statics.getUserShortDataByUsername = async function( + idapp, username) { const User = this; const myrec = await User.findOne({ 'idapp': idapp, 'username': username, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }], + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], }, { idapp: 1, lang: 1, @@ -558,7 +572,7 @@ UserSchema.statics.getUserShortDataByUsername = async function (idapp, username) made_gift: 1, email: 1, date_reg: 1, - img: 1 + img: 1, }).then((ris) => { if (!!ris) { // console.log('ris', ris); @@ -571,59 +585,63 @@ UserSchema.statics.getUserShortDataByUsername = async function (idapp, username) if (myrec) { myrec.qualified = await User.isUserQualified7(idapp, myrec.username); - myrec.numNaviEntrato = await Nave.getnumNaviByUsername(idapp, myrec.username); - myrec.numinvitati = await ListaIngresso.getnumInvitati(idapp, myrec.username); - myrec.numinvitatiattivi = await ListaIngresso.getnumInvitatiAttivi(idapp, myrec.username); + myrec.numNaviEntrato = await Nave.getnumNaviByUsername(idapp, + myrec.username); + myrec.numinvitati = await ListaIngresso.getnumInvitati(idapp, + myrec.username); + myrec.numinvitatiattivi = await ListaIngresso.getnumInvitatiAttivi(idapp, + myrec.username); } - return myrec + return myrec; }; -UserSchema.statics.getDownlineByUsername = async function (idapp, username, includemyself, onlynumber) { +UserSchema.statics.getDownlineByUsername = async function( + idapp, username, includemyself, onlynumber) { if (username === undefined) return null; let arrrec = await ListaIngresso.getInvitati(idapp, username, includemyself, - { - index: 1, - lang: 1, - invitante_username: 1, - ind_order: 1, - username: 1, - name: 1, - surname: 1, - verified_email: 1, - 'profile.teleg_id': 1, - // 'profile.saw_zoom_presentation': 1, - 'profile.ask_zoom_partecipato': 1, - 'profile.qualified': 1, - 'profile.qualified_2invitati': 1, - 'profile.saw_and_accepted': 1, - 'profile.email_paypal': 1, - 'profile.payeer_id': 1, - 'profile.advcash_id': 1, - 'profile.revolut': 1, - 'profile.link_payment': 1, - 'profile.note_payment': 1, - // 'profile.my_dream': 1, - 'profile.paymenttypes': 1, - 'profile.cell': 1, - made_gift: 1, - email: 1, - date_reg: 1, - img: 1 - } + { + index: 1, + lang: 1, + invitante_username: 1, + ind_order: 1, + username: 1, + name: 1, + surname: 1, + verified_email: 1, + 'profile.teleg_id': 1, + // 'profile.saw_zoom_presentation': 1, + 'profile.ask_zoom_partecipato': 1, + 'profile.qualified': 1, + 'profile.qualified_2invitati': 1, + 'profile.saw_and_accepted': 1, + 'profile.email_paypal': 1, + 'profile.payeer_id': 1, + 'profile.advcash_id': 1, + 'profile.revolut': 1, + 'profile.link_payment': 1, + 'profile.note_payment': 1, + // 'profile.my_dream': 1, + 'profile.paymenttypes': 1, + 'profile.cell': 1, + made_gift: 1, + email: 1, + date_reg: 1, + img: 1, + }, ); let myq = { idapp, aportador_solidario: username, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }] + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], }; if (!includemyself) { - myq = { ...myq, username: { $ne: username } } + myq = {...myq, username: {$ne: username}}; } // Ottieni gli invitati che ancora non hanno un'imbarco @@ -642,23 +660,25 @@ UserSchema.statics.getDownlineByUsername = async function (idapp, username, incl if (!!rectrovato) { rectrovato.quanti++; } else { - const myrec = { ...inv }; + const myrec = {...inv}; myrec.quanti = 1; - arrusername.push(myrec) + arrusername.push(myrec); } } arrrec = arrusername; - if (!onlynumber) { if (!!arrrec) { for (const rec of arrrec) { rec.qualified = await User.isUserQualified7(idapp, rec.username); - rec.numNaviEntrato = await Nave.getnumNaviByUsername(idapp, rec.username); - rec.numinvitati = await ListaIngresso.getnumInvitati(idapp, rec.username); - rec.numinvitatiattivi = await ListaIngresso.getnumInvitatiAttivi(idapp, rec.username); + rec.numNaviEntrato = await Nave.getnumNaviByUsername(idapp, + rec.username); + rec.numinvitati = await ListaIngresso.getnumInvitati(idapp, + rec.username); + rec.numinvitatiattivi = await ListaIngresso.getnumInvitatiAttivi(idapp, + rec.username); } } } @@ -666,37 +686,38 @@ UserSchema.statics.getDownlineByUsername = async function (idapp, username, incl }; -UserSchema.statics.getQueryQualified = function () { +UserSchema.statics.getQueryQualified = function() { return [ { - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }], + $or: [ + {deleted: {$exists: false}}, + {deleted: {$exists: true, $eq: false}}], }, { $or: [ { - 'profile.special_req': true + 'profile.special_req': true, }, { verified_email: true, - 'profile.teleg_id': { $gt: 1 }, + 'profile.teleg_id': {$gt: 1}, 'profile.saw_and_accepted': shared_consts.ALL_SAW_AND_ACCEPTED, // 'profile.saw_zoom_presentation': true, $or: [ - { 'profile.link_payment': { $exists: true } }, - { 'profile.email_paypal': { $exists: true } }, - { 'profile.payeer_id': { $exists: true } }, - { 'profile.advcash_id': { $exists: true } }, - { 'profile.revolut': { $exists: true } }, + {'profile.link_payment': {$exists: true}}, + {'profile.email_paypal': {$exists: true}}, + {'profile.payeer_id': {$exists: true}}, + {'profile.advcash_id': {$exists: true}}, + {'profile.revolut': {$exists: true}}, ], // 'profile.paymenttypes': { "$in": ['paypal'] }, // $where: "this.profile.paymenttypes.length >= 1", - }] - } + }], + }, ]; -} +}; - -UserSchema.statics.isUserQualified7 = async function (idapp, username) { +UserSchema.statics.isUserQualified7 = async function(idapp, username) { const User = this; if (username === undefined) @@ -713,7 +734,7 @@ UserSchema.statics.isUserQualified7 = async function (idapp, username) { return !!myrec; }; -UserSchema.statics.isUserResidente = async function (idapp, username) { +UserSchema.statics.isUserResidente = async function(idapp, username) { const User = this; if (username === undefined) @@ -731,10 +752,9 @@ UserSchema.statics.isUserResidente = async function (idapp, username) { return false; } - }; -UserSchema.statics.isUserConsiglio = async function (idapp, username) { +UserSchema.statics.isUserConsiglio = async function(idapp, username) { const User = this; if (username === undefined) @@ -752,10 +772,9 @@ UserSchema.statics.isUserConsiglio = async function (idapp, username) { return false; } - }; -UserSchema.statics.isUserVisuProjects = async function (idapp, username) { +UserSchema.statics.isUserVisuProjects = async function(idapp, username) { const User = this; if (username === undefined) @@ -773,10 +792,9 @@ UserSchema.statics.isUserVisuProjects = async function (idapp, username) { return false; } - }; -UserSchema.statics.isUserAlreadyQualified = async function (idapp, username) { +UserSchema.statics.isUserAlreadyQualified = async function(idapp, username) { const User = this; if (username === undefined) @@ -785,7 +803,7 @@ UserSchema.statics.isUserAlreadyQualified = async function (idapp, username) { const myquery = { 'idapp': idapp, 'username': username, - 'profile.qualified': { $exists: true, $eq: true }, + 'profile.qualified': {$exists: true, $eq: true}, }; const myrec = await User.findOne(myquery); @@ -793,7 +811,8 @@ UserSchema.statics.isUserAlreadyQualified = async function (idapp, username) { return !!myrec; }; -UserSchema.statics.isUserAlreadyQualified_2Invitati = async function (idapp, username) { +UserSchema.statics.isUserAlreadyQualified_2Invitati = async function( + idapp, username) { const User = this; if (username === undefined) @@ -802,7 +821,7 @@ UserSchema.statics.isUserAlreadyQualified_2Invitati = async function (idapp, use const myquery = { 'idapp': idapp, 'username': username, - 'profile.qualified_2invitati': { $exists: true, $eq: true }, + 'profile.qualified_2invitati': {$exists: true, $eq: true}, }; const myrec = await User.findOne(myquery); @@ -810,7 +829,7 @@ UserSchema.statics.isUserAlreadyQualified_2Invitati = async function (idapp, use return !!myrec; }; -UserSchema.statics.setUserQualified = async function (idapp, username) { +UserSchema.statics.setUserQualified = async function(idapp, username) { const User = this; if (username === undefined) @@ -821,12 +840,14 @@ UserSchema.statics.setUserQualified = async function (idapp, username) { 'username': username, }; - const myrec = await User.findOneAndUpdate(myquery, { $set: { 'profile.qualified': true } }, { new: false }); + const myrec = await User.findOneAndUpdate(myquery, + {$set: {'profile.qualified': true}}, {new: false}); return !!myrec; }; -UserSchema.statics.setUserQualified_2Invitati = async function (idapp, username) { +UserSchema.statics.setUserQualified_2Invitati = async function( + idapp, username) { const User = this; if (username === undefined) @@ -837,12 +858,13 @@ UserSchema.statics.setUserQualified_2Invitati = async function (idapp, username) 'username': username, }; - const myrec = await User.findOneAndUpdate(myquery, { $set: { 'profile.qualified_2invitati': true } }, { new: false }); + const myrec = await User.findOneAndUpdate(myquery, + {$set: {'profile.qualified_2invitati': true}}, {new: false}); return !!myrec; }; -UserSchema.statics.isUserQualified9 = async function (idapp, username) { +UserSchema.statics.isUserQualified9 = async function(idapp, username) { const User = this; if (username === undefined) @@ -877,33 +899,36 @@ UserSchema.statics.getnumPaymentOk = function (idapp) { }; */ -UserSchema.statics.getUsersNationalityQuery = function (idapp) { +UserSchema.statics.getUsersNationalityQuery = function(idapp) { const query = [ { - $match: { idapp, $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }], } + $match: { + idapp, + $or: [ + {deleted: {$exists: false}}, + {deleted: {$exists: true, $eq: false}}], + }, }, { - $group: { _id: "$profile.nationality", count: { $sum: 1 } } + $group: {_id: '$profile.nationality', count: {$sum: 1}}, }, { - $sort: { count: -1 } - } + $sort: {count: -1}, + }, ]; - return query + return query; }; - -UserSchema.statics.getindOrderDuplicate = function (idapp) { +UserSchema.statics.getindOrderDuplicate = function(idapp) { const User = this; - return User.aggregate(User.getUsersNationalityQuery(idapp)) - .then(ris => { - // console.table(ris); - return JSON.stringify(ris); - }); + return User.aggregate(User.getUsersNationalityQuery(idapp)).then(ris => { + // console.table(ris); + return JSON.stringify(ris); + }); }; -UserSchema.statics.findByLinkreg = function (idapp, linkreg) { +UserSchema.statics.findByLinkreg = function(idapp, linkreg) { const User = this; return User.findOne({ @@ -912,7 +937,7 @@ UserSchema.statics.findByLinkreg = function (idapp, linkreg) { }); }; -UserSchema.statics.AportadorOrig = function (idapp, id) { +UserSchema.statics.AportadorOrig = function(idapp, id) { const User = this; return User.findOne({ @@ -926,46 +951,44 @@ UserSchema.statics.AportadorOrig = function (idapp, id) { }); }; - -UserSchema.statics.findByLinkTokenforgot = function (idapp, email, tokenforgot) { +UserSchema.statics.findByLinkTokenforgot = function(idapp, email, tokenforgot) { const User = this; return User.findOne({ 'email': email, 'tokenforgot': tokenforgot, - 'date_tokenforgot': { $gte: tools.IncDateNow(-1000 * 60 * 60 * 4) }, // 4 ore fa! + 'date_tokenforgot': {$gte: tools.IncDateNow(-1000 * 60 * 60 * 4)}, // 4 ore fa! 'idapp': idapp, }); }; - -UserSchema.statics.findByEmail = function (idapp, email) { +UserSchema.statics.findByEmail = function(idapp, email) { const User = this; return User.findOne({ 'idapp': idapp, 'email': email, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }], + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], }); }; -UserSchema.statics.getLastUser = function (idapp) { +UserSchema.statics.getLastUser = function(idapp) { const User = this; return User.findOne({ idapp, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }], - }).sort({ index: -1 }) + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], + }).sort({index: -1}); }; -UserSchema.statics.findByIndOrder = function (idapp, ind_order) { +UserSchema.statics.findByIndOrder = function(idapp, ind_order) { const User = this; return User.getRecByIndOrder(idapp, ind_order); }; -UserSchema.statics.findByIndex = function (idapp, index) { +UserSchema.statics.findByIndex = function(idapp, index) { const User = this; try { @@ -973,34 +996,39 @@ UserSchema.statics.findByIndex = function (idapp, index) { return User.findOne({ idapp, index, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }], + $or: [ + {deleted: {$exists: false}}, + {deleted: {$exists: true, $eq: false}}], }); } catch (e) { console.error(e.message); } }; -UserSchema.statics.findByOldOrder = function (idapp, old_order) { +UserSchema.statics.findByOldOrder = function(idapp, old_order) { const User = this; try { return User.findOne({ idapp, old_order, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }], + $or: [ + {deleted: {$exists: false}}, + {deleted: {$exists: true, $eq: false}}], }); } catch (e) { console.error(e.message); } }; - -UserSchema.pre('save', async function (next) { +UserSchema.pre('save', async function(next) { try { if (this.isNew) { try { - const myrec = await User.findOne({ idapp: this.idapp }).limit(1).sort({ index: -1 }); + const myrec = await User.findOne({idapp: this.idapp}). + limit(1). + sort({index: -1}); if (!!myrec) { this.index = myrec._doc.index + 1; @@ -1031,159 +1059,155 @@ UserSchema.pre('save', async function (next) { } }); -UserSchema.methods.removeToken = function (token) { +UserSchema.methods.removeToken = function(token) { const user = this; return user.updateOne({ $pull: { - tokens: { token } - } + tokens: {token}, + }, }); }; -UserSchema.statics.getEmailByUsername = async function (idapp, username) { +UserSchema.statics.getEmailByUsername = async function(idapp, username) { const User = this; return await User.findOne({ idapp, username, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }] - }) - .then((arrrec) => { - return ((arrrec) ? arrrec.email : ''); - }).catch((e) => { - console.error('getEmailByUsername', e); - }); + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], + }).then((arrrec) => { + return ((arrrec) ? arrrec.email : ''); + }).catch((e) => { + console.error('getEmailByUsername', e); + }); }; -UserSchema.statics.getUsernameById = async function (idapp, id) { +UserSchema.statics.getUsernameById = async function(idapp, id) { const User = this; return await User.findOne({ idapp, _id: id, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }] - }, { username: 1 }) - .then((myuser) => { - return ((myuser) ? myuser.username : ''); - }).catch((e) => { + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], + }, {username: 1}).then((myuser) => { + return ((myuser) ? myuser.username : ''); + }).catch((e) => { - }); + }); }; -UserSchema.statics.getUserById = function (idapp, id) { +UserSchema.statics.getUserById = function(idapp, id) { const User = this; return User.findOne({ idapp, _id: id, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }] - }) + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], + }); }; -UserSchema.statics.getAportadorSolidarioByUsername = async function (idapp, username) { +UserSchema.statics.getAportadorSolidarioByUsername = async function( + idapp, username) { const User = this; return await User.findOne({ idapp, username, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }] - }) - .then((rec) => { - return ((rec) ? rec.aportador_solidario : ''); - }).catch((e) => { - console.error('getAportadorSolidarioByUsername', e); - }); + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], + }).then((rec) => { + return ((rec) ? rec.aportador_solidario : ''); + }).catch((e) => { + console.error('getAportadorSolidarioByUsername', e); + }); }; -UserSchema.statics.UserByIdTelegram = async function (idapp, teleg_id) { +UserSchema.statics.UserByIdTelegram = async function(idapp, teleg_id) { const User = this; return await User.findOne({ idapp, 'profile.teleg_id': teleg_id, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }], - }) - .then((rec) => { - return (!!rec) ? rec._doc : null; - }).catch((e) => { - console.error('UserExistByIdTelegram', e); - }); + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], + }).then((rec) => { + return (!!rec) ? rec._doc : null; + }).catch((e) => { + console.error('UserExistByIdTelegram', e); + }); }; -UserSchema.statics.UsersByIdTelegram = async function (idapp, teleg_id) { +UserSchema.statics.UsersByIdTelegram = async function(idapp, teleg_id) { const User = this; return await User.find({ idapp, 'profile.teleg_id': teleg_id, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }] - }) - .then((rec) => { - return (!!rec) ? rec._doc : null; - }).catch((e) => { - console.error('UserExistByIdTelegram', e); - }); + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], + }).then((rec) => { + return (!!rec) ? rec._doc : null; + }).catch((e) => { + console.error('UserExistByIdTelegram', e); + }); }; -UserSchema.statics.TelegIdByUsername = async function (idapp, username) { +UserSchema.statics.TelegIdByUsername = async function(idapp, username) { const User = this; return await User.findOne({ idapp, username, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }] - }, { 'profile.teleg_id': 1 }) - .then((rec) => { - return (!!rec) ? rec.profile.teleg_id : null; - }).catch((e) => { - console.error('TelegIdByUsername', e); - }); + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], + }, {'profile.teleg_id': 1}).then((rec) => { + return (!!rec) ? rec.profile.teleg_id : null; + }).catch((e) => { + console.error('TelegIdByUsername', e); + }); }; -UserSchema.statics.SetTelegramCheckCode = async function (idapp, id, teleg_checkcode) { +UserSchema.statics.SetTelegramCheckCode = async function( + idapp, id, teleg_checkcode) { const User = this; const fields_to_update = { - 'profile.teleg_checkcode': teleg_checkcode + 'profile.teleg_checkcode': teleg_checkcode, }; return await User.findOneAndUpdate({ - _id: id - }, { $set: fields_to_update }, { new: false }).then((record) => { + _id: id, + }, {$set: fields_to_update}, {new: false}).then((record) => { return !!record; }); }; -UserSchema.statics.NonVoglioImbarcarmi = async function (idapp, username) { +UserSchema.statics.NonVoglioImbarcarmi = async function(idapp, username) { const User = this; const fields_to_update = { - non_voglio_imbarcarmi: true + non_voglio_imbarcarmi: true, }; return await User.findOneAndUpdate({ idapp, username, - }, { $set: fields_to_update }, { new: false }).then((record) => { + }, {$set: fields_to_update}, {new: false}).then((record) => { return !!record; }); }; -UserSchema.statics.SetTelegramIdSuccess = async function (idapp, id, teleg_id) { +UserSchema.statics.SetTelegramIdSuccess = async function(idapp, id, teleg_id) { const User = this; const fields_to_update = { 'profile.teleg_id': teleg_id, 'profile.teleg_id_old': 0, - 'profile.teleg_checkcode': 0 + 'profile.teleg_checkcode': 0, }; return await User.findOneAndUpdate({ idapp, - _id: id - }, { $set: fields_to_update }, { new: false }).then((record) => { + _id: id, + }, {$set: fields_to_update}, {new: false}).then((record) => { return record; }); }; -UserSchema.statics.getLangByIndOrder = async function (idapp, ind_order) { +UserSchema.statics.getLangByIndOrder = async function(idapp, ind_order) { const User = this; const rec = await User.getSmallRecByIndOrder(idapp, ind_order); @@ -1191,8 +1215,7 @@ UserSchema.statics.getLangByIndOrder = async function (idapp, ind_order) { }; - -UserSchema.statics.SetLang = async function (idapp, id, lang) { +UserSchema.statics.SetLang = async function(idapp, id, lang) { const User = this; const fields_to_update = { @@ -1200,14 +1223,14 @@ UserSchema.statics.SetLang = async function (idapp, id, lang) { }; return await User.findOneAndUpdate({ - _id: id - }, { $set: fields_to_update }, { new: false }).then((record) => { + _id: id, + }, {$set: fields_to_update}, {new: false}).then((record) => { return record; }); }; -UserSchema.statics.SetTelegramWasBlocked = async function (idapp, teleg_id) { +UserSchema.statics.SetTelegramWasBlocked = async function(idapp, teleg_id) { const User = this; const fields_to_update = { @@ -1215,67 +1238,67 @@ UserSchema.statics.SetTelegramWasBlocked = async function (idapp, teleg_id) { 'profile.teleg_id': 0, }; - if (process.env.PROD === "1") { + if (process.env.PROD === '1') { const ris = await User.findOneAndUpdate({ idapp, - 'profile.teleg_id': teleg_id - }, { $set: fields_to_update }, { new: false }).then((record) => { + 'profile.teleg_id': teleg_id, + }, {$set: fields_to_update}, {new: false}).then((record) => { return record; }); } }; -UserSchema.statics.getNameSurnameByUsername = async function (idapp, username) { +UserSchema.statics.getNameSurnameByUsername = async function(idapp, username) { const User = this; return await User.findOne({ idapp, username, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }] - }, { name: 1, surname: 1 }) - .then((rec) => { - return (!!rec) ? `${rec.name} ${rec.surname}` : ''; - }).catch((e) => { - console.error('getNameSurnameByUsername', e); - }); + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], + }, {name: 1, surname: 1}).then((rec) => { + return (!!rec) ? `${rec.name} ${rec.surname}` : ''; + }).catch((e) => { + console.error('getNameSurnameByUsername', e); + }); }; -UserSchema.statics.getNameSurnameById = async function (idapp, userId) { +UserSchema.statics.getNameSurnameById = async function(idapp, userId) { const User = this; return await User.findOne({ idapp, _id: userId, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }] - }, { name: 1, surname: 1 }) - .then((rec) => { - return (!!rec) ? `${rec.name} ${rec.surname}` : ''; - }).catch((e) => { - console.error('getNameSurnameById', e); - }); + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], + }, {name: 1, surname: 1}).then((rec) => { + return (!!rec) ? `${rec.name} ${rec.surname}` : ''; + }).catch((e) => { + console.error('getNameSurnameById', e); + }); }; -UserSchema.statics.getSmallRecByIndOrder = async function (idapp, ind_order) { +UserSchema.statics.getSmallRecByIndOrder = async function(idapp, ind_order) { try { const rec = await ListaIngresso.getarray(idapp, - { - idapp, - ind_order, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }] - }, - { - idapp: 1, - index: 1, - ind_order: 1, - old_order: 1, - username: 1, - name: 1, - lang: 1, - surname: 1, - 'profile.teleg_id': 1, - }); + { + idapp, + ind_order, + $or: [ + {deleted: {$exists: false}}, + {deleted: {$exists: true, $eq: false}}], + }, + { + idapp: 1, + index: 1, + ind_order: 1, + old_order: 1, + username: 1, + name: 1, + lang: 1, + surname: 1, + 'profile.teleg_id': 1, + }); if (!!rec) return rec[0]; @@ -1288,22 +1311,20 @@ UserSchema.statics.getSmallRecByIndOrder = async function (idapp, ind_order) { }; -UserSchema.statics.NuovoSistema = function (idapp) { +UserSchema.statics.NuovoSistema = function(idapp) { const User = this; try { - return User.findOne({ idapp, old_order: { $exists: true } }) - .then((ris) => { - return !!ris; - }); + return User.findOne({idapp, old_order: {$exists: true}}).then((ris) => { + return !!ris; + }); } catch (e) { console.error('NuovoSistema', e.message); } }; - -UserSchema.statics.getRecByIndOrder = async function (idapp, ind_order) { +UserSchema.statics.getRecByIndOrder = async function(idapp, ind_order) { const User = this; try { @@ -1329,11 +1350,11 @@ UserSchema.statics.getRecByIndOrder = async function (idapp, ind_order) { }; const rec = await ListaIngresso.getarray(idapp, - { - idapp, - ind_order, - }, - filters); + { + idapp, + ind_order, + }, + filters); if (!!rec) return rec[0]; @@ -1346,144 +1367,141 @@ UserSchema.statics.getRecByIndOrder = async function (idapp, ind_order) { }; - -UserSchema.statics.getusersManagers = async function (idapp) { +UserSchema.statics.getusersManagers = async function(idapp) { const User = this; - return await User.find({ idapp, 'profile.manage_telegram': true }, { 'profile.teleg_id': 1, perm: 1 }) - .then((arrrec) => { - return (!!arrrec) ? arrrec : null; - }).catch((e) => { - console.error('getusersManagers', e); - }); + return await User.find({idapp, 'profile.manage_telegram': true}, + {'profile.teleg_id': 1, perm: 1}).then((arrrec) => { + return (!!arrrec) ? arrrec : null; + }).catch((e) => { + console.error('getusersManagers', e); + }); }; -UserSchema.statics.getusersRespList = async function (idapp) { +UserSchema.statics.getusersRespList = async function(idapp) { const User = this; - return await User.find({ idapp, 'profile.resplist': true }, { _id: 1, username: 1, name: 1, surname: 1 }) - .then((arrrec) => { - return (!!arrrec) ? arrrec : null; - }).catch((e) => { - console.error('getusersRespList', e); - }); + return await User.find({idapp, 'profile.resplist': true}, + {_id: 1, username: 1, name: 1, surname: 1}).then((arrrec) => { + return (!!arrrec) ? arrrec : null; + }).catch((e) => { + console.error('getusersRespList', e); + }); }; -UserSchema.statics.getusersWorkersList = async function (idapp) { +UserSchema.statics.getusersWorkersList = async function(idapp) { const User = this; - return await User.find({ idapp, 'profile.workerslist': true }, { _id: 1, username: 1, name: 1, surname: 1 }) - .then((arrrec) => { - return (!!arrrec) ? arrrec : null; - }).catch((e) => { - console.error('getusersWorkersList', e); - }); + return await User.find({idapp, 'profile.workerslist': true}, + {_id: 1, username: 1, name: 1, surname: 1}).then((arrrec) => { + return (!!arrrec) ? arrrec : null; + }).catch((e) => { + console.error('getusersWorkersList', e); + }); }; -UserSchema.statics.getusersManagersAndZoomeri = async function (idapp) { +UserSchema.statics.getusersManagersAndZoomeri = async function(idapp) { const User = this; return await User.find( - { - idapp, - or: [ - { 'profile.manage_telegram': true }, - { - perm: - { - $bit: + { + idapp, + or: [ + {'profile.manage_telegram': true}, + { + perm: + { + $bit: - Number(shared_consts.Permissions.Zoomeri) + Number(shared_consts.Permissions.Zoomeri), - } - } - ], - }, - { 'profile.teleg_id': 1 }) - .then((arrrec) => { - return (!!arrrec) ? arrrec : null; - }).catch((e) => { - console.error('getusersManagers', e); - }); + }, + }, + ], + }, + {'profile.teleg_id': 1}).then((arrrec) => { + return (!!arrrec) ? arrrec : null; + }).catch((e) => { + console.error('getusersManagers', e); + }); }; -UserSchema.statics.getUsersTelegALL = async function (idapp, username) { +UserSchema.statics.getUsersTelegALL = async function(idapp, username) { const User = this; if (!!username) { - return await User.find({ idapp, username, 'profile.teleg_id': { $gt: 0 } }) - .then((arrrec) => { - return (!!arrrec) ? arrrec : null; - }).catch((e) => { - console.error('getUsersTelegALL', e); - }); + return await User.find({idapp, username, 'profile.teleg_id': {$gt: 0}}). + then((arrrec) => { + return (!!arrrec) ? arrrec : null; + }). + catch((e) => { + console.error('getUsersTelegALL', e); + }); } else { - return await User.find({ idapp, 'profile.teleg_id': { $gt: 0 } }) - .then((arrrec) => { - return (!!arrrec) ? arrrec : null; - }).catch((e) => { - console.error('getUsersTelegALL', e); - }); + return await User.find({idapp, 'profile.teleg_id': {$gt: 0}}). + then((arrrec) => { + return (!!arrrec) ? arrrec : null; + }). + catch((e) => { + console.error('getUsersTelegALL', e); + }); } }; -UserSchema.statics.isManagerByIdTeleg = async function (idapp, idtelegram) { +UserSchema.statics.isManagerByIdTeleg = async function(idapp, idtelegram) { const User = this; return await User.findOne({ idapp, 'profile.manage_telegram': true, - 'profile.teleg_id': idtelegram - }, { 'profile.teleg_id': 1 }) - .then((rec) => { - return (!!rec && rec.profile.teleg_id === idtelegram); - }).catch((e) => { - console.error('getusersManagers', e); - return false - }); + 'profile.teleg_id': idtelegram, + }, {'profile.teleg_id': 1}).then((rec) => { + return (!!rec && rec.profile.teleg_id === idtelegram); + }).catch((e) => { + console.error('getusersManagers', e); + return false; + }); }; -UserSchema.statics.isAdminByIdTeleg = async function (idapp, idtelegram) { +UserSchema.statics.isAdminByIdTeleg = async function(idapp, idtelegram) { const User = this; return await User.findOne({ idapp, username: 'paoloar77', 'profile.manage_telegram': true, - 'profile.teleg_id': idtelegram - }, { 'profile.teleg_id': 1 }) - .then((rec) => { - return (!!rec && rec.profile.teleg_id === idtelegram); - }).catch((e) => { - console.error('getusersManagers', e); - return false - }); + 'profile.teleg_id': idtelegram, + }, {'profile.teleg_id': 1}).then((rec) => { + return (!!rec && rec.profile.teleg_id === idtelegram); + }).catch((e) => { + console.error('getusersManagers', e); + return false; + }); }; -UserSchema.statics.getUsersList = function (idapp) { +UserSchema.statics.getUsersList = function(idapp) { const User = this; return User.find({ - 'idapp': idapp, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }] - }, - { - username: 1, - name: 1, - surname: 1, - verified_email: 1, - made_gift: 1, - perm: 1, - email: 1, - date_reg: 1, - img: 1 - } - ) + 'idapp': idapp, + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], + }, + { + username: 1, + name: 1, + surname: 1, + verified_email: 1, + made_gift: 1, + perm: 1, + email: 1, + date_reg: 1, + img: 1, + }, + ); }; - -UserSchema.statics.getUsersListByParams = function (params) { +UserSchema.statics.getUsersListByParams = function(params) { const User = this; myclParamQuery = new queryclass.CParamsQuery(params); @@ -1491,21 +1509,21 @@ UserSchema.statics.getUsersListByParams = function (params) { const filterMatchBefore = `${myclParamQuery.filter}`; return User.find( - { $match: filterMatchBefore }, - { 'idapp': idapp }, - { - username: 1, - name: 1, - surname: 1, - verified_email: 1, - made_gift: 1, - perm: 1, - email: 1, - date_reg: 1, - img: 1, - lasttimeonline: 1, - news_on: 1 - }) + {$match: filterMatchBefore}, + {'idapp': idapp}, + { + username: 1, + name: 1, + surname: 1, + verified_email: 1, + made_gift: 1, + perm: 1, + email: 1, + date_reg: 1, + img: 1, + lasttimeonline: 1, + news_on: 1, + }); }; @@ -1514,51 +1532,52 @@ UserSchema.statics.getUsersListByParams = function (params) { * @returns {Object} Object -> `{ rows, count }` */ -UserSchema.statics.getFieldsForSearch = function () { +UserSchema.statics.getFieldsForSearch = function() { return [ - { field: 'username', type: tools.FieldType.string }, - { field: 'name', type: tools.FieldType.string }, - { field: 'index', type: tools.FieldType.number }, - { field: 'ind_order', type: tools.FieldType.number }, - { field: 'old_order', type: tools.FieldType.number }, - { field: 'surname', type: tools.FieldType.string }, - { field: 'email', type: tools.FieldType.string }, - { field: 'profile.cell', type: tools.FieldType.string }, - { field: 'profile.email_paypal', type: tools.FieldType.string }, - { field: 'profile.payeer_id', type: tools.FieldType.string }, - { field: 'profile.advcash_id', type: tools.FieldType.string }, - { field: 'profile.revolut', type: tools.FieldType.string }, - { field: 'profile.link_payment', type: tools.FieldType.string }, - { field: 'profile.teleg_id', type: tools.FieldType.number }, - { field: 'profile.username_telegram', type: tools.FieldType.string }, - { field: 'aportador_solidario', type: tools.FieldType.string }] + {field: 'username', type: tools.FieldType.string}, + {field: 'name', type: tools.FieldType.string}, + {field: 'index', type: tools.FieldType.number}, + {field: 'ind_order', type: tools.FieldType.number}, + {field: 'old_order', type: tools.FieldType.number}, + {field: 'surname', type: tools.FieldType.string}, + {field: 'email', type: tools.FieldType.string}, + {field: 'profile.cell', type: tools.FieldType.string}, + {field: 'profile.email_paypal', type: tools.FieldType.string}, + {field: 'profile.payeer_id', type: tools.FieldType.string}, + {field: 'profile.advcash_id', type: tools.FieldType.string}, + {field: 'profile.revolut', type: tools.FieldType.string}, + {field: 'profile.link_payment', type: tools.FieldType.string}, + {field: 'profile.teleg_id', type: tools.FieldType.number}, + {field: 'profile.username_telegram', type: tools.FieldType.string}, + {field: 'aportador_solidario', type: tools.FieldType.string}]; }; -UserSchema.statics.executeQueryTable = function (idapp, params) { +UserSchema.statics.executeQueryTable = function(idapp, params) { params.fieldsearch = this.getFieldsForSearch(); return tools.executeQueryTable(this, idapp, params); }; -UserSchema.statics.findAllIdApp = function (idapp) { +UserSchema.statics.findAllIdApp = function(idapp) { const User = this; const myfind = { idapp, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }] + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], }; return User.find(myfind, (err, arrrec) => { - return arrrec + return arrrec; }); }; -UserSchema.statics.DuplicateAllRecords = async function (idapporig, idappdest) { +UserSchema.statics.DuplicateAllRecords = async function(idapporig, idappdest) { return tools.DuplicateAllRecords(this, idapporig, idappdest); }; -UserSchema.statics.getDashboard = async function (idapp, aportador_solidario, username, aportador_solidario_nome_completo) { +UserSchema.statics.getDashboard = async function( + idapp, aportador_solidario, username, aportador_solidario_nome_completo) { try { // DATA: username, name, surname, email, intcode_cell, cell @@ -1571,20 +1590,24 @@ UserSchema.statics.getDashboard = async function (idapp, aportador_solidario, us dashboard.myself = await User.getUserShortDataByUsername(idapp, username); // Data of my Aportador - dashboard.aportador = await User.getUserShortDataByUsername(idapp, aportador_solidario); + dashboard.aportador = await User.getUserShortDataByUsername(idapp, + aportador_solidario); // if (dashboard.aportador === undefined) { // dashboard.aportador = await ExtraList.getUserNotRegisteredByNameSurname(idapp, aportador_solidario_nome_completo); // } - const arrap = await User.getDownlineByUsername(idapp, username, false, true); + const arrap = await User.getDownlineByUsername(idapp, username, false, + true); if (!!arrap) dashboard.numpeople_aportador = arrap.length; - dashboard.arrimbarchi = await ListaIngresso.findAllByUsername(idapp, username); + dashboard.arrimbarchi = await ListaIngresso.findAllByUsername(idapp, + username); // dashboard.arrprofili = await Nave.getArrProfiliByIndOrder(idapp, dashboard.myself.ind_order); - dashboard.arrposizioni = await Nave.getArrPosizioniByUsername(idapp, username); + dashboard.arrposizioni = await Nave.getArrPosizioniByUsername(idapp, + username); /* let arrrec = []; if (dashboard.arrimbarchi.length > 0) { @@ -1593,8 +1616,10 @@ UserSchema.statics.getDashboard = async function (idapp, aportador_solidario, us for (let myimbarco of dashboard.arrimbarchi) { if (!!myimbarco.invitante_username) - dashboard.arrusers[myimbarco.invitante_username] = await User.getUserShortDataByUsername(idapp, myimbarco.invitante_username); - myimbarco._doc.posiz = await Graduatoria.getPosizioneInGraduatoria(idapp, myimbarco.ind_order, myimbarco.username, myimbarco.num_tess); + dashboard.arrusers[myimbarco.invitante_username] = await User.getUserShortDataByUsername( + idapp, myimbarco.invitante_username); + myimbarco._doc.posiz = await Graduatoria.getPosizioneInGraduatoria(idapp, + myimbarco.ind_order, myimbarco.username, myimbarco.num_tess); } dashboard.navi_partenza = await NavePersistente.getListaNavi(idapp); @@ -1602,17 +1627,17 @@ UserSchema.statics.getDashboard = async function (idapp, aportador_solidario, us for (let mypos of dashboard.arrposizioni) { mypos.rec = await Nave.getNaveByRigaCol(idapp, mypos.riga, mypos.col); - mypos.nave_partenza = await NavePersistente.findByRigaColByDonatore(idapp, mypos.riga, mypos.col, 0); + mypos.nave_partenza = await NavePersistente.findByRigaColByDonatore(idapp, + mypos.riga, mypos.col, 0); if (!!mypos.nave_partenza) - mypos.flotta = await Flotta.getFlottaByNavePersistente(idapp, mypos.nave_partenza); + mypos.flotta = await Flotta.getFlottaByNavePersistente(idapp, + mypos.nave_partenza); } - //for (let indriga = 0; indriga < 10; indriga++) { // dashboard.navi_partenza.push(await Nave.getPrimaNaveByRiga(idapp, indriga)); //} - const arrnew = []; try { @@ -1623,7 +1648,8 @@ UserSchema.statics.getDashboard = async function (idapp, aportador_solidario, us if (mypos.num_tess % 2 !== 0) { for (let myrec of dashboard.arrposizioni) { - if (myrec.num_tess === mypos.num_tess + 1 && (myrec.ind_order === mypos.ind_order)) { + if (myrec.num_tess === mypos.num_tess + 1 && + (myrec.ind_order === mypos.ind_order)) { // La Nave di Ritorno (numtess = 2) esiste nella lista ! trovato = true; break; @@ -1649,11 +1675,13 @@ UserSchema.statics.getDashboard = async function (idapp, aportador_solidario, us surname: myr.rec.mediatore.recmediatore.surname, username: myr.rec.mediatore.recmediatore.username, num_tess: myr.rec.mediatore.recmediatore.num_tess + 1, - rec: await Nave.getNaveByRigaCol(idapp, mymediatore.riga, mymediatore.col), + rec: await Nave.getNaveByRigaCol(idapp, mymediatore.riga, + mymediatore.col), nave_partenza: {}, }; - myrec.nave_partenza = await NavePersistente.findByRigaColByDonatore(idapp, myrec.riga, myrec.col, 0); + myrec.nave_partenza = await NavePersistente.findByRigaColByDonatore( + idapp, myrec.riga, myrec.col, 0); arrnew.push(myrec); } @@ -1675,7 +1703,8 @@ UserSchema.statics.getDashboard = async function (idapp, aportador_solidario, us } }; -UserSchema.statics.getDownline = async function (idapp, aportador_solidario, username) { +UserSchema.statics.getDownline = async function( + idapp, aportador_solidario, username) { try { // DATA: username, name, surname, email, intcode_cell, cell @@ -1693,10 +1722,12 @@ UserSchema.statics.getDownline = async function (idapp, aportador_solidario, use downline.downbyuser = {}; for (const down of downline.downline) { - downline.downbyuser[down.username] = await User.getDownlineByUsername(idapp, down.username, false); + downline.downbyuser[down.username] = await User.getDownlineByUsername( + idapp, down.username, false); for (const down2 of downline.downbyuser[down.username]) { - downline.downbyuser[down2.username] = await User.getDownlineByUsername(idapp, down2.username, false); + downline.downbyuser[down2.username] = await User.getDownlineByUsername( + idapp, down2.username, false); } } @@ -1727,7 +1758,8 @@ UserSchema.statics.fixUsername = async function (idapp, aportador_solidario_ind_ }; */ -UserSchema.statics.findByCellAndNameSurname = function (idapp, cell, name, surname) { +UserSchema.statics.findByCellAndNameSurname = function( + idapp, cell, name, surname) { const User = this; return User.findOne({ @@ -1735,17 +1767,16 @@ UserSchema.statics.findByCellAndNameSurname = function (idapp, cell, name, surna 'profile.cell': cell, 'name': name, 'surname': surname, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }] + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], }); }; - -UserSchema.statics.getUsersRegistered = async function (idapp) { +UserSchema.statics.getUsersRegistered = async function(idapp) { const User = this; const myfind = { idapp, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }] + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], }; return await User.count(myfind); @@ -1800,12 +1831,12 @@ UserSchema.statics.getUsersQualified = async function (idapp, numinvitati) { }; */ -UserSchema.statics.visuUtentiNonInNavi = async function (idapp) { +UserSchema.statics.visuUtentiNonInNavi = async function(idapp) { const User = this; const arrusers = await User.find({ idapp, - $and: User.getQueryQualified() + $and: User.getQueryQualified(), }, { name: 1, surname: 1, @@ -1815,7 +1846,6 @@ UserSchema.statics.visuUtentiNonInNavi = async function (idapp) { sospeso: 1, }); - let num = 0; let innave = 0; let noninnave = 0; @@ -1843,12 +1873,14 @@ UserSchema.statics.visuUtentiNonInNavi = async function (idapp) { let visualizza = false; // Controlla se ho un doppione nelle Navi ! - let mienavi = await Nave.find({ idapp, ind_order: user.ind_order }, { num_tess: 1 }); + let mienavi = await Nave.find({idapp, ind_order: user.ind_order}, + {num_tess: 1}); let strnavidoppie = []; if (!!mienavi) { strnavidoppie = mienavi.reduce((acc, currentValue, index, array) => { - if (array.indexOf(currentValue.num_tess) > -1 && !acc.includes(currentValue.num_tess)) + if (array.indexOf(currentValue.num_tess) > -1 && + !acc.includes(currentValue.num_tess)) acc.push(currentValue.num_tess); return acc; }, []); @@ -1858,17 +1890,20 @@ UserSchema.statics.visuUtentiNonInNavi = async function (idapp) { visualizza = true; } - user.numinvitati = await ListaIngresso.getnumInvitati(idapp, user.username); + user.numinvitati = await ListaIngresso.getnumInvitati(idapp, + user.username); reg++; - let mianave = await Nave.findOne({ idapp, ind_order: user.ind_order }); - let mialistaingresso = await ListaIngresso.findOne({ idapp, ind_order: user.ind_order }); + let mianave = await Nave.findOne({idapp, ind_order: user.ind_order}); + let mialistaingresso = await ListaIngresso.findOne( + {idapp, ind_order: user.ind_order}); let trovato = false; if (!mianave) visualizza = true; if (visualizza) { - mystr += user.username + ' ' + user.name + ' ' + user.surname + ' [' + user.index + '] [inv=' + user.numinvitati + ']' + mystr += user.username + ' ' + user.name + ' ' + user.surname + ' [' + + user.index + '] [inv=' + user.numinvitati + ']'; trovato = true; } @@ -1880,7 +1915,6 @@ UserSchema.statics.visuUtentiNonInNavi = async function (idapp) { else innave++; - if (user.sospeso) { numsospesi++; } @@ -1906,7 +1940,6 @@ UserSchema.statics.visuUtentiNonInNavi = async function (idapp) { } } - mystrstart = 'Registrati: ' + reg + tools.ACAPO; mystrstart += '0 Invitati: ' + num0inv + tools.ACAPO; mystrstart += '1 Invitato: ' + num1inv + tools.ACAPO; @@ -1921,7 +1954,7 @@ UserSchema.statics.visuUtentiNonInNavi = async function (idapp) { mystr = mystrstart + mystr; - return { num, mystr }; + return {num, mystr}; }; // UserSchema.statics.getNumUsersQualified = async function (idapp, numinvitati) { @@ -1932,267 +1965,290 @@ UserSchema.statics.visuUtentiNonInNavi = async function (idapp) { // // }; - -UserSchema.statics.getEmailNotVerified = async function (idapp) { +UserSchema.statics.getEmailNotVerified = async function(idapp) { const User = this; const myfind = { idapp, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }], - verified_email: false + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], + verified_email: false, }; return await User.count(myfind); }; -UserSchema.statics.getUsersTelegramAttivo = async function (idapp) { +UserSchema.statics.getUsersTelegramAttivo = async function(idapp) { const User = this; const myfind = { idapp, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }], - 'profile.teleg_id': { $gt: 0 } + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], + 'profile.teleg_id': {$gt: 0}, }; return await User.count(myfind); }; -UserSchema.statics.getUsersTelegramPending = async function (idapp) { +UserSchema.statics.getUsersTelegramPending = async function(idapp) { const User = this; const myfind = { idapp, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }], - 'profile.teleg_checkcode': { $gt: 0 } + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], + 'profile.teleg_checkcode': {$gt: 0}, }; return await User.count(myfind); }; -UserSchema.statics.getUsersZoom = async function (idapp) { +UserSchema.statics.getUsersZoom = async function(idapp) { const User = this; const myfind = { idapp, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }], - 'profile.saw_zoom_presentation': true + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], + 'profile.saw_zoom_presentation': true, }; return await User.count(myfind); }; -UserSchema.statics.getUsersResidenti = async function (idapp) { +UserSchema.statics.getUsersResidenti = async function(idapp) { const User = this; const myfind = { idapp, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }], - 'profile.socioresidente': { $exists: true, $eq: true } + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], + 'profile.socioresidente': {$exists: true, $eq: true}, }; - return await User.find(myfind, { username: 1, name: 1, surname: 1 }); + return await User.find(myfind, {username: 1, name: 1, surname: 1}); }; -UserSchema.statics.getSaw_and_Accepted = async function (idapp) { +UserSchema.statics.getSaw_and_Accepted = async function(idapp) { const User = this; const myfind = { idapp, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }], - 'profile.saw_and_accepted': shared_consts.ALL_SAW_AND_ACCEPTED + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], + 'profile.saw_and_accepted': shared_consts.ALL_SAW_AND_ACCEPTED, }; return await User.count(myfind); }; -UserSchema.statics.getUsersDreams = async function (idapp) { +UserSchema.statics.getUsersDreams = async function(idapp) { const User = this; const myfind = { idapp, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }], - 'profile.my_dream': { $exists: true }, - "$expr": { "$gt": [{ "$strLenCP": "$profile.my_dream" }, 10] } + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], + 'profile.my_dream': {$exists: true}, + '$expr': {'$gt': [{'$strLenCP': '$profile.my_dream'}, 10]}, }; return await User.count(myfind); }; -UserSchema.statics.getLastUsers = async function (idapp) { +UserSchema.statics.getLastUsers = async function(idapp) { const User = this; const lastn = await Settings.getValDbSettings(idapp, 'SHOW_LAST_N_USERS', 5); return await User.find( - { - idapp, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }] - }, - { - username: 1, - name: 1, - surname: 1, - date_reg: 1, - index: 1, - 'profile.nationality': 1, - }).sort({ date_reg: -1 }).limit(lastn).then((arr) => { + { + idapp, + $or: [ + {deleted: {$exists: false}}, + {deleted: {$exists: true, $eq: false}}], + }, + { + username: 1, + name: 1, + surname: 1, + date_reg: 1, + index: 1, + 'profile.nationality': 1, + }).sort({date_reg: -1}).limit(lastn).then((arr) => { //return JSON.stringify(arr) - return arr + return arr; }); }; -UserSchema.statics.checkUser = async function (idapp, username) { +UserSchema.statics.checkUser = async function(idapp, username) { const User = this; - return await User.findOne({ idapp, username }, { + return await User.findOne({idapp, username}, { verified_email: 1, 'profile.teleg_id': 1, 'profile.teleg_checkcode': 1, }).then((rec) => { - return JSON.stringify(rec) + return JSON.stringify(rec); }); }; -UserSchema.statics.calculateStat = async function (idapp, username) { +UserSchema.statics.calculateStat = async function(idapp, username) { const User = this; return calcstat = { numinvitati: await ListaIngresso.getnumInvitati(idapp, username), - numinvitati_attivi: await ListaIngresso.getnumInvitatiAttivi(idapp, username), + numinvitati_attivi: await ListaIngresso.getnumInvitatiAttivi(idapp, + username), }; }; -UserSchema.statics.getDistinctNationalityQuery = function (idapp) { +UserSchema.statics.getDistinctNationalityQuery = function(idapp) { const query = [ { $match: { idapp, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }], - } + $or: [ + {deleted: {$exists: false}}, + {deleted: {$exists: true, $eq: false}}], + }, }, { - $group: { _id: "$profile.nationality", count: { $sum: 1 } } + $group: {_id: '$profile.nationality', count: {$sum: 1}}, }, { - $sort: { count: -1 } - } + $sort: {count: -1}, + }, ]; - return query + return query; }; - -UserSchema.statics.findAllDistinctNationality = async function (idapp) { +UserSchema.statics.findAllDistinctNationality = async function(idapp) { const User = this; - return User.aggregate(User.getDistinctNationalityQuery(idapp)) - .then(ris => { - // console.table(ris); - return JSON.stringify(ris); - }); + return User.aggregate(User.getDistinctNationalityQuery(idapp)).then(ris => { + // console.table(ris); + return JSON.stringify(ris); + }); }; -UserSchema.statics.getUsersRegDaily = function (idapp, nrec) { - - const query = [ - { - $match: { - idapp, date_reg: { $gte: tools.IncDateNow(-(1000 * 60 * 60 * 24 * nrec)) }, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }] - } - }, - { - $group: { - _id: { $dateToString: { format: "%Y-%m-%d", date: "$date_reg", timezone: 'Europe/Rome' } }, - count: { $sum: 1 } - } - }, - { - $sort: { _id: 1 } - } - ]; - return query -}; - -UserSchema.statics.getUsersRegWeekly = function (idapp, nrec) { - - const query = [ - { - $match: { - idapp, date_reg: { $gte: tools.IncDateNow(-(1000 * 60 * 60 * 24 * nrec)) }, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }] - } - }, - { - $group: { - _id: { $dateToString: { format: "%Y-%U", date: "$date_reg", timezone: 'Europe/Rome' } }, - count: { $sum: 1 } - } - }, - { - $sort: { _id: 1 } - } - ]; - return query -}; - -UserSchema.statics.getnumRegNDays = function (idapp, nrec) { - +UserSchema.statics.getUsersRegDaily = function(idapp, nrec) { const query = [ { $match: { idapp, - date_reg: { $lt: tools.IncDateNow(-(1000 * 60 * 60 * 24 * nrec)) }, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }] - } + date_reg: {$gte: tools.IncDateNow(-(1000 * 60 * 60 * 24 * nrec))}, + $or: [ + {deleted: {$exists: false}}, + {deleted: {$exists: true, $eq: false}}], + }, }, { $group: { - _id: { $dateToString: { format: "%Y-%m-%d", date: "$date_reg", timezone: 'Europe/Rome' } }, - count: { $sum: 1 } - } + _id: { + $dateToString: { + format: '%Y-%m-%d', + date: '$date_reg', + timezone: 'Europe/Rome', + }, + }, + count: {$sum: 1}, + }, }, { - $sort: { _id: 1 } - } + $sort: {_id: 1}, + }, ]; - return query + return query; }; -UserSchema.statics.calcnumRegUntilDay = async function (idapp) { +UserSchema.statics.getUsersRegWeekly = function(idapp, nrec) { + + const query = [ + { + $match: { + idapp, + date_reg: {$gte: tools.IncDateNow(-(1000 * 60 * 60 * 24 * nrec))}, + $or: [ + {deleted: {$exists: false}}, + {deleted: {$exists: true, $eq: false}}], + }, + }, + { + $group: { + _id: { + $dateToString: { + format: '%Y-%U', + date: '$date_reg', + timezone: 'Europe/Rome', + }, + }, + count: {$sum: 1}, + }, + }, + { + $sort: {_id: 1}, + }, + ]; + return query; +}; + +UserSchema.statics.getnumRegNDays = function(idapp, nrec) { + + const query = [ + { + $match: { + idapp, + date_reg: {$lt: tools.IncDateNow(-(1000 * 60 * 60 * 24 * nrec))}, + $or: [ + {deleted: {$exists: false}}, + {deleted: {$exists: true, $eq: false}}], + }, + }, + { + $group: { + _id: { + $dateToString: { + format: '%Y-%m-%d', + date: '$date_reg', + timezone: 'Europe/Rome', + }, + }, + count: {$sum: 1}, + }, + }, + { + $sort: {_id: 1}, + }, + ]; + return query; +}; + +UserSchema.statics.calcnumRegUntilDay = async function(idapp) { const User = this; - return await User.aggregate(User.getnumRegNDays(idapp, 30)) - .then((arr) => { - return arr.reduce((sum, rec) => sum + rec.count, 0); - }); + return await User.aggregate(User.getnumRegNDays(idapp, 30)).then((arr) => { + return arr.reduce((sum, rec) => sum + rec.count, 0); + }); }; -UserSchema.statics.calcRegDaily = async function (idapp) { +UserSchema.statics.calcRegDaily = async function(idapp) { const User = this; - return User.aggregate(User.getUsersRegDaily(idapp, 30)) - .then(ris => { - // console.table(ris); - return JSON.stringify(ris); - }); + return User.aggregate(User.getUsersRegDaily(idapp, 30)).then(ris => { + // console.table(ris); + return JSON.stringify(ris); + }); }; -UserSchema.statics.calcRegWeekly = async function (idapp) { +UserSchema.statics.calcRegWeekly = async function(idapp) { const User = this; - return User.aggregate(User.getUsersRegWeekly(idapp, 20 * 7)) - .then(ris => { - // console.table(ris); - return JSON.stringify(ris.slice(0, -1)); - }); + return User.aggregate(User.getUsersRegWeekly(idapp, 20 * 7)).then(ris => { + // console.table(ris); + return JSON.stringify(ris.slice(0, -1)); + }); }; - if (tools.INITDB_FIRSTIME) { console.log(' createIndex User Index...'); // UserSchema.index({ username: 'text', name: 'text', surname: 'text', email: 'text' }); @@ -2207,7 +2263,8 @@ async function addUtentiInLista(idapp, mode, arrusers) { for (const rec of arrusers) { let ok = false; let qualified = await User.isUserQualified7(idapp, rec.username); - let numinvitatiattivi = await ListaIngresso.getnumInvitatiAttivi(idapp, rec.username); + let numinvitatiattivi = await ListaIngresso.getnumInvitatiAttivi(idapp, + rec.username); let numinvitati = await ListaIngresso.getnumInvitati(idapp, rec.username); if (rec.profile.special_req) { @@ -2229,7 +2286,8 @@ async function addUtentiInLista(idapp, mode, arrusers) { } if (ok) { - ris = await ListaIngresso.addUserInListaIngresso(idapp, rec.username, rec.aportador_solidario, rec.lang, false, true); + ris = await ListaIngresso.addUserInListaIngresso(idapp, rec.username, + rec.aportador_solidario, rec.lang, false, true); if (!!ris) num++; } @@ -2238,53 +2296,54 @@ async function addUtentiInLista(idapp, mode, arrusers) { } -UserSchema.statics.getUsernameByIndOrder = async function (idapp, ind_order) { +UserSchema.statics.getUsernameByIndOrder = async function(idapp, ind_order) { const myrec = await User.getSmallRecByIndOrder(idapp, ind_order); return (!!myrec) ? myrec.username : ''; }; -UserSchema.statics.getUsernameByIndex = async function (idapp, index) { +UserSchema.statics.getUsernameByIndex = async function(idapp, index) { const myrec = await User.findOne({ idapp, index, - }, { username: 1 }); + }, {username: 1}); - return (!!myrec) ? myrec.username : '' + return (!!myrec) ? myrec.username : ''; }; -UserSchema.statics.ricalcolaIndex = async function (idapp) { +UserSchema.statics.ricalcolaIndex = async function(idapp) { const User = this; const arrusers = await User.find({ idapp, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }] - }).sort({ old_order: 1 }); + $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], + }).sort({old_order: 1}); let index = 0; try { for (const user of arrusers) { let field = { - index + index, }; index++; - const ris = await User.findOneAndUpdate({ _id: user._id }, { $set: field }, { new: false }); + const ris = await User.findOneAndUpdate({_id: user._id}, {$set: field}, + {new: false}); } } catch (e) { console.error(e.message); } - }; -UserSchema.statics.changeInvitante = async function (idapp, username, invitante_username, ind_order_ingr) { +UserSchema.statics.changeInvitante = async function( + idapp, username, invitante_username, ind_order_ingr) { const User = this; - const rec_ind_order_prima = await ListaIngresso.findOne({ idapp, username }); + const rec_ind_order_prima = await ListaIngresso.findOne({idapp, username}); let modif_aportador = false; // cambia aportador_solidario solo se è la prima nave! @@ -2299,26 +2358,28 @@ UserSchema.statics.changeInvitante = async function (idapp, username, invitante_ } if (modif_aportador) { - await User.findOneAndUpdate({ idapp, username }, { $set: { aportador_solidario: invitante_username } }); + await User.findOneAndUpdate({idapp, username}, + {$set: {aportador_solidario: invitante_username}}); } // ** // ** Cambia invitante_username e ind_order di LISTAINGRESSO // ** - const rec_ingr = await ListaIngresso.findOne({ idapp, username: username }); + const rec_ingr = await ListaIngresso.findOne({idapp, username: username}); if (!!rec_ingr) { // await ListaIngresso.findByIdAndUpdate(rec_ingr._id, { $set: { invitante_username, ind_order: ind_order_ingr } }); - await ListaIngresso.findByIdAndUpdate(rec_ingr._id, { $set: { invitante_username } }); + await ListaIngresso.findByIdAndUpdate(rec_ingr._id, + {$set: {invitante_username}}); } - }; - -UserSchema.statics.NessunaNavePresenteByUsername = async function (idapp, username) { +UserSchema.statics.NessunaNavePresenteByUsername = async function( + idapp, username) { const User = this; - const rec = await User.findOne({ idapp, username }, { username: 1, ind_order: 1 }); + const rec = await User.findOne({idapp, username}, + {username: 1, ind_order: 1}); if (!!rec) { // Controlla se è qualificato! const qualified = await User.isUserQualified7(idapp, rec.username); @@ -2327,20 +2388,21 @@ UserSchema.statics.NessunaNavePresenteByUsername = async function (idapp, userna // Ha un'imbarco almeno? const arrimbarchi = await ListaIngresso.findOne({ idapp, username: rec.username, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }] + $or: [ + {deleted: {$exists: false}}, + {deleted: {$exists: true, $eq: false}}], }); const arrnavi = await Nave.findOne({ idapp, ind_order: rec.old_order, }); - if (!arrimbarchi && !arrnavi) { // NEANCHE 1 ! const recout = await User.findOneAndUpdate({ idapp, - username - }, { $set: { navinonpresenti: true } }, { new: false }); + username, + }, {$set: {navinonpresenti: true}}, {new: false}); return (!!recout); } @@ -2351,16 +2413,17 @@ UserSchema.statics.NessunaNavePresenteByUsername = async function (idapp, userna }; -UserSchema.statics.getInfoUser = async function (idapp, username) { +UserSchema.statics.getInfoUser = async function(idapp, username) { return { username, is7req: await User.isUserQualified7(idapp, username), is9req: await User.isUserQualified9(idapp, username), - } + }; -} +}; -UserSchema.statics.checkIfSbloccatiRequisiti = async function (idapp, allData, id) { +UserSchema.statics.checkIfSbloccatiRequisiti = async function( + idapp, allData, id) { const User = this; const telegrambot = require('../telegram/telegrambot'); @@ -2377,13 +2440,15 @@ UserSchema.statics.checkIfSbloccatiRequisiti = async function (idapp, allData, i const is7req = await User.isUserQualified7(idapp, allData.myuser.username); const is9req = await User.isUserQualified9(idapp, allData.myuser.username); - const userlista = await ListaIngresso.getListaTessByUsername(idapp, allData.myuser.username); + const userlista = await ListaIngresso.getListaTessByUsername(idapp, + allData.myuser.username); //if (userlista.length > 0) { //TOGLIERE if (userlista.length === 0) { // Se non sono ancora dentro alla lista, allora controllo if (!!allData.precDataUser) { - if ((!allData.precDataUser.is7req && is7req) && !await User.isUserAlreadyQualified(idapp, allData.myuser.username)) { + if ((!allData.precDataUser.is7req && is7req) && + !await User.isUserAlreadyQualified(idapp, allData.myuser.username)) { await User.setUserQualified(idapp, allData.myuser.username); // ORA HAI I 7 REQUISITI ! @@ -2391,17 +2456,24 @@ UserSchema.statics.checkIfSbloccatiRequisiti = async function (idapp, allData, i // telegrambot.sendMsgTelegram(idapp, allData.myuser.username, msgtext, true); // Anche a STAFF // Aggiungilo alla ListaIngresso - risingr = await ListaIngresso.addUserInListaIngresso(idapp, allData.myuser.username, allData.myuser.aportador_iniziale, allData.myuser.lang, true, false); + risingr = await ListaIngresso.addUserInListaIngresso(idapp, + allData.myuser.username, allData.myuser.aportador_iniziale, + allData.myuser.lang, true, false); } } } if (!!allData.precDataUser) { - if ((!allData.precDataUser.is9req && is9req) && !await User.isUserAlreadyQualified_2Invitati(idapp, allData.myuser.username)) { + if ((!allData.precDataUser.is9req && is9req) && + !await User.isUserAlreadyQualified_2Invitati(idapp, + allData.myuser.username)) { await User.setUserQualified_2Invitati(idapp, allData.myuser.username); // ORA HAI I 9 REQUISITI ! - const msgtext = telegrambot.getCiao(idapp, allData.myuser.username, allData.myuser.lang) + tools.gettranslate('HAI_I_9_REQUISITI', allData.myuser.lang); - telegrambot.sendMsgTelegram(idapp, allData.myuser.username, msgtext, false); // Anche a STAFF + const msgtext = telegrambot.getCiao(idapp, allData.myuser.username, + allData.myuser.lang) + + tools.gettranslate('HAI_I_9_REQUISITI', allData.myuser.lang); + telegrambot.sendMsgTelegram(idapp, allData.myuser.username, msgtext, + false); // Anche a STAFF } } @@ -2421,16 +2493,18 @@ UserSchema.statics.checkIfSbloccatiRequisiti = async function (idapp, allData, i }; - -UserSchema.statics.mettiSognoePaypal = async function (idapp, modifica) { +UserSchema.statics.mettiSognoePaypal = async function(idapp, modifica) { const User = this; let num = 0; arrusers = await User.find({ 'idapp': idapp, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }, - { subaccount: { $exists: false } }, { subaccount: { $exists: true, $eq: false } }] + $or: [ + {deleted: {$exists: false}}, + {deleted: {$exists: true, $eq: false}}, + {subaccount: {$exists: false}}, + {subaccount: {$exists: true, $eq: false}}], }); for (const rec of arrusers) { @@ -2467,15 +2541,15 @@ UserSchema.statics.mettiSognoePaypal = async function (idapp, modifica) { let allData = {}; allData.myuser = await User.getUserById(idapp, rec.id); if (!!allData.myuser) - allData.precDataUser = await User.getInfoUser(idapp, allData.myuser.username); + allData.precDataUser = await User.getInfoUser(idapp, + allData.myuser.username); else allData.precDataUser = null; - const risupd = await User.findByIdAndUpdate(rec._id, { 'profile.email_paypal': rec.profile.email_paypal, - 'profile.my_dream': rec.profile.my_dream - }, { new: false }); + 'profile.my_dream': rec.profile.my_dream, + }, {new: false}); if (risupd) { await User.checkIfSbloccatiRequisiti(idapp, allData, rec.id); @@ -2484,84 +2558,89 @@ UserSchema.statics.mettiSognoePaypal = async function (idapp, modifica) { } } - return { num }; + return {num}; }; -UserSchema.statics.flagUtentiNaviNonPresenti = async function (idapp) { +UserSchema.statics.flagUtentiNaviNonPresenti = async function(idapp) { const User = this; let num = 0; - await User.updateMany({ idapp }, { $set: { navinonpresenti: false } }); + await User.updateMany({idapp}, {$set: {navinonpresenti: false}}); arrusers = await User.find({ 'idapp': idapp, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }, - { subaccount: { $exists: false } }, { subaccount: { $exists: true, $eq: false } }] + $or: [ + {deleted: {$exists: false}}, + {deleted: {$exists: true, $eq: false}}, + {subaccount: {$exists: false}}, + {subaccount: {$exists: true, $eq: false}}], }); for (const rec of arrusers) { - const nessunanave = await User.NessunaNavePresenteByUsername(idapp, rec.username); + const nessunanave = await User.NessunaNavePresenteByUsername(idapp, + rec.username); if (nessunanave) num++; } - return { num }; + return {num}; }; -UserSchema.statics.addNavePerUtentiNaviNonPresenti = async function (idapp) { +UserSchema.statics.addNavePerUtentiNaviNonPresenti = async function(idapp) { const User = this; let num = 0; arrusers = await User.find({ 'idapp': idapp, - navinonpresenti: true + navinonpresenti: true, }); for (const user of arrusers) { // Controlla se è qualificato! mydata = tools.AddDate(user.date_reg, 7); - const newrecingr = await ListaIngresso.addUserInListaIngresso(idapp, user.username, user.aportador_solidario, user.lang, true, true, mydata); + const newrecingr = await ListaIngresso.addUserInListaIngresso(idapp, + user.username, user.aportador_solidario, user.lang, true, true, mydata); await tools.snooze(1000); num++; } - return { num }; + return {num}; }; -UserSchema.statics.convSubAccount = async function (idapp) { +UserSchema.statics.convSubAccount = async function(idapp) { const User = this; - // Solo i Cancellati ! - arrusers = await User.find({ 'idapp': idapp, deleted: true }); + arrusers = await User.find({'idapp': idapp, deleted: true}); let num = 0; for (const rec of arrusers) { // Cerca il suo Principale! const trovato = await User.findOne({ idapp, 'profile.teleg_id': rec.profile.teleg_id, - $or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }] + $or: [ + {deleted: {$exists: false}}, + {deleted: {$exists: true, $eq: false}}], }); if (trovato) { num++; - await User.findByIdAndUpdate(rec._id, { subaccount: true }, { new: false }); + await User.findByIdAndUpdate(rec._id, {subaccount: true}, {new: false}); } } - return { num }; + return {num}; }; - -UserSchema.statics.getLastRec = async function (idapp) { +UserSchema.statics.getLastRec = async function(idapp) { const User = this; - lastrec = await User.find({ idapp }).sort({ date_reg: -1 }).limit(1); + lastrec = await User.find({idapp}).sort({date_reg: -1}).limit(1); if (!!lastrec) { return lastrec[0]; } else { @@ -2569,37 +2648,39 @@ UserSchema.statics.getLastRec = async function (idapp) { } }; - -UserSchema.statics.DbOp = async function (idapp, mydata) { +UserSchema.statics.DbOp = async function(idapp, mydata) { const User = this; try { if (mydata.dbop === 'changeCellInt') { - arrusers = await User.find({ 'idapp': idapp }); + arrusers = await User.find({'idapp': idapp}); let num = 0; for (const rec of arrusers) { // DISATTIVATO: ORA NON MI SERVE PIU if (false) { - let mycell = tools.removespaces(rec.profile.intcode_cell + rec.profile.cell); - await User.findOneAndUpdate({ _id: rec._id }, { $set: { 'profile.cell': mycell } }); + let mycell = tools.removespaces( + rec.profile.intcode_cell + rec.profile.cell); + await User.findOneAndUpdate({_id: rec._id}, + {$set: {'profile.cell': mycell}}); num++; } } - return { num }; + return {num}; // return await User.updateMany({ idapp }, { $set: { 'profile.cell': { $concat: ["$profile.intcode_cell", "$profile.cell"] } } }) } else if (mydata.dbop === 'changeEmailLowerCase') { - arrusers = await User.find({ 'idapp': idapp }); + arrusers = await User.find({'idapp': idapp}); let num = 0; for (const rec of arrusers) { let myemail = rec.email.toLowerCase(); if (myemail !== rec.email) { - await User.findOneAndUpdate({ _id: rec._id }, { $set: { 'email': myemail } }); + await User.findOneAndUpdate({_id: rec._id}, + {$set: {'email': myemail}}); num++; } } - return { num }; + return {num}; /*} else if (mydata.dbop === 'creaLista') { @@ -2629,10 +2710,33 @@ UserSchema.statics.DbOp = async function (idapp, mydata) { }; +UserSchema.statics.createNewSubRecord = async function(idapp, req) { + const User = this; + + // console.log("GENERA TOKEN : "); + let userId = req.body.data.userId; + let fieldkey = req.body.data.field; + let data = req.body.data.data; + + const filtro = {_id: userId}; + + let fieldsvalue = {...data}; + + fieldsvalue.date_created = new Date(); + fieldsvalue.date_updated = new Date(); + + var set = { + profile: {}, + }; + set.profile[fieldkey] = fieldsvalue; + + const rec = await User.findOneAndUpdate(filtro, {$set: set}, {new: false}); + + return rec; +}; const User = mongoose.model('User', UserSchema); - class Hero { constructor(name, level) { this.name = name; @@ -2645,7 +2749,6 @@ class Hero { } } - -module.exports = { User, Hero }; +module.exports = {User, Hero}; diff --git a/src/server/router/index_router.js b/src/server/router/index_router.js index 5d21775..e0e3bc6 100755 --- a/src/server/router/index_router.js +++ b/src/server/router/index_router.js @@ -41,6 +41,9 @@ const { Contribtype } = require('../models/contribtype'); const { PaymentType } = require('../models/paymenttype'); const { Discipline } = require('../models/discipline'); const { Skill } = require('../models/skill'); +const { MySkill } = require('../models/myskill'); +const { StatusSkill } = require('../models/statusSkill'); +const { City } = require('../models/city'); const { Sector } = require('../models/sector'); const { Level } = require('../models/level'); const { Newstosent } = require('../models/newstosent'); @@ -286,6 +289,12 @@ function getTableByTableName(tablename) { mytable = Graduatoria; else if (tablename === 'skills') mytable = Skill; + else if (tablename === 'myskills') + mytable = MySkill; + else if (tablename === 'statusSkills') + mytable = StatusSkill; + else if (tablename === 'citys') + mytable = City; else if (tablename === 'sectors') mytable = Sector; else if (tablename === 'levels') @@ -314,12 +323,65 @@ router.post('/settable', authenticate, (req, res) => { } } + if (shared_consts.TABLES_USER_ID.includes(params.table)) { + mydata.userId = req.user._id; + } + + + let mytablerec = new mytable(mydata); + // console.log('mytablerec', mytablerec); + + const mytablestrutt = getTableByTableName(params.table); + + return mytablerec.save() + .then(rec => { + // tools.mylog('rec', rec); + return res.send(rec); + + }).catch((e) => { + if (e.code === 11000) { + const id = mytablerec._id; + delete mytablerec._doc['_id']; + const myfields = mytablerec._doc; + if (!myfields.userId) { + myfields.userId = req.user._id.toString(); + } + return mytablestrutt.findByIdAndUpdate(id, { $set: myfields }).then(async (rec) => { + return res.send(rec); + }).catch((err) => { + tools.mylog('error: ', err.message); + return res.status(400).send(err); + }) + } else { + console.log(e.message); + } + }); + +}); + +router.post('/setsubrec', authenticate, (req, res) => { + const params = req.body; + const mytable = getTableByTableName(params.table); + const mydata = req.body.data; + + mydata.idapp = req.user.idapp; let mytablerec = new mytable(mydata); // console.log('mytablerec', mytablerec); const mytablestrutt = getTableByTableName(params.table); + const rec = mytablestrutt.createNewSubRecord(mydata.idapp, req) +.then(rec => { + // tools.mylog('rec', rec); + return res.send(rec); + + }).catch((e) => { + + }); + + return res.send(rec); + return mytablerec.save() .then(rec => { // tools.mylog('rec', rec); @@ -477,6 +539,10 @@ router.patch('/chval', authenticate, async (req, res) => { } } + if (shared_consts.TABLES_UPDATE_LASTMODIFIED.includes(mydata.table)) { + fieldsvalue.date_updated = new Date(); + } + await mytable.findByIdAndUpdate(id, { $set: fieldsvalue }).then(async (rec) => { // tools.mylogshow(' REC TO MODIFY: ', rec); if (!rec) { @@ -1213,6 +1279,7 @@ function load(req, res, version) { let departments = Department.findAllIdApp(idapp); let levels = Level.findAllIdApp(idapp); let skills = Skill.findAllIdApp(idapp); + let statusSkills = StatusSkill.findAllIdApp(idapp); let sectors = Sector.findAllIdApp(idapp); let cart = null; let orderscart = null; @@ -1234,7 +1301,7 @@ function load(req, res, version) { return Promise.all([bookedevent, eventlist, operators, wheres, contribtype, settings, permissions, disciplines, newstosent, mailinglist, mypage, gallery, paymenttype, calcstat, calzoom, producers, cart, storehouses, departments, orderscart, groups, resps, workers, internalpages, - levels, skills, sectors ]) + levels, skills, sectors, statusSkills ]) .then((arrdata) => { // console.table(arrdata); const myuser = req.user; @@ -1271,6 +1338,7 @@ function load(req, res, version) { levels: arrdata[24], skills: arrdata[25], sectors: arrdata[26], + statusSkills: arrdata[27], }); }) .catch((e) => { diff --git a/src/server/server.js b/src/server/server.js index eb02bf9..a05178f 100755 --- a/src/server/server.js +++ b/src/server/server.js @@ -63,6 +63,7 @@ mongoose.set('debug', process.env.DEBUG); const cfgserver = mongoose.model('cfgserver'); const { ObjectID } = require('mongodb'); + myLoad().then(ris => { const { User } = require('./models/user'); @@ -228,6 +229,8 @@ async function mystart() { testmsgwebpush(); + faitest(); + // tools.sendNotifToAdmin('Riparti', 'Riparti'); // sendemail.testemail('2', 'it'); @@ -406,4 +409,21 @@ async function inizia() { // console.log(link2); // } +async function faitest() { + console.log('Fai Test:') + + const testfind = false; + + if (testfind) { + const {City} = require('./models/city'); + + let miacity = 'roma'; + const ris = await City.findByCity(miacity); + + console.log('ris', ris); + } + +} + module.exports = { app }; + diff --git a/src/server/tools/shared_nodejs.js b/src/server/tools/shared_nodejs.js index 7219f53..b1907e8 100755 --- a/src/server/tools/shared_nodejs.js +++ b/src/server/tools/shared_nodejs.js @@ -39,7 +39,9 @@ module.exports = { 'In Contanti alla CNM' ], - TABLES_ID_NUMBER: ['permissions', 'levels'], + TABLES_ID_NUMBER: ['permissions', 'levels', 'statusSkills', 'sectors', 'skills', 'city', 'myskills'], + TABLES_USER_ID: ['myskills'], + TABLES_UPDATE_LASTMODIFIED: ['myskills'], CashType: { None: 0,