Security: - only admins can assign user roles; profile updates are whitelisted - SBP payment confirmation requires an HMAC-signed webhook or an admin, and is idempotent - patient receipts and documents are private media owned by the patient, downloaded via the site - patients cannot change booking price or payment status, forge invoices or write for other accounts CMS images: - every upload field shows a large preview, focal point, server file path and public URL - crop/resize keeps the original in media/originals with a "restore original" action - legacy image URL fields show where the file really lives and can be moved into the media library - specialties get a visual icon picker (Untitled UI) and an optional custom icon upload Fixes: saving the cabinet profile, receipt PDF generation, uploaded doctor photos on the site. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
136 lines
4.9 KiB
TypeScript
136 lines
4.9 KiB
TypeScript
import type { CollectionConfig } from 'payload'
|
|
import { ADMIN_GROUP } from '../admin/groups'
|
|
import { adminOnlyField, isAdminUser } from '../lib/access'
|
|
|
|
const BOOKING_HOLD_MS = 24 * 60 * 60 * 1000
|
|
|
|
export const Bookings: CollectionConfig = {
|
|
slug: 'bookings',
|
|
labels: { singular: 'Запись', plural: 'Записи' },
|
|
admin: {
|
|
group: ADMIN_GROUP.booking,
|
|
useAsTitle: 'id',
|
|
defaultColumns: ['patient', 'doctor', 'service', 'startsAt', 'status', 'paymentStatus'],
|
|
},
|
|
access: {
|
|
create: ({ req: { user } }) => Boolean(user),
|
|
read: ({ req: { user } }) => {
|
|
if (!user) return false
|
|
if (user.role === 'admin') return true
|
|
return { patient: { equals: user.id } }
|
|
},
|
|
update: ({ req: { user } }) => {
|
|
if (!user) return false
|
|
if (user.role === 'admin') return true
|
|
return { patient: { equals: user.id } }
|
|
},
|
|
delete: ({ req: { user } }) => user?.role === 'admin',
|
|
},
|
|
hooks: {
|
|
beforeChange: [
|
|
async ({ data, operation, originalDoc, req }) => {
|
|
const patientRequest = Boolean(req.user) && !isAdminUser(req.user)
|
|
if (operation === 'create' && patientRequest) {
|
|
// Patients book only for themselves and cannot choose the price.
|
|
data.patient = req.user!.id
|
|
const serviceId = typeof data.service === 'object' && data.service ? data.service.id : data.service
|
|
const service = serviceId
|
|
? await req.payload.findByID({ collection: 'services', id: serviceId, depth: 0, req }).catch(() => null)
|
|
: null
|
|
if (!service) throw new Error('Услуга не найдена')
|
|
data.price = Number((service as { price?: unknown }).price) || 0
|
|
data.status = 'pending'
|
|
data.paymentStatus = 'unpaid'
|
|
}
|
|
|
|
if (operation === 'create') {
|
|
data.status = data.status || 'pending'
|
|
data.holdExpiresAt = new Date(Date.now() + BOOKING_HOLD_MS).toISOString()
|
|
data.paymentStatus = data.paymentStatus || 'unpaid'
|
|
}
|
|
|
|
// Patient cancel / reschedule
|
|
if (operation === 'update' && data.status === 'cancelled' && originalDoc?.status !== 'cancelled') {
|
|
data.cancelledAt = new Date().toISOString()
|
|
}
|
|
|
|
return data
|
|
},
|
|
],
|
|
},
|
|
fields: [
|
|
{
|
|
name: 'patient',
|
|
type: 'relationship',
|
|
relationTo: 'users',
|
|
required: true,
|
|
access: { update: adminOnlyField },
|
|
},
|
|
{
|
|
name: 'doctor',
|
|
type: 'relationship',
|
|
relationTo: 'doctors',
|
|
required: true,
|
|
},
|
|
{
|
|
name: 'service',
|
|
type: 'relationship',
|
|
relationTo: 'services',
|
|
required: true,
|
|
},
|
|
{
|
|
name: 'branch',
|
|
type: 'relationship',
|
|
relationTo: 'branches',
|
|
required: true,
|
|
},
|
|
{ name: 'startsAt', type: 'date', required: true, admin: { date: { pickerAppearance: 'dayAndTime' } } },
|
|
{ name: 'endsAt', type: 'date', required: true, admin: { date: { pickerAppearance: 'dayAndTime' } } },
|
|
{
|
|
name: 'status',
|
|
type: 'select',
|
|
required: true,
|
|
defaultValue: 'pending',
|
|
access: {
|
|
// Patients may only cancel their booking; other transitions are for staff / payment flow.
|
|
update: ({ req: { user }, data }) => isAdminUser(user) || data?.status === 'cancelled',
|
|
},
|
|
options: [
|
|
{ label: 'Ожидает подтверждения (бронь до 24ч)', value: 'pending' },
|
|
{ label: 'Подтверждена', value: 'confirmed' },
|
|
{ label: 'Отменена', value: 'cancelled' },
|
|
{ label: 'Завершена', value: 'completed' },
|
|
{ label: 'Истекла (не подтверждена)', value: 'expired' },
|
|
],
|
|
},
|
|
{
|
|
name: 'paymentStatus',
|
|
type: 'select',
|
|
defaultValue: 'unpaid',
|
|
access: { create: adminOnlyField, update: adminOnlyField },
|
|
options: [
|
|
{ label: 'Не оплачено', value: 'unpaid' },
|
|
{ label: 'Ожидает оплаты', value: 'pending' },
|
|
{ label: 'Оплачено', value: 'paid' },
|
|
{ label: 'Ошибка оплаты', value: 'failed' },
|
|
{ label: 'Возврат', value: 'refunded' },
|
|
],
|
|
},
|
|
{ name: 'price', type: 'number', required: true, access: { update: adminOnlyField } },
|
|
{ name: 'holdExpiresAt', type: 'date', admin: { readOnly: true }, access: { update: adminOnlyField } },
|
|
{ name: 'confirmedAt', type: 'date', access: { create: adminOnlyField, update: adminOnlyField } },
|
|
{ name: 'cancelledAt', type: 'date' },
|
|
{ name: 'cancelReason', type: 'text' },
|
|
{ name: 'rescheduledFrom', type: 'relationship', relationTo: 'bookings' },
|
|
{ name: 'patientComment', type: 'textarea' },
|
|
{
|
|
name: 'smsReminderScheduled',
|
|
type: 'checkbox',
|
|
defaultValue: false,
|
|
access: { create: adminOnlyField, update: adminOnlyField },
|
|
admin: { description: 'Флаг для будущей SMS-рассылки напоминаний' },
|
|
},
|
|
],
|
|
timestamps: true,
|
|
}
|