82 lines
2.9 KiB
TypeScript
82 lines
2.9 KiB
TypeScript
import type { CollectionConfig } from 'payload'
|
||
import { ADMIN_GROUP } from '../admin/groups'
|
||
import { isValidEmailAddress, isValidFullName, isValidRuPhone, normalizeRuPhone } from '../lib/contact-fields'
|
||
|
||
export const Inquiries: CollectionConfig = {
|
||
slug: 'inquiries',
|
||
labels: { singular: 'Заявка на запись', plural: 'Заявки на запись' },
|
||
admin: {
|
||
group: ADMIN_GROUP.booking,
|
||
useAsTitle: 'name',
|
||
defaultColumns: ['name', 'phone', 'email', 'status', 'createdAt'],
|
||
description: 'Заявки с сайта без выбора времени. Свяжитесь с пациентом и уточните детали.',
|
||
},
|
||
access: {
|
||
create: () => true,
|
||
read: ({ req: { user } }) => {
|
||
if (!user) return false
|
||
if (user.role === 'admin') return true
|
||
return { patient: { equals: user.id } }
|
||
},
|
||
update: ({ req: { user } }) => user?.role === 'admin',
|
||
delete: ({ req: { user } }) => user?.role === 'admin',
|
||
},
|
||
hooks: {
|
||
beforeValidate: [
|
||
({ data }) => {
|
||
if (!data) return data
|
||
if (typeof data.name === 'string') data.name = data.name.trim().replace(/\s+/g, ' ')
|
||
if (typeof data.email === 'string') data.email = data.email.trim()
|
||
if (typeof data.phone === 'string') {
|
||
const phone = normalizeRuPhone(data.phone)
|
||
if (phone) data.phone = phone
|
||
}
|
||
return data
|
||
},
|
||
],
|
||
},
|
||
fields: [
|
||
{
|
||
name: 'name',
|
||
type: 'text',
|
||
required: true,
|
||
label: 'ФИО',
|
||
validate: (value: string | null | undefined) =>
|
||
isValidFullName(String(value ?? '')) ? true : 'Укажите фамилию и имя буквами',
|
||
},
|
||
{
|
||
name: 'email',
|
||
type: 'email',
|
||
required: true,
|
||
label: 'E-mail',
|
||
validate: (value: string | null | undefined) =>
|
||
isValidEmailAddress(String(value ?? '')) ? true : 'Укажите e-mail в формате name@domain.ru',
|
||
},
|
||
{
|
||
name: 'phone',
|
||
type: 'text',
|
||
required: true,
|
||
label: 'Телефон',
|
||
validate: (value: string | null | undefined) =>
|
||
isValidRuPhone(String(value ?? '')) ? true : 'Укажите телефон в формате +7 и 10 цифр',
|
||
},
|
||
{ name: 'service', type: 'relationship', relationTo: 'services', label: 'Услуга' },
|
||
{ name: 'doctor', type: 'relationship', relationTo: 'doctors', label: 'Врач' },
|
||
{ name: 'branch', type: 'relationship', relationTo: 'branches', required: true, label: 'Филиал' },
|
||
{ name: 'patient', type: 'relationship', relationTo: 'users', label: 'Пациент' },
|
||
{
|
||
name: 'status',
|
||
type: 'select',
|
||
required: true,
|
||
defaultValue: 'new',
|
||
label: 'Статус',
|
||
options: [
|
||
{ label: 'Новая', value: 'new' },
|
||
{ label: 'В работе', value: 'in_progress' },
|
||
{ label: 'Закрыта', value: 'done' },
|
||
],
|
||
},
|
||
],
|
||
timestamps: true,
|
||
}
|