From 1a1348c56326880a6e3bbffce544a74c568b53cc Mon Sep 17 00:00:00 2001 From: Paolo Arena Date: Sun, 20 Oct 2019 22:45:09 +0200 Subject: [PATCH] - Load Events - Edit Events - When a field is updated: undate also memory list record - Duplicate Event --- server/models/myevent.js | 61 ++++----------------------------- server/models/operator.js | 10 ++++++ server/router/api/actions.js | 11 ++++++ server/router/booking_router.js | 15 ++++++-- server/router/index_router.js | 46 +++++++++++++++++++++++-- 5 files changed, 83 insertions(+), 60 deletions(-) diff --git a/server/models/myevent.js b/server/models/myevent.js index 90c768d..93930a1 100644 --- a/server/models/myevent.js +++ b/server/models/myevent.js @@ -29,8 +29,8 @@ const MyEventSchema = new Schema({ details: { type: String, }, - time: { - type: String, + withtime: { + type: Boolean, }, dur: { type: Number, @@ -103,66 +103,17 @@ const MyEventSchema = new Schema({ }, }); -MyEventSchema.statics.findAllByUserIdAndIdApp = function (userId, idapp, sall) { +MyEventSchema.statics.findAllIdApp = function (idapp) { const Event = this; - let myfind = {}; - if (sall === '1') - myfind = { idapp, booked: true }; - else - myfind = { userId, idapp, booked: true }; + const myfind = { idapp }; - return Event.find(myfind, (err, arrbooked) => { - // console.log('ris MyEvent:', arrbooked); - return arrbooked + return Event.find(myfind, (err, arrrec) => { + return arrrec }); }; -function getUsersByEvent(idapp) { - const query = [ - { - $match: { idapp } - }, - { - $group: { _id: "$userId" } - }, - { - $project: { - _id: { - $toString: "$userId" - } - } - }, - { - $Lookup: { - from: "users", - localField: "userId", // field in my collection - foreignField: "ObjectId(_id)", // field in the 'from' collection - as: "fromItems" - } - }, - { - $replaceRoot: { newRoot: { $mergeObjects: [{ $arrayElemAt: ["$fromItems", 0] },] } } - }, - { $project: { username: 1, name: 1, surname: 1 } } - - ]; - return query -} - - -MyEventSchema.statics.findAllDistinctByEvent = function (idapp) { - const Event = this; - - const query = getUsersByEvent(idapp); - - return Event.aggregate(query) - .then(ris => { - return ris - }); -}; - MyEventSchema.statics.executeQueryTable = function (idapp, params) { return tools.executeQueryTable(this, idapp, params); }; diff --git a/server/models/operator.js b/server/models/operator.js index c736d46..c4ce7e5 100644 --- a/server/models/operator.js +++ b/server/models/operator.js @@ -58,6 +58,16 @@ OperatorSchema.statics.executeQueryTable = function (idapp, params) { return tools.executeQueryTable(this, idapp, params); }; +OperatorSchema.statics.findAllIdApp = function (idapp) { + const Operator = this; + + const myfind = { idapp }; + + return Operator.find(myfind, (err, arrrec) => { + return arrrec + }); +}; + const Operator = mongoose.model('Operator', OperatorSchema); module.exports = { Operator }; diff --git a/server/router/api/actions.js b/server/router/api/actions.js index e4e9cd2..2cd4dd5 100644 --- a/server/router/api/actions.js +++ b/server/router/api/actions.js @@ -12,5 +12,16 @@ module.exports = { return false } return true; + }, + doOtherThingsAfterDuplicated: function (tablename, rec) { + try { + if (tablename === 'users') { + // Delete also all the subscribers record of this User + + } + } catch (e) { + return false + } + return true; } }; diff --git a/server/router/booking_router.js b/server/router/booking_router.js index cb159bf..a04c1da 100644 --- a/server/router/booking_router.js +++ b/server/router/booking_router.js @@ -7,6 +7,8 @@ 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 { ObjectID } = require('mongodb'); @@ -25,7 +27,7 @@ function sendNotif(res, idapp, user, recbooking) { } router.post('/', authenticate, (req, res) => { - tools.mylog('INIZIO - booking'); + // tools.mylog('INIZIO - booking'); // tools.mylog('req.body', req.body); const myrec = _.pick(req.body, tools.allfieldBooking()); const id = myrec._id; @@ -113,7 +115,16 @@ router.get('/:userId/:idapp/:sall', authenticate, (req, res) => { // Extract all the todos of the userId only Booking.findAllByUserIdAndIdApp(userId, idapp, sall).then((bookedevent) => { - res.send({ bookedevent }); + + return MyEvent.findAllIdApp(idapp) + .then((eventlist) => { + return Operator.findAllIdApp(idapp) + .then((operators) => { + + res.send({ bookedevent, eventlist, operators }); + }) + }) + }).catch((e) => { console.log(e); res.status(400).send(e); diff --git a/server/router/index_router.js b/server/router/index_router.js index 7f255a6..f70d6a1 100644 --- a/server/router/index_router.js +++ b/server/router/index_router.js @@ -86,7 +86,7 @@ router.post(process.env.LINK_REQUEST_NEWPASSWORD, (req, res) => { router.get(process.env.LINK_CHECK_UPDATES, authenticate, (req, res) => { const userId = req.user._id; - console.log("POST " + process.env.LINK_CHECK_UPDATES + " userId=" + userId); + // console.log("POST " + process.env.LINK_CHECK_UPDATES + " userId=" + userId); if (!ObjectID.isValid(userId)) { return res.status(404).send(); @@ -250,7 +250,7 @@ router.delete('/delrec/:table/:id', authenticate, (req, res) => { return res.status(404).send({ code: server_constants.RIS_CODE_ERR_UNAUTHORIZED, msg: '' }); } - mytable.findByIdAndRemove(id).then((rec) => { + return mytable.findByIdAndRemove(id).then((rec) => { if (!rec) { return res.status(404).send(); } @@ -263,13 +263,53 @@ router.delete('/delrec/:table/:id', authenticate, (req, res) => { tools.mylog('DELETED Others things ...'); }); - res.send({ code: server_constants.RIS_CODE_OK, msg: '' }); + return res.send({ code: server_constants.RIS_CODE_OK, msg: '' }); + }).catch((e) => { res.status(400).send(); }); }); +router.post('/duprec/:table/:id', authenticate, (req, res) => { + const id = req.params.id; + const tablename = req.params.table; + // const idapp = req.body.idapp; + + console.log('id', id, 'table', tablename); + + const mytable = getTableByTableName(tablename); + + if (!User.isAdmin(req.user) && !User.isManager(req.user)) { + // If without permissions, exit + return res.status(404).send({ code: server_constants.RIS_CODE_ERR_UNAUTHORIZED, msg: '' }); + } + + return mytable.find({ id }).then((mydata) => { + mydata._id = ''; + const mynewrec = new mytable(mydata); + + return mynewrec.save().then((rec) => { + if (!rec) { + return res.status(404).send(); + } + + tools.mylog('DUPLICATED ', rec); + + // Do extra things after deleted + actions.doOtherThingsAfterDuplicated(tablename, rec).then((ris) => { + // ... + }); + + return res.send({ code: server_constants.RIS_CODE_OK, record: rec, msg: '' }); + }).catch((e) => { + res.status(400).send(); + }); + }) + +}); + + function doOtherThingsAfterDeleted() { }