- Load Events

- Edit Events
- When a field is updated: undate also memory list record

- Duplicate Event
This commit is contained in:
Paolo Arena
2019-10-20 22:45:09 +02:00
parent 93eae51ab8
commit 1a1348c563
5 changed files with 83 additions and 60 deletions

View File

@@ -29,8 +29,8 @@ const MyEventSchema = new Schema({
details: { details: {
type: String, type: String,
}, },
time: { withtime: {
type: String, type: Boolean,
}, },
dur: { dur: {
type: Number, 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; const Event = this;
let myfind = {}; const myfind = { idapp };
if (sall === '1')
myfind = { idapp, booked: true };
else
myfind = { userId, idapp, booked: true };
return Event.find(myfind, (err, arrbooked) => { return Event.find(myfind, (err, arrrec) => {
// console.log('ris MyEvent:', arrbooked); return arrrec
return arrbooked
}); });
}; };
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) { MyEventSchema.statics.executeQueryTable = function (idapp, params) {
return tools.executeQueryTable(this, idapp, params); return tools.executeQueryTable(this, idapp, params);
}; };

View File

@@ -58,6 +58,16 @@ OperatorSchema.statics.executeQueryTable = function (idapp, params) {
return tools.executeQueryTable(this, 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); const Operator = mongoose.model('Operator', OperatorSchema);
module.exports = { Operator }; module.exports = { Operator };

View File

@@ -12,5 +12,16 @@ module.exports = {
return false return false
} }
return true; 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;
} }
}; };

View File

@@ -7,6 +7,8 @@ const server_constants = require('../tools/server_constants');
const { authenticate } = require('../middleware/authenticate'); const { authenticate } = require('../middleware/authenticate');
const { Booking } = require('../models/booking'); const { Booking } = require('../models/booking');
const { MyEvent } = require('../models/myevent');
const { Operator } = require('../models/operator');
const { ObjectID } = require('mongodb'); const { ObjectID } = require('mongodb');
@@ -25,7 +27,7 @@ function sendNotif(res, idapp, user, recbooking) {
} }
router.post('/', authenticate, (req, res) => { router.post('/', authenticate, (req, res) => {
tools.mylog('INIZIO - booking'); // tools.mylog('INIZIO - booking');
// tools.mylog('req.body', req.body); // tools.mylog('req.body', req.body);
const myrec = _.pick(req.body, tools.allfieldBooking()); const myrec = _.pick(req.body, tools.allfieldBooking());
const id = myrec._id; const id = myrec._id;
@@ -113,7 +115,16 @@ router.get('/:userId/:idapp/:sall', authenticate, (req, res) => {
// Extract all the todos of the userId only // Extract all the todos of the userId only
Booking.findAllByUserIdAndIdApp(userId, idapp, sall).then((bookedevent) => { 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) => { }).catch((e) => {
console.log(e); console.log(e);
res.status(400).send(e); res.status(400).send(e);

View File

@@ -86,7 +86,7 @@ router.post(process.env.LINK_REQUEST_NEWPASSWORD, (req, res) => {
router.get(process.env.LINK_CHECK_UPDATES, authenticate, (req, res) => { router.get(process.env.LINK_CHECK_UPDATES, authenticate, (req, res) => {
const userId = req.user._id; 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)) { if (!ObjectID.isValid(userId)) {
return res.status(404).send(); 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: '' }); 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) { if (!rec) {
return res.status(404).send(); return res.status(404).send();
} }
@@ -263,13 +263,53 @@ router.delete('/delrec/:table/:id', authenticate, (req, res) => {
tools.mylog('DELETED Others things ...'); 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) => { }).catch((e) => {
res.status(400).send(); 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() { function doOtherThingsAfterDeleted() {
} }