Skip to main content

Public Bookings API

The Public Bookings API allows authenticated users to create bookings for active services. Unlike the admin bookings API, this is the customer-facing endpoint.

Authentication

Requires valid user session (any authenticated user can create bookings).

Create booking

Create a new booking for a service. Endpoint: POST /api/services/bookings
Request Body:
string
required
UUID of the service to book
string
required
Date in YYYY-MM-DD format
string
required
Time in HH:MM format (24-hour)
text
Optional message from customer to provider

Validation & business logic

The endpoint performs comprehensive validation:

1. Service validation

  • Service must exist
  • Service status must be "active"
  • Provider (owner) must exist

2. Availability check

  • Converts bookingDate to day of week (0-6)
  • Queries servicesAvailability table for matching slots
  • Verifies bookingTime falls within an available slot
  • Checks isAvailable = true for the slot

3. Conflict detection

  • Queries existing bookings for the same service, date, and time
  • Rejects if another booking exists (no double-booking)

4. Automatic calculations

  • Total price: Uses service’s basePrice
  • Currency: Uses service’s currency
  • Duration: Uses service’s durationMinutes
  • Booking end time: Calculated as bookingTime + durationMinutes

Response

string
Booking UUID
string
Service UUID
string
Provider (service owner) user ID
string
Customer (current user) ID
string
Booking date (YYYY-MM-DD)
string
Start time (HH:MM)
string
Calculated end time (HH:MM)
number
Service duration
number
Price in minor currency units (e.g., cents)
string
Currency code (e.g., “EUR”)
string
Always "pending" for new bookings
text
Customer’s message (if provided)
timestamp
Booking creation timestamp
Example response:

Error responses

Authentication required

Status: 401

Invalid JSON

Status: 400

Missing required fields

Status: 400

Service not found

Status: 404

Service not active

Status: 400

Time slot not available

Status: 400

Booking conflict

Status: 409

Booking workflow

  1. Customer submits booking → Status: pending
  2. Provider reviews (via admin API)
  3. Provider confirms → Status: confirmed
  4. Service delivered → Status: completed
Alternative flows:
  • Provider can cancel or reject bookings
  • Customer no-show → Status: no_show
The booking status can only be changed by the provider through the admin API (/api/admin/services/bookings). Customers cannot cancel or modify bookings through this endpoint.

Day of week calculation

The availability system uses JavaScript’s Date.getDay():
  • 0 = Sunday
  • 1 = Monday
  • 2 = Tuesday
  • 3 = Wednesday
  • 4 = Thursday
  • 5 = Friday
  • 6 = Saturday
Example: 2024-12-15 (Sunday) → dayOfWeek = 0

Time slot matching

A booking time is valid if:
Example:
  • Slot: 09:00 - 17:00
  • Booking time 14:00Valid
  • Booking time 18:00Invalid

Implementation reference

Source: /src/pages/api/services/bookings.ts Key implementation details:
  • Session extraction from locals.session or locals.user
  • Drizzle ORM queries for validation
  • Date/time calculations using JavaScript Date
  • Conflict detection with eq() and and() operators
  • Automatic field population from service data
Timezone Note: The current implementation does not handle timezone conversions. All times are treated as local to the application server. Consider adding timezone support for multi-region deployments.

See also