mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-25 20:33:11 +00:00
- 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
179 lines
7 KiB
TypeScript
179 lines
7 KiB
TypeScript
import { z } from 'zod'
|
|
|
|
import { contactFieldUpdateSchema, contactPhonesSchema } from '@/lib/schemas/contact'
|
|
import {
|
|
optionalPersistedEmail,
|
|
positiveRelationshipId,
|
|
trimmedNullableText,
|
|
trimmedOptionalText,
|
|
} from '@/lib/schemas/primitives'
|
|
|
|
export const leadershipGenders = ['feminino', 'masculino', 'outro', 'nao_informado'] as const
|
|
|
|
export const leadershipSupportStatuses = [
|
|
'engajado',
|
|
'a_abordar',
|
|
'em_disputa',
|
|
'negativo',
|
|
] as const
|
|
|
|
export type SupportStatus = (typeof leadershipSupportStatuses)[number]
|
|
|
|
export const isSupportStatus = (value: unknown): value is SupportStatus =>
|
|
typeof value === 'string' && leadershipSupportStatuses.some((status) => status === value)
|
|
|
|
export const MAX_LEADERSHIP_MUNICIPALITIES = 30
|
|
const MAX_LEADERSHIP_ORGANIZATIONS = 20
|
|
export const MAX_LEADERSHIP_STATE_DEPUTIES = 20
|
|
export const MAX_LEADERSHIP_ADVISORS = 10
|
|
|
|
export const LEADERSHIP_MUNICIPALITY_FLOOR_MESSAGE =
|
|
'Vincule a liderança a pelo menos um município.'
|
|
|
|
export const LEADERSHIP_MUNICIPALITY_CAP_MESSAGE = `Cada liderança aceita no máximo ${MAX_LEADERSHIP_MUNICIPALITIES} municípios.`
|
|
|
|
/** Thrown by the chip toggle on both sides of the relation (B31/B36), so both routes allowlist it. */
|
|
export const LEADERSHIP_STATE_DEPUTIES_CAP_MESSAGE = `Cada liderança aceita no máximo ${MAX_LEADERSHIP_STATE_DEPUTIES} dobradinhas.`
|
|
|
|
/** Thrown by the assessor toggle on the leadership detail (C99), same allowlist contract. */
|
|
export const LEADERSHIP_ADVISORS_CAP_MESSAGE = `Cada liderança aceita no máximo ${MAX_LEADERSHIP_ADVISORS} assessores responsáveis.`
|
|
|
|
export const LEADERSHIP_ADVISORS_UNRESTRICTED_MESSAGE =
|
|
'Somente a coordenação geral ou o candidato gerencia assessores de lideranças.'
|
|
|
|
/**
|
|
* Delta write for the "Assessores responsáveis" section of a liderança (C99) —
|
|
* one chip toggle on `Leadership.advisors`, the owning document (no batch:
|
|
* the relation lives on the leadership itself, mirror of
|
|
* `stateDeputyAdvisorMembershipSchema`).
|
|
*/
|
|
export const leadershipAdvisorMembershipSchema = z.object({
|
|
leadershipId: positiveRelationshipId,
|
|
advisorId: positiveRelationshipId,
|
|
assigned: z.boolean(),
|
|
})
|
|
|
|
export type LeadershipAdvisorMembershipInput = z.input<typeof leadershipAdvisorMembershipSchema>
|
|
|
|
/** Thrown by the actions and matched verbatim by every route's `safeMessages`. */
|
|
export const LEADERSHIP_MUNICIPALITY_SCOPE_MESSAGE =
|
|
'Você só pode vincular lideranças aos municípios que assessora.'
|
|
|
|
/** Same exact-string contract: the staff gate of every leadership write. */
|
|
export const LEADERSHIP_STAFF_MESSAGE =
|
|
'Somente a coordenação e a assessoria podem gerenciar lideranças.'
|
|
|
|
/** Same exact-string contract: one ficha per person (`contact` UNIQUE). */
|
|
export const LEADERSHIP_DUPLICATE_MESSAGE =
|
|
'Esta pessoa já está cadastrada como liderança. Edite a ficha existente para vincular novos municípios.'
|
|
|
|
export const LEADERSHIP_INVALID_CONTACT_MESSAGE = 'Contato da liderança inválido.'
|
|
|
|
const municipalitiesArraySchema = z
|
|
.array(positiveRelationshipId)
|
|
.min(1, LEADERSHIP_MUNICIPALITY_FLOOR_MESSAGE)
|
|
.max(MAX_LEADERSHIP_MUNICIPALITIES)
|
|
.transform((ids) => [...new Set(ids)])
|
|
|
|
const organizationsArraySchema = z
|
|
.array(positiveRelationshipId)
|
|
.max(MAX_LEADERSHIP_ORGANIZATIONS)
|
|
.transform((ids) => [...new Set(ids)])
|
|
|
|
const stateDeputiesArraySchema = z
|
|
.array(positiveRelationshipId)
|
|
.max(MAX_LEADERSHIP_STATE_DEPUTIES)
|
|
.transform((ids) => [...new Set(ids)])
|
|
|
|
export const leadershipCreateSchema = z.object({
|
|
municipalities: municipalitiesArraySchema,
|
|
organizations: organizationsArraySchema.optional(),
|
|
stateDeputies: stateDeputiesArraySchema.optional(),
|
|
name: z.string().trim().min(2).max(120),
|
|
phones: contactPhonesSchema.min(1, 'Informe ao menos um celular.'),
|
|
email: optionalPersistedEmail,
|
|
gender: z.enum(leadershipGenders).optional(),
|
|
exclusive: z.boolean().default(true),
|
|
supportStatus: z.enum(leadershipSupportStatuses).default('a_abordar'),
|
|
notes: trimmedOptionalText(3000),
|
|
})
|
|
|
|
export const leadershipInternalUpdateSchema = z.object({
|
|
id: positiveRelationshipId,
|
|
municipalities: municipalitiesArraySchema.optional(),
|
|
organizations: organizationsArraySchema.nullable().optional(),
|
|
stateDeputies: stateDeputiesArraySchema.nullable().optional(),
|
|
exclusive: z.boolean().optional(),
|
|
supportStatus: z.enum(leadershipSupportStatuses).optional(),
|
|
notes: trimmedNullableText(3000),
|
|
})
|
|
|
|
export type LeadershipInternalUpdateInput = z.input<typeof leadershipInternalUpdateSchema>
|
|
|
|
const leadershipWizardFieldsSchema = z.object({
|
|
name: z.string().trim().min(2).max(120),
|
|
phones: contactPhonesSchema.min(1, 'Informe ao menos um celular.'),
|
|
email: optionalPersistedEmail,
|
|
exclusive: z.boolean().default(true),
|
|
supportStatus: z.enum(leadershipSupportStatuses).default('a_abordar'),
|
|
notes: trimmedOptionalText(3000),
|
|
})
|
|
|
|
export const leadershipWizardCreateSchema = leadershipWizardFieldsSchema.extend({
|
|
municipalityId: positiveRelationshipId,
|
|
})
|
|
|
|
/**
|
|
* B155 — inline create from the "Lideranças" column of `/campanha/municipios`:
|
|
* name only, linked to the município whose popover created it. Strict
|
|
* so the toggle shape (`leadershipId`/`assigned`) can never slip through.
|
|
*/
|
|
export const municipalityLeadershipCreateSchema = z.strictObject({
|
|
municipalityId: positiveRelationshipId,
|
|
name: z.string().trim().min(2).max(120),
|
|
})
|
|
|
|
export type MunicipalityLeadershipCreateInput = z.input<typeof municipalityLeadershipCreateSchema>
|
|
|
|
export const leadershipWizardUpdateSchema = leadershipWizardFieldsSchema.extend({
|
|
id: positiveRelationshipId,
|
|
})
|
|
|
|
export type LeadershipWizardCreateInput = z.input<typeof leadershipWizardCreateSchema>
|
|
export type LeadershipWizardUpdateInput = z.input<typeof leadershipWizardUpdateSchema>
|
|
|
|
/** Delta write for one chip in the "Dobradinhas" column of `/campanha/liderancas` (B31). */
|
|
export const leadershipStateDeputyMembershipSchema = z.object({
|
|
leadershipId: positiveRelationshipId,
|
|
stateDeputyId: positiveRelationshipId,
|
|
assigned: z.boolean(),
|
|
})
|
|
|
|
export type LeadershipStateDeputyMembershipInput = z.input<
|
|
typeof leadershipStateDeputyMembershipSchema
|
|
>
|
|
|
|
/**
|
|
* Delta writes for the "Municípios" column of `/campanha/liderancas` (B34).
|
|
* The batch variant carries a whole território/ZE, so the cap is the schema's
|
|
* own ceiling; the floor of one município is enforced against the *resulting*
|
|
* array by `nextMunicipalityIdsAfterLeadershipMembership`, not here.
|
|
*/
|
|
export const leadershipMunicipalitiesMembershipSchema = z.object({
|
|
leadershipId: positiveRelationshipId,
|
|
municipalityIds: z
|
|
.array(positiveRelationshipId)
|
|
.min(1)
|
|
.max(MAX_LEADERSHIP_MUNICIPALITIES)
|
|
.transform((ids) => [...new Set(ids)]),
|
|
assigned: z.boolean(),
|
|
})
|
|
|
|
export type LeadershipMunicipalitiesMembershipInput = z.input<
|
|
typeof leadershipMunicipalitiesMembershipSchema
|
|
>
|
|
|
|
/** Per-field Contact write for B153 inline edit (lista + detalhe). */
|
|
export const leadershipContactUpdateSchema = contactFieldUpdateSchema
|
|
|
|
export type LeadershipContactUpdateInput = z.input<typeof leadershipContactUpdateSchema>
|