fix: enhance schedule overlap validation in OrderSchema
- Removed outdated overlap validation logic and replaced it with a more comprehensive check for existing schedule dates associated with the same user. - Implemented a new validation mechanism to ensure that the new schedule does not overlap with any existing schedule dates, improving the accuracy of scheduling operations. - This change aims to enhance the integrity of scheduling processes and provide clearer feedback to users regarding scheduling conflicts.
This commit is contained in:
@@ -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: {
|
||||
|
||||
Reference in New Issue
Block a user