From 93ccc735178bcb7521843df283cb01ad0167d1f5 Mon Sep 17 00:00:00 2001 From: Paolo Arena Date: Mon, 28 Oct 2019 16:01:28 +0100 Subject: [PATCH] - Permissions table (probably I'll change for others uses) - Added CDateTime component (to save in the db)... in the component CGridTableRec and for CEventsCalendar --- server/models/myevent.js | 10 ++++++ server/models/permission.js | 59 +++++++++++++++++++++++++++++++++ server/models/user.js | 14 ++++++++ server/models/where.js | 3 ++ server/router/booking_router.js | 4 --- server/router/index_router.js | 20 ++++++++++- server/tools/general.js | 43 ++++++++++++++++++++---- 7 files changed, 142 insertions(+), 11 deletions(-) create mode 100644 server/models/permission.js diff --git a/server/models/myevent.js b/server/models/myevent.js index 93eebd6..6d52f38 100644 --- a/server/models/myevent.js +++ b/server/models/myevent.js @@ -105,10 +105,20 @@ MyEventSchema.statics.findAllIdApp = function (idapp) { }; +MyEventSchema.statics.getFieldsForSearch = function () { + return ['short_tit', 'title', 'teacher', 'details'] +}; + MyEventSchema.statics.executeQueryTable = function (idapp, params) { + params.fieldsearch = this.getFieldsForSearch(); return tools.executeQueryTable(this, idapp, params); }; +if (tools.INITDB_FIRSTIME) { + // console.log(' createIndex MyEvent Index...'); + // MyEventSchema.index({ short_tit: 'text', title: 'text', teacher: 'text', details: 'text' }); +} + const MyEvent = mongoose.model('MyEvent', MyEventSchema); diff --git a/server/models/permission.js b/server/models/permission.js new file mode 100644 index 0000000..03d6c25 --- /dev/null +++ b/server/models/permission.js @@ -0,0 +1,59 @@ +const mongoose = require('mongoose'); +const Schema = mongoose.Schema; + +const tools = require('../tools/general'); + +mongoose.Promise = global.Promise; +mongoose.level = "F"; + + +// Resolving error Unknown modifier: $pushAll +mongoose.plugin(schema => { + schema.options.usePushEach = true +}); + +const PermissionSchema = new Schema({ + _id: { + type: Number, + }, + label: { + type: String, + default: '' + } +},{ _id : false }); + + +PermissionSchema.pre('save', async function (next) { + if (this.isNew) { + const myrec = await Permission.findOne().limit(1).sort({_id:-1}); + if (!!myrec) { + if (myrec._doc._id === 0) + this._id = 1; + else + this._id = myrec._doc._id * 2; + + } else { + this._id = 1; + } + } + + next(); +}); + +PermissionSchema.statics.executeQueryTable = function (idapp, params) { + return tools.executeQueryTable(this, 0, params); +}; + +PermissionSchema.statics.findAllIdApp = function () { + const Permission = this; + + const myfind = { }; + + return Permission.find(myfind, (err, arrrec) => { + return arrrec + }); +}; + +const Permission = mongoose.model('Permission', PermissionSchema); + +module.exports = { Permission }; diff --git a/server/models/user.js b/server/models/user.js index 638465f..927a348 100644 --- a/server/models/user.js +++ b/server/models/user.js @@ -358,13 +358,27 @@ UserSchema.statics.getUsersListByParams = function (params) { * @returns {Object} Object -> `{ rows, count }` */ +UserSchema.statics.getFieldsForSearch = function () { + return ['name', 'surname', 'email'] +}; + UserSchema.statics.executeQueryTable = function (idapp, params) { + params.fieldsearch = this.getFieldsForSearch(); return tools.executeQueryTable(this, idapp, params); }; +if (tools.INITDB_FIRSTIME) { + console.log(' createIndex User Index...'); + // UserSchema.index({ username: 'text', name: 'text', surname: 'text', email: 'text' }); + // UserSchema.index({ name: 'name' }); + // UserSchema.index({ name: 1 }); + // UserSchema.index({ surname: 1 }); +} const User = mongoose.model('User', UserSchema); + + class Hero { constructor(name, level) { this.name = name; diff --git a/server/models/where.js b/server/models/where.js index 308d281..3e0b93d 100644 --- a/server/models/where.js +++ b/server/models/where.js @@ -13,6 +13,9 @@ mongoose.plugin(schema => { }); const WhereSchema = new Schema({ + _id: { + type: mongoose.Schema.Types.ObjectId + }, idapp: { type: String, }, diff --git a/server/router/booking_router.js b/server/router/booking_router.js index 972d620..9b00ab7 100644 --- a/server/router/booking_router.js +++ b/server/router/booking_router.js @@ -7,10 +7,6 @@ const server_constants = require('../tools/server_constants'); const { authenticate } = require('../middleware/authenticate'); const { Booking } = require('../models/booking'); -const { MyEvent } = require('../models/myevent'); -const { Operator } = require('../models/operator'); -const { Where } = require('../models/where'); -const { Contribtype } = require('../models/contribtype'); const { ObjectID } = require('mongodb'); diff --git a/server/router/index_router.js b/server/router/index_router.js index 3b84fb0..fe0c0e7 100644 --- a/server/router/index_router.js +++ b/server/router/index_router.js @@ -17,6 +17,7 @@ const { Where } = require('../models/where'); const { MyEvent } = require('../models/myevent'); const { Contribtype } = require('../models/contribtype'); const { SendMsg } = require('../models/sendmsg'); +const { Permission } = require('../models/permission'); const tools = require('../tools/general'); @@ -141,6 +142,8 @@ function getTableByTableName(tablename) { mytable = MyEvent; else if (tablename === 'contribtype') mytable = Contribtype; + else if (tablename === 'permissions') + mytable = Permission; return mytable } @@ -152,7 +155,20 @@ router.post('/settable', authenticate, (req, res) => { mydata.idapp = req.user.idapp; + if (params.table === 'permissions') { + if (mydata["_id"] === undefined) { + mydata._id = 1; + } + } else { + if (mydata["_id"] === undefined) { + mydata._id = new ObjectID() + } + } + + mytablerec = new mytable(mydata); + console.log('mytablerec', mytablerec); + return mytablerec.save() .then(rec => { @@ -320,9 +336,10 @@ router.get('/loadsite/:userId/:idapp/:sall', authenticate_noerror, (req, res) => const operators = Operator.findAllIdApp(idapp); const wheres = Where.findAllIdApp(idapp); const contribtype = Contribtype.findAllIdApp(idapp); + const permissions = Permission.findAllIdApp(); - return Promise.all([bookedevent, eventlist, operators, wheres, contribtype]) + return Promise.all([bookedevent, eventlist, operators, wheres, contribtype, permissions]) .then((arrdata) => { // console.table(arrdata); res.send({ @@ -331,6 +348,7 @@ router.get('/loadsite/:userId/:idapp/:sall', authenticate_noerror, (req, res) => operators: arrdata[2], wheres: arrdata[3], contribtype: arrdata[4], + permissions: arrdata[5], }); }) .catch((e) => { diff --git a/server/tools/general.js b/server/tools/general.js index 158d910..59a5c4a 100644 --- a/server/tools/general.js +++ b/server/tools/general.js @@ -28,6 +28,8 @@ webpush.setVapidDetails(subject, publicVapidKey, privateVapidKey); module.exports = { + INITDB_FIRSTIME: true, + TYPE_PROJECT: 1, TYPE_TODO: 2, @@ -369,9 +371,28 @@ module.exports = { throw new Error('endRow must be number') } - const query = [ - { $match: Object.assign({ idapp }, params.filter) } - ]; + let query = []; + if (params.filter && params.fieldsearch) { + let myregexp = {}; + myregexp = new RegExp(params.filter.replace(' ', '|'), "ig"); + + const myfilters = []; + params.fieldsearch.forEach((field) => { + const data = {}; + data[field] = myregexp; + myfilters.push(data); + }); + + query = [ + { $match: { $or: myfilters } }, + ] + } + if (idapp > 0) { + query.push( { $match: { idapp } } ); + } + + // console.log('QUERYMATCH', query[0].$match.or); + // console.log('filter', params.filter); if (params.sortBy) { // maybe we want to sort by blog title or something @@ -399,7 +420,10 @@ module.exports = { } } ); - return query + + console.log('query', query); + + return query; }, @@ -408,10 +432,17 @@ module.exports = { return mythistable .aggregate(query) - .then(([{ count, rows }]) => { - return ({ count, rows }) + .then(([ris]) => { + if (ris) { + console.table(ris.rows); + console.log('ROW ', ris.count); + return ({ count: ris.count, rows: ris.rows }) + } else { + return ({ count: 0, rows: [] }) + } }) .catch(err => { + console.error(err); return { count: 0, rows: [] }