|
|
@@ -3,7 +3,7 @@ import passport from "passport";
|
|
|
import {mongoose} from "mongoose";
|
|
|
import Appointment from "../schemas/appointmentSchema.js";
|
|
|
import Booking from "../schemas/bookingSchema.js";
|
|
|
-import {validateDates} from "../utils.js";
|
|
|
+import {appointmentTimeSchema, isDueDatePassed, isValidAppointmentTimes, isValidCustomInput, validateDates} from "../utils.js";
|
|
|
|
|
|
/**
|
|
|
* Router for handling appointments.
|
|
|
@@ -30,18 +30,30 @@ router.use((req, res, next) => {
|
|
|
*/
|
|
|
router.post('/create', function(req, res, next) {
|
|
|
const user = req.userId;
|
|
|
- const { title, description, dueDate, place, startDate, endDate } = req.body;
|
|
|
+ const { title, description, dueDate, place, duration, times, inputs, timeSpans, participants } = req.body;
|
|
|
|
|
|
- if (!title || title.length === 0 || !dueDate || !description || !startDate || !endDate || !place) {
|
|
|
+ if (!title || title.length === 0 || !dueDate || !description || !duration || !place || !times || !inputs || !timeSpans || !participants) {
|
|
|
res.status(400).json({ 'message': 'Empty parameter' });
|
|
|
return;
|
|
|
}
|
|
|
+ ///
|
|
|
+ if (isDueDatePassed(dueDate)) {
|
|
|
+ res.status(400).json({ 'message': 'Due date is invalid or in the past' });
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- if (!validateDates(res, startDate, endDate, dueDate)) {
|
|
|
+ // validate times
|
|
|
+ if (!isValidAppointmentTimes(times)) {
|
|
|
+ res.status(400).json({ 'message': 'Appointment times are invalid' });
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- const newTask = new Appointment({ user, title, description, dueDate, place, startDate, endDate });
|
|
|
+ if (!isValidCustomInput(inputs)) {
|
|
|
+ res.status(400).json({ 'message': 'Custom inputs are invalid' });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const newTask = new Appointment({ user, title, description, dueDate, place, duration, times, inputs, timeSpans, participants });
|
|
|
|
|
|
newTask.save()
|
|
|
.then(doc => res.json({ "id": doc._id }))
|
|
|
@@ -54,19 +66,20 @@ router.post('/create', function(req, res, next) {
|
|
|
router.post('/modify', function (req, res, next) {
|
|
|
const user = req.userId;
|
|
|
|
|
|
- const { appointmentId, title, description, dueDate, place, startDate, endDate } = req.body;
|
|
|
- if (!title || !dueDate || !description || !startDate || !endDate || !place || !appointmentId) {
|
|
|
+ const { appointmentId, title, description, dueDate, place, times, inputs, timeSpans, participants } = req.body;
|
|
|
+ if (!title || !dueDate || !description || !times || !inputs || !timeSpans || !participants) {
|
|
|
res.status(400).json({ 'error': 'Empty parameter' });
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- if (!validateDates(res, startDate, endDate, dueDate)) {
|
|
|
+ if (isDueDatePassed(dueDate)) {
|
|
|
+ res.status(400).json({ 'error': 'Invalid due date' });
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
Appointment.updateOne(
|
|
|
{ _id: appointmentId, user: user },
|
|
|
- { $set: { title, description, dueDate, place, startDate, endDate } }
|
|
|
+ { $set: { title, description, dueDate, place, times, inputs, timeSpans, participants } }
|
|
|
)
|
|
|
.then(result => {
|
|
|
if (result.modifiedCount === 1) {
|
|
|
@@ -80,17 +93,53 @@ router.post('/modify', function (req, res, next) {
|
|
|
.catch(err => res.status(404).json({ 'message': 'Not found' }));
|
|
|
});
|
|
|
|
|
|
+/**
|
|
|
+ * Lock an existing appointment by modifying the due date.
|
|
|
+ */
|
|
|
+router.post('/lock', function (req, res, next) {
|
|
|
+ const user = req.userId;
|
|
|
+
|
|
|
+ const { appointmentId } = req.body;
|
|
|
+ if (!appointmentId) {
|
|
|
+ res.status(400).json({ 'error': 'Empty parameter' });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const dueDate = new Date();
|
|
|
+
|
|
|
+ Appointment.updateOne(
|
|
|
+ { _id: appointmentId, user: user },
|
|
|
+ { $set: { dueDate } }
|
|
|
+ )
|
|
|
+ .then(result => {
|
|
|
+ if (result.modifiedCount === 1) {
|
|
|
+ console.log("Successfully updated the document.");
|
|
|
+ res.json({ "id": appointmentId });
|
|
|
+ } else {
|
|
|
+ console.log("Document not updated");
|
|
|
+ res.status(500).json({ 'message': 'Document has not changed' });
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .catch(err => res.status(404).json({ 'message': 'Not found' }));
|
|
|
+});
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* Delete an appointment.
|
|
|
*/
|
|
|
router.delete('/delete', function (req, res, next) {
|
|
|
const user = req.userId;
|
|
|
const {appointmentId} = req.body;
|
|
|
+ console.log(req.body);
|
|
|
if (!appointmentId) {
|
|
|
res.status(400).json({ 'message': 'Invalid parameter' });
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
+ Booking.deleteMany({ appointment: appointmentId })
|
|
|
+ .then(result => {})
|
|
|
+ .catch(err => res.status(500).json({ 'message': err }));
|
|
|
+
|
|
|
Appointment.deleteOne({ _id: appointmentId, user: user })
|
|
|
.then(result => {
|
|
|
if (result.deletedCount === 1) {
|
|
|
@@ -116,10 +165,39 @@ router.get('/appointments', function (req, res, next) {
|
|
|
});
|
|
|
});
|
|
|
|
|
|
+/**
|
|
|
+ * Count the participants of an appointment
|
|
|
+ */
|
|
|
+router.get('/bookings/count', async function (req, res, next) {
|
|
|
+ const user = req.userId;
|
|
|
+
|
|
|
+ let data = [];
|
|
|
+
|
|
|
+ Appointment.find({ user: user })
|
|
|
+ .then(async appointments => {
|
|
|
+ console.log(appointments);
|
|
|
+ for (const element of appointments) {
|
|
|
+ const count = await Booking.countDocuments({appointment: element._id});
|
|
|
+ console.log(count);
|
|
|
+ data.push({appointmentId: element._id, count: count});
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .catch(err => {
|
|
|
+ res.status(404).json({'message': "Appointments not found"});
|
|
|
+ });
|
|
|
+ res.json(data);
|
|
|
+});
|
|
|
+
|
|
|
+async function findAppointment(userId, appointmentId) {
|
|
|
+ const resp = await Appointment.find({ user: userId, _id: appointmentId });
|
|
|
+ return resp.length > 0;
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
* Get all bookings for a specific appointment.
|
|
|
*/
|
|
|
-router.get('/bookings', function (req, res, next) {
|
|
|
+router.get('/bookings', async function (req, res, next) {
|
|
|
+ const user = req.userId;
|
|
|
const { appointmentId } = req.query;
|
|
|
|
|
|
if (!appointmentId) {
|
|
|
@@ -127,6 +205,10 @@ router.get('/bookings', function (req, res, next) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
+ if (!await findAppointment(user, appointmentId)) {
|
|
|
+ return res.status(400).json({'message': 'Not your appointment'});
|
|
|
+ }
|
|
|
+
|
|
|
Booking.find({ appointment: appointmentId })
|
|
|
.then(bookings => {
|
|
|
if (bookings === null) {
|