สร้างระบบ › สร้างระบบจองคิว
เอกสาร/สร้างระบบ/สร้างระบบจองคิว

สร้างระบบจองคิว

หน้านี้สอนอะไร

สร้างระบบหลังบ้านของร้านเสริมสวยหรือคลินิกจาก config เปล่า: บริการ พนักงาน และการจองที่ใครก็จองได้แต่เฉพาะพนักงานที่เห็น ปฏิทินตามเวลาของร้าน และเคาน์เตอร์ต้อนรับที่เปิดมาเจอคิววันนี้ ไม่ต้องเขียนโค้ดนอก config ใช้เวลาประมาณ 30 นาที

ควรอ่านก่อน: เริ่มใช้งาน หรือไม่ต้องอ่านอะไรเลยถ้าทำตามใน Studio ซึ่งมีระบบที่เสร็จแล้วเป็น template Booking

แต่ละขั้นเพิ่มโค้ดลงใน easy-cms.config.ts บันทึกแล้วหน้าแอดมินจะเปลี่ยนตาม (ใน Studio เปลี่ยนตามที่พิมพ์) config ทั้งหมดอยู่ท้ายหน้า

1. บริการ พนักงาน และการจอง

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. ใครก็จองได้ แต่เฉพาะพนักงานที่เห็นการจอง

ฟอร์มจองบนเว็บสร้างการจองได้โดยไม่ต้อง login และแสดงบริการกับพนักงานให้เลือก ส่วนตัวการจองเห็นได้เฉพาะพนักงานที่ login แล้ว:

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

ฟอร์มส่งไปที่ POST /api/cms/bookings (REST API) และการจองใหม่เริ่มที่ requested ดูกำหนดสิทธิ์

3. ปฏิทินตามเวลาของร้าน

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

หน้ารายการการจองจะมี Calendar แบบเดือน สัปดาห์ วัน และ agenda แยกสีตาม status เมื่อตั้ง timeZone การจองเวลา 10:00 จะเป็น 10:00 สำหรับทุกคนไม่ว่าเบราว์เซอร์อยู่เขตไหน และ "วันนี้" ในขั้นต่อไปคือวันนี้ของร้าน ดูปฏิทิน

4. เมนูตามงาน และแบรนด์ของคุณ

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. role ของเคาน์เตอร์ต้อนรับ

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

'$today' คือเที่ยงคืนต้นวันนี้ตาม timeZone view To confirm จึงหมายถึงคำขอที่ยังไม่ถึงเวลาเสมอ (ฉัน ตอนนี้ และวันนี้)

6. วันนี้บนแดชบอร์ด

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',
  ],
},

เคาน์เตอร์ต้อนรับเห็นคำขอที่ต้องยืนยันและคิวตั้งแต่วันนี้เรียงจากเร็วสุด เจ้าของร้านเห็นการจองที่เสร็จในเดือนนี้เพิ่ม คลิกตัวเลขแล้วเปิดการจองชุดนั้น ดูแดชบอร์ด

7. เคาน์เตอร์ต้อนรับเปิดมาเจอวันนี้

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

หลัง login พนักงานต้อนรับจะเจอปฏิทินของวันนี้ทันที ส่วนเมนู Home ยังเปิดแดชบอร์ดได้เสมอ ใน Studio ใช้ View as เพื่อดูแอดมินในฐานะแต่ละ role

config ทั้งหมด

easy-cms.config.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: [] },
  ],
})

ขั้นต่อไป

  • เริ่มโปรเจกต์จากเทมเพลตนี้: npm create easy-cms@latest -- my-salon --template booking แล้วรัน npm run seed เพื่อใส่ข้อมูลตัวอย่างและผู้ใช้ของแต่ละ role (เริ่มใช้งาน) ถ้าแก้ใน Studio แล้ว ปุ่ม Use in a project จะให้คำสั่ง npm create easy-cms@latest -- --from "…" ที่มีสิ่งที่คุณแก้
  • ส่ง SMS หาลูกค้าเมื่อยืนยันการจอง: ใช้ hook afterChange หรือ task ที่ hook สั่งไว้ บริการ SMS ที่ตอบช้าจะได้ไม่ทำให้การบันทึกช้าไปด้วย
  • เตือนลูกค้าก่อนหนึ่งวัน: workflow ที่พักรอจนถึงเวลานั้น
  • อีกระบบหนึ่งในหมวดนี้: สร้าง CRM