Filter Booking Slots When an Add-On Changes the Job Length
A booking form can display a new price after someone selects an add-on and still offer an appointment based on the old duration. If the extra adds labor, that stale time slot can overlap the next job. Hereβs a small exam
A booking form can display a new price after someone selects an add-on and still offer an appointment based on the old duration. If the extra adds labor, that stale time slot can overlap the next job. Hereβs a small example that tests for it.
Put the duration calculation ahead of slot selection
Weβll use minutes since midnight. A slot occupies a half-open interval, so an appointment ending exactly when the next one starts is allowed. In this example, the occupied intervals already include turnaround time.
def available_starts(starts, base, extra, buffer, occupied, closes_at):
held = base + extra + buffer
return [
start for start in starts
if start + held <= closes_at
and all(start + held <= left or right <= start
for left, right in occupied)
]
starts = [10 * 60 + 30, 11 * 60]
occupied = [(13 * 60, 14 * 60)]
assert available_starts(starts, 120, 0, 30, occupied, 17 * 60) == [630]
assert available_starts(starts, 120, 30, 30, occupied, 17 * 60) == []
The first version fits at 10:30 because it holds the calendar until exactly 1:00. Add 30 minutes for pet hair removal and it no longer fits. The rule considers labor and turnover together, even if the customer sees those as different amounts of time.
Recheck the slot when the customer goes back
If the customer picks a time, returns to the extras step, and changes the job length, run the availability check again. Preserve their service selections. If the time no longer fits, ask them to choose a new one and explain why in the interface.
For a production flow, run the final validation against authoritative availability on the server when confirming the booking. Concurrent requests need an atomic way to claim the resource. In a real implementation, start and end timestamps also need a well-defined time zone. The snippet is a small same-day example that isolates the overlap rule.
Tests worth adding
- The job fits exactly at closing.
- An extra pushes the job past an occupied interval or closing.
- An add-on that changes price alone leaves the same starts available.
- A condition adjustment and an add-on that describe the same work follow the shopβs intended rules.
Publishing notes
Use DEVβs AI disclosure setting for the assistance used. This is an original technical walkthrough. If adding first-party context as further reading, use the live Xenon article. Remove these publishing notes from the article body.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.