mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-14 08:53:11 +00:00
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.
114 lines
3 KiB
TypeScript
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 }),
|
|
],
|
|
}
|