teqo-backup/src/lib/schemas/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

121 lines
4.7 KiB
TypeScript

import { z } from 'zod'
import { resolveMunicipalityName } from '@/lib/municipalityNameAliases'
import { contactPhonesSchema } from '@/lib/schemas/contact'
import {
optionalPersistedEmail,
positiveRelationshipId,
trimmedNullableText,
} from '@/lib/schemas/primitives'
/**
* Refusal messages matched by exact string in the routes' `safeMessages` —
* named once (B32+/B37 contract): a reworded literal at either end silently
* collapses the refusal into the generic error.
*/
export const SUPPORTER_STAFF_MESSAGE =
'Somente a coordenação e a assessoria podem gerenciar apoiadores.'
export const SUPPORTER_UNSCOPED_COORDINATOR_MESSAGE =
'Somente o Coordenador Geral pode cadastrar apoiadores sem município.'
export const SUPPORTER_DUPLICATE_MESSAGE =
'Esta pessoa já está cadastrada como apoiador neste município.'
export const LEADER_SUPPORTER_ONLY_MESSAGE = 'Somente lideranças podem cadastrar contatos por aqui.'
export const SUPPORTER_IMPORT_CSV_UNREADABLE_MESSAGE =
'Não foi possível ler o CSV. Verifique o formato e tente novamente.'
export const SUPPORTER_IMPORT_CSV_EMPTY_MESSAGE = 'O CSV está vazio.'
export const SUPPORTER_IMPORT_BATCH_EMPTY_MESSAGE =
'O lote de importação não contém apoiadores válidos.'
/** Same contract for the two parameterized import refusals — builders, so the limit and the column policy are spelled once. */
export const supporterImportCsvTooManyRowsMessage = (maxRows: number): string =>
`O CSV excede o limite de ${maxRows} linhas.`
export const supporterImportCsvUnknownColumnsMessage = (unknownColumns: string[]): string =>
`Colunas não reconhecidas no CSV: ${unknownColumns
.map((column) => column.replace(/^__unknown_/, ''))
.join(', ')}. Use apenas nome, telefone, municipio e intencao.`
const supporterVoteIntentions = ['certo', 'tende_a_certo', 'indeciso', 'outro'] as const
export type SupporterVoteIntention = (typeof supporterVoteIntentions)[number]
export const isSupporterVoteIntention = (value: unknown): value is SupporterVoteIntention =>
typeof value === 'string' && (supporterVoteIntentions as readonly string[]).includes(value)
const supporterSources = ['import_csv', 'manual', 'lideranca', 'convite', 'evento'] as const
export type SupporterSource = (typeof supporterSources)[number]
export const isSupporterSource = (value: unknown): value is SupporterSource =>
typeof value === 'string' && (supporterSources as readonly string[]).includes(value)
export const resolveBahiaMunicipality = resolveMunicipalityName
const optionalBahiaCity = z
.union([z.string().trim(), z.literal(''), z.null()])
.optional()
.transform((value, context) => {
if (value === undefined || value === null || value === '') return undefined
const city = resolveBahiaMunicipality(value)
if (!city) {
context.addIssue({
code: 'custom',
message: 'Município não reconhecido na Bahia.',
})
return z.NEVER
}
return city
})
export const supporterCreateSchema = z
.object({
name: z.string().trim().min(2).max(120),
phones: contactPhonesSchema.min(1, 'Informe ao menos um celular.'),
email: optionalPersistedEmail,
city: optionalBahiaCity,
municipality: positiveRelationshipId.optional(),
voteIntention: z.enum(supporterVoteIntentions).optional(),
consentAccepted: z.literal(true, {
error: 'É necessário aceitar o consentimento de cadastro.',
}),
voteIntentionConsentAccepted: z.boolean().optional(),
})
.superRefine((data, context) => {
if (data.voteIntention && !data.voteIntentionConsentAccepted) {
context.addIssue({
code: 'custom',
path: ['voteIntentionConsentAccepted'],
message: 'É necessário aceitar o consentimento de intenção de voto.',
})
}
})
export const supporterVoteIntentionSchema = z.object({
id: positiveRelationshipId,
voteIntention: z.enum(supporterVoteIntentions),
voteIntentionConsentAccepted: z.literal(true, {
error: 'É necessário aceitar o consentimento de intenção de voto.',
}),
})
export const supporterImportConfirmSchema = z.object({
operatorAttested: z.literal(true, {
error: 'É necessário atestar o consentimento dos apoiadores importados.',
}),
consentNote: trimmedNullableText(2000),
importToken: z.string().min(1, { error: 'Token de importação ausente.' }),
})
export const leaderSupporterCreateSchema = z.object({
name: z.string().trim().min(2).max(120),
phones: contactPhonesSchema.min(1, 'Informe ao menos um celular.'),
city: optionalBahiaCity,
municipality: positiveRelationshipId,
consentAccepted: z.literal(true, {
error: 'É necessário aceitar o consentimento de cadastro.',
}),
})
export const supporterRemoveSchema = z.object({
id: positiveRelationshipId,
})