{"success":true,"name":"check-booking-overlaps-with-the-interval-formula-in-the-quer","description":"Implementing time-slot booking (courts, coaches, rooms) that must prevent conflicts","category":"ai-brimind","path":"ai-brimind/check-booking-overlaps-with-the-interval-formula-in-the-quer/SKILL.md","file":"SKILL.md","content":"---\nname: check-booking-overlaps-with-the-interval-formula-in-the-quer\ndescription: \"Implementing time-slot booking (courts, coaches, rooms) that must prevent conflicts\"\n---\n\n# Check Booking Overlaps With the Interval Formula in the Query — and Close the Race With a Guard\n\n## When to use\nYou are writing logic that reserves a resource for a time range, and conflict checking is being improvised — comparing only start times, checking in JS after fetching, or ignoring simultaneous requests.\n\n## Example request\nConflict check compares newStart === existingStart → a 10:00–11:30 booking and a 10:30–11:00 booking pass as 'no conflict'; equality checks miss PARTIAL overlaps, which are most overlaps. The standard interval formula — overlap exists iff existingStart < newEnd AND newStart < existingEnd → two comparisons catch every overlap case (contained, containing, straddling either edge); this is a solved classic, never re-derive it wrong. The check done by fetching bookings then deciding in JS → between your read and your insert, another request books the same slot; this is your read-modify-write race wearing a calendar, and the guard must live at write time. Back-to-back bookings 10:00–11:00 and 11:00–12:00 → the strict inequalities make them legal; whether edges may touch is a business decision to state, not an accident of < vs <=.\n\n## Instructions\nAct as the engineer of a booking system who knows the two ways slot conflicts ship broken: the WRONG FORMULA (checking start-time equality or 'is the new start inside an existing booking' — both miss straddling and containing overlaps) and the RIGHT FORMULA AT THE WRONG TIME (a correct check performed on fetched data, leaving a gap before the insert where a rival request books the same slot — your race-condition discipline, on a calendar). For any time-range reservation, do this: (1) Use the canonical interval-overlap formula and only it: intervals A and B overlap ⟺ A.start < B.end AND B.start < A.end. Two comparisons, covers every geometry — new inside old, old inside new, straddle left, straddle right. Decide the edge policy out loud: strict < lets 11:00 end meet 11:00 start (usually wanted for back-to-back slots); document the choice at the query, because < vs <= is a business rule disguised as an operator. (2) Put the formula IN the database query, scoped by your other disciplines: find conflicting bookings WHERE resourceId matches AND tenant matches (your multi-tenant scoping) AND status is an active state (cancelled bookings don't block — your state-machine vocabulary via the shared constants) AND start < :newEnd AND :newStart < end — with an index on (resourceId, start) or similar per your index discipline, since this query runs on every booking attempt. (3) Close the race at write time, because check-then-insert is a gap: MongoDB has no 'unique range' constraint, so pick a deliberate guard — a transaction wrapping the conflict-check + insert (your all-or-nothing discipline), or a lock document per resource+slot-granularity with a unique index that turns the race into a duplicate-key error you translate per your error contract ('slot just taken, pick another'), and say which you chose and why it fits the load. The losing concurrent request must receive a truthful, human answer — never a silent double booking, never a raw E11000. (4) Normalize the time data underneath it all: stored as real UTC Dates per your timezone discipline — the formula compares instants, and instants must be honest; end must be after start, validated at the border like any input. (5) Test the geometry and the race, not the happy path: attempt each overlap shape (inside, around, both straddles, exact-touch edges) and fire two identical booking requests in parallel — exactly one must win. Only ship when every overlap shape is rejected by the query, edges behave per the stated policy, and the parallel test yields one booking and one polite refusal — a booking system that only handles bookings one at a time, in nice shapes, is a demo, and real calendars are neither polite nor sequential.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #248\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}