teqo-backup/src/collections/Leadership.ts
Francisco Ignácio Fontoura Solla cf13c441aa Remodel /campanha around predefined Praças replacing electoral nuclei.
436 seeded Praças (municipality-based; per-TSE-zone in Salvador/Camaçari) become
the operational unit; roles renamed to coordinator/advisor/leader with advisors
scoped to administered Praças. Leadership is one record per person across many
Praças/organizations; vote pledges split leader-declared vs staff-estimated
(estimates never serialized to leaders). Adds organizations, demand workflow
with staff-only cost/receipts, action-plan results with deputy presence, manual
political trend, multi-candidate comparison and the diverging red/white/blue
map mode. Migration 20260721_020109_remodel_plazas deliberately resets the
campaign vertical in production (public site untouched); test suite ported to
the new model (unit 187 / int 306 / e2e 9 green).
2026-07-21 02:28:00 -03:00

226 lines
5.7 KiB
TypeScript

import type { CollectionBeforeValidateHook, CollectionConfig } from 'payload'
import { APIError } from 'payload'
import {
canCreateLeadership,
canDeleteLeadership,
canManageCampaignStaffField,
canManageLeadership,
canReadCampaignStaffField,
canReadLeadership,
canSetAdministrativeLeadershipField,
} from '@/utilities/campaignAccess'
import { relationshipId } from '@/utilities/relationship'
const MAX_LEADERSHIP_PLAZAS = 30
const requireAtLeastOnePlaza: CollectionBeforeValidateHook = ({ data, operation, originalDoc }) => {
if (!data) return data
const plazas =
data.plazas !== undefined
? data.plazas
: operation === 'update'
? originalDoc?.plazas
: undefined
const plazaIDs = (Array.isArray(plazas) ? plazas : [])
.map(relationshipId)
.filter((id): id is number => id !== null)
if (plazaIDs.length === 0) {
throw new APIError('Vincule a liderança a pelo menos uma Praça.', 400)
}
if (new Set(plazaIDs).size !== plazaIDs.length) {
throw new APIError('Cada Praça deve aparecer apenas uma vez.', 400)
}
return data
}
export const Leadership: CollectionConfig = {
slug: 'leadership',
labels: {
singular: 'Liderança',
plural: 'Lideranças',
},
admin: {
group: 'Campanha',
useAsTitle: 'contact',
defaultColumns: ['contact', 'plazas', 'sector', 'supportStatus', 'updatedAt'],
},
access: {
create: canCreateLeadership,
read: canReadLeadership,
update: canManageLeadership,
delete: canDeleteLeadership,
},
hooks: {
beforeValidate: [requireAtLeastOnePlaza],
beforeChange: [
({ data, operation, req }) => {
if (operation === 'create' && req.user?.collection === 'campaignUser') {
data.createdBy = req.user.id
}
return data
},
],
},
fields: [
{
name: 'contact',
type: 'relationship',
relationTo: 'contact',
label: 'Contato',
required: true,
unique: true,
index: true,
access: {
update: canSetAdministrativeLeadershipField,
},
},
{
name: 'plazas',
type: 'relationship',
relationTo: 'plaza',
label: 'Praças',
required: true,
hasMany: true,
index: true,
maxRows: MAX_LEADERSHIP_PLAZAS,
},
{
name: 'organizations',
type: 'relationship',
relationTo: 'organization',
label: 'Organizações',
hasMany: true,
index: true,
},
{
name: 'sector',
type: 'select',
label: 'Setor',
index: true,
options: [
{ label: 'Religioso', value: 'religioso' },
{ label: 'Sindical', value: 'sindical' },
{ label: 'Comunitário', value: 'comunitario' },
{ label: 'Rural', value: 'rural' },
{ label: 'Empresarial', value: 'empresarial' },
{ label: 'Juventude', value: 'juventude' },
{ label: 'Saúde', value: 'saude' },
{ label: 'Educação', value: 'educacao' },
{ label: 'Cultura', value: 'cultura' },
{ label: 'Outro', value: 'outro' },
],
},
{
name: 'sectorNotes',
type: 'textarea',
label: 'Observações do setor',
maxLength: 1000,
},
{
name: 'supportStatus',
type: 'select',
label: 'Status de apoio',
required: true,
defaultValue: 'a_abordar',
index: true,
access: {
create: canManageCampaignStaffField,
read: canReadCampaignStaffField,
update: canManageCampaignStaffField,
},
options: [
{ label: 'Engajado', value: 'engajado' },
{ label: 'A abordar', value: 'a_abordar' },
{ label: 'Em disputa', value: 'em_disputa' },
{ label: 'Negativo', value: 'negativo' },
],
},
{
name: 'user',
type: 'relationship',
relationTo: 'campaignUser',
label: 'Acesso ao app',
index: true,
access: {
create: canSetAdministrativeLeadershipField,
update: canSetAdministrativeLeadershipField,
},
},
{
name: 'consent',
type: 'relationship',
relationTo: 'consent',
label: 'Consentimento confirmado',
access: {
create: canSetAdministrativeLeadershipField,
update: canSetAdministrativeLeadershipField,
},
},
{
name: 'consentContentHash',
type: 'text',
label: 'Hash do conteúdo consentido',
admin: {
readOnly: true,
},
access: {
create: canSetAdministrativeLeadershipField,
update: canSetAdministrativeLeadershipField,
},
},
{
name: 'consentedAt',
type: 'date',
label: 'Consentimento confirmado em',
admin: {
readOnly: true,
},
access: {
create: canSetAdministrativeLeadershipField,
update: canSetAdministrativeLeadershipField,
},
},
{
name: 'notes',
type: 'textarea',
label: 'Observações internas',
maxLength: 3000,
access: {
create: canManageCampaignStaffField,
read: canReadCampaignStaffField,
update: canManageCampaignStaffField,
},
},
{
name: 'consentNote',
type: 'textarea',
label: 'Registro de consentimento externo',
maxLength: 2000,
access: {
create: canManageCampaignStaffField,
read: canReadCampaignStaffField,
update: canManageCampaignStaffField,
},
},
{
name: 'createdBy',
type: 'relationship',
relationTo: 'campaignUser',
label: 'Criado por',
index: true,
admin: {
readOnly: true,
},
access: {
create: canSetAdministrativeLeadershipField,
read: canReadCampaignStaffField,
update: canSetAdministrativeLeadershipField,
},
},
],
}