Under the hood
The database makes double bookings impossible
Checking availability in the app isn’t enough: two guests can pay for the last villa in the same second. So the rule lives in Postgres itself, and every write has to pass it, whichever screen it comes from.
constraint no_double_booking exclude using gist (
room_id with =,
daterange(check_in, check_out, '[)') with &&
) where (status in ('pending_payment', 'confirmed', 'checked_in'))
Tested under a race
Five simultaneous bookings for the last two villas: two reservations, three clean “sold out” answers. If the best room is taken mid-booking, create_reservation() moves on to the next free one.
Prices set by the server
The database works out the price night by night, so the browser can’t change what a guest pays. The same rules run in the app and are unit-tested, including a rounding case where JavaScript and Postgres disagreed by ₱20.
Holds that let go
An unpaid booking holds its room for 15 minutes, then a scheduled job releases it. PayMongo’s webhook is signature-checked and safe to retry; a payment that arrives after the hold lapsed is accepted only if the room is still free.
Guests never read the tables. They reach their booking through SQL functions that need the secret token in their link, and row-level security keeps reservations visible to front-desk accounts only. Confirmation and cancellation texts are queued by a database trigger, so a status change never misses its SMS.