สร้างระบบ › สร้าง CRM
เอกสาร/สร้างระบบ/สร้าง CRM

สร้าง CRM

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

สร้าง CRM เล็ก ๆ จาก config เปล่า: บริษัท ผู้ติดต่อ และดีลที่เดินตามขั้นตอนการขาย บอร์ดของ pipeline sales ที่เห็นตัวเลขของตัวเอง และผู้จัดการที่เห็นทั้งหมด ไม่ต้องเขียนโค้ดนอก config ใช้เวลาประมาณ 30 นาที

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

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

1. บริษัท ผู้ติดต่อ และดีล

collection สามตัวที่เชื่อมกันด้วย relationship:

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: 'companies',
      useAsTitle: 'name',
      fields: [
        { name: 'name', type: 'text', required: true },
        { name: 'website', type: 'text' },
        { name: 'industry', type: 'select', options: ['retail', 'software', 'health', 'education'] },
      ],
    },
    {
      slug: 'contacts',
      useAsTitle: 'name',
      fields: [
        { name: 'name', type: 'text', required: true },
        { name: 'email', type: 'email' },
        { name: 'company', type: 'relationship', to: 'companies' },
        { name: 'title', type: 'text' },
      ],
    },
    {
      slug: 'deals',
      useAsTitle: 'name',
      fields: [
        { name: 'name', type: 'text', required: true },
        { name: 'company', type: 'relationship', to: 'companies', required: true },
        { name: 'contact', type: 'relationship', to: 'contacts' },
        { name: 'value', type: 'number', min: 0 },
        { name: 'closeDate', type: 'date' },
      ],
    },
  ],
})

หน้าแอดมินมีรายการและฟอร์มของแต่ละ collection และตอนเลือกบริษัทในดีลจะค้นหาบริษัทให้

2. pipeline ที่ข้ามขั้นไม่ได้

ดีลเดินจาก lead → qualified → proposal → won หรือ lost ระหว่างทาง state machine เพิ่ม field stage และปุ่มหนึ่งปุ่มต่อหนึ่ง transition นอกจากปุ่มเหล่านี้ไม่มีทางอื่นที่เปลี่ยน stage ได้:

{
  slug: 'deals',
  // …fields
  stateMachine: {
    field: 'stage',
    initial: 'lead',
    states: {
      lead: {},
      qualified: {},
      proposal: { lock: ['value'] }, // the quoted value stays as quoted
      won: { lock: true, final: true },
      lost: {},
    },
    transitions: {
      qualify: { from: 'lead', to: 'qualified' },
      propose: { from: 'qualified', to: 'proposal', requires: ['value', 'contact'] },
      win: { from: 'proposal', to: 'won', confirm: true },
      lose: { from: ['lead', 'qualified', 'proposal'], to: 'lost', confirm: { danger: true } },
      reopen: { from: 'lost', to: 'lead' },
    },
  },
}

ตอนนี้หน้าดีลมีปุ่ม Qualify และ Lose ส่วน Propose จะเป็นสีจางจนกว่าดีลจะมีมูลค่าและผู้ติดต่อ แถบข้างวาดแผนภาพของ pipeline พร้อมบอกว่าดีลอยู่ตรงไหน

3. pipeline บนบอร์ด

admin: { board: { groupBy: 'stage', fields: ['company', 'value'] } },

หน้ารายการดีลจะมี Board คอลัมน์ละหนึ่ง stage ลากการ์ดคือการทำ transition และวางการ์ดในคอลัมน์ที่ไม่มี transition ไปถึงไม่ได้ ดูบอร์ด

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

แอดมินของ CRM ไม่ควรเรียกทุกอย่างว่า "เนื้อหา" ตั้งชื่อกลุ่มตามงาน ย้ายคลังสื่อไปไว้ใต้ตั้งค่า และใส่ชื่อกับสีของคุณ:

admin: {
  brand: { name: 'Northwind CRM', color: '#1d4ed8' },
  nav: [
    { id: 'sales', label: { en: 'Sales', th: 'งานขาย' }, icon: 'briefcase', order: 0 },
    { id: 'customers', label: { en: 'Customers', th: 'ลูกค้า' }, icon: 'building', order: 1 },
  ],
},
collections: [
  { slug: 'companies', admin: { group: 'customers' }, /* … */ },
  { slug: 'contacts', admin: { group: 'customers' }, /* … */ },
  { slug: 'deals', admin: { group: 'sales', /* board */ }, /* … */ },
  { slug: 'media', admin: { group: 'settings' }, fields: [] },
],

ตัวเลขบนแดชบอร์ดจะอยู่ใต้หัวข้อ Sales และ Customers และแท็บของเบราว์เซอร์เป็น "Northwind CRM" ส่วนอื่น ๆ ดูที่คำบนหน้าจอ

5. sales และดีลของแต่ละคน

เพิ่ม role อีกสอง role นอกจาก admin และใส่เจ้าของให้ทุกดีล โดยค่าเริ่มต้นเป็นคนที่สร้าง:

auth: { roles: ['admin', 'manager', 'sales'] },
// on deals:
fields: [
  // …
  { name: 'owner', type: 'relationship', to: 'users', admin: { position: 'sidebar' } },
],
hooks: {
  beforeChange: [
    ({ data, operation, user }) =>
      operation === 'create' && !data.owner && user ? { ...data, owner: user.id } : data,
  ],
},
admin: {
  // …
  views: [{ id: 'mine', label: 'Mine', where: { owner: { equals: '$me' } }, board: true }],
},

'$me' คือผู้ใช้ที่กำลังดู view Mine อันเดียวจึงแสดงบอร์ดของ sales แต่ละคน (ฉัน ตอนนี้ และวันนี้)

6. แดชบอร์ดของแต่ละ role

แดชบอร์ดบอกสิ่งที่แต่ละคนต้องรู้ก่อน รายการที่มี roles แสดงเฉพาะ role เหล่านั้น:

admin: {
  dashboard: [
    { type: 'stat', label: 'My open deals', collection: 'deals',
      where: { owner: { equals: '$me' }, stage: { not_in: ['won', 'lost'] } } },
    { type: 'stat', label: 'Pipeline value', collection: 'deals', sum: 'value',
      where: { stage: { not_in: ['won', 'lost'] } }, roles: ['admin', 'manager'] },
    { type: 'stat', label: 'Won this month', collection: 'deals',
      where: { stage: { equals: 'won' }, updatedAt: { gte: '$startOfMonth' } }, roles: ['admin', 'manager'] },
    { type: 'list', label: 'My deals, closing next', collection: 'deals',
      where: { owner: { equals: '$me' }, closeDate: { gte: '$today' } }, sort: 'closeDate',
      fields: ['company', 'closeDate', 'stage'], roles: ['sales'] },
    { panel: 'recent', roles: ['admin', 'manager'] },
    'collections',
  ],
},

sales เห็นดีลที่ยังเปิดของตัวเองและดีลที่ใกล้ปิด ผู้จัดการเห็นมูลค่า pipeline (ฐานข้อมูลรวมให้) และดีลที่ปิดได้เดือนนี้ คลิกตัวเลขแล้วเปิดดีลชุดนั้น ดูแดชบอร์ด

7. sales เปิดมาเจอบอร์ด

admin: {
  home: ({ user }) => (user.role === 'sales' ? '/collections/deals?board=1' : undefined),
},

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

config ทั้งหมด

easy-cms.config.ts
import { defineConfig } from '@easy-cms/core'
import { sqlite } from '@easy-cms/db-sqlite'
 
// A small CRM: companies, the people in them, and deals moving through a pipeline (a state
// machine, on a board). Salespeople open on the board and see their own numbers; managers see
// the whole pipeline. View the admin as each role with "View as".
export default defineConfig({
  secret: process.env.EASY_CMS_SECRET ?? '',
  db: sqlite({ url: 'file:./cms.db' }),
  auth: { roles: ['admin', 'manager', 'sales'] },
  admin: {
    brand: { name: 'Northwind CRM', color: '#1d4ed8' },
    // The menu by work, not "Content"; the media library under Settings.
    nav: [
      { id: 'sales', label: { en: 'Sales', th: 'งานขาย' }, icon: 'briefcase', order: 0 },
      { id: 'customers', label: { en: 'Customers', th: 'ลูกค้า' }, icon: 'building', order: 1 },
    ],
    dashboard: [
      {
        type: 'stat',
        label: { en: 'My open deals', th: 'ดีลของฉันที่ยังเปิด' },
        collection: 'deals',
        where: { owner: { equals: '$me' }, stage: { not_in: ['won', 'lost'] } },
      },
      {
        type: 'stat',
        label: { en: 'Pipeline value', th: 'มูลค่า pipeline' },
        collection: 'deals',
        sum: 'value',
        where: { stage: { not_in: ['won', 'lost'] } },
        roles: ['admin', 'manager'],
      },
      {
        type: 'stat',
        label: { en: 'Won this month', th: 'ปิดได้เดือนนี้' },
        collection: 'deals',
        where: { stage: { equals: 'won' }, updatedAt: { gte: '$startOfMonth' } },
        roles: ['admin', 'manager'],
      },
      {
        type: 'list',
        label: { en: 'My deals, closing next', th: 'ดีลของฉันที่ใกล้ปิด' },
        collection: 'deals',
        where: { owner: { equals: '$me' }, closeDate: { gte: '$today' } },
        sort: 'closeDate',
        fields: ['company', 'closeDate', 'stage'],
        roles: ['sales'],
      },
      {
        type: 'list',
        label: { en: 'Closing next', th: 'ใกล้ปิด' },
        collection: 'deals',
        where: { closeDate: { gte: '$today' } },
        sort: 'closeDate',
        fields: ['company', 'owner', 'closeDate', 'stage'],
        roles: ['admin', 'manager'],
      },
      { panel: 'recent', roles: ['admin', 'manager'] },
      'collections',
    ],
    // Salespeople start on the pipeline itself.
    home: ({ user }) => (user.role === 'sales' ? '/collections/deals?board=1' : undefined),
  },
  collections: [
    {
      slug: 'companies',
      useAsTitle: 'name',
      admin: { group: 'customers', icon: 'building' },
      fields: [
        { name: 'name', type: 'text', required: true },
        { name: 'website', type: 'text' },
        {
          name: 'industry',
          type: 'select',
          options: ['retail', 'software', 'health', 'education'],
        },
      ],
    },
    {
      slug: 'contacts',
      useAsTitle: 'name',
      admin: { group: 'customers', icon: 'users' },
      fields: [
        { name: 'name', type: 'text', required: true },
        { name: 'email', type: 'email' },
        { name: 'company', type: 'relationship', to: 'companies' },
        { name: 'title', type: 'text' },
      ],
    },
    {
      slug: 'deals',
      useAsTitle: 'name',
      fields: [
        { name: 'name', type: 'text', required: true },
        { name: 'company', type: 'relationship', to: 'companies', required: true },
        { name: 'contact', type: 'relationship', to: 'contacts' },
        { name: 'value', type: 'number', min: 0 },
        { name: 'closeDate', type: 'date' },
        { name: 'owner', type: 'relationship', to: 'users', admin: { position: 'sidebar' } },
      ],
      hooks: {
        // A new deal is its maker's unless they pick someone.
        beforeChange: [
          ({ data, operation, user }) =>
            operation === 'create' && !data.owner && user ? { ...data, owner: user.id } : data,
        ],
      },
      // The pipeline: a stage changes only by these transitions (a button each in the admin).
      stateMachine: {
        field: 'stage',
        initial: 'lead',
        states: {
          lead: {},
          qualified: {},
          proposal: { lock: ['value'] },
          won: { lock: true, final: true },
          lost: {},
        },
        transitions: {
          qualify: { from: 'lead', to: 'qualified' },
          propose: { from: 'qualified', to: 'proposal', requires: ['value', 'contact'] },
          win: { from: 'proposal', to: 'won', confirm: true },
          lose: { from: ['lead', 'qualified', 'proposal'], to: 'lost', confirm: { danger: true } },
          reopen: { from: 'lost', to: 'lead' },
        },
      },
      admin: {
        group: 'sales',
        icon: 'briefcase',
        board: { groupBy: 'stage', fields: ['company', 'value', 'owner'] },
        views: [
          {
            id: 'mine',
            label: { en: 'Mine', th: 'ของฉัน' },
            where: { owner: { equals: '$me' } },
            board: true,
          },
        ],
      },
    },
    // The media library: under Settings, where a CRM keeps it.
    { slug: 'media', admin: { group: 'settings' }, fields: [] },
  ],
})

ขั้นต่อไป

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