diff --git a/server/middleware/authenticate.js b/server/middleware/authenticate.js index 27716ed..98105e8 100644 --- a/server/middleware/authenticate.js +++ b/server/middleware/authenticate.js @@ -7,6 +7,8 @@ const tools = require('../tools/general'); var authenticate = (req, res, next) => { var token = req.header('x-auth'); + console.log('authenticate... '); + const access = 'auth'; User.findByToken(token, access).then((user) => { diff --git a/server/models/booking.js b/server/models/booking.js index ca80f41..6c07428 100644 --- a/server/models/booking.js +++ b/server/models/booking.js @@ -43,16 +43,95 @@ const bookingSchema = new Schema({ }); -bookingSchema.statics.findAllByUserIdAndIdApp = function (userId, idapp) { +bookingSchema.statics.findAllByUserIdAndIdApp = function (userId, idapp, sall) { const Booking = this; - return Booking.find({userId, idapp, booked: true}, (err, arrbooked) => { - // console.log('ris Booking:', arrbooked); - return 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); + 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); module.exports = { Booking }; diff --git a/server/models/sendmsg.js b/server/models/sendmsg.js new file mode 100644 index 0000000..c15afb0 --- /dev/null +++ b/server/models/sendmsg.js @@ -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 }; diff --git a/server/models/user.js b/server/models/user.js index bfc99ba..25cc44b 100644 --- a/server/models/user.js +++ b/server/models/user.js @@ -32,7 +32,7 @@ var UserSchema = new mongoose.Schema({ }*/ }, idapp: { - type: Number, + type: String, required: true, }, username: { diff --git a/server/router/booking_router.js b/server/router/booking_router.js index 5cdc0eb..b34f88f 100644 --- a/server/router/booking_router.js +++ b/server/router/booking_router.js @@ -27,18 +27,19 @@ function sendNotif(res, idapp, user, recbooking) { router.post('/', authenticate, (req, res) => { tools.mylog('INIZIO - booking'); // tools.mylog('req.body', req.body); - const body = _.pick(req.body, tools.allfieldBooking()); - const id = body.id_bookedevent; - const fieldtochange = _.pick(req.body, tools.allfieldBookingChange()); + const myrec = _.pick(req.body, tools.allfieldBooking()); + const id = myrec._id; + const fieldtochange = _.pick(myrec, tools.allfieldBookingChange()); tools.mylog('crea Booking'); - const booking = new Booking(body); + const booking = new Booking(myrec); const check = tools.checkUserOk(booking.userId, req.user._id); if (check.exit) return check.ret; // console.log('fieldtochange', fieldtochange); + // Modify: return Booking.findOne({ id_bookedevent: id }) .then(trovato => { // console.log('trovato', trovato); @@ -49,7 +50,7 @@ router.post('/', authenticate, (req, res) => { }).then((recbooking) => { // tools.mylog('booking:', booking); // 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 }); }); } else { @@ -61,19 +62,42 @@ router.post('/', authenticate, (req, res) => { Booking.findById(idobj) .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 }); }); }); } }) - - }); -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 idapp = req.params.idapp; + const sall = req.params.sall; // var category = req.params.category; 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 - Booking.findAllByUserIdAndIdApp(userId, idapp).then((bookedevent) => { + Booking.findAllByUserIdAndIdApp(userId, idapp, sall).then((bookedevent) => { res.send({ bookedevent }); }).catch((e) => { console.log(e); diff --git a/server/router/sendmsg_router.js b/server/router/sendmsg_router.js new file mode 100644 index 0000000..46c2e13 --- /dev/null +++ b/server/router/sendmsg_router.js @@ -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; diff --git a/server/router/users_router.js b/server/router/users_router.js index f32c490..6a29347 100644 --- a/server/router/users_router.js +++ b/server/router/users_router.js @@ -32,8 +32,8 @@ function existSubScribe(userId, access, browser) { // POST /users router.post('/', (req, res) => { tools.mylog("POST /users"); - var body = _.pick(req.body, ['email', 'password', 'username', 'name', 'surname', 'idapp', 'keyappid', 'lang']); - var user = new User(body); + const body = _.pick(req.body, ['email', 'password', 'username', 'name', 'surname', 'idapp', 'keyappid', 'lang']); + const user = new User(body); // tools.mylog("LANG PASSATO = " + user.lang, "IDAPP", user.idapp); diff --git a/server/sendemail.js b/server/sendemail.js index 5c7bdf7..b61c290 100644 --- a/server/sendemail.js +++ b/server/sendemail.js @@ -107,6 +107,8 @@ module.exports = { }, sendEmail_Registration: function (lang, emailto, user, idapp, idreg) { + console.log('idapp', idapp, tools.getNomeAppByIdApp(idapp)); + mylocalsconf = { locale: lang, nomeapp: tools.getNomeAppByIdApp(idapp), @@ -193,6 +195,28 @@ module.exports = { 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); + }, + }; diff --git a/server/tools/general.js b/server/tools/general.js index 2f6d959..8182540 100644 --- a/server/tools/general.js +++ b/server/tools/general.js @@ -56,6 +56,10 @@ module.exports = { console.error(args) }, + allfieldSendmsg: function () { + return ['userId', 'date', 'usernameDest', 'msg', 'originPage'] + }, + allfieldTodo: function () { 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'] diff --git a/server/tools/server_constants.js b/server/tools/server_constants.js index 4d055bf..edfb928 100644 --- a/server/tools/server_constants.js +++ b/server/tools/server_constants.js @@ -18,6 +18,9 @@ module.exports = Object.freeze({ RIS_SUBSCRIBED_ERR: -1, RIS_SUBSCRIBED_STR: 'subscribed', + MenuAction: { + DELETE: 100, + }, RIS_SUBSCRIBED_MSG: { enUs: 'Subscription to the Newsletter Confirmed!',