teqo/src/collections/Organization.ts
Francisco Ignácio Fontoura Solla e2724c10da Remodel the campaign vertical from Praça to Município
Rename the operational unit "Praça"/"Núcleo" to "Município" (code identifier
`municipality`) and land the 2026-07-23 course changes:

- New `candidate` role with unrestricted visibility (`isCampaignStaff` now
  includes it) alongside coordinator/advisor; leader is locked down to the
  supporter contact tool only.
- Demandas become staff-only; dobradinhas modeled via a `stateDeputy` entity.
- Camaçari consolidated to a single município (ZE 170/171 aggregated);
  Salvador keeps its 19 zone municípios.
- Map moves to the staff dashboard; `/campanha/municipios` is list/overview.
- Vote pledges keep the declared vs estimated (scenario) asymmetry; leaders
  never see estimates.

Destructive migration for the campaign vertical (drops nucleus/plaza-era
tables, wipes campaign rows, seeds 435 municípios). Public-site data untouched.

Includes the post-delivery /simplify cleanups: shared reloadStaffActor /
reloadCoordinatorActor helpers, req.context cache for getFreshCampaignUser,
parallelized dashboard loads, candidate dashboard-copy fix, and dead
leader-dashboard / roleKind code removal.
2026-07-23 21:36:20 -03:00

135 lines
3.4 KiB
TypeScript

import type { CollectionBeforeValidateHook, CollectionConfig } from 'payload'
import { APIError } from 'payload'
import {
canCreateOrganization,
canDeleteOrganization,
canManageOrganization,
canReadOrganization,
canSetCampaignSystemField,
} from '@/utilities/campaignAccess'
import { slugify } from '@/utilities/slug'
const trimmedText = (value: unknown): string => (typeof value === 'string' ? value.trim() : '')
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 Planos de Ação apoiados.',
},
access: {
create: canCreateOrganization,
read: canReadOrganization,
update: canManageOrganization,
delete: canDeleteOrganization,
},
hooks: {
beforeValidate: [setCanonicalOrganizationSlug],
beforeChange: [
({ data, operation, req }) => {
if (operation === 'create' && req.user?.collection === 'campaignUser') {
data.createdBy = req.user.id
}
return data
},
],
},
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,
},
{
name: 'createdBy',
type: 'relationship',
relationTo: 'campaignUser',
label: 'Criado por',
index: true,
admin: {
readOnly: true,
},
access: {
create: canSetCampaignSystemField,
update: canSetCampaignSystemField,
},
},
],
}