From 974ffb2db15fdc8ad09a547396ee9f913724ae3a Mon Sep 17 00:00:00 2001 From: Filip Borum Poulsen Date: Mon, 17 Apr 2023 14:49:52 +0200 Subject: [PATCH] Added startTime and endTime filters --- server/src/migrations/populate.ts | 1 + server/src/routes/timeslot.ts | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/server/src/migrations/populate.ts b/server/src/migrations/populate.ts index 5c87148..af3f7bb 100644 --- a/server/src/migrations/populate.ts +++ b/server/src/migrations/populate.ts @@ -51,6 +51,7 @@ INSERT INTO weekly_timeslots (trainer_id, day_of_week, start_time, end_time) VAL ($1, 1, '11:00:00', '12:00:00'), ($1, 2, '10:00:00', '13:00:00'), ($1, 3, '10:00:00', '15:00:00'), +($1, 4, '10:00:00', '11:30:00'), ($2, 1, '10:00:00', '11:00:00'), ($2, 1, '11:00:00', '12:00:00'), ($2, 1, '12:00:00', '13:00:00') diff --git a/server/src/routes/timeslot.ts b/server/src/routes/timeslot.ts index 5458cfc..492e41d 100644 --- a/server/src/routes/timeslot.ts +++ b/server/src/routes/timeslot.ts @@ -99,6 +99,7 @@ router.get("/timeslot", async (req: Request, res: Response) => { WHERE ((trainer_id = ANY($1)) OR $2) AND (day_of_week = ANY($3)) + AND (trainers.center_id = $4 OR $5) GROUP BY weekly_timeslots.trainer_id, users.first_name, @@ -109,7 +110,9 @@ router.get("/timeslot", async (req: Request, res: Response) => { `, [ timeslotFilters.trainer !== undefined ? timeslotFilters.trainer : [1], timeslotFilters.trainer === undefined, - weekdays + weekdays, + timeslotFilters.center, + timeslotFilters.center === undefined ]); const databaseResult: DatabaseResult[] = queryResult.rows; @@ -129,6 +132,20 @@ router.get("/timeslot", async (req: Request, res: Response) => { timeslots: for (const timeslot of trainer.timeslots) { if (timeslot.day_of_week !== weekDay) continue timeslots; + if (timeslotFilters.startTime) { + if (parseInt(timeslotFilters.startTime.split(":")[0]) > parseInt(timeslot.start_time.split(":")[0])) + continue timeslots; + if (timeslotFilters.startTime.split(":")[0] === timeslot.start_time.split(":")[0] && + parseInt(timeslotFilters.startTime.split(":")[1]) > parseInt(timeslot.start_time.split(":")[1])) + continue timeslots; + } + if (timeslotFilters.endTime) { + if (parseInt(timeslotFilters.endTime.split(":")[0]) < parseInt(timeslot.end_time.split(":")[0])) + continue timeslots; + if (timeslotFilters.endTime.split(":")[0] === timeslot.end_time.split(":")[0] && + parseInt(timeslotFilters.endTime.split(":")[1]) < parseInt(timeslot.end_time.split(":")[1])) + continue timeslots; + } const startTime = day.clone() .hour(parseInt(timeslot.start_time.split(":")[0])) .minute(parseInt(timeslot.start_time.split(":")[1])) @@ -142,7 +159,7 @@ router.get("/timeslot", async (req: Request, res: Response) => { const reservedTimeEnd = dayjs(reservedTimeslot.endTime); if ((!reservedTimeStart.isBefore(startTime) && !reservedTimeStart.isAfter(endTime)) || (!reservedTimeEnd.isBefore(startTime) && !reservedTimeStart.isAfter(endTime))) { - continue timeslots; + continue timeslots; } } trainerWithAvailableTimeslots.timeslots.push({