From d6e571edbf287ea2aadfd3b802599e02967dbcac Mon Sep 17 00:00:00 2001 From: paoloar77 Date: Tue, 21 Dec 2021 01:27:45 +0100 Subject: [PATCH] - Codice internazionale numero + Country --- package.json | 1 + src/server/classes/queryclass.js | 88 ----------------------- src/server/models/extralist.js | 1 - src/server/models/graduatoria.js | 1 - src/server/models/listaingresso.js | 1 - src/server/models/myskill.js | 45 +++++++----- src/server/models/pickup.js | 58 +++++++++++++++ src/server/models/site.js | 18 +++++ src/server/models/user.js | 39 ++++------ src/server/router/index_router.js | 44 ++++++++---- src/server/tools/general.js | 110 +++++++++++++++++++++-------- src/server/tools/shared_nodejs.js | 5 ++ yarn.lock | 5 ++ 13 files changed, 237 insertions(+), 179 deletions(-) delete mode 100755 src/server/classes/queryclass.js create mode 100755 src/server/models/pickup.js diff --git a/package.json b/package.json index af866e2..e49931f 100755 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "bluebird": "^3.7.2", "body-parser": "^1.19.0", "cors": "^2.8.5", + "country-codes-list": "^1.6.8", "crypto-js": "^4.1.1", "dotenv": "^10.0.0", "ejs": "^3.1.6", diff --git a/src/server/classes/queryclass.js b/src/server/classes/queryclass.js deleted file mode 100755 index ff320cc..0000000 --- a/src/server/classes/queryclass.js +++ /dev/null @@ -1,88 +0,0 @@ - -class CParamsQuery { - constructor(params) { - this.table = params.table; - this.startRow = params.startRow; - this.endRow = params.endRow; - this.filter = params.filter; - this.filterand = params.filterand; - this.filtersearch = params.filtersearch; - this.filtercustom = params.filtercustom; - this.sortBy = params.sortBy; - this.descending = params.descending; - this.userId = params.userId; - this.codeId = params.codeId; - this.lk_tab = params.lk_tab; - this.af_objId_tab = params.af_objId_tab; - this.lk_LF = params.lk_LF; - this.lk_FF = params.lk_FF; - this.lk_as = params.lk_as; - this.lk_proj = params.lk_proj; - this.lk_col2 = params.lk_col2; - } - -} - -/** - * Query blog posts by user -> paginated results and a total count. - * @param userId {ObjectId} ID of user to retrieve blog posts for - * @param startRow {Number} First row to return in results - * @param endRow {Number} Last row to return in results - * @param [filter] {Object} Optional extra matching query object - * @param [sort] {Object} Optional sort query object - * @returns {Object} Object -> `{ rows, count }` - */ - -function queryBlogPostsByUser (userId, startRow, endRow, filter = {}, sort = false) { - const User = this; - - if (!(user instanceof mongoose.Types.ObjectId)) { - throw new Error('userId must be ObjectId') - } else if (typeof startRow !== 'number') { - throw new Error('startRow must be number') - } else if (typeof endRow !== 'number') { - throw new Error('endRow must be number') - } - - const query = [ - // more lookups go here if you need them - // we have a many-to-one from blogPost -> user - { $lookup: { - from: 'users', - localField: 'user', - foreignField: '_id', - as: 'user' - } }, - // each blog has a single user (author) so flatten it using $unwind - { $unwind: '$user' }, - // filter the results by our userId - { $match: Object.assign({ 'user._id': userId }, filter) } - ]; - - if (sort) { - // maybe we want to sort by blog title or something - query.push({ $sort: sort }) - } - - query.push( - { $group: { - _id: null, - // get a count of every result that matches until now - count: { $sum: 1 }, - // keep our results for the next operation - results: { $push: '$$ROOT' } - } }, - // and finally trim the results to within the range given by start/endRow - { $project: { - count: 1, - rows: { $slice: ['$results', startRow, endRow] } - } } - ); - - return User - .aggregate(query) - .then(([{ count, rows }]) => ({ count, rows })) -}; - - -module.exports = { CParamsQuery, queryBlogPostsByUser }; diff --git a/src/server/models/extralist.js b/src/server/models/extralist.js index 0db2831..4064a49 100755 --- a/src/server/models/extralist.js +++ b/src/server/models/extralist.js @@ -7,7 +7,6 @@ const _ = require('lodash'); const tools = require('../tools/general'); const shared_consts = require('../tools/shared_nodejs'); -const queryclass = require('../classes/queryclass'); mongoose.Promise = global.Promise; diff --git a/src/server/models/graduatoria.js b/src/server/models/graduatoria.js index 268117d..fa9e132 100755 --- a/src/server/models/graduatoria.js +++ b/src/server/models/graduatoria.js @@ -12,7 +12,6 @@ const { Nave } = require('./nave'); const { Settings } = require('./settings'); const shared_consts = require('../tools/shared_nodejs'); -const queryclass = require('../classes/queryclass'); mongoose.Promise = global.Promise; diff --git a/src/server/models/listaingresso.js b/src/server/models/listaingresso.js index f107d68..e2ae55b 100755 --- a/src/server/models/listaingresso.js +++ b/src/server/models/listaingresso.js @@ -14,7 +14,6 @@ const { Graduatoria } = require('./graduatoria'); const actions = require('../router/api/actions'); const shared_consts = require('../tools/shared_nodejs'); -const queryclass = require('../classes/queryclass'); mongoose.Promise = global.Promise; diff --git a/src/server/models/myskill.js b/src/server/models/myskill.js index 91b3f29..eda24a6 100755 --- a/src/server/models/myskill.js +++ b/src/server/models/myskill.js @@ -30,6 +30,10 @@ const MySkillSchema = new Schema({ { type: Number, }], + idContribType: [ + { + type: String, + }], idCity: [ { type: Number, @@ -109,25 +113,28 @@ MySkillSchema.statics.executeQueryTable = function(idapp, params) { params.fieldsearch = this.getFieldsForSearch(); const otherparams = { - lk_tab: 'users', - lk_LF: 'userId', - lk_FF: '_id', - lk_as: 'user', - af_objId_tab: 'myId', - lk_proj: { - idSkill: 1, - idStatusSkill: 1, - idCity: 1, - numLevel: 1, - photos: 1, - note: 1, - subTitle: 1, - date_created: 1, - date_updated: 1, - userId: 1, - username: 1, - name: 1, - surname: 1, + lookup1: { + lk_tab: 'users', + lk_LF: 'userId', + lk_FF: '_id', + lk_as: 'user', + af_objId_tab: 'myId', + lk_proj: { + idSkill: 1, + idStatusSkill: 1, + idContribType: 1, + idCity: 1, + numLevel: 1, + photos: 1, + note: 1, + subTitle: 1, + date_created: 1, + date_updated: 1, + userId: 1, + username: 1, + name: 1, + surname: 1, + }, }, }; diff --git a/src/server/models/pickup.js b/src/server/models/pickup.js new file mode 100755 index 0000000..d41a62b --- /dev/null +++ b/src/server/models/pickup.js @@ -0,0 +1,58 @@ +const shared_consts = require('../tools/shared_nodejs'); + +const tools = require('../tools/general'); + +const countryCodes = require('country-codes-list'); + +module.exports = { + + async executeQueryTable(idapp, params) { + + const table = params.table; + const strfind = params.search; + + if (strfind === '') { + return []; + } + + let myCountryArr = []; + + let out = {}; + + if (table === shared_consts.TAB_COUNTRY) { + // '[{countryCode}] {countryNameLocal}: +{countryCallingCode}' + out = { + id: '{countryCode}', + value: '{countryNameLocal}', + flag: '{flag}', + }; + + } else if (table === shared_consts.TAB_PHONES) { + out = { + id: '{countryCode}', + value: '{countryNameLocal} +{countryCallingCode}', + code: '+{countryCallingCode}', + flag: '{flag}', + }; + } + + try { + // const myCountryArr = countryCodes.filter('countryNameLocal', strfind); + // const myCountryArr = countryCodes.customList.filter() + + // myCountryArr = countryCodes.customList('countryCode', lavueout, { filter: ((countryData) => { return countryData['countryNameLocal'].toLocaleLowerCase().indexOf(strfind) >= 0 })} ); + myCountryArr = countryCodes.customArray(out, { + filter: ((countryData) => { + return countryData['countryNameLocal'].toLocaleLowerCase(). + startsWith(strfind) || countryData['countryNameEn'].toLocaleLowerCase(). + startsWith(strfind); + }), + }); + + } catch (e) { + console.log('err', e); + } + + return myCountryArr; + }, +}; diff --git a/src/server/models/site.js b/src/server/models/site.js index 6498e8d..ff96f1b 100755 --- a/src/server/models/site.js +++ b/src/server/models/site.js @@ -56,6 +56,24 @@ const SiteSchema = new Schema({ pathreg_add: { type: String, }, + who: { + type: String + }, + status: { + type: String + }, + note: { + type: String + }, + domain_provider: { + type: String, + }, + domain_expiring: { + type: Date + }, + next_payment: { + type: Date + }, }); var Site = module.exports = mongoose.model('Site', SiteSchema); diff --git a/src/server/models/user.js b/src/server/models/user.js index d64b26c..67381a9 100755 --- a/src/server/models/user.js +++ b/src/server/models/user.js @@ -17,7 +17,6 @@ const {NavePersistente} = require('../models/navepersistente'); const {ObjectID} = require('mongodb'); const shared_consts = require('../tools/shared_nodejs'); -const queryclass = require('../classes/queryclass'); mongoose.Promise = global.Promise; @@ -229,6 +228,18 @@ const UserSchema = new mongoose.Schema({ dateofbirth: { type: Date, }, + born_city: { + type: String, + trim: true, + }, + born_province: { + type: String, + trim: true, + }, + born_country: { + type: String, + trim: true, + }, my_dream: { type: String, }, @@ -1504,32 +1515,6 @@ UserSchema.statics.getUsersList = function(idapp) { ); }; -UserSchema.statics.getUsersListByParams = function(params) { - const User = this; - - myclParamQuery = new queryclass.CParamsQuery(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, - }); - -}; - /** * Query blog posts by user -> paginated results and a total count. * @returns {Object} Object -> `{ rows, count }` diff --git a/src/server/router/index_router.js b/src/server/router/index_router.js index 8d196fe..4210ddf 100755 --- a/src/server/router/index_router.js +++ b/src/server/router/index_router.js @@ -47,6 +47,7 @@ const {StatusSkill} = require('../models/statusSkill'); const {City} = require('../models/city'); const {Sector} = require('../models/sector'); const {Level} = require('../models/level'); +const Pickup = require('../models/pickup'); const {Newstosent} = require('../models/newstosent'); const {MyPage} = require('../models/mypage'); const {CalZoom} = require('../models/calzoom'); @@ -259,7 +260,7 @@ function getTableByTableName(tablename) { mytable = Where; else if (tablename === 'myevents') mytable = MyEvent; - else if (tablename === 'contribtype') + else if (tablename === 'contribtypes') mytable = Contribtype; else if (tablename === 'paymenttypes') mytable = PaymentType; @@ -307,6 +308,8 @@ function getTableByTableName(tablename) { mytable = Sector; else if (tablename === 'levels') mytable = Level; + else if (shared_consts.TablePickup.includes(tablename)) + mytable = Pickup; return mytable; } @@ -318,17 +321,6 @@ router.post('/settable', authenticate, (req, res) => { mydata.idapp = req.user.idapp; - if (shared_consts.TABLES_ID_NUMBER.includes(params.table)) { - //if (mydata['_id'] === undefined) { - // mydata._id = 1; - //} - } else if (params.table === 'hours') { - - } else { - if (mydata['_id'] === undefined) { - mydata._id = new ObjectID(); - } - } if (shared_consts.TABLES_USER_ID.includes(params.table)) { mydata.userId = req.user._id; @@ -346,6 +338,15 @@ router.post('/settable', authenticate, (req, res) => { mytablerec.isNew = false; } + if (shared_consts.TABLES_ID_NUMBER.includes(params.table)) { + } else if (params.table === 'hours') { + + } else { + if (mydata['_id'] === undefined) { + mydata._id = new ObjectID(); + } + } + return mytablerec.save().then(rec => { // tools.mylog('rec', rec); return res.send(rec); @@ -441,6 +442,25 @@ router.post('/gettable', authenticate, (req, res) => { }); +router.post('/pickup', authenticate, (req, res) => { + const params = req.body; + const mytable = getTableByTableName(params.table); + // console.log('mytable', mytable); + if (!mytable) { + console.log(`Table ${params.table} not found`); + return res.status(400).send({}); + } + + return mytable.executeQueryTable(req.user.idapp, params).then(ris => { + return res.send(ris); + + }).catch((e) => { + console.log(e.message); + res.status(400).send(e); + }); + +}); + router.post('/getpage', async (req, res) => { const params = req.body; const idapp = req.body.idapp; diff --git a/src/server/tools/general.js b/src/server/tools/general.js index b42d80f..e871255 100755 --- a/src/server/tools/general.js +++ b/src/server/tools/general.js @@ -1106,6 +1106,56 @@ module.exports = { return (myapp) ? myapp.telegram_key : ''; }, + getLookup: function(params, num, pass_proj) { + const query = [] + + if (!params) + return; + + let mylkLF = params.lk_LF; + if (params.af_objId_tab) { + const myobj = {} + myobj['myId' + num] = {'$toObjectId': '$' + params.lk_LF}; + query.push( + {'$addFields': myobj}, + ); + mylkLF = 'myId' + num; + } else { + + } + + if (params.lk_tab) { + let proj = params.lk_proj + if (!!pass_proj) { + proj = pass_proj + } + query.push( + { + $lookup: { + from: params.lk_tab, + localField: mylkLF, // field in my collection + foreignField: params.lk_FF, // field in the 'from' collection + as: params.lk_as, + }, + }, + { + $replaceRoot: { + newRoot: { + $mergeObjects: [ + { + $arrayElemAt: [ + '$' + params.lk_as, 0], + }, '$$ROOT'], + }, + }, + }, + {$project: proj}, + ); + } + + return query; + }, + getQueryTable: function(idapp, params) { if (typeof params.startRow !== 'number') { @@ -1114,7 +1164,14 @@ module.exports = { throw new Error('endRow must be number'); } + let newvers = true; + + if (params.lk_LF) + newvers = false; + let query = []; + + if (params.filter && params.fieldsearch) { let myregexp = {}; @@ -1285,38 +1342,30 @@ module.exports = { if (numrowend < 0) numrowend = 1; - let mylkLF = params.lk_LF; - if (params.af_objId_tab) { - query.push( - {'$addFields': {'myId': {'$toObjectId': '$' + params.lk_LF}}}, - ); - mylkLF = 'myId'; + + if (newvers) { + // NUOVA VERSIONE + let proj = params.lookup2 ? params.lookup2.lk_proj : null; + + const q1 = this.getLookup(params.lookup1, 1, proj); + if (q1) query = [...query, ...q1] + + const q2 = this.getLookup(params.lookup2, 2, proj); + if (q2) query = [...query, ...q2]; + + const q3 = this.getLookup(params.lookup3, 3, proj); + if (q3) query = [...query, ...q3]; + + if (params.filtersearch2.length > 0) { + query.push({$match: {$and: params.filtersearch2}}); + } + + } else { + // VECCHIA VERSIONE + const q1 = this.getLookup(params, 1); + if (q1) query = [...query, ...q1] } - if (params.lk_tab) { - query.push( - { - $lookup: { - from: params.lk_tab, - localField: mylkLF, // field in my collection - foreignField: params.lk_FF, // field in the 'from' collection - as: params.lk_as, - }, - }, - { - $replaceRoot: { - newRoot: { - $mergeObjects: [ - { - $arrayElemAt: [ - '$' + params.lk_as, 0], - }, '$$ROOT'], - }, - }, - }, - {$project: params.lk_proj}, - ); - } query.push( { @@ -1347,6 +1396,7 @@ module.exports = { let query = this.getQueryTable(idapp, params); try { + // console.log('query', query); const [ris] = await mythistable.aggregate(query); if (ris) { diff --git a/src/server/tools/shared_nodejs.js b/src/server/tools/shared_nodejs.js index f862584..6dcfdb0 100755 --- a/src/server/tools/shared_nodejs.js +++ b/src/server/tools/shared_nodejs.js @@ -34,6 +34,11 @@ module.exports = { REPORT_FILT_RESP: 1, REPORT_FILT_ATTIVITA: 2, + TAB_COUNTRY: 'countries', + TAB_PHONES: 'phones', + + TablePickup: ['countries', 'phones'], + PaymentTypes: [ 'Nessuno', 'Bonifico Bancario', diff --git a/yarn.lock b/yarn.lock index d5b420b..45f51af 100755 --- a/yarn.lock +++ b/yarn.lock @@ -2566,6 +2566,11 @@ cors@^2.8.5: object-assign "^4" vary "^1" +country-codes-list@^1.6.8: + version "1.6.8" + resolved "https://registry.yarnpkg.com/country-codes-list/-/country-codes-list-1.6.8.tgz#c6ff506d3c0511f645fdc0997d0b4fa6773d043e" + integrity sha512-wsqH44Yx3pTyPA77jYt2Nxso6kXziMwxI37fJFQLcd/3ome8Gqz6areaeG1w8zfeU9i+cZvJA3k2Mo5EW7rAtg== + cross-spawn@^7.0.3: version "7.0.3" resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"