|
|
@@ -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 {appointmentTimeSchema, isDueDatePassed, isValidAppointmentTimes, validateDates} from "../utils.js";
|
|
|
+import {appointmentTimeSchema, isDueDatePassed, isValidAppointmentTimes, isValidCustomInput, validateDates} from "../utils.js";
|
|
|
|
|
|
/**
|
|
|
* Router for handling appointments.
|
|
|
@@ -30,27 +30,30 @@ router.use((req, res, next) => {
|
|
|
*/
|
|
|
router.post('/create', function(req, res, next) {
|
|
|
const user = req.userId;
|
|
|
- const { title, description, dueDate, place, duration, times } = req.body;
|
|
|
+ const { title, description, dueDate, place, duration, times, inputs } = req.body;
|
|
|
|
|
|
- if (!title || title.length === 0 || !dueDate || !description || !duration || !place || !times) {
|
|
|
+ if (!title || title.length === 0 || !dueDate || !description || !duration || !place || !times || !inputs) {
|
|
|
res.status(400).json({ 'message': 'Empty parameter' });
|
|
|
return;
|
|
|
}
|
|
|
///
|
|
|
- if (!isDueDatePassed(dueDate)) {
|
|
|
- res.status(400).json({ 'message': 'Due date is in the future' });
|
|
|
+ if (isDueDatePassed(dueDate)) {
|
|
|
+ res.status(400).json({ 'message': 'Due date is invalid or in the past' });
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- console.log(times);
|
|
|
-
|
|
|
// validate times
|
|
|
if (!isValidAppointmentTimes(times)) {
|
|
|
res.status(400).json({ 'message': 'Appointment times are invalid' });
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- const newTask = new Appointment({ user, title, description, dueDate, place, duration, times });
|
|
|
+ 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 });
|
|
|
|
|
|
newTask.save()
|
|
|
.then(doc => res.json({ "id": doc._id }))
|
|
|
@@ -63,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 } = req.body;
|
|
|
+ if (!title || !dueDate || !description || !startDate || !endDate || !times || !inputs) {
|
|
|
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 } }
|
|
|
)
|
|
|
.then(result => {
|
|
|
if (result.modifiedCount === 1) {
|