Skip to content

Commit

Permalink
Refactor appointment slot computation to prevent conflicts with excep…
Browse files Browse the repository at this point in the history
…tions and ensure accurate slot availability (Backend Logic) (#10068)
  • Loading branch information
abhimanyurajeesh authored Jan 20, 2025
1 parent 06575f2 commit 96fd5db
Showing 1 changed file with 28 additions and 39 deletions.
67 changes: 28 additions & 39 deletions src/pages/Scheduling/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,47 +70,36 @@ export function computeAppointmentSlots(
let time = startTime;
while (time < endTime) {
const slotEndTime = addMinutes(time, slotSizeInMinutes);
if (slotEndTime > endTime) {
break;
}
slots.push({
start_time: format(time, "HH:mm") as Time,
end_time: format(slotEndTime, "HH:mm") as Time,
isAvailable: true,
exceptions: [],
});
time = slotEndTime;
}

for (const exception of exceptions) {
const exceptionTime = parse(
exception.start_time,
"HH:mm:ss",
referenceDate,
);
const exceptionEndTime = parse(
exception.end_time,
"HH:mm:ss",
referenceDate,
);

slots.forEach((slot) => {
const slotStart = parse(slot.start_time, "HH:mm", referenceDate);
const slotEnd = parse(slot.end_time, "HH:mm", referenceDate);
if (
isWithinInterval(slotStart, {
start: exceptionTime,
end: exceptionEndTime,
}) ||
isWithinInterval(slotEnd, {
start: exceptionTime,
end: exceptionEndTime,
})
) {
slot.isAvailable = false;
slot.exceptions.push(exception);
let conflicting = false;
for (const exception of exceptions) {
const exceptionStartTime = parse(
exception.start_time,
"HH:mm:ss",
referenceDate,
);
const exceptionEndTime = parse(
exception.end_time,
"HH:mm:ss",
referenceDate,
);

if (exceptionStartTime < slotEndTime && exceptionEndTime > time) {
conflicting = true;
break;
}
});
}

if (!conflicting) {
slots.push({
start_time: format(time, "HH:mm") as Time,
end_time: format(slotEndTime, "HH:mm") as Time,
isAvailable: true,
exceptions: [],
});
}

time = slotEndTime;
}

return slots;
Expand Down

0 comments on commit 96fd5db

Please sign in to comment.