# Build a booking system

::: info What you'll learn
Build the back office of a salon or clinic from an empty config: services, staff and bookings
that anyone may make and only staff see, a calendar in the salon's time, and a front desk that
opens on today. No code outside the config, about 30 minutes.

**Before this page:** [Getting started](./getting-started), or nothing if you follow along in
[Studio](./studio), where the finished system is the
[Booking template](https://studio.easycms.dev/?template=booking).
:::

Each step adds to `easy-cms.config.ts`; save it and the admin follows (in Studio, as you type).
The whole config is [at the end](#the-whole-config).

## 1. Services, staff and bookings

```ts [easy-cms.config.ts]
import { defineConfig } from '@easy-cms/core'
import { sqlite } from '@easy-cms/db-sqlite'

export default defineConfig({
  secret: process.env.EASY_CMS_SECRET ?? '',
  db: sqlite({ url: 'file:./cms.db' }),
  collections: [
    {
      slug: 'services',
      useAsTitle: 'name',
      fields: [
        { name: 'name', type: 'text', required: true },
        { name: 'minutes', type: 'number', required: true, min: 15 },
        { name: 'price', type: 'number', min: 0 },
      ],
    },
    {
      slug: 'staff',
      useAsTitle: 'name',
      fields: [
        { name: 'name', type: 'text', required: true },
        { name: 'services', type: 'relationship', to: 'services', hasMany: true },
      ],
    },
    {
      slug: 'bookings',
      useAsTitle: 'customer',
      fields: [
        { name: 'customer', type: 'text', required: true },
        { name: 'phone', type: 'text', required: true },
        { name: 'service', type: 'relationship', to: 'services', required: true },
        { name: 'staff', type: 'relationship', to: 'staff' },
        { name: 'startsAt', type: 'date', required: true },
        {
          name: 'status',
          type: 'select',
          options: ['requested', 'confirmed', 'done', 'cancelled'],
          defaultValue: 'requested',
        },
        { name: 'notes', type: 'textarea' },
      ],
    },
  ],
})
```

## 2. Anyone may book; only staff see the bookings

The site's booking form creates bookings without signing in, and shows the services and staff to
choose from; the bookings themselves are for signed-in staff:

```ts
{ slug: 'services', access: { read: () => true }, /* … */ },
{ slug: 'staff', access: { read: () => true }, /* … */ },
{
  slug: 'bookings',
  access: { create: () => true, read: ({ user }) => !!user },
  // …
},
```

The form posts to `POST /api/cms/bookings` ([REST API](./rest-api)); a new booking starts as
`requested`. See [Access control](./access-control).

## 3. A calendar in the salon's time

```ts
timeZone: 'Asia/Bangkok',
// on bookings:
admin: { calendar: { start: 'startsAt', color: 'status' } },
```

The bookings list gets **Calendar**: month, week, day and agenda, coloured by status. With
`timeZone`, a 10:00 booking is 10:00 for everyone, whatever their browser's zone, and "today"
below is the salon's today. See [Calendar](./admin#calendar).

## 4. A menu by work, and your brand

```ts
admin: {
  brand: { name: 'Lotus Salon', color: '#9d174d' },
  nav: [
    { id: 'bookings', label: { en: 'Bookings', th: 'การจอง' }, icon: 'calendar-days', order: 0 },
    { id: 'salon', label: { en: 'Services & staff', th: 'บริการและพนักงาน' }, icon: 'store', order: 1 },
  ],
},
collections: [
  { slug: 'services', admin: { group: 'salon' }, /* … */ },
  { slug: 'staff', admin: { group: 'salon' }, /* … */ },
  { slug: 'bookings', admin: { group: 'bookings', /* calendar */ }, /* … */ },
  { slug: 'media', admin: { group: 'settings' }, fields: [] },
],
```

## 5. A role for the front desk

```ts
auth: { roles: ['admin', 'reception'] },
// on bookings:
admin: {
  // …
  views: [{
    id: 'to-confirm', label: 'To confirm', sort: 'startsAt',
    where: { status: { equals: 'requested' }, startsAt: { gte: '$today' } },
  }],
},
```

`'$today'` is midnight that starts today in `timeZone`, so **To confirm** always means the
requests still ahead ([Me, now and today](./local-api#where-values)).

## 6. Today on the dashboard

```ts
admin: {
  dashboard: [
    { type: 'stat', label: 'To confirm', collection: 'bookings',
      where: { status: { equals: 'requested' }, startsAt: { gte: '$today' } } },
    { type: 'stat', label: 'Coming up', collection: 'bookings',
      where: { startsAt: { gte: '$today' }, status: { not_in: ['cancelled'] } } },
    { type: 'stat', label: 'Done this month', collection: 'bookings',
      where: { status: { equals: 'done' }, startsAt: { gte: '$startOfMonth' } }, roles: ['admin'] },
    { type: 'list', label: 'Queue from today', collection: 'bookings',
      where: { startsAt: { gte: '$today' }, status: { not_in: ['cancelled'] } },
      sort: 'startsAt', limit: 8, fields: ['startsAt', 'service', 'staff', 'status'], width: 'full' },
    { panel: 'recent', roles: ['admin'] },
    'collections',
  ],
},
```

The front desk sees what to confirm and the queue from today, earliest first; the owner also sees
the month's done bookings. A number opens its bookings. See [The dashboard](./admin#dashboard).

## 7. The front desk starts on today

```ts
admin: {
  home: ({ user }) =>
    user.role === 'reception' ? '/collections/bookings?calendar=day' : undefined,
},
```

After signing in, reception lands on today's calendar; the menu's Home still opens the
dashboard. In Studio, **View as** shows the admin as each role.

## The whole config {#the-whole-config}

::: details easy-cms.config.ts

<!-- template: booking -->
```ts
import { defineConfig } from '@easy-cms/core'
import { sqlite } from '@easy-cms/db-sqlite'

// Bookings for a clinic or salon: services with a length and price, staff, and bookings.
// Anyone may book; only signed-in staff see the bookings. The front desk opens on today's
// calendar; view the admin as each role with "View as".
export default defineConfig({
  secret: process.env.EASY_CMS_SECRET ?? '',
  db: sqlite({ url: 'file:./cms.db' }),
  timeZone: 'Asia/Bangkok',
  auth: { roles: ['admin', 'reception'] },
  admin: {
    brand: { name: 'Lotus Salon', color: '#9d174d' },
    nav: [
      { id: 'bookings', label: { en: 'Bookings', th: 'การจอง' }, icon: 'calendar-days', order: 0 },
      {
        id: 'salon',
        label: { en: 'Services & staff', th: 'บริการและพนักงาน' },
        icon: 'store',
        order: 1,
      },
    ],
    dashboard: [
      {
        type: 'stat',
        label: { en: 'To confirm', th: 'รอยืนยัน' },
        collection: 'bookings',
        where: { status: { equals: 'requested' }, startsAt: { gte: '$today' } },
      },
      {
        type: 'stat',
        label: { en: 'Coming up', th: 'ตั้งแต่วันนี้' },
        collection: 'bookings',
        where: { startsAt: { gte: '$today' }, status: { not_in: ['cancelled'] } },
      },
      {
        type: 'stat',
        label: { en: 'Done this month', th: 'เสร็จเดือนนี้' },
        collection: 'bookings',
        where: { status: { equals: 'done' }, startsAt: { gte: '$startOfMonth' } },
        roles: ['admin'],
      },
      {
        type: 'list',
        label: { en: 'Queue from today', th: 'คิวตั้งแต่วันนี้' },
        collection: 'bookings',
        where: { startsAt: { gte: '$today' }, status: { not_in: ['cancelled'] } },
        sort: 'startsAt',
        limit: 8,
        fields: ['startsAt', 'service', 'staff', 'status'],
        width: 'full',
      },
      { panel: 'recent', roles: ['admin'] },
      'collections',
    ],
    // The front desk starts on today's bookings.
    home: ({ user }) =>
      user.role === 'reception' ? '/collections/bookings?calendar=day' : undefined,
  },
  collections: [
    {
      slug: 'services',
      useAsTitle: 'name',
      access: { read: () => true },
      admin: { group: 'salon', icon: 'star' },
      fields: [
        { name: 'name', type: 'text', required: true },
        { name: 'minutes', type: 'number', required: true, min: 15 },
        { name: 'price', type: 'number', min: 0 },
      ],
    },
    {
      slug: 'staff',
      useAsTitle: 'name',
      access: { read: () => true },
      admin: { group: 'salon', icon: 'users' },
      fields: [
        { name: 'name', type: 'text', required: true },
        { name: 'services', type: 'relationship', to: 'services', hasMany: true },
      ],
    },
    {
      slug: 'bookings',
      useAsTitle: 'customer',
      access: {
        // The booking form on the site creates them; only staff read them.
        create: () => true,
        read: ({ user }) => !!user,
      },
      admin: {
        group: 'bookings',
        icon: 'calendar',
        calendar: { start: 'startsAt', color: 'status' },
        views: [
          {
            id: 'to-confirm',
            label: { en: 'To confirm', th: 'รอยืนยัน' },
            where: { status: { equals: 'requested' }, startsAt: { gte: '$today' } },
            sort: 'startsAt',
          },
        ],
      },
      fields: [
        { name: 'customer', type: 'text', required: true },
        { name: 'phone', type: 'text', required: true },
        { name: 'service', type: 'relationship', to: 'services', required: true },
        { name: 'staff', type: 'relationship', to: 'staff' },
        { name: 'startsAt', type: 'date', required: true },
        {
          name: 'status',
          type: 'select',
          options: ['requested', 'confirmed', 'done', 'cancelled'],
          defaultValue: 'requested',
        },
        { name: 'notes', type: 'textarea' },
      ],
    },
    // The media library: under Settings; a salon rarely opens it.
    { slug: 'media', admin: { group: 'settings' }, fields: [] },
  ],
})
```

:::

## Next steps

- Start a project from it: `npm create easy-cms@latest -- my-salon --template booking`, then `npm run seed`
  for the sample data and a user per role ([Getting started](./getting-started#from-a-template)).
  After changing it in [Studio](https://studio.easycms.dev/?template=booking), **Use in a project** gives
  `npm create easy-cms@latest -- --from "…"` with your changes.
- Text the customer when a booking is confirmed: an `afterChange` hook, or a
  [task](./jobs#tasks) it enqueues so a slow SMS service doesn't hold up the save.
- A reminder the day before: a [workflow](./workflows) that sleeps until then.
- The other system of this section: [Build a CRM](./build-a-crm).
