import type { GlobalConfig } from 'payload' import type { CookieScanLabels } from '../labels.js' import { userMeetsRole } from '../../../access/predicates.js' import { labelFor, labelRecord } from '../labels.js' import { COOKIE_CRAWL_SLUG } from './config.js' /** * The latest crawl only (no history): status, counts, violations, third-party * hosts, and the e-mail recipients for warnings (the only editable field — * opt-in per site). Written by the crawl endpoints through the Local API. */ export function buildCookieCrawlGlobal(labels: Record): GlobalConfig { const L = (key: keyof CookieScanLabels) => labelRecord(labels, key) const ro = { readOnly: true } return { slug: COOKIE_CRAWL_SLUG, access: { read: ({ req }) => userMeetsRole(req, 'editor'), update: ({ req }) => userMeetsRole(req, 'admin'), }, admin: { description: L('crawlGlobalDescription'), group: { en: 'Privacy', pl: 'Prywatność' } }, fields: [ { name: 'notifyEmails', type: 'text', admin: { description: L('notifyEmailsDescription') }, label: L('notifyEmails'), validate: (value: null | string | undefined, { req }: { req: { i18n?: { language?: string } } }) => { const list = (value ?? '').split(',').map((v) => v.trim()).filter(Boolean) return list.every((v) => /^[^\s@]+@[^\s@][^\s.@]*\.[^\s@]+$/.test(v)) || labelFor(labels, req?.i18n?.language, 'notifyEmailsInvalid') }, }, { name: 'status', type: 'select', admin: { ...ro, position: 'sidebar' }, defaultValue: 'idle', label: L('crawlStatus'), options: [ { label: L('crawlIdle'), value: 'idle' }, { label: L('crawlStateRunning'), value: 'running' }, { label: L('crawlStateDone'), value: 'done' }, { label: L('crawlStateFailed'), value: 'failed' }, ], }, { name: 'startedAt', type: 'date', admin: { ...ro, date: { pickerAppearance: 'dayAndTime' }, position: 'sidebar' }, label: L('startedAt') }, { name: 'finishedAt', type: 'date', admin: { ...ro, date: { pickerAppearance: 'dayAndTime' }, position: 'sidebar' }, label: L('finishedAt') }, { name: 'startedBy', type: 'text', admin: { ...ro, position: 'sidebar' }, label: L('startedBy') }, { name: 'error', type: 'textarea', admin: ro, label: L('crawlError') }, { name: 'summary', type: 'group', admin: ro, fields: [ { name: 'pages', type: 'number', admin: ro, label: L('crawlPages') }, { name: 'violations', type: 'number', admin: ro, label: L('crawlViolations') }, { name: 'newUnknown', type: 'number', admin: ro, label: L('crawlNewUnknown') }, { name: 'created', type: 'number', admin: ro, label: L('crawlCreated') }, { name: 'updated', type: 'number', admin: ro, label: L('crawlUpdated') }, ], label: false, }, { name: 'violations', type: 'json', admin: ro, label: L('crawlViolations') }, { name: 'requests', type: 'json', admin: ro, label: L('crawlRequests') }, { name: 'pages', type: 'json', admin: ro, label: L('crawlPages') }, // One-time id of the running crawl; a result with another id is refused. { name: 'jobId', type: 'text', admin: { hidden: true } }, ], label: L('crawlGlobal'), } }