teqo-backup/src/app/(campaign)/campanha/actions/supporter.ts
Francisco Solla 6de8fad8f6 feat(C112): múltiplos telefones por pessoa — Contact.phones array (ordem = prioridade)
- Contact.phone (text único) → phones (array field Payload, subcampo value):
  fonte única; principal = phones[0]; beforeValidate normaliza, filtra vazios,
  rejeita inválido e duplicado DENTRO da ficha (C111 entre fichas preservado)
- migration add_contact_phones_array: backfill phone → phones[0] (gen_random_uuid)
  antes do drop da coluna + índice em value (hand-edit documentado; snapshot .json
  gerado mantém as colunas C99/C100 já existentes)
- primaryPhoneOf / phoneValuesOf / reorderWithPrimaryPhone em src/lib/phone.ts
  (ordem = prioridade; inline edit = seta o principal preservando o resto)
- findOrCreateContactByPhone aceita phones[] (dedupe pelo primário; reuse faz
  append dos faltantes; locks em todos os números digitados)
- sync conta→ficha (CampaignUser): phone da conta faz APPEND quando a ficha não
  tem (nunca reordena nem remove); tombstone LGPD substitui TODOS os telefones
- convite redemption: telefone digitado vira principal, demais preservados
- import CSV: preview sinaliza compartilhado também por número secundário;
  bulk insere contact_phones (JOIN nas leituras, uuid no insert)
- busca por telefone (apoiadores, pickers, combobox) casa qualquer número
- UI: PhonesFieldEditor compartilhado (repetível mascarado, remover, ↑/↓,
  modo inline com Salvar) nas 5 superfícies — liderança (form/ficha/lista),
  apoiador (form/ficha), dobradinha (ficha), assessor (ficha), wizard
- UI pública (petição/WhatsApp) intocada: zod e forms ficam com phone;
  as ações mapeiam phone → phones[0]
- testes: spec novo campaignMultiPhones (aceite completo), specs C111/C99
  atualizados, e2e wizard/apoiador ajustados
2026-08-11 10:47:02 -03:00

384 lines
12 KiB
TypeScript

'use server'
import { revalidatePath } from 'next/cache'
import type { Payload } from 'payload'
import { relationshipId, requireRelationshipId } from '@/lib/relationship'
import { contactFieldUpdateSchema, type ContactFieldUpdateInput } from '@/lib/schemas/contact'
import {
SUPPORTER_DUPLICATE_MESSAGE,
SUPPORTER_STAFF_MESSAGE,
SUPPORTER_UNSCOPED_COORDINATOR_MESSAGE,
supporterCreateSchema,
supporterRemoveSchema,
supporterVoteIntentionSchema,
} from '@/lib/schemas/supporter'
import type { CampaignUser } from '@/payload-types'
import { getCampaignActionContext, reloadStaffActor } from '@/utilities/campaignActionContext'
import {
requireSupporterRegistrationConsent,
requireSupporterVoteIntentionConsent,
type ConsentDescriptor,
} from '@/utilities/campaignConsent'
import { findOrCreateContactByPhone } from '@/utilities/contactIdentity'
import { acquireContactPhoneLocks } from '@/utilities/contactPhoneLocks'
import type { PayloadTransactionRequest } from '@/utilities/payloadTransaction'
import { withPayloadTransaction } from '@/utilities/payloadTransaction'
import { isUniqueSupporterConflict } from '@/utilities/supporter/supporterErrors'
import {
confirmSupporterImport as confirmSupporterImportAction,
confirmSupporterImportRecord as confirmSupporterImportRecordAction,
previewSupporterImport as previewSupporterImportAction,
previewSupporterImportText as previewSupporterImportTextAction,
} from './supporterImport'
const getFreshStaffActor = (
payload: Payload,
actor: CampaignUser,
req?: PayloadTransactionRequest,
): Promise<CampaignUser> => reloadStaffActor(payload, actor, SUPPORTER_STAFF_MESSAGE, req)
const INVALID_FIELD_MESSAGE = 'Campo inválido.'
const assertMunicipalityManagement = async (
payload: Payload,
actor: CampaignUser,
municipalityID: number,
req?: PayloadTransactionRequest,
) =>
payload.findByID({
collection: 'municipality',
id: municipalityID,
depth: 0,
user: actor,
overrideAccess: false,
req,
})
const assertCanManageSupporter = async (
payload: Payload,
actor: CampaignUser,
supporterID: number,
req?: PayloadTransactionRequest,
) =>
payload.findByID({
collection: 'supporter',
id: supporterID,
depth: 0,
user: actor,
overrideAccess: false,
req,
})
const createValidatedSupporter = async (payload: Payload, actor: CampaignUser, input: unknown) => {
const data = supporterCreateSchema.parse(input)
try {
return await withPayloadTransaction(
payload,
async ({ req }) => {
const currentActor = await getFreshStaffActor(payload, actor, req)
if (data.municipality) {
await assertMunicipalityManagement(payload, currentActor, data.municipality, req)
} else if (currentActor.role !== 'coordinator') {
throw new Error(SUPPORTER_UNSCOPED_COORDINATOR_MESSAGE)
}
const registrationConsent = await requireSupporterRegistrationConsent(payload, req)
let voteIntentionConsent: ConsentDescriptor | null = null
if (data.voteIntention) {
voteIntentionConsent = await requireSupporterVoteIntentionConsent(payload, req)
}
const { contactID, reused } = await findOrCreateContactByPhone({
payload,
req,
phones: data.phones,
name: data.name,
email: data.email,
city: data.city,
})
const supporter = await payload.create({
collection: 'supporter',
data: {
contact: contactID,
municipality: data.municipality,
voteIntention: data.voteIntention,
source: 'manual',
consent: registrationConsent.id,
consentContentHash: registrationConsent.contentHash,
consentedAt: new Date().toISOString(),
...(voteIntentionConsent && data.voteIntention
? {
voteIntentionConsent: voteIntentionConsent.id,
voteIntentionConsentContentHash: voteIntentionConsent.contentHash,
voteIntentionConsentedAt: new Date().toISOString(),
}
: {}),
createdBy: currentActor.id,
},
depth: 0,
overrideAccess: true,
req,
})
return { ...supporter, contactReused: reused }
},
{ beginFailureMessage: 'Não foi possível iniciar a transação de cadastro do apoiador.' },
)
} catch (error) {
if (isUniqueSupporterConflict(error)) {
throw new Error(SUPPORTER_DUPLICATE_MESSAGE)
}
throw error
}
}
export const createSupporterRecord = async (
payload: Payload,
actor: CampaignUser,
input: unknown,
) => createValidatedSupporter(payload, actor, input)
export const createSupporter = async (input: unknown) => {
const { payload, actor } = await getCampaignActionContext()
return createValidatedSupporter(payload, actor, input)
}
export const setSupporterVoteIntentionRecord = async (
payload: Payload,
actor: CampaignUser,
input: unknown,
) => {
const data = supporterVoteIntentionSchema.parse(input)
return withPayloadTransaction(
payload,
async ({ req }) => {
const currentActor = await getFreshStaffActor(payload, actor, req)
await assertCanManageSupporter(payload, currentActor, data.id, req)
const voteIntentionConsent = await requireSupporterVoteIntentionConsent(payload, req)
return payload.update({
collection: 'supporter',
id: data.id,
data: {
voteIntention: data.voteIntention,
voteIntentionConsent: voteIntentionConsent.id,
voteIntentionConsentContentHash: voteIntentionConsent.contentHash,
voteIntentionConsentedAt: new Date().toISOString(),
},
depth: 0,
overrideAccess: true,
req,
})
},
{ beginFailureMessage: 'Não foi possível iniciar a transação de intenção de voto.' },
)
}
export const setSupporterVoteIntention = async (input: unknown) => {
const { payload, actor } = await getCampaignActionContext()
return setSupporterVoteIntentionRecord(payload, actor, input)
}
/**
* C112 — the supporter ficha's phone list editor. Staff only (same supporter
* access gate as every supporter write); only the `phones` branch of the
* shared per-field schema is honored.
*/
export const updateSupporterContactRecord = async (
payload: Payload,
actor: CampaignUser,
input: ContactFieldUpdateInput,
) => {
const data = contactFieldUpdateSchema.parse(input)
return withPayloadTransaction(
payload,
async ({ req }) => {
const currentActor = await getFreshStaffActor(payload, actor, req)
const supporter = await assertCanManageSupporter(payload, currentActor, data.id, req)
const contactID = relationshipId(supporter.contact)
if (contactID === null) throw new Error(SUPPORTER_STAFF_MESSAGE)
if (data.field !== 'phones') {
throw new Error(INVALID_FIELD_MESSAGE)
}
// bypass: the supporter row access above established staff scope.
await payload.update({
collection: 'contact',
id: contactID,
data: { phones: data.phones.map((value) => ({ value })) },
depth: 0,
overrideAccess: true,
req,
})
return supporter
},
{ beginFailureMessage: 'Não foi possível atualizar o contato do apoiador.' },
)
}
export const updateSupporterContact = async (input: ContactFieldUpdateInput) => {
const { payload, actor } = await getCampaignActionContext()
const supporter = await updateSupporterContactRecord(payload, actor, input)
revalidatePath(`/campanha/apoiadores/${supporter.id}`, 'page')
return supporter
}
// ---------------------------------------------------------------------------
// CSV import pipeline — implementation lives in ./supporterImport. A top-level
// `'use server'` module cannot re-export values (only async functions defined
// here), so these async wrappers keep the import sites of this module stable.
// ---------------------------------------------------------------------------
export const previewSupporterImportText = async (
payload: Payload,
actor: CampaignUser,
csvText: string,
) => previewSupporterImportTextAction(payload, actor, csvText)
export const previewSupporterImport = async (csvText: string) =>
previewSupporterImportAction(csvText)
export const confirmSupporterImportRecord = async (
payload: Payload,
actor: CampaignUser,
input: unknown,
) => confirmSupporterImportRecordAction(payload, actor, input)
export const confirmSupporterImport = async (input: unknown) => confirmSupporterImportAction(input)
const contactHasOtherJoins = async (
payload: Payload,
contactID: number,
excludeSupporterID: number,
req?: PayloadTransactionRequest,
): Promise<boolean> => {
const [leaderships, signatures, subscriptions, supporters] = await Promise.all([
payload.find({
collection: 'leadership',
where: { contact: { equals: contactID } },
depth: 0,
limit: 1,
pagination: false,
overrideAccess: true,
req,
}),
payload.find({
collection: 'signature',
where: { contact: { equals: contactID } },
depth: 0,
limit: 1,
pagination: false,
overrideAccess: true,
req,
}),
payload.find({
collection: 'subscription',
where: { contact: { equals: contactID } },
depth: 0,
limit: 1,
pagination: false,
overrideAccess: true,
req,
}),
payload.find({
collection: 'supporter',
where: {
and: [{ contact: { equals: contactID } }, { id: { not_equals: excludeSupporterID } }],
},
depth: 0,
limit: 1,
pagination: false,
overrideAccess: true,
req,
}),
])
return (
leaderships.totalDocs > 0 ||
signatures.totalDocs > 0 ||
subscriptions.totalDocs > 0 ||
supporters.totalDocs > 0
)
}
const anonymizeContact = async (
payload: Payload,
contactID: number,
req: PayloadTransactionRequest,
) => {
// Must match Contact phone validation: DDD + 9 + 8 digits (`^[1-9]{2}9\d{8}$`).
const tombstonePhone = `999${String(contactID).padStart(8, '0')}`
await acquireContactPhoneLocks(payload, req, [tombstonePhone])
await payload.update({
collection: 'contact',
id: contactID,
data: {
name: 'Titular removido',
email: null,
// The tombstone REPLACES every number — the remaining phones are the
// person's PII and must not survive the anonymization (C112).
phones: [{ value: tombstonePhone }],
gender: null,
city: null,
postalCode: null,
},
depth: 0,
overrideAccess: true,
req,
})
}
export const removeSupporterDataRecord = async (
payload: Payload,
actor: CampaignUser,
input: unknown,
) => {
const data = supporterRemoveSchema.parse(input)
return withPayloadTransaction(
payload,
async ({ req }) => {
const currentActor = await getFreshStaffActor(payload, actor, req)
const supporter = await assertCanManageSupporter(payload, currentActor, data.id, req)
const contactID = requireRelationshipId(supporter.contact)
// Intentional admin bypass (LGPD titular-rights path): `canDeleteSupporter`
// is admin-only by design, but THIS action is the staff-driven data-removal
// flow — the actor's manage scope was already proven by
// `assertCanManageSupporter` above, and the row being deleted is exactly the
// one that scope check loaded. The asymmetry is deliberate: day-to-day
// deletes stay admin-only while the documented removal request path works.
await payload.delete({
collection: 'supporter',
id: data.id,
overrideAccess: true,
req,
})
const hasOtherJoins = await contactHasOtherJoins(payload, contactID, data.id, req)
if (!hasOtherJoins) {
await anonymizeContact(payload, contactID, req)
}
return { removed: true, contactAnonymized: !hasOtherJoins }
},
{ beginFailureMessage: 'Não foi possível iniciar a transação de remoção de dados.' },
)
}
export const removeSupporterData = async (input: unknown) => {
const { payload, actor } = await getCampaignActionContext()
return removeSupporterDataRecord(payload, actor, input)
}