fix: refine schedule overlap validation in ScheduleSchema

- Updated the logic for checking overlapping schedules by querying existing schedule dates for the same user, improving accuracy in overlap detection.
- Simplified the overlap validation process by consolidating checks into a single function, enhancing code clarity and maintainability.
- This change aims to strengthen the integrity of scheduling operations and provide clearer error messages for users regarding scheduling conflicts.
This commit is contained in:
2025-01-17 23:23:10 +07:00
parent 9aec02568d
commit 03cf3f48a7

View File

@@ -492,51 +492,28 @@ d72a864e-2f41-45ab-9c9b-bf0512a31883,e9be51fd-2382-4e43-9988-74e76fde4b56,2024-1
throw new Error("Overlapping schedule"); throw new Error("Overlapping schedule");
} }
// check if scheduleDate have overlap with other scheduleDate of same user // check if scheduleDate have overlap with other scheduleDate of same user by query all scheduleDate of same user
const existingUserScheduleDates = const existingScheduleDatesOfSameUser =
await this.prisma.scheduleDate.findMany({ await this.prisma.scheduleDate.findMany({
where: { where: {
AND: [ schedule: {
{ managedService: {
participantIds: { mentorId: ctx.me.id,
has: ctx.me?.id ?? "",
},
}, },
{ },
status: {
notIn: [
ScheduleDateStatus.COMPLETED,
ScheduleDateStatus.EXPIRED,
],
},
},
{
end: {
gte: DateTimeUtils.now().toJSDate(),
},
},
],
}, },
}); });
// First, generate the preview schedule dates const hasOverlap = DateTimeUtils.isOverlaps(
const previewScheduleDates = previewSchedule.slots.map((slot) => ({ previewSchedule.slots.map((slot) => ({
start: DateTimeUtils.fromIsoString(slot.start), start: DateTimeUtils.fromIsoString(slot.start),
end: DateTimeUtils.fromIsoString(slot.end), end: DateTimeUtils.fromIsoString(slot.end),
})); })),
existingScheduleDatesOfSameUser.map((date) => ({
// Check if new schedule overlaps with any existing schedule dates for the user start: DateTimeUtils.fromDate(date.start),
const hasOverlap = existingUserScheduleDates.some((existingDate) => end: DateTimeUtils.fromDate(date.end),
previewScheduleDates.some((newScheduleDate) => }))
DateTimeUtils.isOverlap(
DateTimeUtils.fromDate(existingDate.start),
DateTimeUtils.fromDate(existingDate.end),
newScheduleDate.start,
newScheduleDate.end
)
)
); );
if (hasOverlap) { if (hasOverlap) {
throw new Error( throw new Error(
"Schedule date has overlap with existing schedule dates" "Schedule date has overlap with existing schedule dates"