Availability and pricing

How nights are counted, how double bookings are prevented, and how every price is worked out.

All of this lives in src/lib/booking-rules.ts: plain functions with no imports, covered by npm test.

Nights

A night is named by the date it starts. A stay from 5 to 8 November has three nights: the 5th, 6th and 7th. The 8th is the check-out day and is free for the next guest, so back-to-back stays never overlap.

  • countNights("2026-11-05", "2026-11-08") → 3
  • Two stays overlap when one starts before the other ends: aIn < bOut && bIn < aOut.
  • Dates are calendar dates (YYYY-MM-DD) in COMPANY_TIMEZONE; leap years and month ends are handled without time zone shifts.

No double bookings

Every taken night is one row in the StayNight table, with a unique index on (stay, date). A booking or a block inserts its rows inside the same database transaction that checks availability and saves the booking. If two guests try to take the same night at the same moment, the second insert breaks the unique index, the transaction is rolled back, and the guest gets 409 unavailable. The end-to-end test fires five parallel bookings for the same nights and expects exactly one to win.

Cancelling a booking or removing a block deletes its night rows, so the nights are free again at once. Unpaid card bookings stop holding their nights after 30 minutes.

The price of a night

For each night, the first rule that applies:

  1. A seasonal price covering that night (first and last night both included). When seasons overlap, the one that starts later wins: a short Eid season inside a long winter season, for example.
  2. The weekend rate for Thursday and Friday nights (the Omani weekend), if the stay has one.
  3. The nightly rate.

Minimum stay

The minimum is the stay's own "minimum nights", or longer if any night of the stay falls in a season that asks for more. Example: a villa with minimum 2 nights and a National Day season with minimum 3: a stay touching National Day needs 3 nights.

The total

nights total = sum of every night's price
tax          = round((nights total + cleaning fee) × tax % / 100)
total        = nights total + cleaning fee + tax

Money is stored as whole numbers in the currency's smallest unit (baisa: 1 OMR = 1000), so there are no rounding errors. The booking keeps a copy of every night's price, so later price changes never alter an existing booking.

Rules checked for every quote and booking

Code Meaning
bad_dates invalid dates, or check-out not after check-in
in_past check-in before today (arriving today is fine)
too_far check-in further ahead than "Bookable days ahead" in settings
too_long more than 60 nights
min_nights fewer nights than the minimum (the answer includes the minimum)
bad_guests more guests than the stay sleeps
unavailable a night is taken (the answer lists the nights)

Changing the rules

To change Edit
Weekend nights WEEKEND_NIGHTS (0 = Sunday)
Longest stay MAX_NIGHTS
How long unpaid card bookings hold their nights HOLD_MINUTES
When the door code appears doorCodeVisible()
Which season wins when they overlap rateForNight()

Run npm test after any change.