From b33ad5e809348bbcad9215454559be859594d9b3 Mon Sep 17 00:00:00 2001 From: Ly Tuan Kiet Date: Fri, 17 Jan 2025 20:59:05 +0700 Subject: [PATCH] fix: enhance schedule overlap validation in OrderSchema - Added logic to retrieve existing schedule dates for a given schedule ID to improve overlap detection. - Updated the conditions for checking schedule overlaps to include participant IDs, day of the week, and time slots, ensuring more accurate validation. - This change aims to enhance the integrity of scheduling operations and provide better feedback for users regarding scheduling conflicts. --- src/Order/order.schema.ts | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/Order/order.schema.ts b/src/Order/order.schema.ts index 60325d3..1b6d3db 100644 --- a/src/Order/order.schema.ts +++ b/src/Order/order.schema.ts @@ -401,28 +401,36 @@ export class OrderSchema extends PothosSchema { throw new Error("User has already registered for this service"); } // check if user have any scheduledate overlap time with input schedule + const scheduleDatesInSchedule = + await this.prisma.scheduleDate.findMany({ + where: { + scheduleId: args.data.schedule.connect?.id ?? "", + }, + }); const overlapSchedule = await this.prisma.scheduleDate.findFirst({ where: { AND: [ - { - start: { - equals: args.data.schedule.connect?.scheduleStart as Date, - }, - end: { - equals: args.data.schedule.connect?.scheduleEnd as Date, - }, - }, { participantIds: { has: ctx.me?.id ?? "", }, - }, - { - status: { - notIn: [ - ScheduleDateStatus.NOT_STARTED, - ScheduleDateStatus.IN_PROGRESS, - ], + orderId: { + not: null, + }, + dayOfWeek: { + in: Array.isArray(args.data.schedule.connect?.daysOfWeek) + ? args.data.schedule.connect.daysOfWeek + : [], + }, + slot: { + in: Array.isArray(args.data.schedule.connect?.slots) + ? args.data.schedule.connect.slots + : [], + }, + scheduleId: { + notIn: scheduleDatesInSchedule.map( + (scheduleDate) => scheduleDate.scheduleId + ), }, }, ],