import type { Config, Plugin } from 'payload' /** * Test fixture, on only with E2E_LATE_STAFF=1 (e2e/late-staff.spec.ts): a plugin listed AFTER ipalKit that adds * a staff collection (`late-staff`, named in `access.staffCollections`) and a visitor collection with open * sign-up. ipalKit has already run when these collections appear, so it must give them its account rules when * the config goes live — and it must do so in a BUILT site too, where bundlers rename every function. * Never part of a real site: the starter drops `e2e/` and the lines that use this file. */ export const E2E_LATE_STAFF = process.env.E2E_LATE_STAFF === '1' /** Slugs the fixture names as staff (payload.config.ts → `access.staffCollections`). */ export const E2E_STAFF_SLUGS = ['late-staff', 'e2e-crew', 'e2e-late-text', 'e2e-staff', 'e2e-team', 'e2e-copy', 'e2e-copy-own-rules', 'e2e-copy-cross-op', 'e2e-copy-values', 'e2e-copy-team-admin', 'e2e-copy-opt-out'] /** * Listed BEFORE ipalKit: a staff collection of the project's own, without rules, whose roles are the plugin's values * PLUS one more. `admin` on an account of it makes a plugin admin, so its accounts must not hand that value out. */ export const e2eEarlyStaff: Plugin = (config: Config) => E2E_LATE_STAFF ? { ...config, collections: [ ...(config.collections ?? []), { slug: 'e2e-crew', auth: true, fields: [{ name: 'roles', type: 'select', hasMany: true, options: ['user', 'editor', 'admin', 'boss'], saveToJWT: true }] }, // the two originals the later plugin copies: the plugin's roles, and roles of the project's own { slug: 'e2e-staff', auth: true, fields: [{ name: 'roles', type: 'select', hasMany: true, options: ['user', 'editor', 'admin'], saveToJWT: true }] }, { slug: 'e2e-team', auth: true, fields: [{ name: 'roles', type: 'select', hasMany: true, options: ['lead', 'member'], saveToJWT: true }] }, ], } : config /** * Copies of staff collections under new slugs — the shapes in which a rule could become WIDER when the plugin * rewrote copied rules. It never replaces a rule now: its rule for the copy's slug goes in front of what came along. */ const copies = (config: Config): NonNullable => { const of = (slug: string) => (config.collections ?? []).find((c) => c.slug === slug)! const staff = of('e2e-staff') const roles = (options: string[]) => [{ name: 'roles', type: 'select' as const, hasMany: true as const, options, saveToJWT: true }] return [ { ...staff, slug: 'e2e-copy' }, // plain: the original's namesake is not "the account itself" here { ...staff, slug: 'e2e-copy-own-rules', access: { ...staff.access, create: () => false, delete: () => false, read: () => false, update: () => false } }, // "nobody" stays "nobody" { ...staff, slug: 'e2e-copy-cross-op', access: { ...staff.access, read: staff.access?.create, update: staff.access?.create } }, // the admin-only create rule used for read and update { ...staff, slug: 'e2e-copy-values', fields: roles(['user', 'editor', 'admin', 'boss']) }, // one more role value { ...of('e2e-team'), slug: 'e2e-copy-team-admin', fields: roles(['user', 'editor', 'admin']) }, // "an account of the collection" copied onto a collection with `admin` { ...staff, slug: 'e2e-copy-opt-out', custom: { ...staff.custom, ipal: { accountRules: false } }, fields: roles(['user', 'editor', 'admin']) }, // keeps its own rules, brings its own roles field { ...staff, slug: 'e2e-copy-not-staff' }, // not named as a staff collection ] } export const e2eLateStaff: Plugin = (config: Config) => E2E_LATE_STAFF ? { ...config, collections: [ ...(config.collections ?? []), ...copies(config), // no access rules at all: Payload's defaults ("any logged-in user") are what ipalKit must replace { slug: 'late-staff', auth: true, fields: [{ name: 'roles', type: 'select', hasMany: true, options: ['user', 'editor', 'admin'], saveToJWT: true }], versions: true }, // a late staff collection whose `roles` is a text field: any value can be written there, `admin` included { slug: 'e2e-late-text', auth: true, fields: [{ name: 'roles', type: 'text', hasMany: true }] }, { slug: 'e2e-visitors', access: { create: () => true }, auth: true, fields: [] }, ], } : config