Lock down access control and add image previews, file locations and cropping to the CMS.

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>
This commit is contained in:
Alexey Novikov
2026-11-11 16:23:05 +03:00
co-authored by Claude Opus 5.5
parent 959e693dcc
commit 0b447e67bf
30 changed files with 1290 additions and 34 deletions
+26 -4
View File
@@ -1,5 +1,6 @@
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
@@ -27,7 +28,21 @@ export const Bookings: CollectionConfig = {
},
hooks: {
beforeChange: [
async ({ data, operation, originalDoc }) => {
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()
@@ -49,6 +64,7 @@ export const Bookings: CollectionConfig = {
type: 'relationship',
relationTo: 'users',
required: true,
access: { update: adminOnlyField },
},
{
name: 'doctor',
@@ -75,6 +91,10 @@ export const Bookings: CollectionConfig = {
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' },
@@ -87,6 +107,7 @@ export const Bookings: CollectionConfig = {
name: 'paymentStatus',
type: 'select',
defaultValue: 'unpaid',
access: { create: adminOnlyField, update: adminOnlyField },
options: [
{ label: 'Не оплачено', value: 'unpaid' },
{ label: 'Ожидает оплаты', value: 'pending' },
@@ -95,9 +116,9 @@ export const Bookings: CollectionConfig = {
{ label: 'Возврат', value: 'refunded' },
],
},
{ name: 'price', type: 'number', required: true },
{ name: 'holdExpiresAt', type: 'date', admin: { readOnly: true } },
{ name: 'confirmedAt', type: 'date' },
{ 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' },
@@ -106,6 +127,7 @@ export const Bookings: CollectionConfig = {
name: 'smsReminderScheduled',
type: 'checkbox',
defaultValue: false,
access: { create: adminOnlyField, update: adminOnlyField },
admin: { description: 'Флаг для будущей SMS-рассылки напоминаний' },
},
],