import type { Access, CollectionConfig } from 'payload' import { ADMIN_GROUP } from '../admin/groups' export const Documents: CollectionConfig = { slug: 'documents', labels: { singular: 'Документ', plural: 'Документы' }, admin: { group: ADMIN_GROUP.cabinet, useAsTitle: 'title', defaultColumns: ['title', 'type', 'isPublic', 'patient', 'issuedAt'], description: 'Документы пациентов и публичные файлы. Публичные документы без пациента показываются в «Полезное → Документы».', }, access: { read: (({ req: { user } }) => { if (user?.role === 'admin') return true if (!user) return { isPublic: { equals: true } } return { or: [{ patient: { equals: user.id } }, { isPublic: { equals: true } }], } }) as Access, create: ({ req: { user } }) => user?.role === 'admin', update: ({ req: { user } }) => user?.role === 'admin', delete: ({ req: { user } }) => user?.role === 'admin', }, fields: [ { name: 'title', type: 'text', required: true, localized: true, label: 'Название' }, { name: 'isPublic', type: 'checkbox', defaultValue: false, label: 'Публичный документ', admin: { description: 'Показывать в подвале: Полезное → Документы. Пациент не нужен.', }, }, { name: 'patient', type: 'relationship', relationTo: 'users', label: 'Пациент', admin: { condition: (data) => !data?.isPublic, }, validate: (value: unknown, { data }: { data?: { isPublic?: boolean | null } }) => { if (data?.isPublic) return true if (!value) return 'Укажите пациента или отметьте документ как публичный' return true }, }, { name: 'type', type: 'select', required: true, label: 'Тип', options: [ { label: 'Договор', value: 'contract' }, { label: 'Результат анализа', value: 'lab_result' }, { label: 'Заключение', value: 'report' }, { label: 'Чек', value: 'receipt' }, { label: 'Справка для налогового вычета', value: 'tax_deduction' }, { label: 'Прочее', value: 'other' }, ], }, { name: 'file', type: 'upload', relationTo: 'media', required: true, label: 'Файл' }, { name: 'booking', type: 'relationship', relationTo: 'bookings', label: 'Запись' }, { name: 'issuedAt', type: 'date', required: true, label: 'Дата' }, ], }