teqo-backup/src/collections/Organization.ts
Francisco Ignácio Fontoura Solla 4806c417c8 Engineering Pass 3: consolidate patterns and lock them with guards.
Fold duplicated campanha access, loaders, list/filter/form shells, and scripts into shared modules; reorganize utilities by domain; stabilize e2e; and document prevention-first audit workflow so the same smells cannot creep back in.
2026-07-29 06:11:29 -03:00

114 lines
3 KiB
TypeScript

import type { CollectionBeforeValidateHook, CollectionConfig } from 'payload'
import { APIError } from 'payload'
import { slugify } from '@/lib/slug'
import { trimmedText } from '@/lib/text'
import {
canCreateOrganization,
canDeleteOrganization,
canManageOrganization,
canReadOrganization,
canSetCampaignSystemField,
} from '@/utilities/campaignAccess'
import { stampCampaignCreatedBy, systemStampedActorField } from '@/utilities/campaignAuditFields'
const setCanonicalOrganizationSlug: CollectionBeforeValidateHook = ({
data,
operation,
originalDoc,
}) => {
if (!data) return data
const name = trimmedText(data.name ?? originalDoc?.name)
const slug = slugify(name)
if (!slug) {
throw new APIError('Informe um nome com letras ou números.', 400)
}
if (operation === 'update' && data.name !== undefined && name !== originalDoc?.name) {
throw new APIError('O nome da organização não pode ser alterado após a criação.', 409)
}
data.name = name
data.slug = operation === 'create' ? slug : originalDoc?.slug
return data
}
export const Organization: CollectionConfig = {
slug: 'organization',
labels: {
singular: 'Organização',
plural: 'Organizações',
},
admin: {
group: 'Campanha',
useAsTitle: 'name',
defaultColumns: ['name', 'kind', 'updatedAt'],
description:
'Sindicatos, associações, movimentos e afins. Concentra lideranças associadas e atividades apoiadas.',
},
access: {
create: canCreateOrganization,
read: canReadOrganization,
update: canManageOrganization,
delete: canDeleteOrganization,
},
hooks: {
beforeValidate: [setCanonicalOrganizationSlug],
beforeChange: [stampCampaignCreatedBy],
},
fields: [
{
name: 'name',
type: 'text',
label: 'Nome',
required: true,
minLength: 2,
maxLength: 160,
unique: true,
index: true,
},
{
name: 'slug',
type: 'text',
label: 'Slug',
required: true,
unique: true,
index: true,
admin: {
readOnly: true,
},
access: {
create: canSetCampaignSystemField,
update: canSetCampaignSystemField,
},
},
{
name: 'kind',
type: 'select',
label: 'Tipo',
required: true,
index: true,
options: [
{ label: 'Sindicato', value: 'sindicato' },
{ label: 'Associação', value: 'associacao' },
{ label: 'Religiosa', value: 'religioso' },
{ label: 'Movimento', value: 'movimento' },
{ label: 'Categoria profissional', value: 'categoria_profissional' },
{ label: 'Outro', value: 'outro' },
],
},
{
name: 'municipalities',
type: 'relationship',
relationTo: 'municipality',
label: 'Municípios de atuação',
hasMany: true,
index: true,
},
{
name: 'notes',
type: 'textarea',
label: 'Observações',
maxLength: 4000,
},
systemStampedActorField({ setAccess: canSetCampaignSystemField }),
],
}