diff --git a/src/Order/order.schema.ts b/src/Order/order.schema.ts index 60b0e56..bf6bdff 100644 --- a/src/Order/order.schema.ts +++ b/src/Order/order.schema.ts @@ -400,51 +400,6 @@ export class OrderSchema extends PothosSchema { if (userService) { 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: [ - { - participantIds: { - has: ctx.me?.id ?? "", - }, - 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 - ), - }, - start: { - equals: args.data.schedule.connect?.scheduleStart as Date, - }, - end: { - equals: args.data.schedule.connect?.scheduleEnd as Date, - }, - }, - ], - }, - }); - if (overlapSchedule) { - throw new Error("User have overlap schedule"); - } // check if input schedule has order id then throw error const schedule = await this.prisma.schedule.findUnique({ where: { id: args.data.schedule.connect?.id ?? "" }, @@ -461,6 +416,71 @@ export class OrderSchema extends PothosSchema { throw new Error("Schedule already has an order"); } } + // check if scheduleDate have overlap with other scheduleDate of same user + const existingScheduleDates = await this.prisma.scheduleDate.findMany( + { + where: { + AND: [ + { + scheduleId: { + not: args.data.schedule.connect?.id ?? "", + }, + }, + { + participantIds: { + has: ctx.me?.id ?? "", + }, + }, + { + status: { + notIn: [ + ScheduleDateStatus.COMPLETED, + ScheduleDateStatus.EXPIRED, + ], + }, + }, + { + end: { + gte: DateTimeUtils.now().toJSDate(), + }, + }, + ], + }, + } + ); + + // First, fetch the full schedule details to get accurate start and end times + const newSchedule = await this.prisma.schedule.findUnique({ + where: { id: args.data.schedule.connect?.id }, + include: { + dates: true // Include schedule dates to get accurate timing + } + }); + + if (!newSchedule) { + throw new Error("Schedule not found"); + } + + // Check if new schedule overlaps with any existing schedule dates + const hasOverlap = existingScheduleDates.some((existingDate) => { + // Use the actual schedule dates instead of potentially undefined properties + const newScheduleDates = newSchedule.dates; + + return newScheduleDates.some(newScheduleDate => + DateTimeUtils.isOverlap( + DateTimeUtils.fromDate(existingDate.start), + DateTimeUtils.fromDate(existingDate.end), + DateTimeUtils.fromDate(newScheduleDate.start), + DateTimeUtils.fromDate(newScheduleDate.end) + ) + ); + }); + + if (hasOverlap) { + throw new Error( + "Schedule date has overlap with existing schedule dates" + ); + } const order = await this.prisma.order.create({ ...query, data: {