|
|
@@ -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, validateDates} from "../utils.js";
|
|
|
|
|
|
/**
|
|
|
* Router for handling appointments.
|
|
|
@@ -30,18 +30,27 @@ 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 } = req.body;
|
|
|
|
|
|
- if (!title || title.length === 0 || !dueDate || !description || !startDate || !endDate || !place) {
|
|
|
+ if (!title || title.length === 0 || !dueDate || !description || !duration || !place || !times) {
|
|
|
res.status(400).json({ 'message': 'Empty parameter' });
|
|
|
return;
|
|
|
}
|
|
|
+ ///
|
|
|
+ if (!isDueDatePassed(dueDate)) {
|
|
|
+ res.status(400).json({ 'message': 'Due date is in the future' });
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- if (!validateDates(res, startDate, endDate, dueDate)) {
|
|
|
+ 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, startDate, endDate });
|
|
|
+ const newTask = new Appointment({ user, title, description, dueDate, place, duration, times });
|
|
|
|
|
|
newTask.save()
|
|
|
.then(doc => res.json({ "id": doc._id }))
|