Skip to content
Agentpost robot in sunglasses on a red circleAgentpost

Limit hold length and booking window

Decide how long a customer can hold a court and how far ahead they can book it. The booking window limits the time the customer can ask for. The hold length limits how long the court stays set aside while they check out. Holds explains what a hold reserves and when it expires, and Schedules explains how time buckets set what is free to book.

Before you start

This page's code continues the script of the pages above: run its blocks after theirs, in the same file. To run them as a script of their own, first copy each helper from the page that defines it, and set each other value, such as a key or an ID, to one you already have.

1. Create a court

Use api and the dev secret key key for a project that adopted ResourceShark. Create a court with one booking per hourly bucket. Run the blocks in order on your server.

const bookingExample = crypto.randomUUID();
const { resource: bookingCourt } = await api(
  'POST',
  '/v1/resources/resources',
  key,
  {
    key: `court-${bookingExample}`,
    name: 'Practice court',
    unit: { code: 'court', singular: 'court', plural: 'courts', precision: 0 },
    settings: {
      time: 'buckets',
      bucket: { size: 'step', stepMinutes: 60, timeZone: 'UTC' },
      identity: 'count',
      supplyExpiry: 'none',
    },
    scope: { places: false, holders: false },
    tracking: 'none',
    shortage: 'deny',
    capacity: 1,
  },
);

2. Check the request on your server

Allow bookings that start in the future and end within the next 14 days. Allow a hold of at most 15 minutes. This app accepts a hold length in whole minutes and turns it into the required ttl after checking it.

const bookingHourMs = 60 * 60_000;
const bookingWindowMs = 14 * 24 * bookingHourMs;
const bookingMaxHoldMinutes = 15;

function checkBookingWindow(start: number, end: number, now: number) {
  return start > now && end > start && end <= now + bookingWindowMs;
}

function bookingHoldTtl(minutes: number) {
  if (
    !Number.isInteger(minutes) ||
    minutes < 1 ||
    minutes > bookingMaxHoldMinutes
  )
    return undefined;
  return `${minutes}m`;
}

const bookingNow = Date.now();
const bookingStart =
  Math.ceil(bookingNow / bookingHourMs) * bookingHourMs + bookingHourMs;
const bookingEnd = bookingStart + bookingHourMs;
const requestedBookingTtl = bookingHoldTtl(5);
console.log(
  checkBookingWindow(bookingStart, bookingEnd, bookingNow),
  checkBookingWindow(
    bookingStart + bookingWindowMs,
    bookingEnd + bookingWindowMs,
    bookingNow,
  ),
  requestedBookingTtl !== undefined,
  bookingHoldTtl(16) !== undefined,
); // true false true false

3. Place the hold after both checks pass

Send the checked span and the checked ttl together. Apply your hold-length policy to extensions too. An extension’s ttl starts when ResourceShark handles the request, so account for the time since hold.createdAt when you choose it.

if (!checkBookingWindow(bookingStart, bookingEnd, Date.now())) {
  throw new Error('Choose a court hour within the next 14 days');
}
if (requestedBookingTtl === undefined) {
  throw new Error('Choose a hold of 1 to 15 minutes');
}
const { hold: limitedBookingHold } = await api(
  'POST',
  '/v1/resources/holds',
  key,
  {
    lines: [
      {
        resourceId: bookingCourt.id,
        quantity: 1,
        span: {
          start: new Date(bookingStart).toISOString(),
          end: new Date(bookingEnd).toISOString(),
        },
      },
    ],
    ttl: requestedBookingTtl,
    sourceKey: `court-checkout:${bookingExample}`,
  },
  { 'Idempotency-Key': `court-hold-${bookingExample}` },
);

Check it worked

Read the hold and check how long it runs in minutes.

const { hold: savedBookingHold } = await api(
  'GET',
  `/v1/resources/holds/${limitedBookingHold.id}`,
  key,
);
console.log(
  (Date.parse(savedBookingHold.expiresAt) -
    Date.parse(savedBookingHold.createdAt)) /
    60_000,
); // 5

An extension after expiry answers hold_expired while ResourceShark keeps the hold’s details. The holds reference has every field of placement and extension.