@bondsports/online-booking
v1.0.0
Published
Bond Sports online booking package
Maintainers
Keywords
Readme
@bondsports/online-booking
Validation and settings-resolution utilities for online booking flows.
The package answers two questions:
- What booking settings apply to this user right now? —
resolveBookingSettingsmerges the default product-category settings with the most favorable overrides from the user's active memberships. - Are the slots the user wants to book actually allowed? —
validateBookingSlotschecks the requested slots (combined with the user's already-booked slots) against the resolved settings and returns a typed validation result.
Installation
npm install @bondsports/online-bookingUsage
resolveBookingSettings
Pick the effective IBookingSettings for a user. If the user has no active memberships (or none of them override the category settings), the default is returned. Otherwise the lowest minimumBookingNotice and the highest advanceBookingWindow across matching membership overrides win.
import { resolveBookingSettings } from '@bondsports/online-booking';
const settings = resolveBookingSettings(productCategory.onlineBookingSettings, user.activeMemberships);productCategory.onlineBookingSettings—TProductCategoryOnlineBookingSettings({ default, memberships })user.activeMemberships—{ membershipId: number }[](optional; passundefinedor[]for guests)
validateBookingSlots
Validate a list of requested time slots against the resolved settings.
import { validateBookingSlots } from '@bondsports/online-booking';
const result = validateBookingSlots(requestedSlots, userBookedSlots, settings, facilityTimezone);
if (!result.success) {
// result.error is a ValidationError with a stable `code` (see ONLINE_BOOKING_ERRORS)
throw result.error;
}The validator:
- merges requested slots with the user's existing bookings (clamped to the booking window),
- enforces
maxBookingHoursper calendar day (in the facility timezone), - enforces
maxSequentialBookingson each continuous block, - enforces
minimumBookingNoticeandadvanceBookingWindowagainst "now".
Error handling
All failures return { success: false, error } where error is a ValidationError subclass carrying a stable code from ONLINE_BOOKING_ERRORS:
| Error class | Code | Meaning |
| ------------------------- |------------------------|----------------------------------------------------------|
| InvalidTimesError | INVALID_TIMES | Start date is not strictly before end date |
| MinimumNoticeError | MINIMUM_NOTICE_DATE | Start date is before now + minimumBookingNotice |
| AdvanceBookingError | ADVANCE_BOOKING_DATE | Start date extends beyond now + advanceBookingWindow |
| MaxSequentialHoursError | MAX_SEQUENTIAL_HOURS | A contiguous block exceeds maxSequentialBookings |
| MaxBookingHoursError | MAX_BOOKING_HOURS | Total booked duration on a day exceeds maxBookingHours |
import { ONLINE_BOOKING_ERRORS, ValidationError } from '@bondsports/online-booking';
if (!result.success && result.error.code === ONLINE_BOOKING_ERRORS.MINIMUM_NOTICE_DATE) {
// show "book at least N hours in advance" message
}API
import {
validateBookingSlots,
resolveBookingSettings,
ONLINE_BOOKING_ERRORS,
ValidationError,
InvalidTimesError,
MinimumNoticeError,
AdvanceBookingError,
MaxSequentialHoursError,
MaxBookingHoursError,
IBookingValidation,
IBookingSettings,
IMembershipBookingSettings,
TProductCategoryOnlineBookingSettings,
} from '@bondsports/online-booking';Scripts
npm run build # compile to ./dist
npm test # run jest
npm run lint # eslint + prettier check
npm run lint:fix # autofix