- ++Booking List

- ++Delete a Booking also for the Admin.
This commit is contained in:
Paolo Arena
2019-10-12 23:34:32 +02:00
parent 624f929c56
commit cebe1208de
10 changed files with 290 additions and 17 deletions

View File

@@ -7,6 +7,8 @@ const tools = require('../tools/general');
var authenticate = (req, res, next) => { var authenticate = (req, res, next) => {
var token = req.header('x-auth'); var token = req.header('x-auth');
console.log('authenticate... ');
const access = 'auth'; const access = 'auth';
User.findByToken(token, access).then((user) => { User.findByToken(token, access).then((user) => {

View File

@@ -43,16 +43,95 @@ const bookingSchema = new Schema({
}); });
bookingSchema.statics.findAllByUserIdAndIdApp = function (userId, idapp) { bookingSchema.statics.findAllByUserIdAndIdApp = function (userId, idapp, sall) {
const Booking = this; const Booking = this;
return Booking.find({userId, idapp, booked: true}, (err, arrbooked) => { let myfind = {};
if (sall === '1')
myfind = { idapp, booked: true };
else
myfind = { userId, idapp, booked: true };
return Booking.find(myfind, (err, arrbooked) => {
// console.log('ris Booking:', arrbooked); // console.log('ris Booking:', arrbooked);
return arrbooked return arrbooked
}); });
}; };
function getUsersByBooking(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
}
bookingSchema.statics.findAllDistinctByBooking = function (idapp) {
const Booking = this;
const query = getUsersByBooking(idapp);
return Booking.aggregate(query)
.then(ris => {
return ris
});
};
function getQueryBookingUser(filterMatchBefore = {}, userId) {
let filterMatchAfter = {
$or: getQueryFilterTodo(userId)
};
let myobjField = getobjFieldTodo(true);
const query = [
{ $match: filterMatchBefore },
{
$lookup: {
from: "users",
localField: "userId", // field in my collection
foreignField: "_id", // field in the 'from' collection
as: "fromItems"
}
},
{
$replaceRoot: { newRoot: { $mergeObjects: [{ $arrayElemAt: ["$fromItems", 0] }, "$$ROOT"] } }
},
{ $match: filterMatchAfter },
{ $project: myobjField }];
return query;
}
var Booking = mongoose.model('Booking', bookingSchema); var Booking = mongoose.model('Booking', bookingSchema);
module.exports = { Booking }; module.exports = { Booking };

59
server/models/sendmsg.js Normal file
View File

@@ -0,0 +1,59 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
mongoose.Promise = global.Promise;
mongoose.level = "F";
const { ObjectID } = require('mongodb');
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
schema.options.usePushEach = true
});
const sendmsgSchema = new Schema({
idapp: {
type: Number,
},
userId: {
type: String,
},
idappDest: {
type: Number,
},
usernameDest: {
type: String,
},
msg: {
type: String,
},
date: {
type: Date,
},
read: {
type: Boolean,
default: false
},
deleted: {
type: Boolean,
default: false
},
originpage: {
type: String,
},
});
sendmsgSchema.statics.findAllByUserIdAndIdApp = function (userId, idapp) {
const SendMsg = this;
return SendMsg.find({userId, idapp}, (err, arrmsg) => {
console.log('ris arrmsg:', arrmsg);
return arrmsg
});
};
var SendMsg = mongoose.model('SendMsg', sendMsgSchema);
module.exports = { SendMsg };

View File

@@ -32,7 +32,7 @@ var UserSchema = new mongoose.Schema({
}*/ }*/
}, },
idapp: { idapp: {
type: Number, type: String,
required: true, required: true,
}, },
username: { username: {

View File

@@ -27,18 +27,19 @@ 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 body = _.pick(req.body, tools.allfieldBooking()); const myrec = _.pick(req.body, tools.allfieldBooking());
const id = body.id_bookedevent; const id = myrec._id;
const fieldtochange = _.pick(req.body, tools.allfieldBookingChange()); const fieldtochange = _.pick(myrec, tools.allfieldBookingChange());
tools.mylog('crea Booking'); tools.mylog('crea Booking');
const booking = new Booking(body); const booking = new Booking(myrec);
const check = tools.checkUserOk(booking.userId, req.user._id); const check = tools.checkUserOk(booking.userId, req.user._id);
if (check.exit) return check.ret; if (check.exit) return check.ret;
// console.log('fieldtochange', fieldtochange); // console.log('fieldtochange', fieldtochange);
// Modify:
return Booking.findOne({ id_bookedevent: id }) return Booking.findOne({ id_bookedevent: id })
.then(trovato => { .then(trovato => {
// console.log('trovato', trovato); // console.log('trovato', trovato);
@@ -49,7 +50,7 @@ router.post('/', authenticate, (req, res) => {
}).then((recbooking) => { }).then((recbooking) => {
// tools.mylog('booking:', booking); // tools.mylog('booking:', booking);
// tools.mylog('already exist'); // tools.mylog('already exist');
sendNotif(res, body.idapp, req.user, recbooking); sendNotif(res, myrec.idapp, req.user, recbooking);
res.send({ code: server_constants.RIS_CODE_OK, msg: '', id: recbooking._id }); res.send({ code: server_constants.RIS_CODE_OK, msg: '', id: recbooking._id });
}); });
} else { } else {
@@ -61,19 +62,42 @@ router.post('/', authenticate, (req, res) => {
Booking.findById(idobj) Booking.findById(idobj)
.then((recbooking) => { .then((recbooking) => {
sendNotif(res, body.idapp, req.user, recbooking); sendNotif(res, myrec.idapp, req.user, recbooking);
res.send({ code: server_constants.RIS_CODE_OK, msg: '', id: recbooking._id }); res.send({ code: server_constants.RIS_CODE_OK, msg: '', id: recbooking._id });
}); });
}); });
} }
}) })
}); });
router.get('/:userId/:idapp', authenticate, (req, res) => { router.delete('/:id/:notify/:idapp', authenticate, (req, res) => {
console.log('DELETE Booking');
const id = req.params.id;
const notify = req.params.notify;
const idapp = req.params.idapp;
Booking.findByIdAndRemove(id).then((recbooking) => {
if (!recbooking) {
return res.status(404).send();
}
if (notify === '1')
sendNotif(res, idapp, req.user, recbooking);
tools.mylog('DELETED ', recbooking.descr, recbooking._id);
res.send({ code: server_constants.RIS_CODE_OK, msg: '', id: recbooking._id });
}).catch((e) => {
res.status(400).send();
});
});
router.get('/:userId/:idapp/:sall', authenticate, (req, res) => {
const userId = req.params.userId; const userId = req.params.userId;
const idapp = req.params.idapp; const idapp = req.params.idapp;
const sall = req.params.sall;
// var category = req.params.category; // var category = req.params.category;
tools.mylog('GET BOOKINGS : ', req.params); tools.mylog('GET BOOKINGS : ', req.params);
@@ -88,7 +112,7 @@ router.get('/:userId/:idapp', authenticate, (req, res) => {
} }
// Extract all the todos of the userId only // Extract all the todos of the userId only
Booking.findAllByUserIdAndIdApp(userId, idapp).then((bookedevent) => { Booking.findAllByUserIdAndIdApp(userId, idapp, sall).then((bookedevent) => {
res.send({ bookedevent }); res.send({ bookedevent });
}).catch((e) => { }).catch((e) => {
console.log(e); console.log(e);

View File

@@ -0,0 +1,78 @@
const express = require('express');
const router = express.Router();
const tools = require('../tools/general');
const server_constants = require('../tools/server_constants');
const { authenticate } = require('../middleware/authenticate');
const { Sendmsg } = require('../models/Sendmsg');
const { ObjectID } = require('mongodb');
const sendemail = require('../sendemail');
const _ = require('lodash');
function sendNotif(res, idapp, user, recmsg) {
//++Todo: tools.sendNotificationToUser
// Send Msg
sendemail.sendEmail_Msg(res, user.lang, user.email, user, idapp, recmsg);
}
router.post('/', authenticate, (req, res) => {
tools.mylog('INIZIO - Sendmsg');
// tools.mylog('req.body', req.body);
const body = _.pick(req.body, tools.allfieldSendmsg());
tools.mylog('crea Sendmsg');
const Sendmsg = new Sendmsg(body);
const check = tools.checkUserOk(Sendmsg.userId, req.user._id);
if (check.exit) return check.ret;
// console.log('fieldtochange', fieldtochange);
Sendmsg._id = new ObjectID();
return Sendmsg.save().then((writeresult) => {
let idobj = writeresult._id;
Sendmsg.findById(idobj)
.then((recmsg) => {
sendNotif(res, body.idapp, req.user, recmsg);
res.send({ code: server_constants.RIS_CODE_OK, msg: '', id: recmsg._id });
});
});
});
router.get('/:userId/:idapp', authenticate, (req, res) => {
const userId = req.params.userId;
const idapp = req.params.idapp;
// var category = req.params.category;
tools.mylog('GET SendmsgS : ', req.params);
if (!ObjectID.isValid(userId)) {
return res.status(404).send();
}
if (userId !== String(req.user._id)) {
// I'm trying to write something not mine!
return res.status(404).send({ code: server_constants.RIS_CODE_TODO_CREATING_NOTMYUSER });
}
// Extract all the todos of the userId only
Sendmsg.findAllByUserIdAndIdApp(userId, idapp).then((msgall) => {
res.send({ msgall });
}).catch((e) => {
console.log(e);
res.status(400).send(e);
});
});
module.exports = router;

View File

@@ -32,8 +32,8 @@ function existSubScribe(userId, access, browser) {
// POST /users // POST /users
router.post('/', (req, res) => { router.post('/', (req, res) => {
tools.mylog("POST /users"); tools.mylog("POST /users");
var body = _.pick(req.body, ['email', 'password', 'username', 'name', 'surname', 'idapp', 'keyappid', 'lang']); const body = _.pick(req.body, ['email', 'password', 'username', 'name', 'surname', 'idapp', 'keyappid', 'lang']);
var user = new User(body); const user = new User(body);
// tools.mylog("LANG PASSATO = " + user.lang, "IDAPP", user.idapp); // tools.mylog("LANG PASSATO = " + user.lang, "IDAPP", user.idapp);

View File

@@ -107,6 +107,8 @@ module.exports = {
}, },
sendEmail_Registration: function (lang, emailto, user, idapp, idreg) { sendEmail_Registration: function (lang, emailto, user, idapp, idreg) {
console.log('idapp', idapp, tools.getNomeAppByIdApp(idapp));
mylocalsconf = { mylocalsconf = {
locale: lang, locale: lang,
nomeapp: tools.getNomeAppByIdApp(idapp), nomeapp: tools.getNomeAppByIdApp(idapp),
@@ -193,6 +195,28 @@ module.exports = {
this.sendEmail_base('admin/cancelbooking/' + lang, user, tools.getAdminEmailByIdApp(idapp), mylocalsconf); this.sendEmail_base('admin/cancelbooking/' + lang, user, tools.getAdminEmailByIdApp(idapp), mylocalsconf);
}, },
sendEmail_Msg: function (res, lang, emailto, user, idapp, recbooking) {
tools.mylog('sendEmail_Msg');
tools.mylog('tools.getNomeAppByIdApp(idapp)', tools.getNomeAppByIdApp(idapp), idapp);
mylocalsconf = {
locale: lang,
nomeapp: tools.getNomeAppByIdApp(idapp),
name: user.name,
surname: user.surname,
emailto: emailto,
msgbooking: recbooking.msgbooking,
eventtextplain: tools.removeSpecialCharForEmail(recbooking.infoevent),
event: recbooking.infoevent,
};
this.sendEmail_base('msg/sendmsg/' + lang, user, emailto, mylocalsconf);
// Send Email also to the Admin
// this.sendEmail_base('admin/sendmsg/' + lang, user, tools.getAdminEmailByIdApp(idapp), mylocalsconf);
},
}; };

View File

@@ -56,6 +56,10 @@ module.exports = {
console.error(args) console.error(args)
}, },
allfieldSendmsg: function () {
return ['userId', 'date', 'usernameDest', 'msg', 'originPage']
},
allfieldTodo: function () { allfieldTodo: function () {
return ['userId', 'pos', 'category', 'descr', 'priority', 'statustodo', 'created_at', 'modify_at', return ['userId', 'pos', 'category', 'descr', 'priority', 'statustodo', 'created_at', 'modify_at',
'completed_at', 'expiring_at', 'enableExpiring', 'id_prev', 'progress', 'modified', 'phase', 'assigned_to_userId', 'hoursplanned', 'hoursworked', 'start_date', 'themecolor', 'themebgcolor'] 'completed_at', 'expiring_at', 'enableExpiring', 'id_prev', 'progress', 'modified', 'phase', 'assigned_to_userId', 'hoursplanned', 'hoursworked', 'start_date', 'themecolor', 'themebgcolor']

View File

@@ -18,6 +18,9 @@ module.exports = Object.freeze({
RIS_SUBSCRIBED_ERR: -1, RIS_SUBSCRIBED_ERR: -1,
RIS_SUBSCRIBED_STR: 'subscribed', RIS_SUBSCRIBED_STR: 'subscribed',
MenuAction: {
DELETE: 100,
},
RIS_SUBSCRIBED_MSG: { RIS_SUBSCRIBED_MSG: {
enUs: 'Subscription to the Newsletter Confirmed!', enUs: 'Subscription to the Newsletter Confirmed!',