diff --git a/server/src/routes/timeslot.ts b/server/src/routes/timeslot.ts index b853481..a9df470 100644 --- a/server/src/routes/timeslot.ts +++ b/server/src/routes/timeslot.ts @@ -10,8 +10,8 @@ import Trainer from "../interfaces/trainer"; import { ReservedTimeslots, Timeslot, WeeklyTimeslot } from "../interfaces/timeslot"; import { idSchema, timeSchema } from "../schemas"; -dayjs.extend(isoWeek) -dayjs.extend(utc) +dayjs.extend(isoWeek); +dayjs.extend(utc); const router: Router = express.Router(); @@ -29,8 +29,8 @@ const timeslotFiltersSchema = Joi.object({ interface TimeslotFilters { trainer?: number[] center?: number - startDate: Date - endDate: Date + startDate: string + endDate: string startTime?: string endTime?: string } @@ -85,8 +85,8 @@ router.get("/timeslot", async (req: Request, res: Response) => { 'end_time', end_time )) FROM public.reserved_timeslots WHERE - (start_time between '2023-04-16' AND '2023-04-20' - OR end_time between '2023-04-16' AND '2023-04-20') + (start_time between $1 AND $2 + OR end_time between $1 AND $2) AND weekly_timeslots.trainer_id = reserved_timeslots.trainer_id ) as reserved_timeslots FROM @@ -95,9 +95,9 @@ router.get("/timeslot", async (req: Request, res: Response) => { JOIN users ON trainers.user_id = users.id JOIN centers ON trainers.center_id = centers.id WHERE - ((trainer_id = ANY($1)) OR $2) - AND (day_of_week = ANY($3)) - AND (trainers.center_id = $4 OR $5) + ((trainer_id = ANY($3)) OR $4) + AND (day_of_week = ANY($5)) + AND (trainers.center_id = $6 OR $7) GROUP BY weekly_timeslots.trainer_id, users.first_name, @@ -106,6 +106,8 @@ router.get("/timeslot", async (req: Request, res: Response) => { centers.name, trainer_id; `, [ + new Date(timeslotFilters.startDate).toISOString(), + new Date(timeslotFilters.endDate).toISOString(), timeslotFilters.trainer !== undefined ? timeslotFilters.trainer : [1], timeslotFilters.trainer === undefined, weekdays, @@ -125,7 +127,7 @@ router.get("/timeslot", async (req: Request, res: Response) => { for (let day: dayjs.Dayjs = filterStartDate; !day.isAfter(filterEndDate); day = day.add(1, "day")) { const weekDay = day.isoWeekday(); - const reservedTimeslots = trainer.reserved_timeslots; + const reservedTimeslots = trainer.reserved_timeslots !== null ? trainer.reserved_timeslots : []; timeslots: for (const timeslot of trainer.timeslots) { if (timeslot.day_of_week !== weekDay)