From 8d007285d5b018a805855de4be6ee847f62ef04a Mon Sep 17 00:00:00 2001 From: Francisco Solla Date: Tue, 11 Aug 2026 07:41:04 -0300 Subject: [PATCH 1/6] chore(D10): auto-cura de leftovers de consent/supporter + janela missing-consent exclusiva (12/12 sob 6 workers) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit F1: purgeOrphanedConsentRenames sob lease exclusivo (autocommit) nos 3 pontos que tocam a key canônica; renames de teste migrados para chaves privadas; F1b: purge de contatos órfãos no purgeMunicipalityResidue. F2 (medição): shared-writer ABBA refutado; janela do missing-consent exclusiva + Onda0 atômico (down transacional) + provision exclusivo. Contraprova: 7 arquivos da key x 6 workers = 12/12 verdes (baseline 1/4). --- docs/CHANGELOG-AGENTS.md | 2 + docs/plans/escala-dry-pos-d9-impl.md | 112 +++++++++++++ tests/helpers/campaignFixtures.ts | 61 ++++++- tests/helpers/testDatabaseLease.ts | 97 +++++++++-- tests/int/campaignFixtures.int.spec.ts | 54 +++++- tests/int/campaignInvite.int.spec.ts | 4 +- tests/int/onda0Provision.int.spec.ts | 99 ++++++----- tests/int/testDatabaseLease.int.spec.ts | 209 +++++++++++++++++++++++- 8 files changed, 573 insertions(+), 65 deletions(-) create mode 100644 docs/plans/escala-dry-pos-d9-impl.md diff --git a/docs/CHANGELOG-AGENTS.md b/docs/CHANGELOG-AGENTS.md index 76bb9dae..acc0eebd 100644 --- a/docs/CHANGELOG-AGENTS.md +++ b/docs/CHANGELOG-AGENTS.md @@ -1,3 +1,5 @@ +**Recently resolved (2026-08-11):** **D10** (Issue #617) — auto-cura de leftovers de consent/supporter + fim do shared-write latente no lease de consent (pós-D9). **F1 (self-heal):** `purgeOrphanedConsentRenames` (3 nomes explícitos de renames temporários — `temporarily-renamed-consent`, `chave-renomeada`, `chave-temporariamente-ausente`) roda sob lease **exclusivo**, em **autocommit** (delete na transação do lease seguraria row lock e a operation do próprio fixture self-deadlockaria ao reusar a key purgada — observado na primeira implementação), nos 3 pontos que tocam a key canônica (`withMutableConsentFixture` antes do snapshot, setup do `withMissingInviteConsentFixture`, `ensureLeasedConsent`) — uma run abortada no meio de um rename de teste não envenena mais o run seguinte (UNIQUE `consent_key_idx` auto-sustentado). **F1b:** `purgeMunicipalityResidue` também deleta os **contatos** que supporters residue órfãos deixaram (reference-check em leadership/signature/subscription/supporter pós-delete — mesmo critério do `removeSupporterData`, reimplementado no helper). **F2 (refutação por medição + design final):** a hipótese "shared-writer = ABBA latente" caiu sob carga — exclusivo-durante-a-operation → fila de 17 waiters e cascata de 15s (6 workers); operation sem lease → FK `consent_id` (cleanup de fixture apagava linha canônica adotada); o ABBA não é construtível hoje (escritas disjuntas por teste; Onda0 em ordem fixa de 3 keys). As flakes reais do baseline (3/4 runs) eram: consumidores observando a ausência commitada da janela missing-consent (janela D9 era **shared**), down-test da Onda0 deletando as 4 keys **não-atomicamente**, e provision re-textando no meio de asserções de preview. **Fix:** janela do `withMissingInviteConsentFixture` → **exclusiva** (bloqueia consumidores shared durante a ausência; fila limitada ~200–500ms — a medição D9 do "15s tight" era o regime ONDA0-exclusivo, inexistente hoje); down-test da Onda0 **atômico** (delete+count+restore numa transação — FK insert espera o commit via speculative insertion); provision da Onda0 com leases **exclusivos** aninhados (ordem fixa CI→SR→VI); consumidores continuam shared. **Review /simplify (2 reviewers):** P1-1 — os renames vivos de `campaignInvite`/`campaignFixtures` migraram para chaves privadas (`consent-vacated-`/`owned-consent-vacated-`) — nenhum teste vivo produz os 3 nomes do purge; comentários "Onda0 shared" corrigidos; `withExclusiveTestDatabaseLease` colapsado (era cópia do privado); rollback no down-test quando a sessão falta; cleanup no finally nos testes F1 (auto-curáveis — um run falho do próprio teste não polui). Testes novos: contrato da janela exclusiva (waiter SHARED bloqueado), purge em ensure/setup, self-heal do canônico (repro do run abortado, 2 fixtures), F1b. **Contraprova:** 7 arquivos da key × 6 workers → **12/12 verdes** (baseline 1/4; designs intermediários: cascata 15s / FK); suíte int completa 83 arquivos/701 testes; unit 1938; e2e paridade com baseline (10 falhas pré-existentes de chat IA/webauthn/mic reproduzem no código original). Sem migration/access/Consent/`src/`. Intenção [`escala-dry-pos-d9.md`](plans/escala-dry-pos-d9.md); impl [`escala-dry-pos-d9-impl.md`](plans/escala-dry-pos-d9-impl.md). + **Recently resolved (2026-08-10):** **Push /campanha — opt-in automático + fix do prompt nativo (ad-hoc).** O `requestPermission` do card de perfil rodava **dentro de `startTransition(async …)`** — qualquer fronteira assíncrona entre o clique e a chamada derruba a user activation e o Chrome **auto-nega silenciosamente** (resolve `'denied'` sem mostrar o prompt): o teste real em device deu exatamente "Permissão de notificação negada". Fix: os guards sync ficam no handler e **`Notification.requestPermission()` passa a ser chamado sincronamente no clique** (antes de qualquer `await`/transição); mensagens distintas para `denied` (permissão bloqueada — reativar nas configurações do site; um `denied` não pode ser re-perguntado por nenhuma API) vs `default` (prompt não exibido). **Opt-in automático** (pedido do usuário): novo `CampaignPushOptInToast` no shell `(app)` (padrão `InstallPwaToast`/`BiometricEnrollmentToast`), montado só para staff (leader não recebe push — sino idêntico), exibido ~2s após o login quando `Notification.permission === 'default'` (nunca `denied`), sem inscrição ativa no device, consentimento `campanha-notificacoes-push` publicado e VAPID configurado; iOS só com app instalado (standalone); "Aceitar e ativar" roda a mesma cerimônia com o requestPermission síncrono no clique do action; dismiss fica salvo em `localStorage` (chave `campaign-push-opt-in-dismissed`); ativação/desativação/leitura de inscrição extraídas para `src/utilities/campaignPushClient.ts` (compartilhado com o card — don't twin; registrado no pin de top-level com razão). **Diagnóstico de prod (read-only, `db:check:push-chain`):** consentimento `campanha-notificacoes-push` **já publicado** (PASS — nada a criar), envs VAPID presentes, SW versionado, 77 eventos no sino — **único elo quebrado: 0 inscrições push** (o erro de permissão impedia o cadastro). Sem migration/access/Consent/schema. Sem issue — entregue via ad-hoc no worktree `work/3`. **Recently resolved (2026-08-10):** **OPS37** (Issue #605) — o container Postgres compartilhado **não é mais recriado** por um worktree vizinho: `pnpm db:start` virou `scripts/db-start.mjs` (CLI) sobre `scripts/lib/db-start.mjs` — preflight "container `teqo-postgres-1` Up + `healthy`/`starting` → **no-op** (compose nem roda); senão, `compose -p teqo up -d --wait --wait-timeout 120 postgres` como antes. **Causa raiz provada por medição:** o bind mount **relativo** `./docker/postgres/init` do compose resolve um path absoluto por worktree → `config_hash` diferente por worktree (OPS37 `39e89956…` / B195 `cf3cb522…` / C101 `917e11bb…`; diff do `config` = só o `source:` do bind) → qualquer `compose up` cru de outro worktree fazia `replace` e matava dev+e2e de todos (`terminating connection due to administrator command`) — o incidente se repetiu **ao vivo durante o gate**: o worktree `work/4` recriou o container no meio da suíte int (17 testes mortos; os 3 arquivos re-rodaram verdes e a suíte completa 691/691 passou em seguida). **Verificação do aceite:** `StartedAt` inalterado após `db:start` repetido, inclusive rodado de **outro worktree** (D9, hash diferente) — o caso que antes recriava. Recriação legítima (bump de imagem/config) segue por `pnpm db:start --force-recreate` (explícito; não testado live para não matar a run e2e do D9 em voo — flag é pass-through de 1 arg). `worktree.mjs` provisionamento delega para a mesma lib (mesmo risco no `worktree next`; `dockerComposeUp`/`SHARED_COMPOSE_PROJECT` órfãos removidos). Skip deliberado em `starting` (container que um peer acabou de criar): **espera** o boot ficar `healthy` (60s, polling 2s) em vez de recriar — e cai no compose up se travou; **não** skip em `unhealthy`/sem healthcheck (volta à config atual exatamente uma vez); `db:stop`/`db:doctor`/compose file/CI intactos. Unit novos (`dbStart.unit.spec.ts`, 17: matrix running×health + parse do inspect + wrapper mockado — skip não chama compose, argv do up/force-recreate, espera de `starting`). Sem migration/access/Consent/UI. Intenção [`container-postgres-compartilhado-compose-replace.md`](plans/container-postgres-compartilhado-compose-replace.md); impl [`container-postgres-compartilhado-compose-replace-impl.md`](plans/container-postgres-compartilhado-compose-replace-impl.md). diff --git a/docs/plans/escala-dry-pos-d9-impl.md b/docs/plans/escala-dry-pos-d9-impl.md new file mode 100644 index 00000000..9981af2f --- /dev/null +++ b/docs/plans/escala-dry-pos-d9-impl.md @@ -0,0 +1,112 @@ +# Impl: Auto-cura de leftovers de consent/supporter + ABBA do shared-write do lease de consent (pós-D9) + +Status: em execução (F1 + F2 implementados; review /simplify aplicado; gates verdes; PR pendente) +Atualizado em: 2026-08-11 +Issue: #617 +Intenção: docs/plans/escala-dry-pos-d9.md +Appetite restante: herdado (só testes/helpers; zero `src/`; zero migration) + +## Leitura da intenção + +- **Outcome:** (F1) uma run abortada no meio de um fixture de rename/cleanup nunca mais envenena o run seguinte — renames órfãos de consent e contatos/supporters órfãos são reconhecidos e removidos deterministicamente; (F2) o ABBA latente (lease shared de consent + operation escritora) deixa de existir — cascata de timeouts não reproduz sob 6 workers + carga. +- **O que NÃO negociar:** mecanismo de lease (advisory lock) é canônico; orçamento de 15s do vitest fora; 4ª key da Onda0 sem lease fora; nada de `src/` nem migration. +- **O que reavaliar:** a hipótese "lista vs padrão vs re-snapshot por id" da F1 (o re-snapshot é impossível — ver decisões); o alvo exato da F2 (compartilhado entre `withLeasedConsent`/`withInviteConsent`/janela do `withMissingInviteConsentFixture`/`campaignSupporter:95` — o contrato de leitura/escrita cobre todos). + +## Caracterização (auditoria feita no código) + +**F1 — mecanismo exato da poluição auto-sustentada.** Os testes renomeiam a linha canônica para `chave-renomeada` (campaignInvite.int.spec.ts:1615), `chave-temporariamente-ausente` (campaignInvite.int.spec.ts:1679), `temporarily-renamed-consent` (testDatabaseLease.int.spec.ts:311) e `temporarily-unkeyed-` (campaignFixtures.int.spec.ts:254), sempre dentro do lease **exclusivo** do fixture (renomeia + restaura no mesmo holder). Se o run morre entre o commit do rename e o restore, a linha órfã persiste com a key temporária e a key canônica fica ausente. No run seguinte o fixture snapshotta "ausente", cria linha nova, e o teste renomeia para a MESMA key temporária → UNIQUE `consent_key_idx` (`Valor deve ser único`) → poluição auto-sustentada. Garantia de segurança do purge: qualquer renomeador vivo está segurando o lease exclusivo da key — quem faz o purge também o segura, então nenhuma linha temporária viva pode existir durante o purge (renomeadores em fila só agem após o release, e partem do snapshot pós-purge). + +**F2 — ciclo exato do ABBA.** `withLeasedConsent` (e `withInviteConsent`) segura o lease **shared** durante a `operation()`. Operations escritoras (redeem autofill/login → `UPDATE campaignInvite.usedAt`, `UPDATE leadership`, `UPDATE contact`, `CREATE campaignUser`; `createSupporterRecord`/`createLeaderSupporterRecord` → upsert `contact` + `CREATE supporter`) tomam row locks de invite/contato. Dois escritores em spec files diferentes seguram shared (compatível — zero serialização advisory) e intercalam writes sobre linhas sobrepostas → ABBA de row locks. Também: escritor shared + holder exclusivo (`withMutableConsentFixture`, que escreve invites no corpo do teste) → o exclusivo espera o shared enquanto seu corpo segura row locks que o shared-writer quer. Auditoria de ordem canônica e FIFO: a aquisição é sempre "consent-lease primeiro, row locks depois" — o ciclo existe porque DOIS holders seguram locks em ordens opostas (consent↔row); com escritores exclusivos e leitores shared (que não escrevem → não seguram row locks), nenhum ciclo é construtível. + +## Abordagem recomendada + +```mermaid +flowchart LR + F1[F1: purge de renames órfãos] --> H1[purgeOrphanedConsentRenames
lista explícita + padrão temporarily-unkeyed-%] + H1 --> S1[chamado sob o lease exclusivo em
withMutableConsentFixture / withMissingInviteConsentFixture / ensureLeasedConsent] + F1b[F1b: contatos órfãos de residue] --> H2[purgeMunicipalityResidue deleta
contatos sem outras referências] + F2[F2: contrato read/write do withLeasedConsent] --> H3[opção mode:'write' → lease EXCLUSIVO] + H3 --> S2[call sites escritores migram;
leitores continuam shared] + H3 --> S3[janela do withMissingInviteConsentFixture
permanece shared + invariante documentado] +``` + +**Opções consideradas:** A (mode `'write'` explícito por call site, default read) | B (exclusivo default, opt-out read) | C (escritas fora do escopo do lease shared) | D (janela do missing-consent exclusiva) | E (purge por lista explícita + padrão) | F (re-snapshot por id) | G (purge global de consent não-canônico) + +**Recomendação:** + +- **F1 = E**: purge determinístico por lista explícita (`temporarily-renamed-consent`, `chave-renomeada`, `chave-temporariamente-ausente`) + `key LIKE 'temporarily-unkeyed-%'`, executado sob o lease exclusivo **antes do snapshot** nos três pontos que tocam a key canônica. — porque a lista cobre os 4 nomes reais hoje, o padrão cobre a família `temporarily-unkeyed-` sem risco (nenhuma key estável legítima casa com o padrão) e um único DELETE indexado é barato. +- **F2 = A**: `withLeasedConsent` ganha `{ mode: 'write' }` (lease exclusivo para o ensure + operation num único holder; sem loop de retry — ninguém pode deletar a linha sob exclusivo); default continua `'read'` (shared). Call sites que escrevem migram explicitamente. — porque escritores exclusivos serializam entre si (fim da intercalação de row locks), leitores shared não escrevem → não entram em ciclo, e a medição D9 (janela exclusiva estourou o budget de 15s) não se repete: os escritores de invite são operations curtas (~100–300 ms), não provisions multi-segundo. +- **F2 janela missing-consent = shared, preservada (não D)**: a janela shared bloqueia os exclusivos (ensure/fixtures) sem parar leitores shared — a medição D9 foi explícita ("15s tight under load com janela exclusiva"). A operation da janela escreve, mas é a ÚNICA shared-writer da key hoje (1 arquivo por key: campaignSupporter:95 / testDatabaseLease com keys privadas) e o holder do window não espera por ninguém (exclusivos que esperam a janela não seguram row locks — adquirem após o wait) → ciclo não construtível; vira invariante documentado + comentário. + +**Rejeitadas:** B porque serializaria os LEITORES atrás dos escritores (a fila que estourou o budget na medição D9 — leitores são muitos: `testDatabaseLease`, `campaignFixtures`, previews de `campaignInviteUi`); C porque quebra a garantia "a linha canônica está estável durante a operation" (writers sem lease veriam a ausência commitada de uma janela missing-consent → flake de fail-closed); D porque a medição D9 documentou o budget estourando sob carga com janela exclusiva; F porque o snapshot id morre com o run abortado (impossível re-snapshot por id — só o nome temporário sobrevive); G porque apagaria keys legítimas de fixtures (`fixtures.createConsent` gera keys runID-scoped aleatórias que coexistem). + +## Revisão da F2 por medição (2026-08-11 — mudança material pós-gate) + +O plano aprovado assumia a hipótese da intenção ("shared-writer = ABBA latente") e propunha o contrato `mode: 'write'` → lease **exclusivo** durante a operation. A medição sob carga (7 arquivos da key, 6 workers) **refutou a forma e o mecanismo**: + +1. **Exclusivo durante a operation = cascata.** Fila de 17 waiters advisory + timeouts de 15s em cascata (campaignInvite/campaignInviteUi/testDatabaseLease) — o hold do exclusivo por ~30 testes serializa a key inteira. **Descartado medido.** +2. **Operation sem lease = FK violation.** `withMutableConsentFixture` com snapshot ausente recria a linha canônica; o cleanup do fixture a deleta; um consumidor sem lease que a adotou (redeem gravando `leadership.consent_id`) → `supporter/leadership_consent_id_consent_id_fk`. **Descartado medido.** +3. **O ABBA shared↔row não é construtível hoje:** os consumidores escrevem linhas disjuntas por teste; Onda0 é shared desde D9; nenhum ciclo advisory↔row existe. A cascata D9 era o ONDA0 **exclusivo** (multi-segundo) + fila — regime que já não existe. +4. **As flakes residuais reais (baseline medido: 3/4 runs com 1 falha) eram:** (a) consumidores observando a ausência commitada da janela do missing-consent (janela shared D9 não bloqueia consumidores shared) → 'Consentimento ainda não configurado' em redeems; (b) o down-test da Onda0 deletando as 4 keys estáveis **não-atomicamente** (delete autocommit + restore separado) → FK `consent_id`; (c) o provision da Onda0 reescrevendo textos no meio de asserções de preview (mismatch de `requiresConsent`/`consentData`). + +**Design final (medido 8/8 verdes sob 6 workers; baseline 1/4):** + +- **Consumidores ficam SHARED** (D9) — `withLeasedConsent`/`withInviteConsent` inalterados no modo; o contrato `mode` foi **descartado** (call sites revertidos). +- **Janela do `withMissingInviteConsentFixture` vira EXCLUSIVA** (era shared desde D9): nenhum consumidor observa a ausência commitada — é a mudança que mata a classe (a). O custo é fila limitada (~200–500ms) — a medição D9 do "15s tight" era sob o regime ONDA0-exclusivo, que não existe mais. +- **Down-test da Onda0 atômico** (delete + count + restore numa transação) — mata a classe (b). +- **Provision da Onda0 EXCLUSIVO** (~500ms, fila limitada) — mata a classe (c). +- **Purge F1 sem o padrão `temporarily-unkeyed-%`**: o padrão só era produzido pelo teste `campaignFixtures` ('deletes the stable invite consent'), que agora usa chave privada `owned-consent-vacated-` — a linha canônica renomeada com key no padrão do purge podia ser apagada por purge de outro arquivo no meio da operation (falha NotFound observada). O purge cobre os 3 nomes explícitos. +- **Testes F1 auto-curáveis**: plantas de órfãos sob lease exclusivo + purge no início + cleanup no finally (uma falha do próprio teste não deixa poluição para o run seguinte). + +### Componentes / mudanças (final) + +- **`purgeOrphanedConsentRenames`** (`tests/helpers/testDatabaseLease.ts`): DELETE `WHERE key IN (3 nomes)` via `execute`; autocommit (nunca dentro da transação do lease — delete não-commitado seguraria row lock e a operation do próprio fixture self-deadlockaria ao reusar a key purgada). Chamado sob o lease exclusivo por: (1) `withMutableConsentFixture` antes do snapshot; (2) `withMissingInviteConsentFixture` no setup antes do snapshot; (3) `ensureLeasedConsent` dentro da janela exclusiva antes do find. +- **`withMissingInviteConsentFixture`** (`tests/helpers/testDatabaseLease.ts`): `serializeWindow: true` (default) → janela **exclusiva**; `false` inalterado (teste de ensures concorrentes em key privada). +- **`withExclusiveTestDatabaseLease`** (`tests/helpers/testDatabaseLease.ts`): wrapper novo exportado (mirror do shared). +- **`onda0Provision.int.spec.ts`**: provision com leases exclusivos; down-test com delete+assert+restore numa transação (`beginTransaction` + session db + commit/rollback). +- **`campaignFixtures.ts`** (`purgeMunicipalityResidue`): além do purge de residue existente, deleta os **contatos** que os supporters residue órfãos deixaram (referência-check em leadership/signature/subscription/supporter — mesmo critério do `removeSupporterData`, reimplementado no helper). +- **`campaignFixtures.int.spec.ts`** (teste 'deletes the stable invite consent...'): chave de vacância privada `owned-consent-vacated-` (fora do padrão do purge). +- **Testes novos** (`testDatabaseLease.int.spec.ts`): contrato da janela exclusiva (waiter SHARED bloqueado durante a janela — estende o teste D9); purge em ensure; purge no setup do missing; self-heal do canônico (repro do run abortado, 2 fixtures); **F1b** (`campaignFixtures.int.spec.ts`): purge de contatos órfãos preservando referenciados. +- **Migration:** sem migration (só testes/helpers). +- **Access / Consent:** nenhuma mudança em runtime; chaves e fail-closed intactos. +- **UI:** nenhuma. + +## Fases verificáveis (executadas) + +1. **F2 contrato** — janela exclusiva + contrato testado (waiter SHARED bloqueado durante a janela); consumidores shared preservados. +2. **F1 self-heal** — purge nos 3 pontos; repros: órfão plantado (sob lease), auto-cura no run seguinte (fixture 2×), sem UNIQUE; testes auto-curáveis (cleanup no finally). +3. **F1b residue** — purge de contatos sem referências; repro: plant + purge direto, preservação de referenciado. +4. **Gates + carga** — contraprova: 7 arquivos da key (`campaignInvite`, `campaignInviteUi`, `campaignFixtures`, `testDatabaseLease`, `collectionAccessLockdown`, `campaignSupporter`, `onda0Provision`) com workers=6 → **8/8 runs verdes** (baseline original: 1/4; designs intermediários: cascata de 15s / FK violations); suíte int completa 83 arquivos/701 testes verdes; gates abaixo em andamento. + +## Rabbit holes / Não escopo (engenharia) + +- Mecanismo de lease/advisory lock (canônico, fora — só o modo de aquisição da janela muda). +- Orçamento de 15s do vitest (gatilho: recorrência com pool ativo). +- 4ª key da Onda0 sem lease (gatilho: primeiro escritor do push key). +- Atacar a poluição residual de contatos legados (inertes; fora — o F1b só impede acúmulo novo). +- Re-trabalhar a spec Onda0 ou a Onda0 em si. +- Gap setup→janela do missing-consent (ausência commitada por ~ms entre o commit do setup e a aquisição da janela; consumidor que insira com FK nesse gap ainda pode falhar — classe residual rara, mesma ordem da aceita na D9; gatilho de revisita: recorrência sob carga). + +## Revisão /simplify (2 reviewers independentes, 2026-08-11) — fixes aplicados + +- **P1-1 (ambos reviewers, obrigatório):** o purge deleta os 3 nomes globalmente sob a lease do chamador; os renomeadores vivos de `campaignInvite.int.spec.ts` (linhas 1615/1679) produziam `chave-renomeada`/`chave-temporariamente-ausente` sob a lease invite — um purge sob outra lease key (supporter/privada) podia apagar a linha renomeada viva (a classe exata que a D10 mata). **Fix aplicado:** os dois renames migraram para chave privada `consent-vacated-` (mesmo padrão do `campaignFixtures`); agora NENHUM teste vivo produz os 3 nomes — o único produtor restante é a planta do self-heal (sob a lease invite, imediatamente antes do fixture curador). Invariante documentada no helper. +- **P2-1:** comentários do diff diziam "Onda0 holds shared, never exclusive" — falsos pós-diff (provision é exclusivo). Reescritos. +- **P2-2:** `withExclusiveTestDatabaseLease` era cópia verbatim do privado — colapsado em um único exportado (usado também internamente). +- **P2-3:** teste 'purges orphaned renames during the missing-consent fixture setup' sem cleanup no finally — adicionado. +- **P2 (reviewer 2):** down-test da Onda0 lançava sem rollback quando a sessão da transação faltava — rollback adicionado antes do throw. +- **Débitos registrados (não-fix):** fault `beforeRestoreLeaseAcquire` vestigial com janela exclusiva (só tem efeito com `serializeWindow: false` — documentar/remover); exposição transitória do gap do self-heal (ms, mesma classe residual aceita); `privacy_policy.published=false` persiste após o down-test (comportamento D9, inalterado); micro-otimização dos selects do reference-check; callback `execute` do purge podia receber `payload` direto. + +## Riscos e mitigação (medidos) + +- **Fila nova na janela exclusiva / provision exclusivo** (consumidores esperam ~200–500ms): medido 8/8 verdes sob 6 workers — sem timeouts. +- **F1 purge apagar linha viva:** impossível por construção (renomeador vivo segura o exclusivo; purge roda sob o exclusivo); testes plantam órfãos sob lease e limpam no finally. +- **Down-test atômico:** consumers com FK no meio do delete+restore transacional esperam o commit (speculative insertion) e progridem — classe FK eliminada (medido). +- **F1b deletar contato referenciado:** o filtro de outras-referências roda antes do delete; teste cobre ambos os lados (deleta só órfão; preserva referenciado). + +## Aceite de engenharia + +- [x] Run abortado (rename órfão plantado) não envenena o run seguinte (F1 — repros + suíte) +- [x] Nenhum consumidor observa a ausência commitada da janela missing-consent (janela exclusiva + contrato testado) +- [x] Cascata de timeouts não reproduz sob 6 workers ×12 (F2 — repro 12/12 vs baseline 1/4) +- [x] Review /simplify: P1-1 e P2s aplicados; débitos registrados +- [x] Invariantes AGENTS/engineering-standards; só testes/helpers; sem migration; zero `src/` (tsc, lint, knip, cycles, format, unit 1938, int 701, e2e paridade com baseline) diff --git a/tests/helpers/campaignFixtures.ts b/tests/helpers/campaignFixtures.ts index 81186c88..bbbc4cf4 100644 --- a/tests/helpers/campaignFixtures.ts +++ b/tests/helpers/campaignFixtures.ts @@ -233,8 +233,9 @@ const nextAllocatedMunicipalityIndex = async ( * Delete campaign rows attached to a freshly claimed municipality. The allocator * guarantees no OTHER live test owns this municipality, so anything found here is * residue from an aborted previous run that would poison count assertions. + * Also deletes the CONTACT rows those residue supporters orphaned (D10 F1). */ -const purgeMunicipalityResidue = async ( +export const purgeMunicipalityResidue = async ( payload: Payload, municipalityID: number, ): Promise => { @@ -273,7 +274,6 @@ const purgeMunicipalityResidue = async ( where: { municipality: { equals: municipalityID } }, depth: 0, pagination: false, - select: {}, }), payload.find({ collection: 'activity', @@ -336,6 +336,63 @@ const purgeMunicipalityResidue = async ( depth: 0, }) } + + // D10 F1: an aborted run leaves residue supporters AND their contact rows. + // The supporters are gone above; delete the contacts they orphaned (no other + // join references them) so the residue stops accumulating — same reference + // rule as the production `removeSupporterData` flow, reimplemented here so + // the fixture never runs production code under test. + const residueContactIDs = [ + ...new Set( + supporters.docs.map((doc) => relationId(doc.contact)).filter((id) => id !== undefined), + ), + ] + if (residueContactIDs.length > 0) { + const [leadershipRefs, signatureRefs, subscriptionRefs, supporterRefs] = await Promise.all([ + payload.find({ + collection: 'leadership', + where: { contact: { in: residueContactIDs } }, + depth: 0, + pagination: false, + }), + payload.find({ + collection: 'signature', + where: { contact: { in: residueContactIDs } }, + depth: 0, + pagination: false, + }), + payload.find({ + collection: 'subscription', + where: { contact: { in: residueContactIDs } }, + depth: 0, + pagination: false, + }), + payload.find({ + collection: 'supporter', + where: { contact: { in: residueContactIDs } }, + depth: 0, + pagination: false, + }), + ]) + const referenced = new Set() + for (const doc of [ + ...leadershipRefs.docs, + ...signatureRefs.docs, + ...subscriptionRefs.docs, + ...supporterRefs.docs, + ]) { + const contactID = relationId(doc.contact) + if (contactID !== undefined) referenced.add(contactID) + } + const orphanContactIDs = residueContactIDs.filter((id) => !referenced.has(id)) + if (orphanContactIDs.length > 0) { + await payload.delete({ + collection: 'contact', + where: { id: { in: orphanContactIDs } }, + depth: 0, + }) + } + } } export class CampaignFixtures { diff --git a/tests/helpers/testDatabaseLease.ts b/tests/helpers/testDatabaseLease.ts index 0b67ba5b..da79bfa2 100644 --- a/tests/helpers/testDatabaseLease.ts +++ b/tests/helpers/testDatabaseLease.ts @@ -88,6 +88,41 @@ export const restoreConsentRows = async ( } } +/** + * Keys the consent fixtures rename the canonical stable row to while a test + * exercises the fail-closed "key absent" state. A run aborted between the + * rename commit and the fixture restore leaves the canonical key absent and an + * orphan row holding one of these names; without a purge, the next run + * snapshots the key as absent, creates a fresh row, and the same rename then + * hits the UNIQUE `consent_key_idx` — the pollution is self-sustaining (D10 F1). + */ +const ORPHANED_CONSENT_RENAME_KEYS = [ + 'temporarily-renamed-consent', + 'chave-renomeada', + 'chave-temporariamente-ausente', +] as const + +/** + * Deletes orphaned consent rows left by an aborted run: the explicit temporary + * rename keys. MUST be called while holding the lease key's EXCLUSIVE lease. + * The 3 names are produced by NO live test anymore (the fail-closed rename + * tests vacate the canonical row to PRIVATE keys, D10 review finding P1-1), so + * a row carrying one of them is guaranteed orphaned — the only producer left + * is the self-heal repro's own plant, which runs under this key's exclusive + * lease immediately before the healing fixture. + */ +export const purgeOrphanedConsentRenames = async ( + execute: (query: ReturnType) => Promise, +): Promise => { + await execute(sql` + DELETE FROM "consent" + WHERE "key" IN (${sql.join( + ORPHANED_CONSENT_RENAME_KEYS.map((key) => sql`${key}`), + sql`, `, + )}) + `) +} + export const CAMPAIGN_INVITE_CONSENT_LEASE_KEY = 'campaign-invite-consent' export const SUPPORTER_REGISTRATION_CONSENT_LEASE_KEY = 'supporter-registration-consent' export const SUPPORTER_VOTE_INTENTION_CONSENT_LEASE_KEY = 'supporter-vote-intention-consent' @@ -104,7 +139,7 @@ export type LeasedConsentFixture = { consentKey: string leaseKey: string } -const fixtureConsentText: Consent['text'] = { +export const fixtureConsentText: Consent['text'] = { root: { type: 'root', children: [ @@ -385,7 +420,7 @@ const withDatabaseLease = async ( } } -const withTestDatabaseLease = async ( +export const withExclusiveTestDatabaseLease = async ( payload: Payload, leaseKey: string, operation: () => Promise, @@ -394,7 +429,7 @@ const withTestDatabaseLease = async ( payload, leaseKey, acquireTestDatabaseLease, - 'The leased operation and database lease release both failed.', + 'The exclusive leased operation and database lease release both failed.', operation, ) @@ -415,7 +450,13 @@ export const ensureLeasedConsent = async ( payload: Payload, { consentKey, leaseKey }: LeasedConsentFixture, ): Promise => - withTestDatabaseLease(payload, leaseKey, async () => { + withExclusiveTestDatabaseLease(payload, leaseKey, async () => { + // D10 F1: a run aborted mid-rename leaves an orphan row holding a + // temporary name and the canonical key absent; the purge clears it so the + // ensure below can recreate the canonical row without the next rename + // hitting the UNIQUE constraint. Autocommit is safe: we hold the + // exclusive lease, so no live renamer can be mid-flight. + await purgeOrphanedConsentRenames((query) => payload.db.drizzle.execute(query)) const existing = await payload.find({ collection: 'consent', where: { key: { equals: consentKey } }, @@ -440,6 +481,17 @@ export const withLeasedConsent = async ( { consentKey, leaseKey }: LeasedConsentFixture, operation: (consent: Consent) => Promise, ): Promise => { + // D10 F2 (measurement-driven): the operation holds the SHARED consent lease. + // Holding exclusive across the operation serialized every writer on the key + // into a 17-waiter queue and a 15s cascade under 6 workers; running the + // operation UNLEASED let a parallel fixture's created-row cleanup delete the + // canonical row out from under a consumer (FK violation). Shared is the + // stable middle: consumers never serialize against each other (their writes + // touch disjoint per-test rows, so no lock-order cycle is constructible — + // every advisory holder acquires at most one lease at a time, and Onda0 + // acquires its three keys in a fixed order) and the exclusive + // missing-consent window blocks every consumer while the canonical key is + // absent, so no consumer observes the transient state. for (;;) { await ensureLeasedConsent(payload, { consentKey, leaseKey }) const lease = await beginTestDatabaseLease(payload, leaseKey, 'shared') @@ -500,6 +552,13 @@ export const withMissingInviteConsentFixture = async ( let setupError: unknown try { await faults.beforeSnapshotSetup?.() + // D10 F1: clear orphaned renames left by an aborted run BEFORE snapshotting + // (we hold the exclusive setup lease, so no live renamer exists). The + // DELETE runs in autocommit, NOT in the lease transaction: an uncommitted + // delete would keep its row lock and the fixture's own operation would + // self-deadlock when it re-uses the purged key (observed on the first D10 + // implementation). + await purgeOrphanedConsentRenames((query) => payload.db.drizzle.execute(query)) snapshot = rowsFrom( await setupLease.transaction.execute(sql` SELECT "id", "key", "text", "updated_at", "created_at" @@ -545,21 +604,23 @@ export const withMissingInviteConsentFixture = async ( } if (setupError !== undefined) throw setupError - // Serialize the operation window under a SHARED lease (same precedent as - // onda0Provision's leases on the stable keys): parallel spec files must - // never observe the committed-absent state, or their ensure-then-read - // helpers create an ephemeral row that this restore deletes out from under - // them. Shared is enough — the ephemeral-row creators (ensures and other - // fixtures) are all EXCLUSIVE holders, which a shared window blocks without - // stalling the harmless shared readers behind it (the 15s test budget - // stayed tight under load with an exclusive window). + // Serialize the operation window under an EXCLUSIVE lease (D10 F2, + // measurement-driven change from D9's shared window): every consumer of the + // key — the shared `withLeasedConsent` holders AND the exclusive fixtures — + // is blocked while the canonical key is committed-absent, so no consumer can + // observe the transient absence or read/reference a row the restore deletes + // out from under it (baseline load runs flaked 3/4 with exactly this class: + // 'Consentimento ainda não configurado' in redeem tests and + // `consent_id` FK violations). The window is short (a fail-closed action or + // a no-op), so the queueing behind it is bounded — the D9-era 15s budget + // blowout happened with the Onda0 provision holding EXCLUSIVE for + // multi-second writes; since D10 the provision holds exclusive too, but + // only for ~500ms of short upserts. // `serializeWindow: false` keeps the unleased window for operations that // deliberately exercise concurrent consent writers. NOTE: with the window // leased, the operation must NOT acquire the same lease itself (e.g. via // ensureLeasedConsent on the same key) — that would self-deadlock. - const windowLease = serializeWindow - ? await beginTestDatabaseLease(payload, leaseKey, 'shared') - : undefined + const windowLease = serializeWindow ? await beginTestDatabaseLease(payload, leaseKey) : undefined // Close the gap between the setup's committed delete and this acquisition: // a writer that queued during the setup (advisory locks grant FIFO) can @@ -653,6 +714,12 @@ export const withMutableConsentFixture = async ( let operationError: unknown try { + // D10 F1: clear orphaned renames left by an aborted run BEFORE snapshotting + // (we hold the exclusive lease, so no live renamer exists). Autocommit, + // never inside the lease transaction — an uncommitted delete would keep + // its row lock and the fixture's own operation would self-deadlock when it + // re-uses the purged key. + await purgeOrphanedConsentRenames((query) => payload.db.drizzle.execute(query)) snapshot = rowsFrom( await lease.transaction.execute(sql` SELECT "id", "key", "text", "updated_at", "created_at" diff --git a/tests/int/campaignFixtures.int.spec.ts b/tests/int/campaignFixtures.int.spec.ts index 4631a7ba..a6a8b676 100644 --- a/tests/int/campaignFixtures.int.spec.ts +++ b/tests/int/campaignFixtures.int.spec.ts @@ -5,7 +5,11 @@ import { beforeAll, describe, expect, it } from 'vitest' import type { Consent } from '@/payload-types' import config from '@/payload.config' -import { createCampaignFixtures, withCampaignFixtures } from '../helpers/campaignFixtures' +import { + createCampaignFixtures, + purgeMunicipalityResidue, + withCampaignFixtures, +} from '../helpers/campaignFixtures' import { withInviteConsent, withMutableConsentFixture } from '../helpers/testDatabaseLease' let payload: Payload @@ -40,7 +44,8 @@ const exists = async ( | 'votePledge' | 'campaignDemand' | 'organization' - | 'municipalityUpdate', + | 'municipalityUpdate' + | 'supporter', id: number, ): Promise => { const result = await payload.find({ @@ -248,10 +253,15 @@ describe('campaign integration fixtures', () => { let consentID = 0 await withMutableConsentFixture(payload, async (configuredConsent) => { + // The vacating rename uses a PRIVATE key, deliberately OUTSIDE the + // orphan-rename purge list: the canonical row is shared with parallel + // spec files and any key the purge recognizes could be deleted by a + // concurrent fixture's cleanup mid-operation (D10 F2 measurement). + const vacatedKey = `owned-consent-vacated-${configuredConsent.id}` await payload.update({ collection: 'consent', id: configuredConsent.id, - data: { key: `temporarily-unkeyed-${configuredConsent.id}` }, + data: { key: vacatedKey }, depth: 0, }) await withCampaignFixtures(payload, async (fixtures) => { @@ -397,4 +407,42 @@ describe('campaign integration fixtures', () => { }) expect(persistedMunicipality.advisors ?? []).toEqual([]) }) + + it('purges residue supporters and the contacts they orphaned on a municipality claim', async () => { + // D10 F1: an aborted run leaves supporters AND their contact rows on the + // claimed municipality. The purge must delete the orphaned contacts (no + // other join references them) while preserving contacts that a live + // leadership outside the purged municipality still references. + const fixtures = createCampaignFixtures(payload) + try { + const municipality = await fixtures.getMunicipality() + const coordinator = await fixtures.createCampaignUser('coordinator') + const orphanContact = await fixtures.createContact() + const residueSupporter = await fixtures.createSupporter({ + contact: orphanContact, + municipality, + createdBy: coordinator, + }) + const preservedContact = await fixtures.createContact() + await fixtures.createSupporter({ + contact: preservedContact, + municipality, + createdBy: coordinator, + }) + const otherMunicipality = await fixtures.getMunicipality() + await fixtures.createLeadership({ + contact: preservedContact, + municipalities: [otherMunicipality.id], + createdBy: coordinator, + }) + + await purgeMunicipalityResidue(payload, municipality.id) + + expect(await exists('supporter', residueSupporter.id)).toBe(false) + expect(await exists('contact', orphanContact.id)).toBe(false) + expect(await exists('contact', preservedContact.id)).toBe(true) + } finally { + await fixtures.cleanup() + } + }) }) diff --git a/tests/int/campaignInvite.int.spec.ts b/tests/int/campaignInvite.int.spec.ts index 1875d0dd..6f2ec591 100644 --- a/tests/int/campaignInvite.int.spec.ts +++ b/tests/int/campaignInvite.int.spec.ts @@ -1611,7 +1611,7 @@ describe('campaign invite domain', () => { await payload.update({ collection: 'consent', id: consent.id, - data: { key: 'chave-renomeada' }, + data: { key: `consent-vacated-${consent.id}` }, }) await expect( @@ -1675,7 +1675,7 @@ describe('campaign invite domain', () => { await payload.update({ collection: 'consent', id: consent.id, - data: { key: 'chave-temporariamente-ausente' }, + data: { key: `consent-vacated-${consent.id}` }, }) const submission = { token: invite.token, diff --git a/tests/int/onda0Provision.int.spec.ts b/tests/int/onda0Provision.int.spec.ts index 07281a63..0622eead 100644 --- a/tests/int/onda0Provision.int.spec.ts +++ b/tests/int/onda0Provision.int.spec.ts @@ -20,6 +20,7 @@ import { SUPPORTER_REGISTRATION_CONSENT_LEASE_KEY, SUPPORTER_VOTE_INTENTION_CONSENT_LEASE_KEY, restoreConsentRows, + withExclusiveTestDatabaseLease, withSharedTestDatabaseLease, type ConsentSnapshot, } from '../helpers/testDatabaseLease' @@ -40,22 +41,18 @@ describe('Onda 0 provision (integration)', () => { }) it('upserts consent keys and publishes privacy-policy idempotently', async () => { - // Shared leases on the stable keys other spec files exercise: the - // provision UPSERTS the shared rows and the spec below REMOVES them, so - // the writes must never interleave a parallel file's reads of the same - // rows (D9 flake). SHARED — not exclusive — because this spec only - // touches the four canonical keys and an exclusive holder here widened - // the consent-lease deadlock surface with the invite-domain writers - // (cascade under parallel load). The missing-consent fail-closed paths in - // other files now serialize their own windows under the lease, and the - // down-test below restores the removed rows with their original ids, so - // the shared rows are never observed absent or re-created with a new id. - // (The fourth Onda0 key, campanha-notificacoes-push, has no lease - // constant because no spec file writes it today; the down-test below - // still restores it.) - await withSharedTestDatabaseLease(payload, CAMPAIGN_INVITE_CONSENT_LEASE_KEY, () => - withSharedTestDatabaseLease(payload, SUPPORTER_REGISTRATION_CONSENT_LEASE_KEY, () => - withSharedTestDatabaseLease( + // EXCLUSIVE leases on the stable keys other spec files exercise (D10 F2): + // the provision REWRITES the shared rows' texts, and a consumer that reads + // the canonical consent between two of those writes would assert against a + // text that changed under it (observed: campaignInviteUi's preview + // `requiresConsent`/`consentData` mismatch). Exclusive serializes every + // consumer for the ~500ms of the provision — bounded, unlike the pre-D10 + // cascade where the ONDA0 exclusive sat behind a queue of EXCLUSIVE + // writers. The down-test below restores the removed rows with their + // original ids atomically, so the shared rows are never observed absent. + await withExclusiveTestDatabaseLease(payload, CAMPAIGN_INVITE_CONSENT_LEASE_KEY, () => + withExclusiveTestDatabaseLease(payload, SUPPORTER_REGISTRATION_CONSENT_LEASE_KEY, () => + withExclusiveTestDatabaseLease( payload, SUPPORTER_VOTE_INTENTION_CONSENT_LEASE_KEY, async () => { @@ -113,34 +110,54 @@ describe('Onda 0 provision (integration)', () => { expect(privacyRows.rows[0]?.published).toBe(true) expect(privacyRows.rows[0]?.body).toBeTruthy() - await removeOnda0ConsentAndPrivacyDb(db) + // The down-migration must stay invisible to parallel spec files: + // delete + count + restore run in ONE transaction, so no consumer + // ever observes the stable keys committed-absent or re-created + // with a new id (D10 F2 — the non-atomic version flaked load runs + // with `consent_id` FK violations and fail-closed reads). + const transactionID = await payload.db.beginTransaction() + if (transactionID === null) { + throw new Error('Expected a PostgreSQL transaction for the Onda0 down.') + } + const transactionDb = payload.db.sessions?.[String(transactionID)]?.db as + | typeof db + | undefined + if (!transactionDb) { + await payload.db.rollbackTransaction(transactionID).catch(() => undefined) + throw new Error('The Onda0 down transaction session is unavailable.') + } + try { + await removeOnda0ConsentAndPrivacyDb(transactionDb) - const consentAfterDown = await db.execute(sql` - SELECT count(*)::integer AS count - FROM "consent" - WHERE "key" IN (${sql.join( - ONDA0_CONSENT_KEY_LIST.map((key) => sql`${key}`), - sql`, `, - )}) - `) - expect(consentAfterDown.rows[0]?.count).toBe(0) + const consentAfterDown = await transactionDb.execute(sql` + SELECT count(*)::integer AS count + FROM "consent" + WHERE "key" IN (${sql.join( + ONDA0_CONSENT_KEY_LIST.map((key) => sql`${key}`), + sql`, `, + )}) + `) + expect(consentAfterDown.rows[0]?.count).toBe(0) - const privacyAfterDown = await db.execute(sql` - SELECT "published" - FROM "privacy_policy" - LIMIT 1 - `) - expect(privacyAfterDown.rows[0]?.published).toBe(false) + const privacyAfterDown = await transactionDb.execute(sql` + SELECT "published" + FROM "privacy_policy" + LIMIT 1 + `) + expect(privacyAfterDown.rows[0]?.published).toBe(false) - // The down-migration must stay a no-op for parallel spec files: - // restoring the removed rows with their ORIGINAL ids (inside the - // exclusive lease, invisible to them) keeps the stable keys - // present with unchanged ids, so no other file's restore assertion - // can observe the absence (D9 flake). - await restoreConsentRows( - (query) => db.execute(query), - consentRows.rows as ConsentSnapshot[], - ) + // Restore the removed rows with their ORIGINAL ids inside the + // same transaction — the commit publishes delete+restore as one + // atomic state change. + await restoreConsentRows( + (query) => transactionDb.execute(query), + consentRows.rows as ConsentSnapshot[], + ) + await payload.db.commitTransaction(transactionID) + } catch (error) { + await payload.db.rollbackTransaction(transactionID).catch(() => undefined) + throw error + } }, ), ), diff --git a/tests/int/testDatabaseLease.int.spec.ts b/tests/int/testDatabaseLease.int.spec.ts index 7bb01d0b..4231385c 100644 --- a/tests/int/testDatabaseLease.int.spec.ts +++ b/tests/int/testDatabaseLease.int.spec.ts @@ -10,6 +10,8 @@ import { acquireTestDatabaseLease, CAMPAIGN_INVITE_CONSENT_LEASE_KEY, ensureLeasedConsent, + fixtureConsentText, + purgeOrphanedConsentRenames, startTestDatabaseLeaseAcquisition, waitForAdvisoryLockWaiter, withInviteConsent, @@ -182,6 +184,7 @@ describe('test database lease', () => { const before = await withInviteConsent(payload, async (consent) => consent) let writerAcquisition: Promise | undefined + let readerAcquisition: Promise | undefined try { await withMissingInviteConsentFixture(payload, async () => { let writerEntered = false @@ -202,14 +205,44 @@ describe('test database lease', () => { }) expectExactWaiter(waiting, writerPID, 'ExclusiveLock') expect(writerEntered).toBe(false) + + // D10 F2: the window is EXCLUSIVE, so even a SHARED consumer queues + // while the canonical key is committed-absent — no consumer can + // observe the transient absence (baseline load runs flaked 3/4 with + // consumers reading the absent key or referencing a row the restore + // deleted out from under them). The reader is NOT awaited here: it + // only unblocks when this window releases, which is after the + // operation returns. + let readerEntered = false + const pendingReader = startTestDatabaseLeaseAcquisition( + payload, + CAMPAIGN_INVITE_CONSENT_LEASE_KEY, + 'shared', + ) + readerAcquisition = pendingReader.acquisition.then((lease) => { + readerEntered = true + return lease + }) + const readerPID = await pendingReader.backendPID + const readerWaiting = await waitForAdvisoryLockWaiter(payload, { + key: `test:${CAMPAIGN_INVITE_CONSENT_LEASE_KEY}`, + mode: 'ShareLock', + waiterPID: readerPID, + }) + expectExactWaiter(readerWaiting, readerPID, 'ShareLock') + expect(readerEntered).toBe(false) }) } finally { - // Release the waiter lease even when an assertion above fails, so the - // pending acquisition never blocks parallel spec files until teardown. + // Release the waiter leases even when an assertion above fails, so the + // pending acquisitions never block parallel spec files until teardown. if (writerAcquisition) { const writer = await writerAcquisition await writer.release() } + if (readerAcquisition) { + const reader = await readerAcquisition + await reader.release() + } } const after = await withInviteConsent(payload, async (consent) => consent) @@ -473,4 +506,176 @@ describe('test database lease', () => { const subsequent = await acquireTestDatabaseLease(payload, leaseKey) await subsequent.release() }) + + it('purges orphaned consent renames left by an aborted run on ensure', async () => { + // D10 F1: every consent fixture must clear the rename shapes an aborted + // run can leave behind before snapshotting, or the next run's rename hits + // the UNIQUE constraint. Legitimate rows (the private key below) survive. + const privateKey = `purge-ensure-${Date.now()}-${Math.random()}` + const privateLeaseKey = `purge-ensure-lease-${Date.now()}-${Math.random()}` + const legitKey = `purge-legit-${Date.now()}-${Math.random()}` + const orphanIDs: number[] = [] + let legitID = 0 + // The orphans are planted under the key's EXCLUSIVE lease: the rename + // keys are LIVE inside other spec files' fixtures, and an unleased + // create could hit their UNIQUE constraint mid-rename (D10 F2 load + // measurement). The lease start also purges any leftover from a previous + // failed run of this very test, so the plant never collides with it. + const plantLease = await acquireTestDatabaseLease(payload, privateLeaseKey) + try { + await purgeOrphanedConsentRenames((query) => payload.db.drizzle.execute(query)) + for (const key of [ + 'temporarily-renamed-consent', + 'chave-renomeada', + 'chave-temporariamente-ausente', + ]) { + const orphan = await payload.create({ + collection: 'consent', + data: { key, text: fixtureConsentText }, + depth: 0, + }) + orphanIDs.push(orphan.id) + } + const legit = await payload.create({ + collection: 'consent', + data: { key: legitKey, text: fixtureConsentText }, + depth: 0, + }) + legitID = legit.id + } finally { + await plantLease.release() + } + + try { + const configured = await ensureLeasedConsent(payload, { + consentKey: privateKey, + leaseKey: privateLeaseKey, + }) + expect(configured.key).toBe(privateKey) + + for (const orphanID of orphanIDs) { + const orphan = await payload.find({ + collection: 'consent', + where: { id: { equals: orphanID } }, + depth: 0, + limit: 1, + }) + expect(orphan.docs).toHaveLength(0) + } + expect( + await payload.findByID({ collection: 'consent', id: legitID, depth: 0 }), + ).toMatchObject({ key: legitKey }) + } finally { + // Cleanup runs even on failure — a failed run of this very test must + // not leave the orphans it planted behind (self-healing tests). + for (const orphanID of orphanIDs) { + await payload + .delete({ collection: 'consent', id: orphanID, depth: 0 }) + .catch(() => undefined) + } + if (legitID > 0) { + await payload + .delete({ collection: 'consent', id: legitID, depth: 0 }) + .catch(() => undefined) + } + } + }) + + it('purges orphaned renames during the missing-consent fixture setup', async () => { + const privateKey = `purge-missing-${Date.now()}-${Math.random()}` + const privateLeaseKey = `purge-missing-lease-${Date.now()}-${Math.random()}` + // Planted under the exclusive lease (the rename keys are live inside + // other spec files' fixtures — an unleased create could hit their UNIQUE + // constraint mid-rename). The lease start also purges any leftover from + // a previous failed run of this very test. + const plantLease = await acquireTestDatabaseLease(payload, privateLeaseKey) + let orphanID = 0 + try { + await purgeOrphanedConsentRenames((query) => payload.db.drizzle.execute(query)) + orphanID = ( + await payload.create({ + collection: 'consent', + data: { key: 'chave-temporariamente-ausente', text: fixtureConsentText }, + depth: 0, + }) + ).id + } finally { + await plantLease.release() + } + + await withMissingInviteConsentFixture(payload, async () => undefined, { + consentKey: privateKey, + leaseKey: privateLeaseKey, + }) + + try { + const orphan = await payload.find({ + collection: 'consent', + where: { id: { equals: orphanID } }, + depth: 0, + limit: 1, + }) + expect(orphan.docs).toHaveLength(0) + } finally { + // A failed run of this very test must not leave its plant behind. + await payload.delete({ collection: 'consent', id: orphanID, depth: 0 }).catch(() => undefined) + } + }) + + it('self-heals the canonical invite consent after an aborted rename on the next fixture run', async () => { + // The exact residue an aborted run leaves: the canonical row committed + // with a temporary key and the snapshot lost. The plant holds the + // exclusive lease so parallel spec files never observe the state; the + // fixture's own purge then heals it — the in-fixture rename must succeed + // instead of hitting the UNIQUE `consent_key_idx`. The lease start also + // purges any leftover from a previous failed run of this very test. + const plantLease = await acquireTestDatabaseLease(payload, CAMPAIGN_INVITE_CONSENT_LEASE_KEY) + try { + await purgeOrphanedConsentRenames((query) => payload.db.drizzle.execute(query)) + const canonical = await payload.find({ + collection: 'consent', + where: { key: { equals: 'lideranca-autopreenchimento' } }, + limit: 1, + sort: 'id', + depth: 0, + }) + if (!canonical.docs[0]) { + throw new Error( + 'The canonical invite consent row must exist before the aborted-rename repro.', + ) + } + await payload.update({ + collection: 'consent', + id: canonical.docs[0].id, + data: { key: 'chave-renomeada' }, + depth: 0, + }) + } finally { + await plantLease.release() + } + + let renamedDuringFixture = false + await withMutableConsentFixture(payload, async (consent) => { + // Without the F1 purge this rename throws "Valor deve ser único". + await payload.update({ + collection: 'consent', + id: consent.id, + data: { key: 'chave-renomeada' }, + depth: 0, + }) + renamedDuringFixture = true + }) + expect(renamedDuringFixture).toBe(true) + + // Second run proves the run following the healed one stays clean. + await withMutableConsentFixture(payload, async () => undefined) + + const orphans = await payload.find({ + collection: 'consent', + where: { key: { equals: 'chave-renomeada' } }, + depth: 0, + pagination: false, + }) + expect(orphans.totalDocs).toBe(0) + }) }) From 6de8fad8f67b51f0f560d023344ef9f1f194038c Mon Sep 17 00:00:00 2001 From: Francisco Solla Date: Tue, 11 Aug 2026 10:47:02 -0300 Subject: [PATCH 2/6] =?UTF-8?q?feat(C112):=20m=C3=BAltiplos=20telefones=20?= =?UTF-8?q?por=20pessoa=20=E2=80=94=20Contact.phones=20array=20(ordem=20?= =?UTF-8?q?=3D=20prioridade)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- .../multiplos-telefones-por-pessoa-impl.md | 71 + scripts/seed-minimal.mjs | 16 +- .../acoes/wizardLeadershipFormActions.ts | 9 +- .../(app)/apoiadores/[id]/formActions.ts | 33 +- .../campanha/(app)/apoiadores/[id]/page.tsx | 16 +- .../(app)/apoiadores/novo/formActions.ts | 15 +- .../campanha/(app)/assessores/[id]/page.tsx | 30 +- .../campanha/(app)/assessores/formActions.ts | 22 + .../(app)/atividades/contactSearchActions.ts | 11 +- .../campanha/(app)/dobradinhas/[id]/page.tsx | 2 +- .../campanha/(app)/dobradinhas/formActions.ts | 7 + .../campanha/(app)/liderancas/[id]/page.tsx | 2 +- .../campanha/(app)/liderancas/formActions.ts | 7 + .../(app)/liderancas/nova/formActions.ts | 9 +- .../(campaign)/campanha/actions/advisor.ts | 24 + .../campanha/actions/leaderSupporter.ts | 17 +- .../(campaign)/campanha/actions/leadership.ts | 32 +- .../campanha/actions/stateDeputy.ts | 23 +- .../(campaign)/campanha/actions/supporter.ts | 60 +- .../campanha/actions/supporterImport.ts | 23 +- .../actions/submitPetitionSignature.ts | 8 +- src/app/(frontend)/actions/submitWhatsapp.ts | 8 +- src/collections/CampaignUser.ts | 30 +- src/collections/Contact.ts | 94 +- .../campaign/leadership/LeaderContactForm.tsx | 34 +- .../leadership/LeadershipContactSection.tsx | 20 +- .../campaign/leadership/LeadershipForm.tsx | 22 +- .../leadership/WizardLeadershipForm.tsx | 24 +- .../campaign/shared/PhonesFieldEditor.tsx | 285 + .../stateDeputy/StateDeputyContactSection.tsx | 21 +- .../campaign/supporter/SupporterForm.tsx | 30 +- src/lib/formData.ts | 18 + src/lib/phone.ts | 35 + src/lib/schemas/contact.ts | 20 + src/lib/schemas/leadership.ts | 7 +- src/lib/schemas/supporter.ts | 6 +- src/lib/wizardLeadershipContract.ts | 2 + ...60811_111828_add_contact_phones_array.json | 13337 ++++++++++++++++ ...0260811_111828_add_contact_phones_array.ts | 41 + src/migrations/index.ts | 6 + src/payload-types.ts | 14 +- src/utilities/activityLeadershipOptions.ts | 2 +- src/utilities/activityResponsibleSearch.ts | 2 +- src/utilities/activityViewModels.ts | 3 +- src/utilities/advisorData.ts | 28 +- src/utilities/campaignInviteCreation.ts | 6 +- src/utilities/campaignInvitePageData.ts | 5 +- src/utilities/campaignInviteRedemption.ts | 22 +- src/utilities/contactIdentity.ts | 58 +- .../homeSearch/searchHomeLeaderships.ts | 7 +- src/utilities/leaderContactsPageData.ts | 3 +- src/utilities/leadership/leadershipData.ts | 22 +- src/utilities/people/peopleData.ts | 3 +- src/utilities/people/personDelete.ts | 7 +- src/utilities/signatureExport.ts | 3 +- src/utilities/stateDeputyData.ts | 11 +- .../supporter/supporterImportBulk.ts | 40 +- .../supporter/supporterListFilters.ts | 5 +- .../supporter/supporterListSqlFilters.ts | 9 +- .../supporter/supporterViewModels.ts | 10 +- tests/e2e/campaignHomeActions.e2e.spec.ts | 2 +- tests/e2e/campaignMunicipalities.e2e.spec.ts | 4 +- tests/e2e/fixtures/campaignE2EFixtures.ts | 2 +- tests/helpers/campaignFixtures.ts | 20 +- tests/int/campaignContactIdentity.int.spec.ts | 40 +- tests/int/campaignFixtures.int.spec.ts | 6 +- tests/int/campaignInvite.int.spec.ts | 25 +- tests/int/campaignInviteUi.int.spec.ts | 10 +- tests/int/campaignLeadership.int.spec.ts | 65 +- .../int/campaignLeadershipActions.int.spec.ts | 26 +- tests/int/campaignMultiPhones.int.spec.ts | 458 + ...aignMunicipalityLeadershipList.int.spec.ts | 6 +- ...nMunicipalityStateDeputyCreate.int.spec.ts | 8 +- tests/int/campaignSupporter.int.spec.ts | 36 +- tests/int/campaignUserPhone.int.spec.ts | 1 - .../int/collectionAccessLockdown.int.spec.ts | 6 +- .../int/contactPhoneNormalization.int.spec.ts | 30 +- tests/int/personDelete.int.spec.ts | 3 +- tests/int/submitPetitionSignature.int.spec.ts | 2 +- tests/int/submitWhatsapp.int.spec.ts | 2 +- tests/int/supporterListFilters.int.spec.ts | 2 +- tests/unit/signatureExport.unit.spec.ts | 4 +- tests/unit/supporterSchema.unit.spec.ts | 10 +- 83 files changed, 15116 insertions(+), 389 deletions(-) create mode 100644 docs/plans/multiplos-telefones-por-pessoa-impl.md create mode 100644 src/components/campaign/shared/PhonesFieldEditor.tsx create mode 100644 src/migrations/20260811_111828_add_contact_phones_array.json create mode 100644 src/migrations/20260811_111828_add_contact_phones_array.ts create mode 100644 tests/int/campaignMultiPhones.int.spec.ts diff --git a/docs/plans/multiplos-telefones-por-pessoa-impl.md b/docs/plans/multiplos-telefones-por-pessoa-impl.md new file mode 100644 index 00000000..b7e70624 --- /dev/null +++ b/docs/plans/multiplos-telefones-por-pessoa-impl.md @@ -0,0 +1,71 @@ +# Impl: Múltiplos telefones por pessoa + +Status: aprovado +Atualizado em: 2026-08-11 +Issue: #626 +Intenção: docs/plans/multiplos-telefones-por-pessoa.md +Appetite restante: ~1–2 dias eng (intenção); sem cortes de aceite + +## Leitura da intenção + +- **Outcome:** toda pessoa (liderança, apoiador, dobradinha, assessor) tem N telefones na ficha; listas mostram 1 (o principal); ficha mostra todos; WhatsApp/convite/import usam o principal; busca por telefone acha por qualquer número; guardrail C111 (números compartilhados entre pessoas) preservado. +- **O que NÃO negociar:** ordem = prioridade sem flag explícita; sem rótulos/tipos de telefone; import CSV com uma coluna; login da liderança continua o `username` (não muda); dedupe dentro da MESMA ficha bloqueia salvar; **UI pública intocada** — formulários de petição/WhatsApp (e o componente `PhoneInput`) não mudam; a mudança de dados entra só no mapeamento server-side das ações públicas. +- **O que reavaliar:** a hipótese da intenção listava `src/lib/phone.ts`, view models e import — ok; mas **assessor** tem camada extra (o telefone mora em `campaignUser.phone`, espelhado na ficha) e **`Contact.phone` é lido pelo site público** (petição/WhatsApp) e por export — a intenção não citava esses dois. + +## Abordagem recomendada + +```mermaid +flowchart LR + C[Contact] -->|array field| P[phones: {value}[]] + P -->|primary| H[primaryPhoneOf] + H --> VM[view models / listas / wa.me / convite] + W[formulários criação/edição] -->|PhonesFieldEditor| P + S[busca/import/findOrCreate] -->|where phones.value| P +``` + +**Opções consideradas:** A | B | C +**Recomendação:** A — array field Payload `phones` com subcampo `value` na `Contact`, removendo `phone` (fonte única). Payload onde com `'phones.value': { equals/contains }` resolve busca/dedupe com JOIN nativo, `_order` preserva ordem = prioridade, e o admin UI ganha a edição de graça. +**Rejeitadas:** + +- B — `jsonb phones: string[]` (custom/JSON): perde query/index Payload; `supporterListSqlFilters` e `supporterImportBulk` teriam que viver de SQL cru e a busca Payload (supporter filters, pickers) ficaria sem where nativo. Pior contrato para o caso de uso mais comum. +- C — collection join `contactPhone`: entidade demais para um campo sem rótulos/tipos; clareza zero de "principal" (teria `_order` escondido do mesmo jeito); admin group extra. +- D — manter `phone` + `phones` desnormalizados: duas fontes de verdade sincronizando em N write paths; rejeitado por invariante (uma fonte). + +### Componentes / mudanças + +- **`phones`** (`src/collections/Contact.ts`): array field `phones` (label "Telefones", subcampo `value` text 11 dígitos, `required: false`); remove o campo `phone`. `beforeValidate` da collection normaliza cada item (`normalizeBrazilianPhone` / regex 11 dígitos), filtra vazios, rejeita inválido com `BRAZILIAN_PHONE_INVALID_MESSAGE` e rejeita **duplicado dentro da ficha** (nova copy: `BRAZILIAN_PHONE_DUPLICATE_MESSAGE` = "Telefone repetido na ficha."). +- **Migration** `add_contact_phones_array`: tabela gerada `contact_phones` (\_parent_id, \_order, value) + backfill manual `INSERT ... SELECT id, phone, 0 FROM contact WHERE phone IS NOT NULL` + `DROP COLUMN contact.phone` (derruba `contact_phone_idx` junto) + índice manual `contact_phones (value)` (Payload não indexa subcampo de array; o equals/in da busca precisa). +- **`primaryPhoneOf`** (`src/lib/phone.ts`): `phones[0]?.value ?? null` — o único tradutor array→principal. Todos os readers de lista/ficha passam por ele (view models `leadershipData`, `stateDeputyData`, `peopleData`, `supporterViewModels`, `leaderContactsPageData`, `campaignInvitePageData`, `activityViewModels`, home search, `MunicipalityDossier`, `ContactCombobox`, `wizardLeadershipContract`). +- **`setContactPhones`** (nova utility `src/utilities/contactPhones.ts`): read-modify-write transacional do array (usada pelas actions de edição inline "seta principal preservando o resto" e pelos editors de ficha); `normalizeContactPhones` (pure, compartilhada com o hook e com o zod). +- **`PhonesFieldEditor`** (novo componente `src/components/campaign/shared/PhonesFieldEditor.tsx`): inputs mascarados repetíveis (reusa `FormattedInput` + `formatBrazilianPhoneInput`), remover por linha, "+ Adicionar telefone", setas ↑/↓ de reordenação (ordem = prioridade; sem arrasto — anti-goal). Serializa `phones` repetido no FormData (padrão `repeatedRelationshipFormValues`; novo `repeatedPhoneFormValues` em `src/lib/formData.ts`). +- **Schemas** (`src/lib/schemas/`): **`contactSchema` permanece com `phone`** (só o público usa — petição/WhatsApp); as ações públicas mapeiam `phone → phones: [{ value }]` ao gravar a `Contact` (UI e zod públicos intocados). Nos schemas de campanha: `leadershipCreateSchema`/`wizard`/`supporter`/`leaderSupporter`/`invite` trocam `phone` por `phones` (array, min 1 nos fluxos que exigiam); `contactFieldUpdateSchema`: branch `field: 'phone'` passa a significar "escrever principal preservando o resto" e novo branch `field: 'phones'` (array completo) para os editors de ficha. +- **Access / Consent:** nenhum (campo interno de `Contact`; sem Consent novo). +- **UI:** Impeccable B — formulários de criação/edição + ficha + listas existentes; shape→craft→critique→polish no editor compartilhado; shells e células inline existentes (`CampaignInlineEditableCell`) permanecem para as listas (mostram o principal, editam o principal). + +## Fases verificáveis + +1. **Tracer schema+server** — campo array + hook + migration (backfill+drop+índice) + `primaryPhoneOf` + `findOrCreateContactByPhone` via `phones.value` + view models trocando para o principal + ações públicas mapeando `phone → phones[0]` (sem tocar UI/zod públicos) + `signatureExport` + seed/fixtures. **Spike aqui:** confirmar semântica do `where: { 'phones.value': { equals/contains } }` em Payload 3.82 (int spec) — se falhar, fallback é JOIN cru nos poucos sites Payload-where. +2. **Write paths** — zod/schemas das 5 superfícies + actions (leadership create/wizard/inline, supporter create/tombstone LGPD, stateDeputy inline, leaderSupporter, convite redemption, sync `CampaignUser` (append se faltar), import CSV preview+bulk) + where de busca (`supporterListFilters`, `supporterListSqlFilters`, pickers de atividade, combobox). +3. **UI** — `PhonesFieldEditor` + forms de criação (LeadershipForm, SupporterForm, LeaderContactForm, WizardLeadershipForm) + fichas (LeadershipContactSection, StateDeputyContactSection, "Dados de contato" do apoiador com editor novo, assessor ganha seção "Telefones" na ficha) + células inline inalteradas na lista. +4. **Gates** — unit (primaryPhoneOf, normalize/dedupe, schemas, formData) + int (multi-telefone nas 5 superfícies, dedupe bloqueia, principal preservado no inline, import, convite, busca por qualquer número, tombstone substitui TODOS os telefones, sync append) + `pnpm gate:fast` + `pnpm push`. + +## Rabbit holes / Não escopo (engenharia) + +- Não adicionar busca por telefone nas listas que hoje só buscam por nome (lideranças, dobradinhas, pessoas, assessores) — o aceite é "a busca por telefone encontra por qualquer número" onde busca por telefone existe (apoiadores, pickers); adicionar nas outras é item futuro se a mesa pedir. **Gatilho de revisitação:** mesa perguntar "por que não acho a liderança pelo número na lista de lideranças". +- Sem cap de quantidade de telefones; sem flag "principal"; sem arrasto (setas são suficientes para trocar prioridade). +- `campaignUser.phone` permanece campo único da conta (canal de contato/login); não vira array. +- Sem espelho ficha→conta (conta continua espelhando UNICAMENTE via sync existente conta→ficha, agora append). + +## Riscos e mitigação + +- **Payload onde em subcampo de array** (`'phones.value'`): spike na Fase 1 com int spec; fallback JOIN cru documentado nos poucos sites. +- **Migration destrutiva em prod (PII real):** backfill roda ANTES do drop no mesmo migration; `migrate:status` local + revisão do SQL no checklist de deploy. +- **Churn de testes** (muitos specs leem `contact.phone`): helper de fixture `createContact` continua aceitando `phone` e mapeia internamente para `phones` — maioria dos specs int fica intocada; asserts com `where: { phone }` viram `where: { 'phones.value': … }`. +- **Race do sync conta→ficha (append)**: sem transação garantida no hook; se o append colidir com edição concorrente, o dedupe fail-closed rejeita o save da conta (raro, mensagem clara). +- **Inline edit de lista** apagar principal: "limpar o campo" remove o index 0 (o resto sobe); nunca zera os demais. + +## Aceite de engenharia + +- [ ] Aceite de produto da intenção ainda coberto (N telefones, lista=principal, ficha=todos, wa.me/convite/import=principal, busca por qualquer, C111 preservado, dedupe interno) +- [ ] Invariantes AGENTS/engineering-standards (fonte única em `Contact`; transação+req nas escritas multi-collection; copy pt-BR; identificadores inglês) +- [ ] Testes de domínio previstos (unit/int) onde access/write paths mudam — acima diff --git a/scripts/seed-minimal.mjs b/scripts/seed-minimal.mjs index 60d97938..73c6cb5d 100644 --- a/scripts/seed-minimal.mjs +++ b/scripts/seed-minimal.mjs @@ -199,8 +199,12 @@ try { for (const entry of MINIMAL_LEADERSHIPS) { const contact = await upsert( 'contact', - { phone: { equals: entry.contactPhone } }, - { name: entry.contactName, phone: entry.contactPhone, state: 'BA' }, + { 'phones.value': { equals: entry.contactPhone } }, + { + name: entry.contactName, + phones: [{ value: entry.contactPhone }], + state: 'BA', + }, `contact ${entry.contactPhone}`, ) const consent = consentIdByKey.get(entry.consentKey) @@ -226,8 +230,12 @@ try { for (const entry of MINIMAL_SUPPORTERS) { const contact = await upsert( 'contact', - { phone: { equals: entry.contactPhone } }, - { name: entry.contactName, phone: entry.contactPhone, state: 'BA' }, + { 'phones.value': { equals: entry.contactPhone } }, + { + name: entry.contactName, + phones: [{ value: entry.contactPhone }], + state: 'BA', + }, `contact ${entry.contactPhone}`, ) const municipalityID = await municipalityIdBySlug(entry.municipalitySlug) diff --git a/src/app/(campaign)/campanha/(app)/acoes/wizardLeadershipFormActions.ts b/src/app/(campaign)/campanha/(app)/acoes/wizardLeadershipFormActions.ts index dea6c4d2..fc94467d 100644 --- a/src/app/(campaign)/campanha/(app)/acoes/wizardLeadershipFormActions.ts +++ b/src/app/(campaign)/campanha/(app)/acoes/wizardLeadershipFormActions.ts @@ -5,7 +5,12 @@ import { createLeadershipWizard, updateLeadershipWizard, } from '@/app/(campaign)/campanha/actions/leadership' -import { optionalFormText, requiredFormText, requiredRelationshipFormValue } from '@/lib/formData' +import { + optionalFormText, + repeatedPhoneFormValues, + requiredFormText, + requiredRelationshipFormValue, +} from '@/lib/formData' import { LEADERSHIP_DUPLICATE_MESSAGE, LEADERSHIP_INVALID_CONTACT_MESSAGE, @@ -37,7 +42,7 @@ const parseSupportStatus = (formData: FormData) => { const wizardFieldsFromForm = (formData: FormData) => ({ name: requiredFormText(formData, 'name'), - phone: requiredFormText(formData, 'phone'), + phones: repeatedPhoneFormValues(formData, 'phones'), email: optionalFormText(formData, 'email'), exclusive: parseExclusive(formData), supportStatus: parseSupportStatus(formData), diff --git a/src/app/(campaign)/campanha/(app)/apoiadores/[id]/formActions.ts b/src/app/(campaign)/campanha/(app)/apoiadores/[id]/formActions.ts index 6ff4c709..eb0635e9 100644 --- a/src/app/(campaign)/campanha/(app)/apoiadores/[id]/formActions.ts +++ b/src/app/(campaign)/campanha/(app)/apoiadores/[id]/formActions.ts @@ -10,9 +10,15 @@ import { redirect } from 'next/navigation' import { removeSupporterData, setSupporterVoteIntention, + updateSupporterContact, } from '@/app/(campaign)/campanha/actions/supporter' import { SUPPORTER_VOTE_INTENTION_CONSENT_MISSING_MESSAGE } from '@/lib/campaignConsentKeys' -import { checkboxFormValue, requiredRelationshipFormValue } from '@/lib/formData' +import { + checkboxFormValue, + repeatedPhoneFormValues, + requiredRelationshipFormValue, +} from '@/lib/formData' +import { BRAZILIAN_PHONE_DUPLICATE_MESSAGE, BRAZILIAN_PHONE_INVALID_MESSAGE } from '@/lib/phone' import type { SupporterVoteIntention } from '@/lib/schemas/supporter' import { SUPPORTER_STAFF_MESSAGE, @@ -21,6 +27,8 @@ import { } from '@/lib/schemas/supporter' import { mapCampaignFormActionError, + runCampaignFormAction, + type CampaignFormActionState, type CampaignFormErrorState, } from '@/utilities/campaignFormActionError' import { firstFormActionMessage } from '@/utilities/campaignFormFields' @@ -41,6 +49,12 @@ const safeVoteIntentionMessages = [ SUPPORTER_STAFF_MESSAGE, ] as const +const safeContactMessages = [ + SUPPORTER_STAFF_MESSAGE, + BRAZILIAN_PHONE_INVALID_MESSAGE, + BRAZILIAN_PHONE_DUPLICATE_MESSAGE, +] as const + const toMessageOnlyState = (mapped: CampaignFormErrorState): { message?: string } => { const message = firstFormActionMessage(mapped) return message ? { message } : {} @@ -98,3 +112,20 @@ export const removeSupporterDataFormAction = async ( ) } } + +export const updateSupporterContactFormAction = async ( + _state: CampaignFormActionState, + formData: FormData, +): Promise => + runCampaignFormAction({ + execute: async () => { + await updateSupporterContact({ + id: requiredRelationshipFormValue(formData, 'supporterId'), + field: 'phones', + phones: repeatedPhoneFormValues(formData, 'phones'), + }) + return { message: 'Telefones atualizados.' } + }, + safeMessages: safeContactMessages, + genericMessage: 'Não foi possível salvar os telefones.', + }) diff --git a/src/app/(campaign)/campanha/(app)/apoiadores/[id]/page.tsx b/src/app/(campaign)/campanha/(app)/apoiadores/[id]/page.tsx index 7b6f2bac..5ada0bee 100644 --- a/src/app/(campaign)/campanha/(app)/apoiadores/[id]/page.tsx +++ b/src/app/(campaign)/campanha/(app)/apoiadores/[id]/page.tsx @@ -4,8 +4,12 @@ import Link from 'next/link' import { notFound, redirect } from 'next/navigation' import { getPayload } from 'payload' -import { setSupporterVoteIntentionFormAction } from '@/app/(campaign)/campanha/(app)/apoiadores/[id]/formActions' +import { + setSupporterVoteIntentionFormAction, + updateSupporterContactFormAction, +} from '@/app/(campaign)/campanha/(app)/apoiadores/[id]/formActions' import { ConsentText } from '@/components/campaign/shared/ConsentText' +import { PhonesFieldEditor } from '@/components/campaign/shared/PhonesFieldEditor' import { SetCampaignPageChrome } from '@/components/campaign/shell/CampaignPageChromeContext' import { RemoveSupporterDataButton } from '@/components/campaign/supporter/RemoveSupporterDataButton' import { SupporterShareKit } from '@/components/campaign/supporter/SupporterShareKit' @@ -112,7 +116,15 @@ export default async function SupporterDetailPage({ params }: SupporterDetailPag Dados de contato -

{supporter.phoneDisplay || '—'}

+ {supporter.email ?

{supporter.email}

: null}

{supporter.municipalityName ? ( diff --git a/src/app/(campaign)/campanha/(app)/apoiadores/novo/formActions.ts b/src/app/(campaign)/campanha/(app)/apoiadores/novo/formActions.ts index 777b905d..1e62292b 100644 --- a/src/app/(campaign)/campanha/(app)/apoiadores/novo/formActions.ts +++ b/src/app/(campaign)/campanha/(app)/apoiadores/novo/formActions.ts @@ -7,8 +7,12 @@ import { SUPPORTER_REGISTRATION_CONSENT_MISSING_MESSAGE, SUPPORTER_VOTE_INTENTION_CONSENT_MISSING_MESSAGE, } from '@/lib/campaignConsentKeys' -import { checkboxFormValue, nullableRelationshipFormValue, optionalFormText } from '@/lib/formData' -import { sanitizeBrazilianPhoneInput } from '@/lib/phone' +import { + checkboxFormValue, + nullableRelationshipFormValue, + optionalFormText, + repeatedPhoneFormValues, +} from '@/lib/formData' import { SUPPORTER_DUPLICATE_MESSAGE, SUPPORTER_STAFF_MESSAGE, @@ -28,7 +32,7 @@ export type SupporterFormState = { type SupporterFormValues = { name?: string - phone?: string + phones?: string[] email?: string city?: string municipality?: string @@ -50,7 +54,7 @@ export const createSupporterFormAction = async ( // Parsed before the ladder (non-throwing readers) so failures echo them back. const values: SupporterFormValues = { name: optionalFormText(formData, 'name'), - phone: optionalFormText(formData, 'phone'), + phones: repeatedPhoneFormValues(formData, 'phones'), email: optionalFormText(formData, 'email'), city: optionalFormText(formData, 'city'), municipality: optionalFormText(formData, 'municipality'), @@ -59,13 +63,12 @@ export const createSupporterFormAction = async ( return runCampaignFormAction({ execute: async () => { - const phone = sanitizeBrazilianPhoneInput(values.phone ?? '') const voteIntention = values.voteIntention?.trim() || undefined const municipality = nullableRelationshipFormValue(formData, 'municipality') const input = supporterCreateSchema.parse({ name: values.name ?? '', - phone, + phones: values.phones ?? [], email: values.email, city: values.city, ...(municipality ? { municipality } : {}), diff --git a/src/app/(campaign)/campanha/(app)/assessores/[id]/page.tsx b/src/app/(campaign)/campanha/(app)/assessores/[id]/page.tsx index a11b028f..d698d1da 100644 --- a/src/app/(campaign)/campanha/(app)/assessores/[id]/page.tsx +++ b/src/app/(campaign)/campanha/(app)/assessores/[id]/page.tsx @@ -6,6 +6,7 @@ import { getPayload } from 'payload' import { AdvisorDebouncedTextCell } from '@/components/campaign/advisor/AdvisorDebouncedTextCell' import { AdvisorPasswordResetButton } from '@/components/campaign/advisor/AdvisorPasswordResetButton' +import { PhonesFieldEditor } from '@/components/campaign/shared/PhonesFieldEditor' import { SetCampaignPageChrome } from '@/components/campaign/shell/CampaignPageChromeContext' import { CampaignPageShell } from '@/components/campaign/shell/CampaignPageShell' import { Badge } from '@/components/ui/Badge' @@ -14,7 +15,11 @@ import { formatBrazilianPhoneInput } from '@/lib/phone' import { isPlanilhaPlaceholderEmail } from '@/lib/schemas/advisor' import { loadAdvisorDetail } from '@/utilities/advisorData' import { requireCampaignPageActor } from '@/utilities/campaignPageActor' -import { sendAdvisorPasswordResetFormAction, updateAdvisorProfileFormAction } from '../formActions' +import { + sendAdvisorPasswordResetFormAction, + updateAdvisorContactFormAction, + updateAdvisorProfileFormAction, +} from '../formActions' export async function generateMetadata({ params }: AdvisorDetailPageProps) { const { id: rawId } = await params @@ -115,6 +120,29 @@ export default async function AdvisorDetailPage({ params }: AdvisorDetailPagePro )} +

+

+ Telefones da pessoa +

+

+ Todos os números da ficha — o primeiro é o principal (listas, WhatsApp e convites). +

+ {advisor.contactID !== null ? ( + + ) : ( +

Ficha de contato ainda não vinculada.

+ )} +
+

Acesso diff --git a/src/app/(campaign)/campanha/(app)/assessores/formActions.ts b/src/app/(campaign)/campanha/(app)/assessores/formActions.ts index bc59f427..83afa3d8 100644 --- a/src/app/(campaign)/campanha/(app)/assessores/formActions.ts +++ b/src/app/(campaign)/campanha/(app)/assessores/formActions.ts @@ -4,10 +4,12 @@ import { createAdvisor, sendAdvisorPasswordReset, setAdvisorMunicipalitiesBatch, + updateAdvisorContactFicha, updateAdvisorProfile, } from '@/app/(campaign)/campanha/actions/advisor' import { nullableFormText, + repeatedPhoneFormValues, repeatedRelationshipFormValues, requiredFormBoolean, requiredFormText, @@ -88,6 +90,26 @@ export const sendAdvisorPasswordResetFormAction = async ( genericMessage: 'Não foi possível enviar o link de redefinição de senha.', }) +/** + * C112 — the advisor's ficha (Contact) phone list. The account keeps its own + * single channel phone ("Conta"); this editor targets the person's ficha. + */ +export const updateAdvisorContactFormAction = async ( + _state: CampaignFormActionState, + formData: FormData, +): Promise => + runCampaignFormAction({ + execute: async () => { + await updateAdvisorContactFicha({ + contactId: requiredRelationshipFormValue(formData, 'contactId'), + phones: repeatedPhoneFormValues(formData, 'phones'), + }) + return { message: 'Telefones atualizados.' } + }, + safeMessages: advisorSafeMessages, + genericMessage: 'Não foi possível salvar os telefones.', + }) + export const createAdvisorFormAction = async ( _state: CampaignFormActionState, formData: FormData, diff --git a/src/app/(campaign)/campanha/(app)/atividades/contactSearchActions.ts b/src/app/(campaign)/campanha/(app)/atividades/contactSearchActions.ts index 44d7e7aa..c708e250 100644 --- a/src/app/(campaign)/campanha/(app)/atividades/contactSearchActions.ts +++ b/src/app/(campaign)/campanha/(app)/atividades/contactSearchActions.ts @@ -5,6 +5,7 @@ import { getPayload } from 'payload' import type { ContactComboboxOption } from '@/components/campaign/shared/ContactCombobox' import { isContactSearchQueryReady, normalizeContactSearchQuery } from '@/lib/contactSearchQuery' +import { primaryPhoneOf } from '@/lib/phone' import { searchActivityResponsibleOptions, type ActivityResponsibleSearchOption, @@ -28,18 +29,22 @@ export const searchActivityContactOptions = async ( const result = await payload.find({ collection: 'contact', where: { - or: [{ name: { contains: trimmed } }, { phone: { contains: digits || trimmed } }], + or: [{ name: { contains: trimmed } }, { 'phones.value': { contains: digits || trimmed } }], }, depth: 0, limit: CONTACT_OPTION_LIMIT, page: 1, sort: 'name', - select: { name: true, phone: true }, + select: { name: true, phones: { value: true } }, user, overrideAccess: false, }) - return result.docs.map(({ id, name, phone }) => ({ id, name, phone: phone ?? null })) + return result.docs.map(({ id, name, phones }) => ({ + id, + name, + phone: primaryPhoneOf(phones), + })) } export const searchActivityResponsibleOptionsAction = async ( diff --git a/src/app/(campaign)/campanha/(app)/dobradinhas/[id]/page.tsx b/src/app/(campaign)/campanha/(app)/dobradinhas/[id]/page.tsx index ed5f5538..b2087d34 100644 --- a/src/app/(campaign)/campanha/(app)/dobradinhas/[id]/page.tsx +++ b/src/app/(campaign)/campanha/(app)/dobradinhas/[id]/page.tsx @@ -69,7 +69,7 @@ export default async function StateDeputyDetailPage({ params }: StateDeputyDetai stateDeputyId={stateDeputy.id} name={stateDeputy.name} email={stateDeputy.email} - phone={stateDeputy.phone} + phones={stateDeputy.phones} /> {stateDeputy.notes ? (

{stateDeputy.notes}

diff --git a/src/app/(campaign)/campanha/(app)/dobradinhas/formActions.ts b/src/app/(campaign)/campanha/(app)/dobradinhas/formActions.ts index 88e25e7a..ac66627d 100644 --- a/src/app/(campaign)/campanha/(app)/dobradinhas/formActions.ts +++ b/src/app/(campaign)/campanha/(app)/dobradinhas/formActions.ts @@ -9,6 +9,7 @@ import { } from '@/app/(campaign)/campanha/actions/stateDeputy' import { nullableFormText, + repeatedPhoneFormValues, repeatedRelationshipFormValues, requiredFormBoolean, requiredFormText, @@ -69,6 +70,12 @@ export const updateStateDeputyContactFormAction = async ( field: 'phone', phone: nullableFormText(formData, 'phone'), }) + } else if (field === 'phones') { + await updateStateDeputyContact({ + id, + field: 'phones', + phones: repeatedPhoneFormValues(formData, 'phones'), + }) } else { throw new Error(STATE_DEPUTY_INVALID_FIELD_MESSAGE) } diff --git a/src/app/(campaign)/campanha/(app)/liderancas/[id]/page.tsx b/src/app/(campaign)/campanha/(app)/liderancas/[id]/page.tsx index 7da9f977..c63de589 100644 --- a/src/app/(campaign)/campanha/(app)/liderancas/[id]/page.tsx +++ b/src/app/(campaign)/campanha/(app)/liderancas/[id]/page.tsx @@ -97,7 +97,7 @@ export default async function LeadershipDetailPage({ params }: LeadershipDetailP leadershipId={leadership.id} name={leadership.name} email={leadership.email} - phone={leadership.phone} + phones={leadership.phones} /> {leadership.stateDeputies.length > 0 ? ( diff --git a/src/app/(campaign)/campanha/(app)/liderancas/formActions.ts b/src/app/(campaign)/campanha/(app)/liderancas/formActions.ts index f1833ac1..8d9d409b 100644 --- a/src/app/(campaign)/campanha/(app)/liderancas/formActions.ts +++ b/src/app/(campaign)/campanha/(app)/liderancas/formActions.ts @@ -8,6 +8,7 @@ import { } from '@/app/(campaign)/campanha/actions/leadership' import { nullableFormText, + repeatedPhoneFormValues, repeatedRelationshipFormValues, requiredFormBoolean, requiredFormText, @@ -65,6 +66,12 @@ export const updateLeadershipContactFormAction = async ( field: 'phone', phone: nullableFormText(formData, 'phone'), }) + } else if (field === 'phones') { + await updateLeadershipContact({ + id, + field: 'phones', + phones: repeatedPhoneFormValues(formData, 'phones'), + }) } else { throw new Error(INVALID_FIELD_MESSAGE) } diff --git a/src/app/(campaign)/campanha/(app)/liderancas/nova/formActions.ts b/src/app/(campaign)/campanha/(app)/liderancas/nova/formActions.ts index bad50f50..3374efc7 100644 --- a/src/app/(campaign)/campanha/(app)/liderancas/nova/formActions.ts +++ b/src/app/(campaign)/campanha/(app)/liderancas/nova/formActions.ts @@ -2,7 +2,12 @@ import { leadershipStaffEditSafeMessages } from '@/app/(campaign)/campanha/(app)/liderancas/leadershipStaffEditMessages' import { createLeadership } from '@/app/(campaign)/campanha/actions/leadership' -import { optionalFormText, repeatedRelationshipFormValues, requiredFormText } from '@/lib/formData' +import { + optionalFormText, + repeatedPhoneFormValues, + repeatedRelationshipFormValues, + requiredFormText, +} from '@/lib/formData' import { LEADERSHIP_DUPLICATE_MESSAGE, LEADERSHIP_MUNICIPALITY_SCOPE_MESSAGE, @@ -33,7 +38,7 @@ export const createLeadershipFormAction = async ( return createLeadership({ name: requiredFormText(formData, 'name'), - phone: requiredFormText(formData, 'phone'), + phones: repeatedPhoneFormValues(formData, 'phones'), email: optionalFormText(formData, 'email'), municipalities: repeatedRelationshipFormValues(formData, 'municipalities'), organizations: repeatedRelationshipFormValues(formData, 'organizations'), diff --git a/src/app/(campaign)/campanha/actions/advisor.ts b/src/app/(campaign)/campanha/actions/advisor.ts index 4149f3fe..790aa2aa 100644 --- a/src/app/(campaign)/campanha/actions/advisor.ts +++ b/src/app/(campaign)/campanha/actions/advisor.ts @@ -23,6 +23,7 @@ import { type AdvisorPasswordResetInput, type AdvisorProfileUpdateInput, } from '@/lib/schemas/advisor' +import { contactPhonesSchema } from '@/lib/schemas/contact' import type { CampaignUser } from '@/payload-types' import { getCampaignActionContext, @@ -163,6 +164,29 @@ export const updateAdvisorProfile = async (input: AdvisorProfileUpdateInput) => return updated } +/** + * C112 — write the advisor's ficha (Contact) phone list. The advisor page is + * unrestricted-staff gated; the ficha is the person's record, distinct from + * the account's single channel phone. + */ +export const updateAdvisorContactFicha = async (input: { contactId: number; phones: string[] }) => { + const { payload, actor } = await getCampaignActionContext() + await reloadUnrestrictedActor(payload, actor, ADVISOR_UNRESTRICTED_MESSAGE) + // Same zod gate as the other ficha editors: valid mobiles, no duplicates. + const parsed = contactPhonesSchema.parse(input.phones) + + await payload.update({ + collection: 'contact', + id: input.contactId, + data: { phones: parsed.map((value) => ({ value })) }, + depth: 0, + // Intentional admin bypass: the unrestricted route gate above established + // staff scope; the ficha is the advisor's own normalized Contact. + overrideAccess: true, + }) + revalidatePath('/campanha/assessores', 'page') +} + export const setAdvisorMunicipalitiesBatchRecord = async ( payload: Payload, actor: CampaignUser, diff --git a/src/app/(campaign)/campanha/actions/leaderSupporter.ts b/src/app/(campaign)/campanha/actions/leaderSupporter.ts index d2da6385..a6d6f2a6 100644 --- a/src/app/(campaign)/campanha/actions/leaderSupporter.ts +++ b/src/app/(campaign)/campanha/actions/leaderSupporter.ts @@ -4,8 +4,12 @@ import { revalidatePath } from 'next/cache' import type { Payload } from 'payload' import { SUPPORTER_REGISTRATION_CONSENT_MISSING_MESSAGE } from '@/lib/campaignConsentKeys' -import { checkboxFormValue, nullableRelationshipFormValue, optionalFormText } from '@/lib/formData' -import { sanitizeBrazilianPhoneInput } from '@/lib/phone' +import { + checkboxFormValue, + nullableRelationshipFormValue, + optionalFormText, + repeatedPhoneFormValues, +} from '@/lib/formData' import { LEADER_SUPPORTER_ONLY_MESSAGE, leaderSupporterCreateSchema, @@ -32,7 +36,7 @@ export type LeaderSupporterFormState = { type LeaderSupporterFormValues = { name?: string - phone?: string + phones?: string[] city?: string municipality?: string } @@ -89,7 +93,7 @@ export const createLeaderSupporterRecord = async ( const { contactID, reused } = await findOrCreateContactByPhone({ payload, req, - phone: data.phone, + phones: data.phones, name: data.name, city: data.city, }) @@ -133,18 +137,17 @@ export const createLeaderSupporterFormAction = async ( ): Promise => { const values: LeaderSupporterFormValues = { name: optionalFormText(formData, 'name'), - phone: optionalFormText(formData, 'phone'), + phones: repeatedPhoneFormValues(formData, 'phones'), city: optionalFormText(formData, 'city'), municipality: optionalFormText(formData, 'municipality'), } return runCampaignFormAction({ execute: async () => { - const phone = sanitizeBrazilianPhoneInput(values.phone ?? '') const municipality = nullableRelationshipFormValue(formData, 'municipality') const input = leaderSupporterCreateSchema.parse({ name: values.name ?? '', - phone, + phones: values.phones ?? [], city: values.city, municipality, consentAccepted: checkboxFormValue(formData, 'consentAccepted') ? true : undefined, diff --git a/src/app/(campaign)/campanha/actions/leadership.ts b/src/app/(campaign)/campanha/actions/leadership.ts index be8b4f16..ace7df07 100644 --- a/src/app/(campaign)/campanha/actions/leadership.ts +++ b/src/app/(campaign)/campanha/actions/leadership.ts @@ -6,6 +6,7 @@ import type { Payload } from 'payload' import { nextLeadershipAdvisorIdsAfterMembership } from '@/lib/leadershipAdvisorMembership' import { nextMunicipalityIdsAfterLeadershipMembership } from '@/lib/leadershipMunicipalityMembership' import { nextStateDeputyIdsAfterMembership } from '@/lib/leadershipStateDeputyMembership' +import { reorderWithPrimaryPhone } from '@/lib/phone' import { relationshipId, uniqueRelationshipIds } from '@/lib/relationship' import { LEADERSHIP_ADVISORS_UNRESTRICTED_MESSAGE, @@ -81,8 +82,8 @@ const isUniqueLeadershipConflict = (error: unknown): boolean => { } type LeadershipCreateData = ReturnType -type ValidatedLeadershipCreateData = Omit & { - phone?: string | null +type ValidatedLeadershipCreateData = Omit & { + phones?: string[] } const createValidatedLeadershipRecord = async ( @@ -118,7 +119,7 @@ const createValidatedLeadershipRecord = async ( const { contactID, reused } = await findOrCreateContactByPhone({ payload, req, - phone: data.phone ?? null, + phones: data.phones ?? [], name: data.name, email: data.email, city: city ?? undefined, @@ -254,18 +255,25 @@ export const updateLeadershipContactRecord = async ( throw new Error(LEADERSHIP_INVALID_CONTACT_MESSAGE) } - const contactData: Partial> = {} + const contactData: Partial> & { + phones?: { value: string }[] + } = {} if (data.field === 'name') { contactData.name = data.name } else if (data.field === 'email') { contactData.email = data.email ?? null } else if (data.field === 'phone') { - if (data.phone) { - contactData.phone = data.phone - } else { - contactData.phone = null - } + // Inline cell edit = set the PRIMARY phone, keeping the rest of the + // list untouched: the new number goes first (an earlier occurrence + // moves, never duplicates) and clearing removes the primary. + const contact = current.contact + contactData.phones = reorderWithPrimaryPhone( + typeof contact === 'object' && contact !== null ? contact.phones : null, + data.phone, + ).map((value) => ({ value })) + } else if (data.field === 'phones') { + contactData.phones = data.phones.map((value) => ({ value })) } // bypass: contact write is staff-scoped via leadership access check above. @@ -336,7 +344,7 @@ const updateLeadershipWizardRecord = async ( id: contactID, data: { name: data.name, - phone: data.phone, + phones: data.phones.map((value) => ({ value })), email: data.email, }, depth: 0, @@ -370,7 +378,7 @@ const createLeadershipWizardRecord = async ( const data = leadershipWizardCreateSchema.parse(input) return createValidatedLeadershipRecord(payload, actor, { name: data.name, - phone: data.phone, + phones: data.phones, email: data.email, municipalities: [data.municipalityId], exclusive: data.exclusive, @@ -415,7 +423,7 @@ export const createMunicipalityLeadershipRecord = async ( const data = municipalityLeadershipCreateSchema.parse(input) const leadership = await createValidatedLeadershipRecord(payload, actor, { name: data.name, - phone: null, + phones: [], municipalities: [data.municipalityId], email: undefined, exclusive: true, diff --git a/src/app/(campaign)/campanha/actions/stateDeputy.ts b/src/app/(campaign)/campanha/actions/stateDeputy.ts index 9aca7413..7db83759 100644 --- a/src/app/(campaign)/campanha/actions/stateDeputy.ts +++ b/src/app/(campaign)/campanha/actions/stateDeputy.ts @@ -4,6 +4,7 @@ import { revalidatePath } from 'next/cache' import type { Payload } from 'payload' import { nextStateDeputyIdsAfterMunicipalityMembership } from '@/lib/municipalityStateDeputyMembership' +import { reorderWithPrimaryPhone } from '@/lib/phone' import { relationshipId, uniqueRelationshipIds } from '@/lib/relationship' import { contactFieldUpdateSchema, type ContactFieldUpdateInput } from '@/lib/schemas/contact' import { @@ -69,7 +70,7 @@ const createStateDeputyWithContact = async ( data: { name: data.name, email: null, - phone: null, + phones: [], state: 'BA' as Contact['state'], city: null, }, @@ -165,7 +166,7 @@ export const updateStateDeputyContactRecord = async ( const current = await payload.findByID({ collection: 'stateDeputy', id: data.id, - depth: 0, + depth: 1, select: { contact: true }, user: currentActor, overrideAccess: false, @@ -175,18 +176,24 @@ export const updateStateDeputyContactRecord = async ( const contactID = relationshipId(current.contact) if (contactID === null) throw new Error(STATE_DEPUTY_INVALID_CONTACT_MESSAGE) - const contactData: Partial> = {} + const contactData: Partial> & { + phones?: { value: string }[] + } = {} if (data.field === 'name') { await assertStateDeputyNameAvailable(payload, req, data.name, data.id) contactData.name = data.name } else if (data.field === 'email') { contactData.email = data.email ?? null } else if (data.field === 'phone') { - if (data.phone) { - contactData.phone = data.phone - } else { - contactData.phone = null - } + // Inline cell edit = set the PRIMARY phone, keeping the rest of the + // list untouched (see the leadership sibling). + const contact = current.contact + contactData.phones = reorderWithPrimaryPhone( + typeof contact === 'object' && contact !== null ? contact.phones : null, + data.phone, + ).map((value) => ({ value })) + } else if (data.field === 'phones') { + contactData.phones = data.phones.map((value) => ({ value })) } // Bypass: the StateDeputy row access above established staff scope. diff --git a/src/app/(campaign)/campanha/actions/supporter.ts b/src/app/(campaign)/campanha/actions/supporter.ts index 0e2f62db..f502500d 100644 --- a/src/app/(campaign)/campanha/actions/supporter.ts +++ b/src/app/(campaign)/campanha/actions/supporter.ts @@ -1,8 +1,10 @@ 'use server' +import { revalidatePath } from 'next/cache' import type { Payload } from 'payload' -import { requireRelationshipId } from '@/lib/relationship' +import { relationshipId, requireRelationshipId } from '@/lib/relationship' +import { contactFieldUpdateSchema, type ContactFieldUpdateInput } from '@/lib/schemas/contact' import { SUPPORTER_DUPLICATE_MESSAGE, SUPPORTER_STAFF_MESSAGE, @@ -37,6 +39,8 @@ const getFreshStaffActor = ( req?: PayloadTransactionRequest, ): Promise => reloadStaffActor(payload, actor, SUPPORTER_STAFF_MESSAGE, req) +const INVALID_FIELD_MESSAGE = 'Campo inválido.' + const assertMunicipalityManagement = async ( payload: Payload, actor: CampaignUser, @@ -92,7 +96,7 @@ const createValidatedSupporter = async (payload: Payload, actor: CampaignUser, i const { contactID, reused } = await findOrCreateContactByPhone({ payload, req, - phone: data.phone, + phones: data.phones, name: data.name, email: data.email, city: data.city, @@ -183,6 +187,54 @@ export const setSupporterVoteIntention = async (input: unknown) => { 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 @@ -275,7 +327,9 @@ const anonymizeContact = async ( data: { name: 'Titular removido', email: null, - phone: tombstonePhone, + // 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, diff --git a/src/app/(campaign)/campanha/actions/supporterImport.ts b/src/app/(campaign)/campanha/actions/supporterImport.ts index a8e5ca44..ed6d5fc2 100644 --- a/src/app/(campaign)/campanha/actions/supporterImport.ts +++ b/src/app/(campaign)/campanha/actions/supporterImport.ts @@ -224,28 +224,35 @@ export const previewSupporterImportText = async ( if (candidatePhones.length > 0) { const contacts = await payload.find({ collection: 'contact', - where: { phone: { in: candidatePhones } }, + where: { 'phones.value': { in: candidatePhones } }, depth: 0, // No LIMIT: a phone can now legitimately match many fichas (C111), so a // finite limit would silently miss a shared phone past the cutoff and // the preview would promise rows the confirm then aborts. limit: 0, pagination: false, - select: { phone: true }, + select: { phones: { value: true } }, overrideAccess: true, }) // C111 — the phone is not unique: a CSV phone resolving to two or more // fichas cannot be matched to a person, so every row with that phone is - // flagged for manual resolution (never a silent "last one wins"). + // flagged for manual resolution (never a silent "last one wins"). Keyed by + // the CSV phone ITSELF, not the ficha's primary: the JOIN matched any + // position, so a number carried as secondary by two fichas must be shared + // here — exactly what the confirm's fail-closed check will abort on. + const candidatePhoneSet = new Set(candidatePhones) const contactByPhone = new Map() const sharedPhones = new Set() for (const doc of contacts.docs) { - if (!doc.phone) continue - if (contactByPhone.has(doc.phone)) { - sharedPhones.add(doc.phone) - continue + for (const entry of doc.phones ?? []) { + const phone = entry.value + if (!phone || !candidatePhoneSet.has(phone)) continue + if (contactByPhone.has(phone)) { + sharedPhones.add(phone) + continue + } + contactByPhone.set(phone, doc.id) } - contactByPhone.set(doc.phone, doc.id) } const contactIDs = [...contactByPhone.values()] diff --git a/src/app/(frontend)/actions/submitPetitionSignature.ts b/src/app/(frontend)/actions/submitPetitionSignature.ts index f4205879..03a8f7d5 100644 --- a/src/app/(frontend)/actions/submitPetitionSignature.ts +++ b/src/app/(frontend)/actions/submitPetitionSignature.ts @@ -37,9 +37,15 @@ export const submitPetitionSignature = async ({ } try { + const { phone, ...contactFields } = data const contact = await payload.create({ collection: 'contact', - data, + data: { + ...contactFields, + // The public forms keep a single phone input; the ficha stores the + // phones array with that number as primary (C112). + phones: phone ? [{ value: phone }] : [], + }, req: { transactionID }, }) diff --git a/src/app/(frontend)/actions/submitWhatsapp.ts b/src/app/(frontend)/actions/submitWhatsapp.ts index 807d457e..54126aed 100644 --- a/src/app/(frontend)/actions/submitWhatsapp.ts +++ b/src/app/(frontend)/actions/submitWhatsapp.ts @@ -28,9 +28,15 @@ export const submitWhatsapp = async (input: WhatsAppFormInput) => { } try { + const { phone, ...contactFields } = contactInput const contact = await payload.create({ collection: 'contact', - data: contactInput, + data: { + ...contactFields, + // The public forms keep a single phone input; the ficha stores the + // phones array with that number as primary (C112). + phones: phone ? [{ value: phone }] : [], + }, req: { transactionID }, }) diff --git a/src/collections/CampaignUser.ts b/src/collections/CampaignUser.ts index d64a2844..192e541b 100644 --- a/src/collections/CampaignUser.ts +++ b/src/collections/CampaignUser.ts @@ -146,7 +146,7 @@ const resolveContactForAccount = async ({ const { contactID } = await findOrCreateContactByPhone({ payload, req, - phone: typeof phone === 'string' && phone.length > 0 ? phone : null, + phones: typeof phone === 'string' && phone.length > 0 ? [phone] : [], name, email: email && !isPlanilhaPlaceholderEmail(email) ? email : null, }) @@ -205,7 +205,11 @@ const ensureCampaignUserContactIdentity: CollectionBeforeChangeHook 0 && data.phone !== originalDoc?.phone ) { - contactData.phone = data.phone + const contact = await req.payload.findByID({ + collection: 'contact', + id: linkedContactID, + depth: 0, + select: { phones: { value: true } }, + // Intentional admin bypass: same account-scoped policy as the ficha + // write below — the read only derives the append target. + overrideAccess: true, + req, + }) + const existingPhones = (contact.phones ?? []) + .map((entry) => entry.value) + .filter((value): value is string => Boolean(value)) + if (!existingPhones.includes(data.phone)) { + contactData.phones = [...existingPhones, data.phone].map((value) => ({ value })) + } } if (Object.keys(contactData).length === 0) return data diff --git a/src/collections/Contact.ts b/src/collections/Contact.ts index 180de13d..743e3e7f 100644 --- a/src/collections/Contact.ts +++ b/src/collections/Contact.ts @@ -1,11 +1,63 @@ import { CitiesByState } from '@/lib/cities' -import { BRAZILIAN_PHONE_INVALID_MESSAGE, normalizeBrazilianPhone } from '@/lib/phone' +import { + BRAZILIAN_PHONE_DUPLICATE_MESSAGE, + BRAZILIAN_PHONE_INVALID_MESSAGE, + normalizeBrazilianPhone, +} from '@/lib/phone' import { trimmedText } from '@/lib/text' import { canManageContacts, canReadContacts } from '@/utilities/campaignAccess' import { assertStateDeputyNameAvailable } from '@/utilities/stateDeputy/nameInvariant' import type { CollectionBeforeChangeHook, CollectionConfig } from 'payload' import { APIError } from 'payload' +type ContactPhoneValue = { value: string } + +/** + * Normalizes the ficha's phone list: each entry is a valid Brazilian mobile + * (11 digits, `^[1-9]{2}9\d{8}$`), empty entries are dropped, and the same + * number cannot be stored twice in the same ficha (the person already has it; + * sharing a number BETWEEN fichas stays allowed — C111). `undefined` (admin + * untouched) passes through; `null`/`''` collapse to an empty list. + */ +const normalizeContactPhones = (data: unknown): ContactPhoneValue[] => { + const phones = Array.isArray(data) ? data : [] + const seen = new Set() + const normalized: ContactPhoneValue[] = [] + + for (const entry of phones) { + if (entry === null || entry === undefined) continue + const raw = + typeof entry === 'object' && 'value' in (entry as Record) + ? (entry as Record).value + : entry + + if (raw === null || raw === undefined || raw === '') continue + + const input = String(raw) + let value: string | null + if (/^\d{11}$/.test(input)) { + if (/^[1-9]{2}9\d{8}$/.test(input)) { + value = input + } else { + throw new APIError(BRAZILIAN_PHONE_INVALID_MESSAGE, 400) + } + } else { + value = normalizeBrazilianPhone(input) + if (!value) { + throw new APIError(BRAZILIAN_PHONE_INVALID_MESSAGE, 400) + } + } + + if (seen.has(value)) { + throw new APIError(BRAZILIAN_PHONE_DUPLICATE_MESSAGE, 400) + } + seen.add(value) + normalized.push({ value }) + } + + return normalized +} + const enforceStateDeputyName: CollectionBeforeChangeHook = async ({ data, operation, @@ -57,27 +109,14 @@ export const Contact: CollectionConfig = { hooks: { beforeValidate: [ ({ data }) => { - if (!data || data.phone === undefined) return data + if (!data || data.phones === undefined) return data - if (data.phone === null || data.phone === '') { - data.phone = null + if (data.phones === null || data.phones === '') { + data.phones = [] return data } - const input = String(data.phone) - if (/^\d{11}$/.test(input)) { - if (!/^[1-9]{2}9\d{8}$/.test(input)) { - throw new APIError(BRAZILIAN_PHONE_INVALID_MESSAGE, 400) - } - return data - } - - const phone = normalizeBrazilianPhone(input) - if (!phone) { - throw new APIError(BRAZILIAN_PHONE_INVALID_MESSAGE, 400) - } - - data.phone = phone + data.phones = normalizeContactPhones(data.phones) return data }, ], @@ -99,15 +138,22 @@ export const Contact: CollectionConfig = { required: false, }, { - name: 'phone', - type: 'text', - label: 'Celular', - minLength: 11, - maxLength: 11, + name: 'phones', + type: 'array', + label: 'Telefones', // Optional at the collection level so name-only records (e.g. leaderships imported // from the projection sheet) can exist; UI flows still require it via zod schemas. required: false, - index: true, + fields: [ + { + name: 'value', + type: 'text', + label: 'Celular', + minLength: 11, + maxLength: 11, + index: true, + }, + ], }, { name: 'gender', diff --git a/src/components/campaign/leadership/LeaderContactForm.tsx b/src/components/campaign/leadership/LeaderContactForm.tsx index 534f5f4a..299b3bf9 100644 --- a/src/components/campaign/leadership/LeaderContactForm.tsx +++ b/src/components/campaign/leadership/LeaderContactForm.tsx @@ -8,10 +8,10 @@ import { type LeaderSupporterFormState, } from '@/app/(campaign)/campanha/actions/leaderSupporter' import { CampaignFormActionMessage } from '@/components/campaign/shared/CampaignFormActionMessage' +import { PhonesFieldEditor } from '@/components/campaign/shared/PhonesFieldEditor' import type { RelationOption } from '@/components/campaign/shared/RelationMultiSelect' import { StrictCombobox } from '@/components/campaign/shared/StrictCombobox' import { useCampaignFormSuccessToast } from '@/components/campaign/shared/useCampaignFormSuccessToast' -import { FormattedInput } from '@/components/FormattedInput' import { Alert, AlertDescription, AlertTitle } from '@/components/ui/Alert' import { Button } from '@/components/ui/button' import { Checkbox } from '@/components/ui/Checkbox' @@ -26,7 +26,6 @@ import { import { Input } from '@/components/ui/input' import { NativeSelect, NativeSelectOption } from '@/components/ui/native-select' import { Spinner } from '@/components/ui/Spinner' -import { formatBrazilianPhoneInput, sanitizeBrazilianPhoneInput } from '@/lib/phone' import { errorProps as buildErrorProps, fieldError } from '@/utilities/campaignFormFields' import { municipalityComboboxOptions } from '@/utilities/territory/territoryComboboxOptions' @@ -55,7 +54,6 @@ export const LeaderContactForm = ({ useCampaignFormSuccessToast(state) - const phoneField = errorProps(state.fieldErrors, 'phone') const nameField = errorProps(state.fieldErrors, 'name') const cityField = errorProps(state.fieldErrors, 'city') const municipalityField = errorProps(state.fieldErrors, 'municipality') @@ -88,33 +86,19 @@ export const LeaderContactForm = ({ ) : null} - - Celular * - - {phoneField.error ? ( - {phoneField.error} - ) : null} - + Nome * @@ -193,7 +176,6 @@ export const LeaderContactForm = ({ id="leader-contact-consent" name="consentAccepted" value="true" - required aria-invalid={Boolean(consentError)} aria-describedby={consentError ? 'leader-contact-consent-error' : undefined} /> diff --git a/src/components/campaign/leadership/LeadershipContactSection.tsx b/src/components/campaign/leadership/LeadershipContactSection.tsx index 232ac990..76ff1ed6 100644 --- a/src/components/campaign/leadership/LeadershipContactSection.tsx +++ b/src/components/campaign/leadership/LeadershipContactSection.tsx @@ -2,19 +2,21 @@ import { updateLeadershipContactFormAction } from '@/app/(campaign)/campanha/(app)/liderancas/formActions' import { CampaignInlineEditableCell } from '@/components/campaign/shared/CampaignInlineEditableCell' +import { PhonesFieldEditor } from '@/components/campaign/shared/PhonesFieldEditor' type LeadershipContactSectionProps = { leadershipId: number name: string email: string | null - phone: string | null + /** Every number of the ficha, order = priority (C112) — primary first. */ + phones: string[] } export const LeadershipContactSection = ({ leadershipId, name, email, - phone, + phones, }: LeadershipContactSectionProps) => (

@@ -50,16 +52,16 @@ export const LeadershipContactSection = ({
-
Celular
+
+ Celulares (o primeiro é o principal) +
-
diff --git a/src/components/campaign/leadership/LeadershipForm.tsx b/src/components/campaign/leadership/LeadershipForm.tsx index 37fb9071..f1952658 100644 --- a/src/components/campaign/leadership/LeadershipForm.tsx +++ b/src/components/campaign/leadership/LeadershipForm.tsx @@ -3,6 +3,7 @@ import { useActionState } from 'react' import { CampaignFormActionMessage } from '@/components/campaign/shared/CampaignFormActionMessage' +import { PhonesFieldEditor } from '@/components/campaign/shared/PhonesFieldEditor' import { RelationMultiSelect, type RelationOption, @@ -46,7 +47,6 @@ export const LeadershipForm = ({
- - Celular (com DDD) - - {fieldError(state.fieldErrors, 'phone') ? ( - {fieldError(state.fieldErrors, 'phone')} - ) : null} - + E-mail (opcional) diff --git a/src/components/campaign/leadership/WizardLeadershipForm.tsx b/src/components/campaign/leadership/WizardLeadershipForm.tsx index 99235210..02288c42 100644 --- a/src/components/campaign/leadership/WizardLeadershipForm.tsx +++ b/src/components/campaign/leadership/WizardLeadershipForm.tsx @@ -7,6 +7,7 @@ import { updateLeadershipWizardFormAction, } from '@/app/(campaign)/campanha/(app)/acoes/wizardLeadershipFormActions' import { CampaignFormActionMessage } from '@/components/campaign/shared/CampaignFormActionMessage' +import { PhonesFieldEditor } from '@/components/campaign/shared/PhonesFieldEditor' import { Button } from '@/components/ui/button' import { Checkbox } from '@/components/ui/Checkbox' import { Field, FieldError, FieldLabel } from '@/components/ui/field' @@ -66,7 +67,6 @@ export const WizardLeadershipForm = ({ ref={nameRef} id="wizard-leadership-name" name="name" - required minLength={2} maxLength={120} defaultValue={leadership?.name ?? ''} @@ -78,21 +78,13 @@ export const WizardLeadershipForm = ({
- - Celular - - {fieldError(state.fieldErrors, 'phone') ? ( - {fieldError(state.fieldErrors, 'phone')} - ) : null} - + E-mail `phone-${++rowKey}` +const EMPTY_ROW_KEY = 'phone-empty' + +const displayValue = (value: string): string => formatBrazilianPhoneInput(value) + +/** + * The repeatable phone list editor (C112): one masked input per number, order + * = priority (first = primary), with remove and ↑/↓ reorder — no labels, no + * drag (product cut). In a create/edit form it renders plain inputs named + * `phones` for the form action to collect; with `saveAction` it becomes the + * ficha's inline editor (Salvar/Cancelar + refresh on success). + */ +export const PhonesFieldEditor = ({ + name = 'phones', + defaultValues = [], + minRows = 0, + label = 'Celulares', + error, + saveAction, + recordId, + recordIdField, + saveLabel = 'Salvar', + cancelLabel = 'Cancelar', + className, +}: { + name?: string + defaultValues?: string[] + /** Rows below this count cannot be removed (create flows that require a phone). */ + minRows?: number + label?: string + error?: string | string[] | undefined + /** Inline mode: submits the full list through the per-surface contact update action. */ + saveAction?: ( + state: CampaignFormActionState, + formData: FormData, + ) => Promise + recordId?: number + recordIdField?: string + saveLabel?: string + cancelLabel?: string + className?: string +}) => { + const router = useRouter() + const [rows, setRows] = useState(() => { + const initial = defaultValues.map((value) => ({ key: nextRowKey(), value })) + while (initial.length < minRows) initial.push({ key: nextRowKey(), value: '' }) + return initial + }) + const [isPending, setIsPending] = useState(false) + const [saveError, setSaveError] = useState(null) + const [isEditing, setIsEditing] = useState(!saveAction) + const rowsRef = useRef(rows) + rowsRef.current = rows + const appliedDefaultsRef = useRef(defaultValues) + + useEffect(() => { + if (!saveAction) return + // Apply the server values only when they actually changed (a post-save + // refresh or an unrelated refresh of the page must not drop what the + // user is typing). + if (defaultValues.join('|') === appliedDefaultsRef.current.join('|')) return + appliedDefaultsRef.current = defaultValues + setRows((current) => { + const existing = new Map(current.map((row) => [row.value, row])) + const next = defaultValues.map((value) => existing.get(value) ?? { key: nextRowKey(), value }) + return next.length > 0 || current.length === 0 ? next : current + }) + }, [defaultValues, saveAction]) + + const updateRow = (key: string, value: string): void => { + setRows((current) => current.map((row) => (row.key === key ? { ...row, value } : row))) + setSaveError(null) + } + + const addRow = (): void => { + setRows((current) => [...current, { key: nextRowKey(), value: '' }]) + setSaveError(null) + } + + const removeRow = (key: string): void => { + setRows((current) => + current.length > minRows ? current.filter((row) => row.key !== key) : current, + ) + setSaveError(null) + } + + const moveRow = (index: number, direction: -1 | 1): void => { + setRows((current) => { + const target = index + direction + if (target < 0 || target >= current.length) return current + const next = [...current] + ;[next[index], next[target]] = [next[target]!, next[index]!] + return next + }) + setSaveError(null) + } + + const errorId = 'phones-field-error' + const hasError = saveError !== null || (Array.isArray(error) ? error.length > 0 : Boolean(error)) + + const save = async (): Promise => { + if (!saveAction || recordId === undefined) return + const formData = new FormData() + if (recordIdField) formData.set(recordIdField, String(recordId)) + formData.set('field', 'phones') + for (const row of rowsRef.current) { + formData.append(name, row.value) + } + setIsPending(true) + setSaveError(null) + try { + const result = await saveAction({}, formData) + setIsPending(false) + if (result.status === 'success') { + setIsEditing(false) + router.refresh() + return + } + const message = + result.fieldErrors?.phones?.[0] ?? + result.fieldErrors?.[name]?.[0] ?? + result.fieldErrors?.form?.[0] ?? + result.message + const refusal = message ?? 'Não foi possível salvar.' + setSaveError(refusal) + toast.error(refusal) + } catch { + setIsPending(false) + const message = 'Não foi possível salvar. Tente novamente.' + setSaveError(message) + toast.error(message) + } + } + + const rowInputs = rows.map((row, index) => ( +
+ updateRow(row.key, event.currentTarget.value)} + /> + {rows.length > 1 ? ( + <> + + + + ) : null} + +
+ )) + + const addButton = ( + + ) + + if (saveAction && !isEditing) { + return ( +
+
    + {(rows.length ? rows : [{ key: EMPTY_ROW_KEY, value: '' }]).map((row, index) => ( +
  • + {index + 1}. + + {row.value ? formatBrazilianPhoneInput(row.value) : '—'} + +
  • + ))} +
+ +
+ ) + } + + return ( +
+ {label} +
+ {rowInputs} + {addButton} +
+ {hasError ? ( + + {saveError ?? (Array.isArray(error) ? error[0] : error)} + + ) : null} + {saveAction ? ( +
+ + + {isPending ? : null} +
+ ) : null} +
+ ) +} diff --git a/src/components/campaign/stateDeputy/StateDeputyContactSection.tsx b/src/components/campaign/stateDeputy/StateDeputyContactSection.tsx index 3ca19172..f7671792 100644 --- a/src/components/campaign/stateDeputy/StateDeputyContactSection.tsx +++ b/src/components/campaign/stateDeputy/StateDeputyContactSection.tsx @@ -2,19 +2,21 @@ import { updateStateDeputyContactFormAction } from '@/app/(campaign)/campanha/(app)/dobradinhas/formActions' import { CampaignInlineEditableCell } from '@/components/campaign/shared/CampaignInlineEditableCell' +import { PhonesFieldEditor } from '@/components/campaign/shared/PhonesFieldEditor' type StateDeputyContactSectionProps = { stateDeputyId: number name: string email: string | null - phone: string | null + /** Every number of the ficha, order = priority (C112) — primary first. */ + phones: string[] } export const StateDeputyContactSection = ({ stateDeputyId, name, email, - phone, + phones, }: StateDeputyContactSectionProps) => (

@@ -31,6 +33,7 @@ export const StateDeputyContactSection = ({ value={name} label="Nome" formAction={updateStateDeputyContactFormAction} + href={`/campanha/dobradinhas/${stateDeputyId}`} />

@@ -44,19 +47,21 @@ export const StateDeputyContactSection = ({ value={email} label="E-mail" formAction={updateStateDeputyContactFormAction} + readBehavior="copy" />
-
Telefone
+
+ Telefones (o primeiro é o principal) +
-
diff --git a/src/components/campaign/supporter/SupporterForm.tsx b/src/components/campaign/supporter/SupporterForm.tsx index 4673a924..f17c68d5 100644 --- a/src/components/campaign/supporter/SupporterForm.tsx +++ b/src/components/campaign/supporter/SupporterForm.tsx @@ -5,10 +5,10 @@ import { useActionState, useState } from 'react' import type { SupporterFormState } from '@/app/(campaign)/campanha/(app)/apoiadores/novo/formActions' import { CampaignFormActionMessage } from '@/components/campaign/shared/CampaignFormActionMessage' +import { PhonesFieldEditor } from '@/components/campaign/shared/PhonesFieldEditor' import type { RelationOption } from '@/components/campaign/shared/RelationMultiSelect' import { StrictCombobox } from '@/components/campaign/shared/StrictCombobox' import { useCampaignFormSuccessToast } from '@/components/campaign/shared/useCampaignFormSuccessToast' -import { FormattedInput } from '@/components/FormattedInput' import { Alert, AlertDescription, AlertTitle } from '@/components/ui/Alert' import { Button } from '@/components/ui/button' import { Checkbox } from '@/components/ui/Checkbox' @@ -24,7 +24,6 @@ import { Input } from '@/components/ui/input' import { NativeSelect, NativeSelectOption } from '@/components/ui/native-select' import { Spinner } from '@/components/ui/Spinner' import { ToggleGroup, ToggleGroupItem } from '@/components/ui/ToggleGroup' -import { formatBrazilianPhoneInput, sanitizeBrazilianPhoneInput } from '@/lib/phone' import type { SupporterVoteIntention } from '@/lib/schemas/supporter' import { errorProps as buildErrorProps, fieldError } from '@/utilities/campaignFormFields' import { supporterVoteIntentionLabels } from '@/utilities/supporter/supporterUi' @@ -118,7 +117,6 @@ export const SupporterForm = ({ }) const nameField = errorProps(state.fieldErrors, 'name') - const phoneField = errorProps(state.fieldErrors, 'phone') const emailField = errorProps(state.fieldErrors, 'email') const cityField = errorProps(state.fieldErrors, 'city') const municipalityField = errorProps(state.fieldErrors, 'municipality') @@ -140,7 +138,6 @@ export const SupporterForm = ({ - - Celular * - - {phoneField.error ? ( - {phoneField.error} - ) : null} - + E-mail diff --git a/src/lib/formData.ts b/src/lib/formData.ts index b1981455..946170e5 100644 --- a/src/lib/formData.ts +++ b/src/lib/formData.ts @@ -176,6 +176,24 @@ export const repeatedFormTexts = (formData: FormData, field: string): string[] = return values } +/** + * The ficha's phone list from the repeatable editor: order is preserved (= + * priority, first = primary) and empty values are dropped. Duplicates are NOT + * dropped here — the zod `contactPhonesSchema` refine reports them so the + * form can show the "Telefone repetido" refusal instead of silently merging. + */ +export const repeatedPhoneFormValues = (formData: FormData, field: string): string[] => { + const values: string[] = [] + for (const value of formData.getAll(field)) { + if (typeof value !== 'string') { + throw new FormDataBoundaryError(field, 'Arquivos não são aceitos neste campo.') + } + const trimmed = value.trim() + if (trimmed) values.push(trimmed) + } + return values +} + export const checkboxFormValue = (formData: FormData, field: string): boolean => { const entry = formEntry(formData, field) if (!entry.present) return false diff --git a/src/lib/phone.ts b/src/lib/phone.ts index d3e01267..68a129b5 100644 --- a/src/lib/phone.ts +++ b/src/lib/phone.ts @@ -4,6 +4,9 @@ const BRAZILIAN_MOBILE_PHONE_LENGTH = 11 /** One refusal copy for the URL builder, the zod primitive and the Contact hook — rewording in one layer only would give the same refusal two phrasings. */ export const BRAZILIAN_PHONE_INVALID_MESSAGE = 'Celular brasileiro inválido.' +/** A ficha may not hold the same number twice (the person already has it); sharing a number BETWEEN fichas stays allowed (C111). */ +export const BRAZILIAN_PHONE_DUPLICATE_MESSAGE = 'Telefone repetido na ficha.' + export const sanitizeBrazilianPhoneInput = (value: string): string => { const digits = value.replace(/\D/g, '') const domesticDigits = @@ -61,3 +64,35 @@ export const whatsAppHrefForPhone = (phone: string | null): string | null => { if (!phone || !normalizeBrazilianPhone(phone)) return null return buildWhatsAppUrl(phone) } + +/** + * The person's primary phone from the `Contact.phones` array: order is + * priority (first = primary), so the row/list/WhatsApp/convite readers all go + * through this single translation and never touch the array shape. + */ +export const primaryPhoneOf = ( + phones: ReadonlyArray<{ value?: string | null }> | null | undefined, +): string | null => phones?.[0]?.value || null + +/** Every number of the ficha in priority order, empties dropped. */ +export const phoneValuesOf = ( + phones: ReadonlyArray<{ value?: string | null }> | null | undefined, +): string[] => + (phones ?? []).map((entry) => entry.value).filter((value): value is string => Boolean(value)) + +/** + * Edit the primary phone preserving the rest: the next primary goes first + * (any earlier occurrence removed — the number the person already had moves, + * never duplicates), and an empty `primary` removes the current first number + * so the rest shifts up. + */ +export const reorderWithPrimaryPhone = ( + phones: ReadonlyArray<{ value?: string | null }> | null | undefined, + primary: string | null, +): string[] => { + const current = (phones ?? []) + .map((entry) => entry.value) + .filter((value): value is string => Boolean(value)) + const rest = current.filter((phone) => phone !== primary) + return primary ? [primary, ...rest] : current.slice(1) +} diff --git a/src/lib/schemas/contact.ts b/src/lib/schemas/contact.ts index e0aec974..768fd0e4 100644 --- a/src/lib/schemas/contact.ts +++ b/src/lib/schemas/contact.ts @@ -1,4 +1,5 @@ import { CitiesByState } from '@/lib/cities' +import { BRAZILIAN_PHONE_DUPLICATE_MESSAGE } from '@/lib/phone' import { z } from 'zod' import { @@ -44,6 +45,20 @@ const nullableContactPhone = contactPhoneInput.transform((value) => value === '' || value === undefined ? null : value, ) +/** + * The ficha's phone list: valid mobiles, never the same number twice within + * the SAME person (C112 — the mesa blocks it at the form); sharing a number + * BETWEEN persons stays free (C111). Order = priority (first = primary). + */ +export const contactPhonesSchema = z.array(brazilianMobile).superRefine((phones, context) => { + if (new Set(phones).size !== phones.length) { + context.addIssue({ + code: 'custom', + message: BRAZILIAN_PHONE_DUPLICATE_MESSAGE, + }) + } +}) + /** Per-field Contact write shared by campaign person joins. */ export const contactFieldUpdateSchema = z.discriminatedUnion('field', [ z.object({ @@ -61,6 +76,11 @@ export const contactFieldUpdateSchema = z.discriminatedUnion('field', [ field: z.literal('phone'), phone: nullableContactPhone, }), + z.object({ + id: positiveRelationshipId, + field: z.literal('phones'), + phones: contactPhonesSchema, + }), ]) export type ContactFieldUpdateInput = z.input diff --git a/src/lib/schemas/leadership.ts b/src/lib/schemas/leadership.ts index d6445bb9..230ccfdc 100644 --- a/src/lib/schemas/leadership.ts +++ b/src/lib/schemas/leadership.ts @@ -1,8 +1,7 @@ import { z } from 'zod' -import { contactFieldUpdateSchema } from '@/lib/schemas/contact' +import { contactFieldUpdateSchema, contactPhonesSchema } from '@/lib/schemas/contact' import { - brazilianMobile, optionalPersistedEmail, positiveRelationshipId, trimmedNullableText, @@ -91,7 +90,7 @@ export const leadershipCreateSchema = z.object({ organizations: organizationsArraySchema.optional(), stateDeputies: stateDeputiesArraySchema.optional(), name: z.string().trim().min(2).max(120), - phone: brazilianMobile, + phones: contactPhonesSchema.min(1, 'Informe ao menos um celular.'), email: optionalPersistedEmail, gender: z.enum(leadershipGenders).optional(), exclusive: z.boolean().default(true), @@ -113,7 +112,7 @@ export type LeadershipInternalUpdateInput = z.input { + await db.execute(sql` + CREATE TABLE "contact_phones" ( + "_order" integer NOT NULL, + "_parent_id" integer NOT NULL, + "id" varchar PRIMARY KEY NOT NULL, + "value" varchar + ); + + ALTER TABLE "contact_phones" ADD CONSTRAINT "contact_phones_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."contact"("id") ON DELETE cascade ON UPDATE no action; + CREATE INDEX "contact_phones_order_idx" ON "contact_phones" USING btree ("_order"); + CREATE INDEX "contact_phones_parent_id_idx" ON "contact_phones" USING btree ("_parent_id"); + CREATE INDEX "contact_phones_value_idx" ON "contact_phones" USING btree ("value"); + INSERT INTO "contact_phones" ("id", "_parent_id", "_order", "value") + SELECT gen_random_uuid()::text, "id", 0, "phone" FROM "contact" WHERE "phone" IS NOT NULL; + DROP INDEX "contact_phone_idx"; + ALTER TABLE "contact" DROP COLUMN "phone";`) +} + +export async function down({ db, payload, req }: MigrateDownArgs): Promise { + // Rollback is lossy BY DESIGN: only the primary (`_order` 0) is restored to + // `contact.phone` — every appended number is discarded. Rollbacks are + // best-effort here (C111-era policy); the forward path is lossless. + await db.execute(sql` + ALTER TABLE "contact" ADD COLUMN "phone" varchar; + UPDATE "contact" SET "phone" = "contact_phones"."value" + FROM "contact_phones" WHERE "contact_phones"."_parent_id" = "contact"."id" AND "contact_phones"."_order" = 0; + CREATE INDEX "contact_phone_idx" ON "contact" USING btree ("phone"); + ALTER TABLE "contact_phones" DISABLE ROW LEVEL SECURITY; + DROP TABLE "contact_phones" CASCADE;`) +} diff --git a/src/migrations/index.ts b/src/migrations/index.ts index 0081665e..a6d78dfe 100644 --- a/src/migrations/index.ts +++ b/src/migrations/index.ts @@ -47,6 +47,7 @@ import * as migration_20260808_134911_add_calendar_feed from './20260808_134911_ import * as migration_20260808_184113_remodel_activity_responsible from './20260808_184113_remodel_activity_responsible'; import * as migration_20260809_204728_add_campaign_user_contact_and_leadership_advisors from './20260809_204728_add_campaign_user_contact_and_leadership_advisors'; import * as migration_20260810_010844_add_activity_all_day from './20260810_010844_add_activity_all_day'; +import * as migration_20260811_111828_add_contact_phones_array from './20260811_111828_add_contact_phones_array'; export const migrations = [ { @@ -294,4 +295,9 @@ export const migrations = [ down: migration_20260810_010844_add_activity_all_day.down, name: '20260810_010844_add_activity_all_day', }, + { + up: migration_20260811_111828_add_contact_phones_array.up, + down: migration_20260811_111828_add_contact_phones_array.down, + name: '20260811_111828_add_contact_phones_array' + }, ]; diff --git a/src/payload-types.ts b/src/payload-types.ts index d2d53137..3b72eaa0 100644 --- a/src/payload-types.ts +++ b/src/payload-types.ts @@ -298,7 +298,12 @@ export interface Contact { id: number; name: string; email?: string | null; - phone?: string | null; + phones?: + | { + value?: string | null; + id?: string | null; + }[] + | null; gender?: ('feminino' | 'masculino' | 'outro' | 'nao_informado') | null; state: | 'AC' @@ -1977,7 +1982,12 @@ export interface PetitionSelect { export interface ContactSelect { name?: T; email?: T; - phone?: T; + phones?: + | T + | { + value?: T; + id?: T; + }; gender?: T; state?: T; city?: T; diff --git a/src/utilities/activityLeadershipOptions.ts b/src/utilities/activityLeadershipOptions.ts index 7d7c61d4..748eede2 100644 --- a/src/utilities/activityLeadershipOptions.ts +++ b/src/utilities/activityLeadershipOptions.ts @@ -42,7 +42,7 @@ export const searchActivityLeadershipOptions = async ( { or: [ { 'contact.name': { contains: trimmed } }, - { 'contact.phone': { contains: digits || trimmed } }, + { 'contact.phones.value': { contains: digits || trimmed } }, ], }, ], diff --git a/src/utilities/activityResponsibleSearch.ts b/src/utilities/activityResponsibleSearch.ts index df9fcb5d..deb90368 100644 --- a/src/utilities/activityResponsibleSearch.ts +++ b/src/utilities/activityResponsibleSearch.ts @@ -96,7 +96,7 @@ const searchStateDeputyResponsibles = async ( where: { or: [ { 'contact.name': { contains: trimmed } }, - { 'contact.phone': { contains: digits || trimmed } }, + { 'contact.phones.value': { contains: digits || trimmed } }, ], }, depth: 1, diff --git a/src/utilities/activityViewModels.ts b/src/utilities/activityViewModels.ts index 9832d8e8..de005c8c 100644 --- a/src/utilities/activityViewModels.ts +++ b/src/utilities/activityViewModels.ts @@ -1,5 +1,6 @@ import { formatAllDayRangeLabel } from '@/lib/activityAllDay' import { formatBahiaDateTimeLabel } from '@/lib/campaignTime' +import { primaryPhoneOf } from '@/lib/phone' import { isPopulatedRelationship, populatedContactName, relationshipId } from '@/lib/relationship' import { activityResponsibleTypeLabels, @@ -287,7 +288,7 @@ export const toActivityFormViewModel = (activity: Activity): ActivityFormViewMod ? { id: task.responsible.id, name: task.responsible.name, - phone: task.responsible.phone ?? null, + phone: primaryPhoneOf(task.responsible.phones), } : null, due: task.due ?? null, diff --git a/src/utilities/advisorData.ts b/src/utilities/advisorData.ts index c0258856..57667df0 100644 --- a/src/utilities/advisorData.ts +++ b/src/utilities/advisorData.ts @@ -2,6 +2,9 @@ import 'server-only' import type { Payload } from 'payload' +import type { Contact } from '@/payload-types' + +import { phoneValuesOf } from '@/lib/phone' import { relationshipId } from '@/lib/relationship' import { advisorListHrefForPage, @@ -35,6 +38,10 @@ export type AdvisorRowViewModel = AdvisorAccountViewModel & { /** The detail page renders real names and links, so it keeps the labels. */ export type AdvisorDetailViewModel = AdvisorAccountViewModel & { + /** The advisor's normalized ficha (C99) — target of the ficha phones editor. */ + contactID: number | null + /** Every number of the ficha, order = priority (C112) — primary first. */ + fichaPhones: string[] municipalities: AdvisorMunicipalityViewModel[] } @@ -222,6 +229,7 @@ export const loadAdvisorDetail = async ( email?: string | null phone?: string | null role: string + contact?: number | Contact | null } try { // Intentional admin bypass — same rationale as `loadAdvisorListPageData`. @@ -229,7 +237,7 @@ export const loadAdvisorDetail = async ( collection: 'campaignUser', id: advisorId, depth: 0, - select: { name: true, email: true, phone: true, role: true }, + select: { name: true, email: true, phone: true, role: true, contact: true }, overrideAccess: true, }) } catch { @@ -238,12 +246,30 @@ export const loadAdvisorDetail = async ( if (advisor.role !== 'advisor') return null + const contactID = typeof advisor.contact === 'number' ? advisor.contact : null + const fichaPhones = + contactID === null + ? [] + : await payload + .findByID({ + collection: 'contact', + id: contactID, + depth: 0, + select: { phones: { value: true } }, + // Intentional admin bypass: same staff-scoped read rationale. + overrideAccess: true, + }) + .then((contact) => phoneValuesOf(contact.phones)) + .catch(() => []) + const municipalitiesByAdvisor = await municipalitiesByAdvisorIds(payload, [advisor.id]) return { id: advisor.id, name: advisor.name, email: advisor.email ?? null, phone: advisor.phone ?? null, + contactID, + fichaPhones, municipalities: municipalitiesByAdvisor.get(advisor.id) ?? [], } } diff --git a/src/utilities/campaignInviteCreation.ts b/src/utilities/campaignInviteCreation.ts index c0742b1f..bc5e08d2 100644 --- a/src/utilities/campaignInviteCreation.ts +++ b/src/utilities/campaignInviteCreation.ts @@ -3,6 +3,7 @@ import 'server-only' import type { Payload } from 'payload' import { CREATE_CAMPAIGN_INVITE_MISSING_CONSENT_MESSAGE } from '@/lib/campaignInviteClient' +import { primaryPhoneOf } from '@/lib/phone' import { requireRelationshipId } from '@/lib/relationship' import { campaignInviteCreateSchema, type CampaignInviteCreateInput } from '@/lib/schemas/invite' import type { CampaignUser } from '@/payload-types' @@ -78,14 +79,15 @@ export const createCampaignInviteForActor = async ( overrideAccess: true, req, }) - if (!contact.phone) { + const primaryPhone = primaryPhoneOf(contact.phones) + if (!primaryPhone) { throw new Error('Cadastre o celular da liderança antes de gerar o convite.') } const inviteUrl = `${inviteBaseURL}/campanha/convite/${generated.token}` return { inviteUrl, whatsappUrl: buildCampaignInviteWhatsAppLink({ - phone: contact.phone, + phone: primaryPhone, recipientName: contact.name, senderName: currentActor.name, inviteUrl, diff --git a/src/utilities/campaignInvitePageData.ts b/src/utilities/campaignInvitePageData.ts index 622139d0..b8fee5f7 100644 --- a/src/utilities/campaignInvitePageData.ts +++ b/src/utilities/campaignInvitePageData.ts @@ -2,6 +2,7 @@ import 'server-only' import type { Payload } from 'payload' +import { primaryPhoneOf } from '@/lib/phone' import { relationshipId } from '@/lib/relationship' import type { Consent } from '@/payload-types' import { getLeadershipConsent } from '@/utilities/campaignConsent' @@ -73,7 +74,7 @@ export const getCampaignInvitePageData = async ( depth: 0, select: { name: true, - phone: true, + phones: { value: true }, email: true, gender: true, }, @@ -89,7 +90,7 @@ export const getCampaignInvitePageData = async ( kind: invite.kind, profile: { name: contact.name, - phone: contact.phone ?? '', + phone: primaryPhoneOf(contact.phones) ?? '', email: contact.email ?? null, gender: contact.gender ?? null, }, diff --git a/src/utilities/campaignInviteRedemption.ts b/src/utilities/campaignInviteRedemption.ts index c67312c6..47667137 100644 --- a/src/utilities/campaignInviteRedemption.ts +++ b/src/utilities/campaignInviteRedemption.ts @@ -2,6 +2,7 @@ import 'server-only' import type { Payload } from 'payload' +import { primaryPhoneOf, reorderWithPrimaryPhone } from '@/lib/phone' import { relationshipId, requireRelationshipId } from '@/lib/relationship' import { campaignInviteAutofillSchema, @@ -9,7 +10,7 @@ import { type CampaignInviteAutofillInput, type CampaignInviteLoginInput, } from '@/lib/schemas/invite' -import type { CampaignUser } from '@/payload-types' +import type { CampaignUser, Contact } from '@/payload-types' import { requireLeadershipConsent, resolveInviteConsent } from '@/utilities/campaignConsent' import { acquireCampaignInviteAccountLocks, @@ -32,9 +33,12 @@ type ProfileInput = { gender?: 'feminino' | 'masculino' | 'outro' | 'nao_informado' | null } -const profileContactData = (data: ProfileInput) => ({ +const profileContactData = (data: ProfileInput, originalPhones: Contact['phones']) => ({ name: data.name, - phone: data.phone, + // The typed phone becomes the PRIMARY of the anchored ficha; every number + // the ficha already had is kept (an earlier occurrence of the typed phone + // moves, never duplicates) — the person's other numbers survive the invite. + phones: reorderWithPrimaryPhone(originalPhones, data.phone).map((value) => ({ value })), ...(data.email !== undefined ? { email: data.email } : {}), ...(data.gender !== undefined ? { gender: data.gender } : {}), }) @@ -123,7 +127,9 @@ export const redeemCampaignInviteAutofillRecord = async ( payload, req, contactPhoneLockKeys( - [originalContact.phone, data.phone].filter((phone): phone is string => Boolean(phone)), + [primaryPhoneOf(originalContact.phones), data.phone].filter((phone): phone is string => + Boolean(phone), + ), ), ) // C111 — the phone is a contact channel, not a unique person identity: @@ -132,7 +138,7 @@ export const redeemCampaignInviteAutofillRecord = async ( await payload.update({ collection: 'contact', id: contactID, - data: profileContactData(data), + data: profileContactData(data, originalContact.phones), depth: 0, overrideAccess: true, req, @@ -194,7 +200,9 @@ export const redeemCampaignInviteLoginRecord = async ( let account = await findReusableLeadershipAccount(payload, req, leadership) await acquireCampaignInviteAccountLocks(payload, req, [ ...contactPhoneLockKeys( - [originalContact.phone, data.phone].filter((phone): phone is string => Boolean(phone)), + [primaryPhoneOf(originalContact.phones), data.phone].filter((phone): phone is string => + Boolean(phone), + ), ), ...(account?.username ? [`account-username:${account.username}`] : []), `account-username:${data.phone}`, @@ -224,7 +232,7 @@ export const redeemCampaignInviteLoginRecord = async ( await payload.update({ collection: 'contact', id: originalContact.id, - data: profileContactData(data), + data: profileContactData(data, originalContact.phones), depth: 0, overrideAccess: true, req, diff --git a/src/utilities/contactIdentity.ts b/src/utilities/contactIdentity.ts index 9cafaa03..34d6bbf2 100644 --- a/src/utilities/contactIdentity.ts +++ b/src/utilities/contactIdentity.ts @@ -10,16 +10,19 @@ type ContactIdentityRequest = PayloadTransactionRequest | PayloadRequest /** * Find-or-create the normalized `Contact` ficha for a person, inside the - * caller's transaction. With a phone, the advisory lock is acquired HERE and - * an existing ficha with that phone is reused — but only when the phone is - * unambiguous (exactly one ficha): that is the phone-matching dedupe C6/C99 - * keeps for re-imports and re-registration of the SAME person. When the phone - * already belongs to TWO OR MORE fichas there is no person the phone can - * identify — C111 — so a fresh ficha is created (the identity of the person - * being created is the ficha just written; never a guess among the existing - * ones). Without a phone, a fresh name-only ficha is created. Every "cria ou - * vincula a ficha" path shares one owner for the "BA default + shared-phone" - * policy. + * caller's transaction. With phones, the advisory locks are acquired HERE for + * every typed number and an existing ficha that already carries the PRIMARY + * (first) phone is reused — but only when that phone is unambiguous (exactly + * one ficha): that is the phone-matching dedupe C6/C99 keeps for re-imports + * and re-registration of the SAME person. When the primary phone already + * belongs to TWO OR MORE fichas there is no person the phone can identify — + * C111 — so a fresh ficha is created (the identity of the person being + * created is the ficha just written; never a guess among the existing ones). + * The remaining typed numbers follow the ficha wherever it lands: they fill + * the fresh ficha, or are appended (only the ones it lacks, at the END — a + * re-registration never reorders the mesa's priority). Without phones, a + * fresh name-only ficha is created. Every "cria ou vincula a ficha" path + * shares one owner for the "BA default + shared-phone" policy. * * Callers must already be inside an active Payload transaction (the advisory * lock requires it) and authorized to own the write (staff creation flows run @@ -29,7 +32,7 @@ type ContactIdentityRequest = PayloadTransactionRequest | PayloadRequest export const findOrCreateContactByPhone = async ({ payload, req, - phone, + phones, name, email, city, @@ -37,18 +40,22 @@ export const findOrCreateContactByPhone = async ({ }: { payload: Payload req: ContactIdentityRequest - phone: string | null + phones: string[] name: string email?: string | null city?: string | null gender?: Contact['gender'] | null }): Promise<{ contactID: number; reused: boolean }> => { - if (phone) { - await acquireContactPhoneLocks(payload, req, [phone]) + const primary = phones[0] ?? null + + if (primary) { + await acquireContactPhoneLocks(payload, req, phones) const contacts = await payload.find({ collection: 'contact', - where: { phone: { equals: phone } }, + // Matches ANY of the ficha's phones — the primary typed here is the + // dedupe key, but it may sit at any position of the existing ficha. + where: { 'phones.value': { equals: primary } }, depth: 0, limit: 2, pagination: false, @@ -59,7 +66,24 @@ export const findOrCreateContactByPhone = async ({ }) if (contacts.totalDocs === 1) { - return { contactID: contacts.docs[0]!.id, reused: true } + const existing = contacts.docs[0]! + const existingPhones = (existing.phones ?? []) + .map((entry) => entry.value) + .filter((value): value is string => Boolean(value)) + const missing = phones.filter((phone) => !existingPhones.includes(phone)) + if (missing.length > 0) { + await payload.update({ + collection: 'contact', + id: existing.id, + data: { + phones: [...existingPhones, ...missing].map((value) => ({ value })), + }, + depth: 0, + overrideAccess: true, + req, + }) + } + return { contactID: existing.id, reused: true } } } @@ -67,7 +91,7 @@ export const findOrCreateContactByPhone = async ({ collection: 'contact', data: { name, - phone, + phones: phones.map((value) => ({ value })), email: email ?? null, state: 'BA' as Contact['state'], city: city ?? null, diff --git a/src/utilities/homeSearch/searchHomeLeaderships.ts b/src/utilities/homeSearch/searchHomeLeaderships.ts index 4a2a02c2..64feb02c 100644 --- a/src/utilities/homeSearch/searchHomeLeaderships.ts +++ b/src/utilities/homeSearch/searchHomeLeaderships.ts @@ -14,6 +14,7 @@ import { compareHomeSearchNameRelevance, normalizeHomeSearchName, } from '@/lib/homeSearchMunicipalityMatch' +import { primaryPhoneOf } from '@/lib/phone' import { populatedContactName } from '@/lib/relationship' import { matchesNormalizedAtWordStart } from '@/lib/wordStartFilter' import type { CampaignUser, Leadership } from '@/payload-types' @@ -21,9 +22,9 @@ import { truncatedNamesLabel } from '@/utilities/campaignListUrl' import { buildLeadershipListWhere } from '@/utilities/leadership/leadershipListUrl' const populatedContactPhone = (contact: unknown): string | null => { - if (typeof contact !== 'object' || contact === null || !('phone' in contact)) return null - const { phone } = contact - return typeof phone === 'string' && phone.length > 0 ? phone : null + if (typeof contact !== 'object' || contact === null || !('phones' in contact)) return null + const phones = (contact as { phones?: unknown }).phones + return Array.isArray(phones) ? primaryPhoneOf(phones as { value?: string | null }[]) : null } const municipalityNamesFromLeadership = (doc: Leadership): string[] => { diff --git a/src/utilities/leaderContactsPageData.ts b/src/utilities/leaderContactsPageData.ts index f45fc8e6..97779ba9 100644 --- a/src/utilities/leaderContactsPageData.ts +++ b/src/utilities/leaderContactsPageData.ts @@ -3,6 +3,7 @@ import 'server-only' import type { Payload } from 'payload' import type { RelationOption } from '@/components/campaign/shared/RelationMultiSelect' +import { primaryPhoneOf } from '@/lib/phone' import type { CampaignUser, Contact, Municipality } from '@/payload-types' import { getEngagedLeaderMunicipalityIds } from '@/utilities/campaignAccess' import { getSupporterRegistrationConsent } from '@/utilities/campaignConsent' @@ -36,7 +37,7 @@ const toLeaderContactListItem = (supporter: { return { id: supporter.id, name: typeof contact === 'object' && contact ? contact.name : 'Contato', - phone: typeof contact === 'object' && contact ? (contact.phone ?? null) : null, + phone: typeof contact === 'object' && contact ? primaryPhoneOf(contact.phones) : null, city: typeof contact === 'object' && contact ? (contact.city ?? null) : null, municipalityName: typeof municipality === 'object' && municipality ? municipality.name : null, createdAt: supporter.createdAt, diff --git a/src/utilities/leadership/leadershipData.ts b/src/utilities/leadership/leadershipData.ts index b086bf9e..a2c469f4 100644 --- a/src/utilities/leadership/leadershipData.ts +++ b/src/utilities/leadership/leadershipData.ts @@ -2,6 +2,7 @@ import 'server-only' import type { Payload } from 'payload' +import { phoneValuesOf, primaryPhoneOf } from '@/lib/phone' import { relationshipId } from '@/lib/relationship' import type { SupportStatus } from '@/lib/schemas/leadership' import { isSupportStatus } from '@/lib/schemas/leadership' @@ -42,16 +43,23 @@ export type LeadershipRowViewModel = { const contactSummary = ( contact: Leadership['contact'], -): { id: number; name: string; phone: string | null; email: string | null } => { +): { + id: number + name: string + phone: string | null + phones: { value: string }[] + email: string | null +} => { if (typeof contact === 'object' && contact !== null) { return { id: contact.id, name: contact.name ?? 'Contato', - phone: contact.phone ?? null, + phone: primaryPhoneOf(contact.phones), + phones: phoneValuesOf(contact.phones).map((value) => ({ value })), email: contact.email ?? null, } } - return { id: Number(contact), name: 'Contato', phone: null, email: null } + return { id: Number(contact), name: 'Contato', phone: null, phones: [], email: null } } const findMunicipalityLeadershipDocs = async ( @@ -78,6 +86,9 @@ const toWizardLeadershipTile = (doc: Leadership): WizardLeadershipTileViewModel return { id: doc.id, name: contact.name, + phones: (contact.phones ?? []) + .map((entry) => entry.value) + .filter((value): value is string => Boolean(value)), phone: contact.phone, email: contact.email, supportStatus: isSupportStatus(doc.supportStatus) ? doc.supportStatus : null, @@ -337,6 +348,8 @@ export const loadLeadershipListPageData = async ( } export type LeadershipDetailViewModel = LeadershipRowViewModel & { + /** Every number of the ficha, order = priority (C112). */ + phones: string[] municipalityIDs: number[] organizationIDs: number[] stateDeputyIDs: number[] @@ -372,6 +385,9 @@ export const loadLeadershipDetail = async ( return { ...row, + phones: phoneValuesOf( + doc.contact && typeof doc.contact === 'object' ? doc.contact.phones : null, + ), municipalityIDs: (doc.municipalities ?? []) .map(relationshipId) .filter((id): id is number => id !== null), diff --git a/src/utilities/people/peopleData.ts b/src/utilities/people/peopleData.ts index 8ca04724..c8edfeaa 100644 --- a/src/utilities/people/peopleData.ts +++ b/src/utilities/people/peopleData.ts @@ -2,6 +2,7 @@ import 'server-only' import type { Payload } from 'payload' +import { primaryPhoneOf } from '@/lib/phone' import { relationshipId, uniqueRelationshipIds } from '@/lib/relationship' import { isSupportStatus, type SupportStatus } from '@/lib/schemas/leadership' import type { CampaignUser, Contact, Leadership, StateDeputy } from '@/payload-types' @@ -293,7 +294,7 @@ const contactSummary = (contact: Contact | number | null | undefined): ContactSu return { id: contact.id, name: contact.name ?? 'Contato', - phone: contact.phone ?? null, + phone: primaryPhoneOf(contact.phones), email: contact.email ?? null, city: contact.city ?? null, } diff --git a/src/utilities/people/personDelete.ts b/src/utilities/people/personDelete.ts index d59b703f..1061e5d7 100644 --- a/src/utilities/people/personDelete.ts +++ b/src/utilities/people/personDelete.ts @@ -2,6 +2,7 @@ import 'server-only' import type { Payload } from 'payload' +import { primaryPhoneOf } from '@/lib/phone' import { relationshipId } from '@/lib/relationship' import { PERSON_DELETE_FORBIDDEN_MESSAGE, @@ -153,7 +154,9 @@ const anonymizeContact = async ( data: { name: 'Titular removido', email: null, - phone: tombstonePhone, + // 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, @@ -300,7 +303,7 @@ export const loadPersonDeleteManifest = async ( contact: { id: contact.id, name: contact.name ?? 'Contato', - phone: contact.phone ?? null, + phone: primaryPhoneOf(contact.phones), email: contact.email ?? null, }, leaderships: leadershipRows, diff --git a/src/utilities/signatureExport.ts b/src/utilities/signatureExport.ts index e45e2be8..9a88d7e2 100644 --- a/src/utilities/signatureExport.ts +++ b/src/utilities/signatureExport.ts @@ -1,3 +1,4 @@ +import { primaryPhoneOf } from '@/lib/phone' import { isPopulatedRelationship } from '@/lib/relationship' import type { Contact, Petition } from '@/payload-types' import type { ToCSVFunction } from '@payloadcms/plugin-import-export/types' @@ -27,7 +28,7 @@ export const signatureContactToCSV: ToCSVFunction = ({ value, row }) => { row.contact_name = value.name row.contact_email = value.email ?? '' - row.contact_phone = value.phone ?? '' + row.contact_phone = primaryPhoneOf(value.phones) ?? '' row.contact_state = value.state ?? '' row.contact_city = value.city ?? '' return undefined diff --git a/src/utilities/stateDeputyData.ts b/src/utilities/stateDeputyData.ts index be1e3574..95919ec2 100644 --- a/src/utilities/stateDeputyData.ts +++ b/src/utilities/stateDeputyData.ts @@ -2,6 +2,7 @@ import 'server-only' import type { Payload } from 'payload' +import { phoneValuesOf, primaryPhoneOf } from '@/lib/phone' import { populatedContactName, relationshipId, uniqueRelationshipIds } from '@/lib/relationship' import type { CampaignUser, StateDeputy } from '@/payload-types' import { NO_PARTY_FILTER_VALUE } from '@/utilities/campaignListUrl' @@ -40,7 +41,7 @@ const stateDeputyContactSummary = ( id: contact.id, name: contact.name, email: contact.email ?? null, - phone: contact.phone ?? null, + phone: primaryPhoneOf(contact.phones), } } @@ -240,6 +241,8 @@ export type StateDeputySummary = { export type StateDeputyDetailViewModel = StateDeputySummary & { email: string | null phone: string | null + /** Every number of the ficha, order = priority (C112) — primary first. */ + phones: string[] notes: string | null municipalities: Array<{ id: number; name: string; slug: string }> leaderships: Array<{ id: number; name: string }> @@ -300,8 +303,12 @@ export const loadStateDeputyDetail = async ( loadCampaignUserSummaries(payload, user, uniqueRelationshipIds(stateDeputy.advisors)), ]) + const contactSummary = stateDeputyContactSummary(stateDeputy.contact) + const contact = stateDeputy.contact + return { - ...stateDeputyContactSummary(stateDeputy.contact), + ...contactSummary, + phones: phoneValuesOf(typeof contact === 'object' && contact !== null ? contact.phones : null), id: stateDeputy.id, slug: stateDeputy.slug, party: stateDeputy.party ?? null, diff --git a/src/utilities/supporter/supporterImportBulk.ts b/src/utilities/supporter/supporterImportBulk.ts index d5b3b322..d31847fa 100644 --- a/src/utilities/supporter/supporterImportBulk.ts +++ b/src/utilities/supporter/supporterImportBulk.ts @@ -1,5 +1,7 @@ import 'server-only' +import { randomUUID } from 'node:crypto' + import { sql } from '@payloadcms/db-postgres' import type { Payload } from 'payload' @@ -43,6 +45,7 @@ type DrizzleTx = { } const CONTACT_TABLE = 'contact' +const CONTACT_PHONES_TABLE = 'contact_phones' const SUPPORTER_TABLE = 'supporter' /** @@ -68,13 +71,15 @@ const SUPPORTER_COLUMNS = [ 'updatedAt', ] as const +/** The `Contact.phones` join table (C112): order = priority, value = the number. */ +const CONTACT_PHONES_COLUMNS = ['_order', '_parentID', 'value'] as const + const buildContactRows = ( rows: SupporterImportBulkRow[], now: string, ): Array> => rows.map((row) => ({ name: row.nome, - phone: row.telefone, email: null, state: 'BA', city: row.municipio ?? null, @@ -128,6 +133,12 @@ const asContactID = (row: Record): number | null => { return Number.isSafeInteger(parsed) ? parsed : null } +const asCreatedContactID = (row: Record): number | null => { + const value = row.id + const parsed = typeof value === 'number' ? value : Number(value) + return Number.isSafeInteger(parsed) ? parsed : null +} + /** * Bulk-insert contacts and municipality-less supporters for a CSV import inside the * caller's Payload transaction. Phone advisory locks MUST already be held for @@ -159,8 +170,10 @@ export const bulkInsertSupporterImport = async (args: { const tables = getDrizzleTables(payload) const contactTable = requireTable(tables, CONTACT_TABLE) + const contactPhonesTable = requireTable(tables, CONTACT_PHONES_TABLE) const supporterTable = requireTable(tables, SUPPORTER_TABLE) assertDrizzleColumns(supporterTable, SUPPORTER_TABLE, SUPPORTER_COLUMNS) + assertDrizzleColumns(contactPhonesTable, CONTACT_PHONES_TABLE, CONTACT_PHONES_COLUMNS) const database = await getPostgresTransactionDatabase(payload, req) const tx = database as unknown as DrizzleTx @@ -172,10 +185,12 @@ export const bulkInsertSupporterImport = async (args: { for (const phoneBatch of chunk(phones, INSERT_CHUNK_SIZE)) { const existingContactRows = drizzleResultRows( await tx.execute( - sql`SELECT "id", "phone" FROM "contact" WHERE "phone" IN (${sql.join( - phoneBatch.map((phone) => sql`${phone}`), - sql`, `, - )})`, + sql`SELECT "contact"."id" AS "id", "contact_phones"."value" AS "phone" FROM "contact" + JOIN "contact_phones" ON "contact_phones"."_parent_id" = "contact"."id" + WHERE "contact_phones"."value" IN (${sql.join( + phoneBatch.map((phone) => sql`${phone}`), + sql`, `, + )})`, ), ) for (const row of existingContactRows) { @@ -205,9 +220,18 @@ export const bulkInsertSupporterImport = async (args: { const newRows = [...newContactByPhone.values()] for (const batch of chunk(newRows, INSERT_CHUNK_SIZE)) { const created = await tx.insert(contactTable).values(buildContactRows(batch, now)).returning() - for (const row of created) { - const inserted = asPhoneKeyedRow(row) - if (inserted) contactIdByPhone.set(inserted.phone, inserted.id) + // The phones join-table rows follow the inserted contacts: each new + // ficha starts with its CSV number as the single (primary) phone. + const phoneRows: Array> = [] + created.forEach((row, index) => { + const insertedID = asCreatedContactID(row) + const telefone = batch[index]?.telefone + if (insertedID === null || !telefone) return + contactIdByPhone.set(telefone, insertedID) + phoneRows.push({ id: randomUUID(), _parentID: insertedID, _order: 0, value: telefone }) + }) + if (phoneRows.length > 0) { + await tx.insert(contactPhonesTable).values(phoneRows) } } } diff --git a/src/utilities/supporter/supporterListFilters.ts b/src/utilities/supporter/supporterListFilters.ts index fbc15fa7..f8528e96 100644 --- a/src/utilities/supporter/supporterListFilters.ts +++ b/src/utilities/supporter/supporterListFilters.ts @@ -40,9 +40,10 @@ const buildPayloadSearchWhere = (terms: SupporterSearchTerms): Where => { ] if (terms.normalizedPhone) { - searchFilters.push({ 'contact.phone': { equals: terms.normalizedPhone } }) + // Matches ANY of the ficha's numbers (C112). + searchFilters.push({ 'contact.phones.value': { equals: terms.normalizedPhone } }) } else if (terms.phoneDigits) { - searchFilters.push({ 'contact.phone': { contains: terms.phoneDigits } }) + searchFilters.push({ 'contact.phones.value': { contains: terms.phoneDigits } }) } return { or: searchFilters } diff --git a/src/utilities/supporter/supporterListSqlFilters.ts b/src/utilities/supporter/supporterListSqlFilters.ts index 4c9300d0..722ad1f7 100644 --- a/src/utilities/supporter/supporterListSqlFilters.ts +++ b/src/utilities/supporter/supporterListSqlFilters.ts @@ -20,9 +20,14 @@ const buildSqlSearchCondition = (terms: SupporterSearchTerms): ReturnType { await expect(page.getByRole('heading', { name: 'Nova liderança' })).toBeVisible() await page.getByLabel('Nome').fill(`Liderança B70 ${suffix}`) - await page.getByLabel('Celular').fill(`719${suffix}`) + await page.getByRole('textbox', { name: 'Celular 1' }).fill(`719${suffix}`) await page.getByRole('button', { name: 'Salvar' }).click() await expect(page.getByRole('heading', { name: 'Quem coordena por aqui?' })).toBeVisible({ diff --git a/tests/e2e/campaignMunicipalities.e2e.spec.ts b/tests/e2e/campaignMunicipalities.e2e.spec.ts index d865848b..b613c04c 100644 --- a/tests/e2e/campaignMunicipalities.e2e.spec.ts +++ b/tests/e2e/campaignMunicipalities.e2e.spec.ts @@ -641,7 +641,7 @@ test.describe('Municípios — jornadas por papel', () => { collection: 'contact', data: { name: fixtures.value('Contato Liderança'), - phone: leaderPhone, + phones: [{ value: leaderPhone }], state: 'BA', city: administered.name, }, @@ -702,7 +702,7 @@ test.describe('Municípios — jornadas por papel', () => { const supporterName = fixtures.value('Apoiador Liderança') const supporterPhone = fixtures.phone() await page.getByLabel('Nome *').fill(supporterName) - await page.getByLabel('Celular *').fill(supporterPhone) + await page.getByRole('textbox', { name: 'Celular 1' }).fill(supporterPhone) // Radix checkbox: a pre-hydration click is a silent no-op (B13/B17 flake). await checkRadixWhenHydrated(page, 'A pessoa autorizou o cadastro *') await page.getByRole('button', { name: 'Cadastrar contato' }).click() diff --git a/tests/e2e/fixtures/campaignE2EFixtures.ts b/tests/e2e/fixtures/campaignE2EFixtures.ts index 64f82b26..6407ce5a 100644 --- a/tests/e2e/fixtures/campaignE2EFixtures.ts +++ b/tests/e2e/fixtures/campaignE2EFixtures.ts @@ -185,7 +185,7 @@ class CampaignE2EOwnership { collection: 'contact', data: { name: contactName, - phone: this.phone(), + phones: [{ value: this.phone() }], state: 'BA', city: municipalities[0]?.name ?? 'Salvador', }, diff --git a/tests/helpers/campaignFixtures.ts b/tests/helpers/campaignFixtures.ts index 81186c88..3ada076b 100644 --- a/tests/helpers/campaignFixtures.ts +++ b/tests/helpers/campaignFixtures.ts @@ -62,7 +62,9 @@ type EditorUserInput = { } type ConsentInput = Partial> type ContactInput = Partial< - Pick + Pick & { + phones: Array<{ value: string }> + } > type LeadershipInput = Partial< Pick< @@ -544,7 +546,7 @@ export class CampaignFixtures { or: [ { name: { contains: this.runID } }, { email: { contains: this.runID } }, - { phone: { contains: this.runID } }, + { 'phones.value': { contains: this.runID } }, ], }, depth: 0, @@ -656,11 +658,12 @@ export class CampaignFixtures { } async createContact(input: ContactInput = {}): Promise { + const phone = input.phones?.[0]?.value ?? this.phone() const contact = await this.rootPayload.create({ collection: 'contact', data: { name: this.value('Contato'), - phone: this.phone(), + phones: [{ value: phone }], state: 'BA', city: 'Salvador', ...input, @@ -668,12 +671,13 @@ export class CampaignFixtures { depth: 0, }) this.own('contact', contact) - // Contact.phone is optional at the collection level (name-only imports), but - // every fixture contact carries one — narrow so specs can rely on it. - if (typeof contact.phone !== 'string') { + // The fixture contract is "one primary phone": every fixture contact carries + // one — narrow so specs can rely on it (C112 keeps the array under the hood). + const primary = contact.phones?.[0]?.value + if (typeof primary !== 'string') { throw new Error('Fixture contacts must always carry a phone.') } - return { ...contact, phone: contact.phone } + return { ...contact, phone: primary } } /** Assign advisors to a seeded municipality (tracked for reset on cleanup). */ @@ -888,7 +892,7 @@ export class CampaignFixtures { for (const contact of contacts.docs) { if ( this.owned.contact.has(contact.id) || - this.hasMarker(contact.name, contact.email, contact.phone) + this.hasMarker(contact.name, contact.email, contact.phones?.[0]?.value ?? null) ) { this.own('contact', contact) } diff --git a/tests/int/campaignContactIdentity.int.spec.ts b/tests/int/campaignContactIdentity.int.spec.ts index 3a7a61e1..025d22e8 100644 --- a/tests/int/campaignContactIdentity.int.spec.ts +++ b/tests/int/campaignContactIdentity.int.spec.ts @@ -47,7 +47,7 @@ describe('campaignUser → Contact identity (C99)', () => { }) expect(contact).toMatchObject({ name: 'Maria Assessora', - phone: user.phone, + phones: [{ value: user.phone }], state: 'BA', }) }) @@ -84,7 +84,7 @@ describe('campaignUser → Contact identity (C99)', () => { depth: 0, overrideAccess: true, }) - expect(contact).toMatchObject({ name: 'Sem Telefone', phone: null, state: 'BA' }) + expect(contact).toMatchObject({ name: 'Sem Telefone', phones: [], state: 'BA' }) }) it('never copies placeholder e-mails to the ficha', async () => { @@ -174,7 +174,10 @@ describe('campaignUser → Contact identity (C99)', () => { depth: 0, overrideAccess: true, }) - expect(contact).toMatchObject({ name: 'Nome Atualizado', phone: nextPhone }) + expect(contact).toMatchObject({ + name: 'Nome Atualizado', + phones: expect.arrayContaining([expect.objectContaining({ value: nextPhone })]), + }) }) it('shares the phone with another ficha when the account phone is updated (C111)', async () => { @@ -200,11 +203,13 @@ describe('campaignUser → Contact identity (C99)', () => { depth: 0, overrideAccess: true, }) - expect(contact.phone).toBe(other.phone) + expect(contact.phones?.map((entry) => entry.value)).toEqual( + expect.arrayContaining([user.phone, other.phone]), + ) const othersWithPhone = await payload.find({ collection: 'contact', - where: { phone: { equals: other.phone } }, + where: { 'phones.value': { equals: other.phone } }, depth: 0, limit: 2, pagination: false, @@ -244,13 +249,22 @@ describe('campaignUser → Contact identity (C99)', () => { it('creates a fresh ficha when the phone already belongs to two fichas (C111)', async () => { const fixtures = campaignFixtures() const phone = fixtures.phone() + const sharedNameA = fixtures.value('Compartilhado A') + const sharedNameB = fixtures.value('Compartilhado B') // Two people sharing one phone is a legitimate C111 state; it is planted // directly because the collection hook no longer enforces uniqueness. await payload.db.drizzle.execute( sql.raw(` - INSERT INTO "contact" ("name", "email", "phone", "state", "city") - VALUES ('${fixtures.value('Compartilhado A')}', '', '${phone}', 'BA', ''), - ('${fixtures.value('Compartilhado B')}', '', '${phone}', 'BA', '') + INSERT INTO "contact" ("name", "state", "city") + VALUES ('${sharedNameA}', 'BA', ''), + ('${sharedNameB}', 'BA', '') + `), + ) + await payload.db.drizzle.execute( + sql.raw(` + INSERT INTO "contact_phones" ("id", "_parent_id", "_order", "value") + SELECT gen_random_uuid()::text, "id", 0, '${phone}' FROM "contact" WHERE "name" IN ( + '${sharedNameA}', '${sharedNameB}') `), ) @@ -265,11 +279,11 @@ describe('campaignUser → Contact identity (C99)', () => { // The identity of the person being created is the fresh ficha — never a // guess among the existing ones. expect(contact.name).toBe(user.name) - expect(contact.phone).toBe(phone) + expect(contact.phones?.[0]?.value).toBe(phone) const allWithPhone = await payload.find({ collection: 'contact', - where: { phone: { equals: phone } }, + where: { 'phones.value': { equals: phone } }, depth: 0, limit: 0, pagination: false, @@ -323,7 +337,7 @@ describe('campaignUser → Contact identity (C99)', () => { const contacts = await payload.find({ collection: 'contact', - where: { phone: { equals: phone } }, + where: { 'phones.value': { equals: phone } }, depth: 0, limit: 1, pagination: false, @@ -353,7 +367,7 @@ describe('campaignUser → Contact identity (C99)', () => { depth: 0, overrideAccess: true, }) - expect(contact.phone).toBe(user.phone) + expect(contact.phones?.[0]?.value).toBe(user.phone) }) it('does not rewrite the ficha on a non-identity update (username)', async () => { @@ -377,7 +391,7 @@ describe('campaignUser → Contact identity (C99)', () => { depth: 0, overrideAccess: true, }) - expect(contact).toMatchObject({ name: user.name, phone: user.phone }) + expect(contact).toMatchObject({ name: user.name, phones: [{ value: user.phone }] }) }) it('swaps a placeholder e-mail for the real one on the ficha in update', async () => { diff --git a/tests/int/campaignFixtures.int.spec.ts b/tests/int/campaignFixtures.int.spec.ts index 4631a7ba..2185a945 100644 --- a/tests/int/campaignFixtures.int.spec.ts +++ b/tests/int/campaignFixtures.int.spec.ts @@ -217,7 +217,7 @@ describe('campaign integration fixtures', () => { collection: 'contact', data: { name: fixtures.value('Contato criado pelo servidor E2E'), - phone: fixtures.phone(), + phones: [{ value: fixtures.phone() }], state: 'BA', city: 'Salvador', }, @@ -276,14 +276,14 @@ describe('campaign integration fixtures', () => { const findOrCreateSentinelContact = async (name: string, phone: string) => { const existing = await payload.find({ collection: 'contact', - where: { phone: { equals: phone } }, + where: { 'phones.value': { equals: phone } }, depth: 0, limit: 1, }) if (existing.docs[0]) return existing.docs[0] return payload.create({ collection: 'contact', - data: { name, phone, state: 'BA', city: 'Salvador' }, + data: { name, phones: [{ value: phone }], state: 'BA', city: 'Salvador' }, depth: 0, }) } diff --git a/tests/int/campaignInvite.int.spec.ts b/tests/int/campaignInvite.int.spec.ts index 1875d0dd..7ffaa6b8 100644 --- a/tests/int/campaignInvite.int.spec.ts +++ b/tests/int/campaignInvite.int.spec.ts @@ -194,7 +194,6 @@ const createInviteLeadershipGraph = async ( ) => { const contact = await campaignFixtures().createContact({ name: campaignFixtures().value('Liderança convite'), - phone: campaignFixtures().phone(), }) const leadership = await campaignFixtures().createLeadership({ contact, @@ -960,7 +959,7 @@ describe('campaign invite domain', () => { }) expect(updatedContact).toMatchObject({ name: 'Maria Confirmada', - phone: submission.phone, + phones: expect.arrayContaining([expect.objectContaining({ value: submission.phone })]), email: 'confirmada@example.com', gender: 'feminino', }) @@ -1063,10 +1062,14 @@ describe('campaign invite domain', () => { await expect( payload.findByID({ collection: 'contact', id: target.contact.id, depth: 0 }), - ).resolves.toMatchObject({ phone: owner.contact.phone }) + ).resolves.toMatchObject({ + phones: expect.arrayContaining([expect.objectContaining({ value: owner.contact.phone })]), + }) await expect( payload.findByID({ collection: 'contact', id: owner.contact.id, depth: 0 }), - ).resolves.toMatchObject({ phone: owner.contact.phone }) + ).resolves.toMatchObject({ + phones: expect.arrayContaining([expect.objectContaining({ value: owner.contact.phone })]), + }) }, ) @@ -1114,7 +1117,11 @@ describe('campaign invite domain', () => { payload.findByID({ collection: 'contact', id, depth: 0 }), ), ) - expect(contacts.every(({ phone }) => phone === targetPhone)).toBe(true) + expect( + contacts.every((contact) => + (contact.phones ?? []).some((entry) => entry.value === targetPhone), + ), + ).toBe(true) }, ) @@ -1156,8 +1163,12 @@ describe('campaign invite domain', () => { payload.findByID({ collection: 'contact', id: first.contact.id, depth: 0 }), payload.findByID({ collection: 'contact', id: second.contact.id, depth: 0 }), ]) - expect(firstContact.phone).toBe(second.contact.phone) - expect(secondContact.phone).toBe(first.contact.phone) + expect(firstContact.phones?.map((entry) => entry.value)).toEqual( + expect.arrayContaining([second.contact.phone]), + ) + expect(secondContact.phones?.map((entry) => entry.value)).toEqual( + expect.arrayContaining([first.contact.phone]), + ) }, ) diff --git a/tests/int/campaignInviteUi.int.spec.ts b/tests/int/campaignInviteUi.int.spec.ts index 8a92c2be..3d20c037 100644 --- a/tests/int/campaignInviteUi.int.spec.ts +++ b/tests/int/campaignInviteUi.int.spec.ts @@ -126,7 +126,7 @@ const createPublicInviteScenario = async ( collection: 'contact', data: { name: 'Maria da Conceição', - phone: campaignFixtures().phone(), + phones: [{ value: campaignFixtures().phone() }], email: 'maria@example.com', state: 'BA', city: 'Salvador', @@ -216,7 +216,7 @@ describe('campaign invite UI contracts', () => { kind: 'autopreenchimento', profile: { name: fixture.contact.name, - phone: fixture.contact.phone, + phone: fixture.contact.phones?.[0]?.value ?? '', email: fixture.contact.email, gender: fixture.contact.gender, }, @@ -354,7 +354,7 @@ describe('campaign invite UI contracts', () => { const fixture = await createPublicInviteScenario('login') const formData = new FormData() formData.set('name', fixture.contact.name) - formData.set('phone', fixture.contact.phone!) + formData.set('phone', fixture.contact.phones?.[0]?.value ?? '') formData.set('email', fixture.contact.email ?? '') formData.set('password', 'senha-segura') formData.set('passwordConfirmation', 'senha-diferente') @@ -377,7 +377,7 @@ describe('campaign invite UI contracts', () => { const fixture = await createPublicInviteScenario('login') const formData = new FormData() formData.set('name', fixture.contact.name) - formData.set('phone', fixture.contact.phone!) + formData.set('phone', fixture.contact.phones?.[0]?.value ?? '') formData.set('email', fixture.contact.email ?? '') formData.set('password', ' senha-segura ') formData.set('passwordConfirmation', 'senha-segura') @@ -401,7 +401,7 @@ describe('campaign invite UI contracts', () => { const fixture = await createPublicInviteScenario('autopreenchimento') const formData = new FormData() formData.set('name', 'Maria Atualizada') - formData.set('phone', fixture.contact.phone!) + formData.set('phone', fixture.contact.phones?.[0]?.value ?? '') formData.set('email', fixture.contact.email ?? '') formData.set('gender', 'feminino') formData.set('consentAccepted', 'on') diff --git a/tests/int/campaignLeadership.int.spec.ts b/tests/int/campaignLeadership.int.spec.ts index 5742d881..e60b9857 100644 --- a/tests/int/campaignLeadership.int.spec.ts +++ b/tests/int/campaignLeadership.int.spec.ts @@ -44,7 +44,7 @@ describe('campaign leadership domain', () => { expect(Contact.fields).toEqual( expect.arrayContaining([ expect.objectContaining({ name: 'email', required: false }), - expect.objectContaining({ name: 'phone', index: true }), + expect.objectContaining({ name: 'phones', type: 'array' }), expect.objectContaining({ name: 'gender', type: 'select' }), ]), ) @@ -63,14 +63,14 @@ describe('campaign leadership domain', () => { const parsed = leadershipCreateSchema.parse({ municipalities: [1, 1, 2], name: 'Maria da Silva', - phone: '+55 (71) 99999-0000', + phones: ['+55 (71) 99999-0000'], supportStatus: 'engajado', createdBy: 999, user: 999, consent: 999, }) - expect(parsed.phone).toBe('71999990000') + expect(parsed.phones).toEqual(['71999990000']) expect(parsed.municipalities).toEqual([1, 2]) expect(Object.hasOwn(parsed, 'createdBy')).toBe(false) expect(Object.hasOwn(parsed, 'user')).toBe(false) @@ -79,7 +79,7 @@ describe('campaign leadership domain', () => { leadershipCreateSchema.parse({ municipalities: [], name: 'Sem município', - phone: '71999990000', + phones: ['71999990000'], }), ).toThrow() const update = leadershipInternalUpdateSchema.parse({ @@ -124,7 +124,7 @@ describe('campaign leadership domain', () => { const first = await createLeadershipRecord(payload, coordinator, { municipalities: [firstMunicipality.id], name: 'Maria de Jesus', - phone: `+55 ${phone}`, + phones: [`+55 ${phone}`], email: '', gender: 'feminino', supportStatus: 'engajado', @@ -135,7 +135,7 @@ describe('campaign leadership domain', () => { const contacts = await payload.find({ collection: 'contact', - where: { phone: { equals: phone } }, + where: { 'phones.value': { equals: phone } }, pagination: false, depth: 0, }) @@ -149,7 +149,7 @@ describe('campaign leadership domain', () => { createLeadershipRecord(payload, coordinator, { municipalities: [secondMunicipality.id], name: 'Nome que não sobrescreve contato existente', - phone, + phones: [phone], supportStatus: 'a_abordar', }), ).rejects.toThrow(DUPLICATE_LEADERSHIP_MESSAGE) @@ -159,12 +159,15 @@ describe('campaign leadership domain', () => { const coordinator = await campaignFixtures().createCampaignUser('coordinator') const municipality = await campaignFixtures().getMunicipality() const phone = campaignFixtures().phone() - const contact = await campaignFixtures().createContact({ name: 'Contato Existente', phone }) + const contact = await campaignFixtures().createContact({ + name: 'Contato Existente', + phones: [{ value: phone }], + }) const created = await createLeadershipRecord(payload, coordinator, { municipalities: [municipality.id], name: 'Nome que não sobrescreve contato existente', - phone: `+55 ${phone}`, + phones: [`+55 ${phone}`], supportStatus: 'engajado', }) @@ -209,7 +212,7 @@ describe('campaign leadership domain', () => { const first = createLeadershipRecord(payload, coordinator, { municipalities: [firstMunicipality.id], name: 'Contato Concorrente', - phone, + phones: [phone], supportStatus: 'engajado', }) await firstReadReached @@ -230,7 +233,7 @@ describe('campaign leadership domain', () => { const second = createLeadershipRecord(payload, coordinator, { municipalities: [secondMunicipality.id], name: 'Contato Concorrente Duplicado', - phone: `+55 ${phone}`, + phones: [`+55 ${phone}`], supportStatus: 'a_abordar', }) @@ -267,7 +270,7 @@ describe('campaign leadership domain', () => { const contacts = await payload.find({ collection: 'contact', - where: { phone: { equals: phone } }, + where: { 'phones.value': { equals: phone } }, pagination: false, depth: 0, }) @@ -288,13 +291,13 @@ describe('campaign leadership domain', () => { await payload.create({ collection: 'contact', - data: { name: 'Primeiro Contato', phone, state: 'BA', city: 'Salvador' }, + data: { name: 'Primeiro Contato', phones: [{ value: phone }], state: 'BA', city: 'Salvador' }, }) // C111 — the phone is a contact channel, not a unique identity. await payload.create({ collection: 'contact', - data: { name: 'Segundo Contato', phone, state: 'BA', city: 'Salvador' }, + data: { name: 'Segundo Contato', phones: [{ value: phone }], state: 'BA', city: 'Salvador' }, }) // Two fichas already share the phone, so the leadership create cannot @@ -304,7 +307,7 @@ describe('campaign leadership domain', () => { createLeadershipRecord(payload, coordinator, { municipalities: [municipality.id], name: 'Contato Reutilizado', - phone, + phones: [phone], supportStatus: 'engajado', }), ).resolves.toMatchObject({ contactReused: false }) @@ -345,14 +348,14 @@ describe('campaign leadership domain', () => { const ownLeadership = await createLeadershipRecord(payload, advisor, { municipalities: [assigned.id], name: 'Liderança do município', - phone: campaignFixtures().phone(), + phones: [campaignFixtures().phone()], supportStatus: 'engajado', notes: 'Avaliação interna', }) await createLeadershipRecord(payload, coordinator, { municipalities: [other.id], name: 'Liderança Alheia', - phone: campaignFixtures().phone(), + phones: [campaignFixtures().phone()], supportStatus: 'engajado', }) @@ -376,7 +379,7 @@ describe('campaign leadership domain', () => { createLeadershipRecord(payload, advisor, { municipalities: [other.id], name: 'Cadastro Fora do Escopo', - phone: campaignFixtures().phone(), + phones: [campaignFixtures().phone()], supportStatus: 'engajado', }), ).rejects.toThrow(OUT_OF_SCOPE_MUNICIPALITY_MESSAGE) @@ -392,7 +395,7 @@ describe('campaign leadership domain', () => { const own = await createLeadershipRecord(payload, advisor, { municipalities: [assigned.id], name: 'Ação Permitida', - phone: campaignFixtures().phone(), + phones: [campaignFixtures().phone()], supportStatus: 'engajado', }) @@ -413,7 +416,7 @@ describe('campaign leadership domain', () => { const otherLeadership = await createLeadershipRecord(payload, coordinator, { municipalities: [other.id], name: 'Outra Liderança', - phone: campaignFixtures().phone(), + phones: [campaignFixtures().phone()], supportStatus: 'engajado', }) await expect( @@ -432,7 +435,7 @@ describe('campaign leadership domain', () => { createLeadershipRecord(payload, leader, { municipalities: [municipality.id], name: 'Criação por liderança', - phone: campaignFixtures().phone(), + phones: [campaignFixtures().phone()], supportStatus: 'engajado', }), ).rejects.toThrow('Somente a coordenação e a assessoria podem gerenciar lideranças.') @@ -473,7 +476,7 @@ describe('campaign leadership domain', () => { createLeadershipRecord(payload, coordinator, { municipalities: [municipality.id], name: 'Contato que Deve Reverter', - phone, + phones: [phone], supportStatus: 'engajado', }), ).rejects.toThrow('falha forçada após contato') @@ -482,7 +485,7 @@ describe('campaign leadership domain', () => { const [contacts, leaderships] = await Promise.all([ payload.find({ collection: 'contact', - where: { phone: { equals: phone } }, + where: { 'phones.value': { equals: phone } }, depth: 0, pagination: false, }), @@ -602,7 +605,7 @@ describe('campaign leadership domain', () => { const created = await createLeadershipRecord(payload, coordinator, { municipalities: [municipality.id], name: 'Registro Permanente', - phone: campaignFixtures().phone(), + phones: [campaignFixtures().phone()], supportStatus: 'a_abordar', }) @@ -663,7 +666,7 @@ describe('campaign leadership domain', () => { const created = await createLeadershipRecord(payload, coordinator, { municipalities: [municipality.id], name: 'Nome Original', - phone, + phones: [phone], email: 'original@example.com', supportStatus: 'engajado', }) @@ -693,7 +696,7 @@ describe('campaign leadership domain', () => { }) expect(contact.name).toBe('Nome Corrigido') expect(contact.email).toBe('corrigido@example.com') - expect(contact.phone).toBe(newPhone) + expect(contact.phones?.[0]?.value).toBe(newPhone) }) it('shares a leadership contact phone with another person (C111)', async () => { @@ -701,11 +704,11 @@ describe('campaign leadership domain', () => { const coordinator = await fixtures.createCampaignUser('coordinator') const municipality = await fixtures.getMunicipality() const takenPhone = fixtures.phone() - await fixtures.createContact({ phone: takenPhone }) + await fixtures.createContact({ phones: [{ value: takenPhone }] }) const leadership = await createLeadershipRecord(payload, coordinator, { municipalities: [municipality.id], name: 'Liderança Sem Conflito', - phone: fixtures.phone(), + phones: [fixtures.phone()], supportStatus: 'engajado', }) @@ -721,11 +724,11 @@ describe('campaign leadership domain', () => { depth: 0, overrideAccess: true, }) - expect(contact.phone).toBe(takenPhone) + expect(contact.phones?.[0]?.value).toBe(takenPhone) const withPhone = await payload.find({ collection: 'contact', - where: { phone: { equals: takenPhone } }, + where: { 'phones.value': { equals: takenPhone } }, depth: 0, limit: 2, pagination: false, @@ -744,7 +747,7 @@ describe('campaign leadership domain', () => { const leadership = await createLeadershipRecord(payload, coordinator, { municipalities: [away.id], name: 'Fora da Carteira', - phone: fixtures.phone(), + phones: [fixtures.phone()], supportStatus: 'engajado', }) diff --git a/tests/int/campaignLeadershipActions.int.spec.ts b/tests/int/campaignLeadershipActions.int.spec.ts index 914636f2..75e4b560 100644 --- a/tests/int/campaignLeadershipActions.int.spec.ts +++ b/tests/int/campaignLeadershipActions.int.spec.ts @@ -37,7 +37,7 @@ const createFormData = (municipalityIds: number[], name: string, phone: string) for (const municipalityId of municipalityIds) formData.append('municipalities', String(municipalityId)) formData.set('name', name) - formData.set('phone', phone) + formData.append('phones', phone) formData.set('supportStatus', 'engajado') return formData } @@ -82,7 +82,7 @@ describe('campaign leadership exported form actions', () => { const contacts = await payload.find({ collection: 'contact', - where: { phone: { equals: phone } }, + where: { 'phones.value': { equals: phone } }, limit: 2, depth: 0, }) @@ -114,14 +114,14 @@ describe('campaign leadership exported form actions', () => { const contacts = await payload.find({ collection: 'contact', - where: { phone: { equals: phone } }, + where: { 'phones.value': { equals: phone } }, limit: 2, depth: 0, }) expect(contacts.totalDocs).toBe(1) expect(contacts.docs[0]).toMatchObject({ name: 'Francisco', - phone, + phones: [{ value: phone }], }) const leaderships = await payload.find({ collection: 'leadership', @@ -145,7 +145,7 @@ describe('campaign leadership exported form actions', () => { const formData = new FormData() formData.append('municipalities', String(municipality.id)) formData.set('name', 'Liderança mínima') - formData.set('phone', phone) + formData.append('phones', phone) authState.user = coordinator await expectRedirectTo(createLeadershipFormAction({}, formData), '/campanha/liderancas/') @@ -153,14 +153,14 @@ describe('campaign leadership exported form actions', () => { const contact = ( await payload.find({ collection: 'contact', - where: { phone: { equals: phone } }, + where: { 'phones.value': { equals: phone } }, limit: 1, depth: 0, }) ).docs[0]! expect(contact).toMatchObject({ name: 'Liderança mínima', - phone, + phones: [{ value: phone }], email: null, gender: null, }) @@ -188,7 +188,7 @@ describe('campaign leadership exported form actions', () => { await createLeadershipRecord(payload, coordinator, { municipalities: [municipality.id], name: 'Pessoa já cadastrada', - phone, + phones: [phone], supportStatus: 'engajado', }) @@ -207,7 +207,7 @@ describe('campaign leadership exported form actions', () => { const created = await createLeadershipRecord(payload, coordinator, { municipalities: [municipality.id], name: 'Liderança com campos internos', - phone: campaignFixtures().phone(), + phones: [campaignFixtures().phone()], exclusive: true, notes: 'Nota para limpar', }) @@ -234,14 +234,14 @@ describe('campaign leadership exported form actions', () => { const defaulted = await createLeadershipRecord(payload, coordinator, { municipalities: [municipality.id], name: 'Liderança exclusiva default', - phone: campaignFixtures().phone(), + phones: [campaignFixtures().phone()], }) expect(defaulted.exclusive).toBe(true) const divided = await createLeadershipRecord(payload, coordinator, { municipalities: [municipality.id], name: 'Liderança dividida', - phone: campaignFixtures().phone(), + phones: [campaignFixtures().phone()], exclusive: false, }) expect(divided.exclusive).toBe(false) @@ -258,7 +258,7 @@ describe('campaign leadership exported form actions', () => { const otherLeadership = await createLeadershipRecord(payload, coordinator, { municipalities: [other.id], name: 'Liderança fora do escopo', - phone: campaignFixtures().phone(), + phones: [campaignFixtures().phone()], supportStatus: 'engajado', }) authState.user = advisor @@ -288,7 +288,7 @@ describe('campaign leadership exported form actions', () => { const existing = await createLeadershipRecord(payload, coordinator, { municipalities: [municipality.id], name: 'Registro protegido', - phone: campaignFixtures().phone(), + phones: [campaignFixtures().phone()], supportStatus: 'engajado', }) authState.user = leaderAccount diff --git a/tests/int/campaignMultiPhones.int.spec.ts b/tests/int/campaignMultiPhones.int.spec.ts new file mode 100644 index 00000000..428bc39a --- /dev/null +++ b/tests/int/campaignMultiPhones.int.spec.ts @@ -0,0 +1,458 @@ +// @vitest-environment node + +import type { Payload } from 'payload' +import { getPayload } from 'payload' +import { beforeAll, describe, expect, it, vi } from 'vitest' + +import type { CampaignUser } from '@/payload-types' + +const authState = vi.hoisted(() => ({ + user: null as CampaignUser | null, +})) +const requestHeadersState = vi.hoisted(() => ({ + value: new Headers({ origin: 'http://localhost:3212' }), +})) + +vi.mock('@/utilities/campaignAuth', () => ({ + getCampaignUser: async () => authState.user, +})) +vi.mock('next/headers', () => ({ + headers: async () => requestHeadersState.value, +})) +vi.mock('next/cache', () => ({ + revalidatePath: vi.fn(), +})) + +import { + createLeadershipRecord, + updateLeadershipContactRecord, +} from '@/app/(campaign)/campanha/actions/leadership' +import { + createSupporterRecord, + updateSupporterContactRecord, +} from '@/app/(campaign)/campanha/actions/supporter' +import { SUPPORTER_REGISTRATION_CONSENT_KEY } from '@/lib/campaignConsentKeys' +import { + BRAZILIAN_PHONE_DUPLICATE_MESSAGE, + BRAZILIAN_PHONE_INVALID_MESSAGE, + primaryPhoneOf, + reorderWithPrimaryPhone, +} from '@/lib/phone' +import config from '@/payload.config' +import { toSupporterDetailViewModel } from '@/utilities/supporter/supporterViewModels' +import { + ensureLeasedConsent, + SUPPORTER_REGISTRATION_CONSENT_LEASE_KEY, +} from '../helpers/testDatabaseLease' + +import { installCampaignFixtures, relationId } from '../helpers/campaignFixtures' + +let payload: Payload +const campaignFixtures = installCampaignFixtures({ + getPayload: () => payload, + setPayload: (nextPayload) => { + payload = nextPayload + }, +}) + +describe('C112 — múltiplos telefones por pessoa', () => { + beforeAll(async () => { + payload = await getPayload({ config: await config }) + }) + + it('keeps order = priority through the pure helpers', () => { + expect(primaryPhoneOf(null)).toBeNull() + expect(primaryPhoneOf([])).toBeNull() + expect(primaryPhoneOf([{ value: '71911110000' }, { value: '71922220000' }])).toBe('71911110000') + + expect(reorderWithPrimaryPhone([], '71911110000')).toEqual(['71911110000']) + expect( + reorderWithPrimaryPhone([{ value: '71911110000' }, { value: '71922220000' }], '71922220000'), + ).toEqual(['71922220000', '71911110000']) + expect(reorderWithPrimaryPhone([{ value: '71911110000' }], null)).toEqual([]) + }) + + it('creates a ficha with N phones on leadership create and lists the primary', async () => { + const coordinator = await campaignFixtures().createCampaignUser('coordinator') + const municipality = await campaignFixtures().getMunicipality() + const primary = campaignFixtures().phone() + const secondary = campaignFixtures().phone() + + const created = await createLeadershipRecord(payload, coordinator, { + municipalities: [municipality.id], + name: 'Liderança Multi Telefones', + phones: [primary, secondary], + supportStatus: 'engajado', + }) + + const contact = await payload.findByID({ + collection: 'contact', + id: relationId(created.contact) as number, + depth: 0, + overrideAccess: true, + }) + expect(contact.phones?.map((entry) => entry.value)).toEqual([primary, secondary]) + + const summary = await payload.find({ + collection: 'leadership', + where: { id: { equals: created.id } }, + depth: 1, + limit: 1, + pagination: false, + user: coordinator, + overrideAccess: false, + }) + const contactSummary = summary.docs[0]?.contact + expect( + typeof contactSummary === 'object' && contactSummary !== null + ? primaryPhoneOf(contactSummary.phones) + : null, + ).toBe(primary) + }) + + it('rejects the same number twice in one ficha (dedupe within the person)', async () => { + const coordinator = await campaignFixtures().createCampaignUser('coordinator') + const municipality = await campaignFixtures().getMunicipality() + const phone = campaignFixtures().phone() + + await expect( + createLeadershipRecord(payload, coordinator, { + municipalities: [municipality.id], + name: 'Liderança Repetida', + phones: [phone, phone], + supportStatus: 'engajado', + }), + ).rejects.toThrow(BRAZILIAN_PHONE_DUPLICATE_MESSAGE) + + await expect( + payload.create({ + collection: 'contact', + data: { + name: 'Ficha Repetida', + phones: [{ value: phone }, { value: phone }], + state: 'BA', + city: 'Salvador', + }, + depth: 0, + overrideAccess: true, + }), + ).rejects.toThrow(BRAZILIAN_PHONE_DUPLICATE_MESSAGE) + + // The invalid-number refusal is unchanged. + await expect( + payload.create({ + collection: 'contact', + data: { + name: 'Ficha Inválida', + phones: [{ value: '1199999999' }], + state: 'BA', + city: 'Salvador', + }, + depth: 0, + overrideAccess: true, + }), + ).rejects.toThrow(BRAZILIAN_PHONE_INVALID_MESSAGE) + }) + + it('sets the primary phone preserving the rest through the inline edit', async () => { + const coordinator = await campaignFixtures().createCampaignUser('coordinator') + const municipality = await campaignFixtures().getMunicipality() + const first = campaignFixtures().phone() + const second = campaignFixtures().phone() + + const created = await createLeadershipRecord(payload, coordinator, { + municipalities: [municipality.id], + name: 'Liderança Troca Principal', + phones: [first, second], + supportStatus: 'engajado', + }) + + const updated = await updateLeadershipContactRecord(payload, coordinator, { + id: created.id, + field: 'phone', + phone: second, + }) + const contact = await payload.findByID({ + collection: 'contact', + id: relationId(updated.contact) as number, + depth: 0, + overrideAccess: true, + }) + expect(contact.phones?.map((entry) => entry.value)).toEqual([second, first]) + + // Clearing the primary removes it — the rest shifts up. + await updateLeadershipContactRecord(payload, coordinator, { + id: created.id, + field: 'phone', + phone: null, + }) + const afterClear = await payload.findByID({ + collection: 'contact', + id: relationId(created.contact) as number, + depth: 0, + overrideAccess: true, + }) + expect(afterClear.phones?.map((entry) => entry.value)).toEqual([first]) + }) + + it('writes the full list through the phones branch (ficha editor)', async () => { + const coordinator = await campaignFixtures().createCampaignUser('coordinator') + const municipality = await campaignFixtures().getMunicipality() + const created = await createLeadershipRecord(payload, coordinator, { + municipalities: [municipality.id], + name: 'Liderança Editor', + phones: [campaignFixtures().phone()], + supportStatus: 'engajado', + }) + const kept = campaignFixtures().phone() + const added = campaignFixtures().phone() + + const updated = await updateLeadershipContactRecord(payload, coordinator, { + id: created.id, + field: 'phones', + phones: [kept, added], + }) + const contact = await payload.findByID({ + collection: 'contact', + id: relationId(updated.contact) as number, + depth: 0, + overrideAccess: true, + }) + expect(contact.phones?.map((entry) => entry.value)).toEqual([kept, added]) + }) + + it('supporter detail shows every number and the list still shows the primary', async () => { + const fixtures = campaignFixtures() + await ensureLeasedConsent(payload, { + consentKey: SUPPORTER_REGISTRATION_CONSENT_KEY, + leaseKey: SUPPORTER_REGISTRATION_CONSENT_LEASE_KEY, + }) + const coordinator = await fixtures.createCampaignUser('coordinator') + const municipality = await fixtures.getMunicipality() + const primary = fixtures.phone() + const secondary = fixtures.phone() + + const created = await createSupporterRecord(payload, coordinator, { + name: fixtures.value('Apoiador Multi Telefones'), + phones: [primary, secondary], + municipality: municipality.id, + city: 'Salvador', + consentAccepted: true, + }) + const supporter = await payload.findByID({ + collection: 'supporter', + id: created.id, + depth: 1, + overrideAccess: true, + }) + const detail = toSupporterDetailViewModel(supporter) + expect(detail.phones).toEqual([primary, secondary]) + expect(detail.phone).toBe(primary) + + // The supporter ficha editor (phones branch) replaces the whole list. + const third = fixtures.phone() + const updated = await updateSupporterContactRecord(payload, coordinator, { + id: created.id, + field: 'phones', + phones: [third, primary], + }) + const contact = await payload.findByID({ + collection: 'contact', + id: relationId(updated.contact) as number, + depth: 0, + overrideAccess: true, + }) + expect(contact.phones?.map((entry) => entry.value)).toEqual([third, primary]) + }) +}) + +describe('C112 — busca e convite com números secundários', () => { + beforeAll(async () => { + payload = await getPayload({ config: await config }) + }) + + it('finds the supporter by a NON-primary number (payload and SQL search paths)', async () => { + const fixtures = campaignFixtures() + await ensureLeasedConsent(payload, { + consentKey: SUPPORTER_REGISTRATION_CONSENT_KEY, + leaseKey: SUPPORTER_REGISTRATION_CONSENT_LEASE_KEY, + }) + const coordinator = await fixtures.createCampaignUser('coordinator') + const municipality = await fixtures.getMunicipality() + const primary = fixtures.phone() + const secondary = fixtures.phone() + + const created = await createSupporterRecord(payload, coordinator, { + name: fixtures.value('Apoiador Busca Secundária'), + phones: [primary, secondary], + municipality: municipality.id, + city: 'Salvador', + consentAccepted: true, + }) + + // Payload where path: the list search terms normalize to the full number. + const { toPayloadWhere } = await import('@/utilities/supporter/supporterListFilters') + const where = toPayloadWhere({ q: secondary }) + const hits = await payload.find({ + collection: 'supporter', + where, + depth: 1, + limit: 10, + pagination: false, + overrideAccess: true, + }) + expect(hits.docs.map((doc) => doc.id)).toContain(created.id) + + // SQL aggregate path: the raw-conditions builder must match a secondary too. + const { toAggregateSqlConditions } = + await import('@/utilities/supporter/supporterListSqlFilters') + const sql = await import('@payloadcms/db-postgres') + const { conditions, needsContactJoin } = toAggregateSqlConditions({ + q: secondary, + }) + expect(needsContactJoin).toBe(true) + expect(conditions.length).toBeGreaterThan(0) + const conditionSql = sql.sql.join(conditions, sql.sql` AND `) + const result = await payload.db.drizzle.execute( + sql.sql`SELECT "supporter"."id" FROM "supporter" + JOIN "contact" ON "contact"."id" = "supporter"."contact_id" + WHERE ${conditionSql}`, + ) + const rows = Array.isArray(result) + ? result + : ((result as { rows: unknown }).rows as Array<{ id: number }>) + const ids = rows.map((row) => row.id) + expect(ids).toContain(created.id) + }) + + it('builds the invite WhatsApp link with the PRIMARY of a multi-phone ficha', async () => { + const { buildCampaignInviteWhatsAppLink } = await import('@/utilities/campaignInvite') + const primary = campaignFixtures().phone() + const secondary = campaignFixtures().phone() + const url = buildCampaignInviteWhatsAppLink({ + phone: primary, + recipientName: 'Liderança', + senderName: 'Coordenação', + inviteUrl: 'https://pt.jorgesolla.com.br/campanha/convite/abc', + kind: 'autopreenchimento', + }) + expect(url).toContain(`wa.me/55${primary}`) + expect(url).not.toContain(`wa.me/55${secondary}`) + + // And the invitation creation gate uses the ficha's primary. + const coordinator = await campaignFixtures().createCampaignUser('coordinator') + const municipality = await campaignFixtures().getMunicipality() + const created = await createLeadershipRecord(payload, coordinator, { + municipalities: [municipality.id], + name: campaignFixtures().value('Liderança Convite Multi'), + phones: [primary, secondary], + supportStatus: 'engajado', + }) + const { createCampaignInvite } = await import('@/app/(campaign)/campanha/actions/invite') + authState.user = coordinator + const invite = await createCampaignInvite({ + leadership: created.id, + kind: 'autopreenchimento', + }) + expect(invite.whatsappUrl).toContain(`wa.me/55${primary}`) + }) +}) + +describe('C112 — dobradinha e assessor com N telefones', () => { + beforeAll(async () => { + payload = await getPayload({ config: await config }) + }) + + it('writes the dobradinha ficha list through the phones branch', async () => { + const { updateStateDeputyContactRecord } = + await import('@/app/(campaign)/campanha/actions/stateDeputy') + const fixtures = campaignFixtures() + const coordinator = await fixtures.createCampaignUser('coordinator') + const stateDeputy = await fixtures.createStateDeputy({ + name: `${fixtures.value('Dobradinha')} Multi`, + }) + const first = fixtures.phone() + const second = fixtures.phone() + + const updated = await updateStateDeputyContactRecord(payload, coordinator, { + id: stateDeputy.id, + field: 'phones', + phones: [first, second], + }) + const contact = await payload.findByID({ + collection: 'contact', + id: relationId(updated.contact) as number, + depth: 0, + overrideAccess: true, + }) + expect(contact.phones?.map((entry) => entry.value)).toEqual([first, second]) + }) + + it('writes the advisor ficha list and rejects duplicates', async () => { + const { updateAdvisorContactFicha } = await import('@/app/(campaign)/campanha/actions/advisor') + const fixtures = campaignFixtures() + const coordinator = await fixtures.createCampaignUser('coordinator') + authState.user = coordinator + const advisor = await fixtures.createCampaignUser('advisor', { + name: fixtures.value('Assessora Multi Telefones'), + }) + const first = fixtures.phone() + const second = fixtures.phone() + + const contactID = relationId( + await payload + .findByID({ + collection: 'campaignUser', + id: advisor.id, + depth: 0, + select: { contact: true }, + overrideAccess: true, + }) + .then((account) => account.contact), + ) + expect(contactID).not.toBeNull() + + await updateAdvisorContactFicha({ contactId: contactID!, phones: [first, second] }) + const contact = await payload.findByID({ + collection: 'contact', + id: contactID!, + depth: 0, + overrideAccess: true, + }) + expect(contact.phones?.map((entry) => entry.value)).toEqual([first, second]) + + await expect( + updateAdvisorContactFicha({ contactId: contactID!, phones: [first, first] }), + ).rejects.toThrow(BRAZILIAN_PHONE_DUPLICATE_MESSAGE) + }) +}) + +describe('C112 — CSV import com telefone compartilhado como secundário', () => { + beforeAll(async () => { + payload = await getPayload({ config: await config }) + }) + + it('flags a CSV phone carried as SECONDARY by two fichas (preview = confirm fail-closed)', async () => { + const fixtures = campaignFixtures() + await ensureLeasedConsent(payload, { + consentKey: SUPPORTER_REGISTRATION_CONSENT_KEY, + leaseKey: SUPPORTER_REGISTRATION_CONSENT_LEASE_KEY, + }) + const coordinator = await fixtures.createCampaignUser('coordinator') + const sharedSecondary = fixtures.phone() + // Two fichas whose PRIMARY differs but which both carry the CSV number. + await fixtures.createContact({ + phones: [{ value: fixtures.phone() }, { value: sharedSecondary }], + }) + await fixtures.createContact({ + phones: [{ value: fixtures.phone() }, { value: sharedSecondary }], + }) + + const { previewSupporterImportText } = + await import('@/app/(campaign)/campanha/actions/supporterImport') + const csv = `nome,telefone,municipio,intencao\nMaria Souza,${sharedSecondary},Salvador,certo` + const preview = await previewSupporterImportText(payload, coordinator, csv) + expect(preview.counts.ok).toBe(0) + expect(preview.sampleRows.some((row) => row.status === 'telefone_compartilhado')).toBe(true) + }) +}) diff --git a/tests/int/campaignMunicipalityLeadershipList.int.spec.ts b/tests/int/campaignMunicipalityLeadershipList.int.spec.ts index 207d9b48..7f50c832 100644 --- a/tests/int/campaignMunicipalityLeadershipList.int.spec.ts +++ b/tests/int/campaignMunicipalityLeadershipList.int.spec.ts @@ -152,7 +152,7 @@ describe('B155 — lideranças na lista de municípios (leitura reversa + opçõ depth: 0, overrideAccess: true, }) - expect(contact.phone).toBeNull() + expect(contact.phones).toEqual([]) const { leadershipIDsByMunicipality, summariesById } = await loadMunicipalityLeadershipSummaries(payload, coordinator, [municipality.id]) @@ -170,13 +170,13 @@ describe('B155 — lideranças na lista de municípios (leitura reversa + opçõ const first = await createLeadershipRecord(payload, coordinator, { municipalities: [municipality.id], name: 'Nome Original', - phone, + phones: [phone], }) await expect( createLeadershipRecord(payload, coordinator, { municipalities: [municipality.id], name: 'Outro Nome', - phone, + phones: [phone], }), ).rejects.toThrow('Esta pessoa já está cadastrada como liderança') diff --git a/tests/int/campaignMunicipalityStateDeputyCreate.int.spec.ts b/tests/int/campaignMunicipalityStateDeputyCreate.int.spec.ts index 0de78fbe..deaa1365 100644 --- a/tests/int/campaignMunicipalityStateDeputyCreate.int.spec.ts +++ b/tests/int/campaignMunicipalityStateDeputyCreate.int.spec.ts @@ -229,7 +229,7 @@ describe('StateDeputy contact fields (B163)', () => { expect(updated.contact).toMatchObject({ name: correctedName, email: 'corrigido@example.com', - phone, + phones: expect.arrayContaining([expect.objectContaining({ value: phone })]), }) }) @@ -240,7 +240,7 @@ describe('StateDeputy contact fields (B163)', () => { name: `${fixtures.value('Dobradinha')} Principal`, }) const takenPhone = fixtures.phone() - await fixtures.createContact({ phone: takenPhone }) + await fixtures.createContact({ phones: [{ value: takenPhone }] }) const updated = await updateStateDeputyContactRecord(payload, coordinator, { id: stateDeputy.id, @@ -254,11 +254,11 @@ describe('StateDeputy contact fields (B163)', () => { depth: 0, overrideAccess: true, }) - expect(contact.phone).toBe(takenPhone) + expect(contact.phones?.[0]?.value).toBe(takenPhone) const withPhone = await payload.find({ collection: 'contact', - where: { phone: { equals: takenPhone } }, + where: { 'phones.value': { equals: takenPhone } }, depth: 0, limit: 2, pagination: false, diff --git a/tests/int/campaignSupporter.int.spec.ts b/tests/int/campaignSupporter.int.spec.ts index bc647a98..3ba7ee4a 100644 --- a/tests/int/campaignSupporter.int.spec.ts +++ b/tests/int/campaignSupporter.int.spec.ts @@ -74,7 +74,7 @@ describe('campaign supporter domain', () => { expect( supporterCreateSchema.safeParse({ name: 'Maria Silva', - phone: '71999990000', + phones: ['71999990000'], consentAccepted: true, }).success, ).toBe(true) @@ -82,14 +82,14 @@ describe('campaign supporter domain', () => { expect( supporterCreateSchema.safeParse({ name: 'Maria Silva', - phone: '71999990000', + phones: ['71999990000'], }).success, ).toBe(false) expect( supporterCreateSchema.safeParse({ name: 'Maria Silva', - phone: 'invalid', + phones: ['invalid'], consentAccepted: true, }).success, ).toBe(false) @@ -105,7 +105,7 @@ describe('campaign supporter domain', () => { await expect( createSupporterRecord(payload, coordinator, { name: fixtures.value('Apoiador'), - phone: fixtures.phone(), + phones: [fixtures.phone()], consentAccepted: true, }), ).rejects.toThrow(/Consentimento de cadastro de apoiador ainda não configurado/) @@ -126,7 +126,7 @@ describe('campaign supporter domain', () => { const created = await createSupporterRecord(payload, coordinator, { name: fixtures.value('Apoiador'), - phone, + phones: [phone], city: 'Salvador', consentAccepted: true, }) @@ -146,7 +146,7 @@ describe('campaign supporter domain', () => { await expect( createSupporterRecord(payload, coordinator, { name: contact.name, - phone: contact.phone, + phones: [contact.phone], municipality: municipality.id, consentAccepted: true, }), @@ -164,7 +164,7 @@ describe('campaign supporter domain', () => { await expect( createSupporterRecord(payload, advisor, { name: fixtures.value('Apoiador'), - phone: fixtures.phone(), + phones: [fixtures.phone()], consentAccepted: true, }), ).rejects.toThrow(/sem município/) @@ -172,7 +172,7 @@ describe('campaign supporter domain', () => { await expect( createSupporterRecord(payload, advisor, { name: fixtures.value('Apoiador'), - phone: fixtures.phone(), + phones: [fixtures.phone()], municipality: otherMunicipality.id, consentAccepted: true, }), @@ -180,7 +180,7 @@ describe('campaign supporter domain', () => { const created = await createSupporterRecord(payload, advisor, { name: fixtures.value('Apoiador'), - phone: fixtures.phone(), + phones: [fixtures.phone()], municipality: municipality.id, consentAccepted: true, }) @@ -247,7 +247,7 @@ describe('campaign supporter domain', () => { const advisor = await fixtures.createCampaignUser('advisor') const phoneOk = fixtures.phone() const phoneDup = fixtures.phone() - const existing = await fixtures.createContact({ phone: phoneDup }) + const existing = await fixtures.createContact({ phones: [{ value: phoneDup }] }) await fixtures.createSupporter({ contact: existing.id, createdBy: coordinator.id, @@ -288,7 +288,7 @@ describe('campaign supporter domain', () => { const contact = await payload.find({ collection: 'contact', - where: { phone: { equals: phoneOk } }, + where: { 'phones.value': { equals: phoneOk } }, depth: 0, limit: 1, overrideAccess: true, @@ -312,8 +312,8 @@ describe('campaign supporter domain', () => { await ensureSupporterConsents() const coordinator = await fixtures.createCampaignUser('coordinator') const sharedPhone = fixtures.phone() - await fixtures.createContact({ name: 'Comitê Central', phone: sharedPhone }) - await fixtures.createContact({ name: 'Ana', phone: sharedPhone }) + await fixtures.createContact({ name: 'Comitê Central', phones: [{ value: sharedPhone }] }) + await fixtures.createContact({ name: 'Ana', phones: [{ value: sharedPhone }] }) const csv = [ 'nome,telefone,municipio,intencao', @@ -341,7 +341,7 @@ describe('campaign supporter domain', () => { await ensureSupporterConsents() const coordinator = await fixtures.createCampaignUser('coordinator') const phone = fixtures.phone() - await fixtures.createContact({ phone }) + await fixtures.createContact({ phones: [{ value: phone }] }) const csv = `nome,telefone,municipio,intencao\nAna Silva,${phone},Salvador,certo` const preview = await previewSupporterImportText(payload, coordinator, csv) @@ -349,7 +349,7 @@ describe('campaign supporter domain', () => { // Simulate the race: between preview and confirm another ficha claims the // same phone. The confirm holds the phone locks and must fail closed. - await fixtures.createContact({ name: 'Segunda Ficha', phone }) + await fixtures.createContact({ name: 'Segunda Ficha', phones: [{ value: phone }] }) await expect( confirmSupporterImportRecord(payload, coordinator, { @@ -394,7 +394,7 @@ describe('campaign supporter domain', () => { }) const contact = await payload.find({ collection: 'contact', - where: { phone: { equals: phoneOk } }, + where: { 'phones.value': { equals: phoneOk } }, depth: 0, limit: 1, overrideAccess: true, @@ -417,7 +417,7 @@ describe('campaign supporter domain', () => { const phone = fixtures.phone() const created = await createSupporterRecord(payload, coordinator, { name: fixtures.value('Apoiador'), - phone, + phones: [phone], consentAccepted: true, }) const contactID = relationId(created.contact) @@ -434,7 +434,7 @@ describe('campaign supporter domain', () => { depth: 0, }) expect(contact.name).toBe('Titular removido') - expect(contact.phone).not.toBe(phone) + expect(contact.phones?.map((entry) => entry.value)).not.toContain(phone) }) it('aggregates supporter overview KPIs in a single SQL query for the coordinator', async () => { diff --git a/tests/int/campaignUserPhone.int.spec.ts b/tests/int/campaignUserPhone.int.spec.ts index f8838fc1..f88911b4 100644 --- a/tests/int/campaignUserPhone.int.spec.ts +++ b/tests/int/campaignUserPhone.int.spec.ts @@ -29,7 +29,6 @@ const createEngagedPhoneAccessGraph = async ( ) => { const contact = await fixtures.createContact({ name: fixtures.value('Contato telefone'), - phone: fixtures.phone(), }) return fixtures.createLeadership({ contact, diff --git a/tests/int/collectionAccessLockdown.int.spec.ts b/tests/int/collectionAccessLockdown.int.spec.ts index df1001f1..f64acec4 100644 --- a/tests/int/collectionAccessLockdown.int.spec.ts +++ b/tests/int/collectionAccessLockdown.int.spec.ts @@ -337,7 +337,7 @@ describe('leader supporter creation scope', () => { const supporter = await withSupporterRegistrationConsent(() => createLeaderSupporterRecord(payload, leaderAccount, { name: fixtures.value('Apoiador'), - phone: fixtures.phone(), + phones: [fixtures.phone()], city: 'Salvador', municipality: linked.id, consentAccepted: true, @@ -367,7 +367,7 @@ describe('leader supporter creation scope', () => { withSupporterRegistrationConsent(() => createLeaderSupporterRecord(payload, leaderAccount, { name: fixtures.value('Apoiador'), - phone: fixtures.phone(), + phones: [fixtures.phone()], city: 'Salvador', municipality: outside.id, consentAccepted: true, @@ -384,7 +384,7 @@ describe('leader supporter creation scope', () => { await expect( createLeaderSupporterRecord(payload, advisor, { name: fixtures.value('Apoiador'), - phone: fixtures.phone(), + phones: [fixtures.phone()], city: 'Salvador', municipality: municipality.id, consentAccepted: true, diff --git a/tests/int/contactPhoneNormalization.int.spec.ts b/tests/int/contactPhoneNormalization.int.spec.ts index a9d94cf7..61ee973a 100644 --- a/tests/int/contactPhoneNormalization.int.spec.ts +++ b/tests/int/contactPhoneNormalization.int.spec.ts @@ -25,14 +25,14 @@ const contactPhoneHook = Contact.hooks?.beforeValidate?.[0] type ContactPhoneHookArgs = Parameters>[0] -const runContactPhoneHook = async (phone: string) => { +const runContactPhoneHook = async (phones: unknown) => { if (typeof contactPhoneHook !== 'function') { throw new Error('Expected Contact.beforeValidate phone hook.') } - const data = { phone } + const data: { phones?: unknown } = { phones } await contactPhoneHook(stub({ data })) - return data.phone + return data.phones } describe('Contact phone normalization boundary', () => { @@ -44,21 +44,35 @@ describe('Contact phone normalization boundary', () => { const parsed = leadershipCreateSchema.parse({ municipalities: [1], name: 'Liderança normalizada', - phone: '+55 (71) 99999-1234', + phones: ['+55 (71) 99999-1234'], }) - expect(parsed.phone).toBe('71999991234') - expect(await runContactPhoneHook(parsed.phone)).toBe('71999991234') + expect(parsed.phones).toEqual(['71999991234']) + expect(await runContactPhoneHook([{ value: parsed.phones[0] }])).toEqual([ + { value: '71999991234' }, + ]) expect(phoneMocks.normalizeBrazilianPhone).toHaveBeenCalledTimes(1) }) it('normalizes a formatted phone sent directly to the Contact API boundary', async () => { - expect(await runContactPhoneHook('+55 (71) 99999-1234')).toBe('71999991234') + expect(await runContactPhoneHook([{ value: '+55 (71) 99999-1234' }])).toEqual([ + { value: '71999991234' }, + ]) expect(phoneMocks.normalizeBrazilianPhone).toHaveBeenCalledTimes(1) }) it('rejects an invalid canonical-length direct API phone without re-normalizing it', async () => { - await expect(runContactPhoneHook('00000000000')).rejects.toThrow('Celular brasileiro inválido.') + await expect(runContactPhoneHook([{ value: '00000000000' }])).rejects.toThrow( + 'Celular brasileiro inválido.', + ) expect(phoneMocks.normalizeBrazilianPhone).not.toHaveBeenCalled() }) + + it('drops empty entries and rejects a duplicate within the same ficha', async () => { + expect(await runContactPhoneHook([{ value: '' }, { value: null }])).toEqual([]) + + await expect( + runContactPhoneHook([{ value: '71999991234' }, { value: '71999991234' }]), + ).rejects.toThrow('Telefone repetido na ficha.') + }) }) diff --git a/tests/int/personDelete.int.spec.ts b/tests/int/personDelete.int.spec.ts index edfc412f..4ea6b495 100644 --- a/tests/int/personDelete.int.spec.ts +++ b/tests/int/personDelete.int.spec.ts @@ -130,7 +130,8 @@ describe('C100 — apagar pessoa (manifest + cascata)', () => { overrideAccess: true, }) expect(remaining.name).toBe('Titular removido') - expect(remaining.phone).toMatch(/^999\d{8}$/) + expect(remaining.phones?.[0]?.value).toMatch(/^999\d{8}$/) + expect(remaining.phones).toHaveLength(1) expect(remaining.email).toBeNull() expect(remaining.city).toBeNull() } finally { diff --git a/tests/int/submitPetitionSignature.int.spec.ts b/tests/int/submitPetitionSignature.int.spec.ts index 1e8a984d..52ff75e1 100644 --- a/tests/int/submitPetitionSignature.int.spec.ts +++ b/tests/int/submitPetitionSignature.int.spec.ts @@ -95,7 +95,7 @@ describe('submitPetitionSignature (server-side consent resolution)', () => { const contacts = await payload.find({ collection: 'contact', - where: { phone: { equals: phone } }, + where: { 'phones.value': { equals: phone } }, depth: 0, limit: 1, pagination: false, diff --git a/tests/int/submitWhatsapp.int.spec.ts b/tests/int/submitWhatsapp.int.spec.ts index d28a2368..26b448e5 100644 --- a/tests/int/submitWhatsapp.int.spec.ts +++ b/tests/int/submitWhatsapp.int.spec.ts @@ -70,7 +70,7 @@ describe('submitWhatsapp (consent by stable key)', () => { const contacts = await payload.find({ collection: 'contact', - where: { phone: { equals: phone } }, + where: { 'phones.value': { equals: phone } }, depth: 0, limit: 1, pagination: false, diff --git a/tests/int/supporterListFilters.int.spec.ts b/tests/int/supporterListFilters.int.spec.ts index 017cdd68..baee77f1 100644 --- a/tests/int/supporterListFilters.int.spec.ts +++ b/tests/int/supporterListFilters.int.spec.ts @@ -46,7 +46,7 @@ describe('supporterListFilters', () => { or: [ { 'contact.name': { contains: '(71) 98888-7777' } }, { 'contact.city': { contains: '(71) 98888-7777' } }, - { 'contact.phone': { equals: '71988887777' } }, + { 'contact.phones.value': { equals: '71988887777' } }, ], }, ], diff --git a/tests/unit/signatureExport.unit.spec.ts b/tests/unit/signatureExport.unit.spec.ts index 22683456..c65569a8 100644 --- a/tests/unit/signatureExport.unit.spec.ts +++ b/tests/unit/signatureExport.unit.spec.ts @@ -26,7 +26,7 @@ describe('signatureExport', () => { id: 1, name: 'Maria Silva', email: 'maria@example.com', - phone: '71999990000', + phones: [{ value: '71999990000' }], state: 'BA', city: 'Salvador', }), @@ -44,7 +44,7 @@ describe('signatureExport', () => { id: 2, name: 'João Souza', email: null, - phone: '71988887777', + phones: [{ value: '71988887777' }], state: 'BA', city: 'Feira de Santana', }) diff --git a/tests/unit/supporterSchema.unit.spec.ts b/tests/unit/supporterSchema.unit.spec.ts index 61daed57..a07d66a1 100644 --- a/tests/unit/supporterSchema.unit.spec.ts +++ b/tests/unit/supporterSchema.unit.spec.ts @@ -10,15 +10,15 @@ import { } from '@/lib/schemas/supporter' describe('supporter schemas', () => { - it('normalizes phone and optional BA municipality', () => { + it('normalizes phones and optional BA municipality', () => { const parsed = supporterCreateSchema.parse({ name: 'Maria da Silva', - phone: '+55 (71) 98888-7777', + phones: ['+55 (71) 98888-7777'], city: 'salvador', consentAccepted: true, }) - expect(parsed.phone).toBe('71988887777') + expect(parsed.phones).toEqual(['71988887777']) expect(parsed.city).toBe('Salvador') expect(resolveBahiaMunicipality('feira de santana')).toBe('Feira de Santana') expect(resolveBahiaMunicipality('Nowhere')).toBeNull() @@ -28,7 +28,7 @@ describe('supporter schemas', () => { expect( supporterCreateSchema.safeParse({ name: 'Maria Silva', - phone: '71988887777', + phones: ['71988887777'], voteIntention: 'certo', consentAccepted: true, }).success, @@ -37,7 +37,7 @@ describe('supporter schemas', () => { expect( supporterCreateSchema.safeParse({ name: 'Maria Silva', - phone: '71988887777', + phones: ['71988887777'], voteIntention: 'certo', consentAccepted: true, voteIntentionConsentAccepted: true, From be4bb49da94799ae56f17791303cb36b37df1fe0 Mon Sep 17 00:00:00 2001 From: Francisco Solla Date: Tue, 11 Aug 2026 13:17:05 -0300 Subject: [PATCH 3/6] =?UTF-8?q?plans(C123,C124):=20agenda=20=E2=80=94=20mo?= =?UTF-8?q?dal=20central=20como=20=C3=BAnica=20superf=C3=ADcie=20de=20cria?= =?UTF-8?q?r/editar=20e=20tarefa=20sem=20data/hora?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/plans/agenda-criacao-modal-central.md | 83 ++++++++++++++++++++++ docs/plans/atividades-tarefas-sem-prazo.md | 69 ++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 docs/plans/agenda-criacao-modal-central.md create mode 100644 docs/plans/atividades-tarefas-sem-prazo.md diff --git a/docs/plans/agenda-criacao-modal-central.md b/docs/plans/agenda-criacao-modal-central.md new file mode 100644 index 00000000..97796da3 --- /dev/null +++ b/docs/plans/agenda-criacao-modal-central.md @@ -0,0 +1,83 @@ +# C123 — Agenda: modal central como única superfície de criação/edição de atividade + +Status: rascunho +Atualizado em: 2026-08-11 +Issue: #667 +Priority: P1 +Model: composer-2.5 +Impeccable: C — fluxo de criação/edição inline redesenhado na agenda (popover → modal central; formulário completo some) +Canvas UI: /home/fsolla/.cursor/projects/home-fsolla-Code-teqo/canvases/plan-c123-ui-draft.canvas.tsx +Appetite: ~1–1,5 dia eng; um outcome verificável +Responsável: — + +## Intenção + +A criação/edição de compromisso hoje pede **dois passos**: o popover de criação inline (estilo Google Calendar, C91) e depois o link "Mais detalhes" para preencher o resto no formulário completo — que vive em páginas próprias (`/campanha/atividades/nova` e `/campanha/atividades/[slug]/editar`). A mesa quer **uma** superfície só: o overlay da agenda (modal central no desktop, drawer no mobile) com **todas as configurações** — criação E edição ali mesmo, sem páginas de formulário. O popover atual também acumulou defeitos visíveis: o campo "Local" aparece **duplicado** na mesma tela (mesmo input renderizado duas vezes), e o seletor de dia/hora (calendário + seletores de hora/minuto empilhados) exige **rolar** dentro do popover para encontrar o seletor de horário — que fica escondido abaixo do calendário e ninguém descobre. + +## Persona e fluxo + +- **Persona / contexto:** staff na mesa (coordenador/assessor) no `/campanha/agenda`, criando ou ajustando um compromisso no slot certo do calendário — à pressa, entre uma ligação e outra. +- **Job principal:** lançar o compromisso completo no calendário **sem sair da agenda e sem navegar** para outra página. +- **Fluxo desejado (criar):** clica num slot → overlay abre já com o dia/hora do slot → preenche **todas** as configurações ali mesmo → Salvar → overlay fecha, evento aparece na agenda, sem reload. +- **Fluxo desejado (editar):** clica num evento da agenda → overlay abre com os valores do compromisso → ajusta → Salvar → evento atualizado no calendário. O detalhe `/campanha/atividades/[slug]` continua existindo, alcançável por link no próprio overlay (e o botão "Editar" do detalhe abre o mesmo overlay). +- **Anti-goals de produto:** não vira planilha nem redesenho do calendário em si; a página de detalhe `/campanha/atividades/[slug]` permanece como leitura/registro de resultado (não é formulário completo); recorrência não entra. + +### Esboço de fluxo (C/D) + +```text +[criar: clique no slot | editar: clique no evento] +→ [modal central (desktop) / drawer (mobile) com TODAS as configurações, dados pré-preenchidos] +→ [Início: data + hora separados | Término: data + hora separados] +→ [Salvar] → [overlay fecha, refetch da janela, calendário atualizado] +``` + +## Objetivo e aceite + +- Clicar num slot abre um **modal central** (desktop), não mais um popover ancorado ao ponto de clique. +- O overlay expõe **todas as configurações** do compromisso — todas as seções do formulário completo de hoje (básicas, data e horário, onde, pessoas e organizações, tarefas, demandas). +- Clicar num evento abre o **overlay de edição** (mesmo modal/drawer, valores preenchidos) — o clique não navega mais para o detalhe; o detalhe fica acessível por link no overlay. +- **As páginas `/campanha/atividades/nova` e `/campanha/atividades/[slug]/editar` saem sem redirecionamento** — o overlay vira a única superfície de criação/edição (o "Mais detalhes" morre junto); links antigos para essas rotas dão 404. +- O campo **"Local" aparece uma única vez** no overlay (remover o bloco duplicado). +- Início e Término têm **dois seletores separados**: um só de **data** e um só de **horário**, ambos visíveis **sem rolar** — nada de horário escondido abaixo do calendário. +- **Mobile:** continua o bottom sheet (C103) — mesma superfície única de criação/edição — com **duas linhas empilhadas** (Início em cima, Término embaixo), cada uma com o seletor de **dia** e o seletor de **hora lado a lado na mesma linha**: `[dia de início][hora de início]` / `[dia de término][hora de término]`. +- Salvar não navega; refetch da janela visível. +- Guardrails existentes intactos: contrato civil `YYYY-MM-DDTHH:mm` de parse/validação, validação início ≤ término, modo "Todo o dia" (só data), prefill do slot, permissões staff, título imutável na edição. + +## Dados (intenção) + +- **Vou apresentar dados?** Não — são campos de entrada de data/hora, sem decisão de apresentação. +- **Decisões desbloqueadas:** nenhuma de dados; forma fica para o plano de implementação. + +## Direção no codebase (hipótese) + +- **Áreas prováveis:** `src/components/campaign/activity/ActivityInlineCreate.tsx` (vira o overlay único de criar/editar — popover vira modal; remover o bloco duplicado de Local — hoje dois `` idênticos; "Mais detalhes"/`buildActivityCreateHref` morrem), `src/components/campaign/activity/ActivityDateTimeField.tsx` (dividir o picker em seletor de data + seletor de hora — provavelmente dois controles lado a lado mantendo o contrato civil), `ActivityForm.tsx` (conteúdo do overlay — a forma em página morre), páginas `/campanha/atividades/nova` e `/[slug]/editar` (remover junto com `formActions`/prefill correspondentes; a página de detalhe segue e seu botão de editar passa a abrir o overlay), `src/utilities/activityUi.ts` (href de prefill), `src/app/(campaign)/campanha/actions/activity.ts` (ações de criar/editar se unificam no overlay). +- **Precedente a olhar:** plans C91 (`agenda-criar-evento-inline.md`), C103 (`agenda-mobile-form-criacao-usavel.md`), C97 (seletor de dia/hora shadcn), C104 (todo o dia); testes `tests/unit/activityInlineCreate.unit.spec.tsx` e `tests/e2e/campaignActivity.e2e.spec.ts` usam "Mais detalhes" e as páginas de formulário — serão atualizados pelo executor. +- **Risco de acoplamento:** leader lockdown não toca criação/edição staff (o toggle de tarefa no detalhe segue como está); o contrato de valor civil `YYYY-MM-DDTHH:mm` é compartilhado com a validação do submit e com o modo todo-o-dia — não quebrar; o detalhe perde o botão de editar em página — ele passa a abrir o overlay; e2e das rotas removidas morrem junto (sem redirecionamento — 404). + +## Dependências + +- **C124** (tarefas sem prazo): **dura** — o overlay inclui a seção de tarefas e deve nascer já com o modelo simplificado. + +## Fora de escopo + +- Redesenho da página de detalhe `/campanha/atividades/[slug]` (leitura/registro de resultado permanece como está). +- Recorrência; mudanças no FullCalendar ou nos filtros/feed iCal; drag-and-drop de remarcação (intocado). + +## Rabbit holes de produto + +- **"Overlay vira mini-CRM".** Se alguém "só completar" o desejo de ter tudo no overlay, ele vira o formulário completo empilhado sem hierarquia. **Corte:** seções agrupadas como no formulário atual, overlay rolável internamente com rodapé fixo (Cancelar/Salvar). +- **"Unificar com o sheet mobile".** O bottom sheet (C103) é o padrão mobile aceito; não redesenhar o mobile além dos seletores divididos. **Corte:** mobile = mesmo conteúdo, mesma anatomia de sheet. + +## Questões em aberto (produto) + +- **Como a edição é acessada?** **Opções:** A) clique no evento abre o overlay de edição (estilo Google Calendar), e o botão "Editar" do detalhe abre o mesmo overlay; B) só o botão "Editar" do detalhe abre o overlay; C) clique no evento abre o overlay e o detalhe vira acessível por link no próprio overlay. **Recomendação:** A. _(decidido no gate — clique no evento abre o overlay de edição; o detalhe continua alcançável por link no próprio overlay; o botão "Editar" do detalhe abre o mesmo overlay)_ +- **Rotas `/campanha/atividades/nova` e `/[slug]/editar`:** redirecionar ou 404? **Recomendação:** redirecionar. _(decidido no gate — remover sem redirecionar, 404 para links antigos)_ + +## Referências + +- GitHub Issue #667 (C123) +- Plans C91 (`agenda-criar-evento-inline.md`), C103, C104, C105, C124 (dependência) +- Canvas UI (gate): `/home/fsolla/.cursor/projects/home-fsolla-Code-teqo/canvases/plan-c123-ui-draft.canvas.tsx` +- `src/components/campaign/activity/ActivityInlineCreate.tsx` · `ActivityDateTimeField.tsx` · `ActivityForm.tsx` · `ActivityTaskFields.tsx` +- `src/app/(campaign)/campanha/atividades/nova/page.tsx` · `[slug]/editar/page.tsx` · `[slug]/page.tsx` +- `tests/unit/activityInlineCreate.unit.spec.tsx` · `tests/e2e/campaignActivity.e2e.spec.ts` diff --git a/docs/plans/atividades-tarefas-sem-prazo.md b/docs/plans/atividades-tarefas-sem-prazo.md new file mode 100644 index 00000000..01af95b3 --- /dev/null +++ b/docs/plans/atividades-tarefas-sem-prazo.md @@ -0,0 +1,69 @@ +# C124 — Atividades: tarefa só com texto, responsável e concluída (sem data/hora) + +Status: rascunho +Atualizado em: 2026-08-11 +Issue: #668 +Priority: P2 +Model: composer-2.5 +Impeccable: B — encaixe nas superfícies existentes (campos de tarefa do formulário + checklist do detalhe) +Canvas UI: /home/fsolla/.cursor/projects/home-fsolla-Code-teqo/canvases/plan-c124-ui-draft.canvas.tsx +Appetite: ~0,5–1 dia eng; um outcome verificável +Responsável: — + +## Intenção + +O modelo de tarefas da atividade carrega um **prazo** (data/hora) que a mesa não usa. No dia a dia, tarefa é "o que fazer, com quem, e já foi feito?" — o prazo só polui: um input `datetime-local` apertado na linha da tarefa no formulário e um "Prazo: …" no checklist do detalhe, sem desbloquear nenhuma decisão (não há alerta nem filtro por prazo). O usuário pediu para tirar data/hora das tarefas: fica só **texto, responsável e concluída**. + +## Persona e fluxo + +- **Persona / contexto:** staff criando/editando atividade no formulário completo (`/nova` e `/editar`); liderança marcando tarefas como concluídas no detalhe da atividade (toggle, como hoje). +- **Job principal:** cadastrar e acompanhar tarefas da atividade sem atrito — digitar o que fazer, apontar o responsável, marcar feito. +- **Fluxo desejado:** no formulário, cada tarefa = título + responsável (pessoa) + checkbox "concluída"; no detalhe, o checklist mostra título + "Resp: …" e o toggle de concluída. +- **Anti-goals de produto:** prazo não migra para outro campo da atividade nem vira notificação/relatório; nada de segunda ficha de pessoa. + +## Objetivo e aceite + +- Criar/editar tarefa tem **exatamente** três controles: título (texto), responsável (pessoa) e "concluída" (checkbox) — o campo de data/hora sai da UI. +- O checklist do detalhe mostra só título + responsável (sem "Prazo: …"). +- O **schema** da tarefa não expõe mais prazo: dados existentes com prazo são **descartados** pela migração (verificar no impl que nenhum consumidor usa o campo antes de dropar). +- Guardrail de liderança intacto: liderança só pode marcar "concluída" (não altera título/responsável, não adiciona/remove tarefa) — a comparação de campos do guardrail perde a referência ao prazo. +- `doneAt` (derivado, invisível, "concluída em") permanece — auditoria de quando foi marcada. + +## Dados (intenção) + +- **Vou apresentar dados?** Não — simplificação de um modelo de entrada; o contador de tarefas concluídas (taskDoneCount) segue como está. +- **Decisões desbloqueadas:** nenhuma de apresentação. + +## Direção no codebase (hipótese) + +- **Áreas prováveis:** `src/collections/Activity.ts` (campo `due` do array `tasks` sai do schema; `doneAt` permanece; guard de liderança em `beforeValidate` perde a comparação de `due`), `src/components/campaign/activity/ActivityTaskFields.tsx` (remove o input de prazo e o serializador), `ActivityTaskChecklist.tsx` (remove a linha de prazo), `src/utilities/activityFormData.ts`, `src/lib/schemas/activity.ts`, `src/utilities/activityViewModels.ts` (parsing/validação/view model), migration nova (drop da coluna de prazo do array de tarefas — história de migrations intacta), testes unit/e2e que exercitam tarefas. +- **Precedente a olhar:** migration de adição do campo (`20260718_222832_add_action_plan`) e remodel de schema recente de atividade (`20260808_184113_remodel_activity_responsible`, `20260810_010844_add_activity_all_day`). +- **Risco de acoplamento:** o guardrail de liderança (Activity.ts, `beforeValidate`) compara tarefa a tarefa — mudar o shape exige tocar essa comparação no mesmo item, senão liderança quebra ou perde o fail-closed. + +## Dependências + +- Nenhuma dura para si. **Ela é dependida pela C123** (modal central da agenda): o overlay único de criação/edição inclui a seção de tarefas e nasce já com o modelo simplificado — a C123 roda depois desta. + +## Fora de escopo + +- Prazo em demandas/atividades (não criar campo equivalente em outro lugar). +- Notificações/alertas por prazo de tarefa. +- Editar `doneAt` ou o contador `taskDoneCount`. + +## Rabbit holes de produto + +- **"Prazo volta na listagem".** Alguém pode querer mostrar o prazo na lista de atividades. **Corte:** o campo sai do modelo; se a mesa pedir prazo de novo, é um item novo com intenção própria. +- **"Migrar prazo para outro lugar".** **Corte:** nada consome o prazo hoje (a verificação no impl fecha isso); migração de conteúdo é trabalho morto. + +## Questões em aberto (produto) + +- **`doneAt` ("concluída em") permanece no modelo?** **Opções:** A) sim, como campo derivado invisível (auditoria sem custo de UI); B) remove junto. **Recomendação:** A — custo zero, mantém rastreabilidade de quando cada tarefa foi marcada. _(assumido — validar com produto)_ +- **Prazo com valor preenchido em produção: descartar ou preservar?** **Opções:** A) descartar (nenhum consumidor; dados de campanha internos); B) preservar em coluna morta. **Recomendação:** A — limpeza real, sem coluna fantasma. _(decidido no gate — descartar e remover)_ + +## Referências + +- GitHub Issue #668 (C124) +- Plans C91/C103 (formulário de criação — o overlay da C123 inclui tarefas), C14 (modelo de atividades) +- Canvas UI (gate): `/home/fsolla/.cursor/projects/home-fsolla-Code-teqo/canvases/plan-c124-ui-draft.canvas.tsx` +- `src/collections/Activity.ts` · `src/components/campaign/activity/ActivityTaskFields.tsx` · `ActivityTaskChecklist.tsx` +- `src/utilities/activityFormData.ts` · `src/lib/schemas/activity.ts` · `src/utilities/activityViewModels.ts` From a740f1eb16fb874a4237b48d0c1724be04edcf64 Mon Sep 17 00:00:00 2001 From: Francisco Solla Date: Tue, 11 Aug 2026 13:16:54 -0300 Subject: [PATCH 4/6] =?UTF-8?q?fix(e2e):=20first()=20no=20getByLabel('Ativ?= =?UTF-8?q?idade=20relacionada')=20sob=20duplica=C3=A7=C3=A3o=20transit?= =?UTF-8?q?=C3=B3ria=20(flake=20pr=C3=A9-existente,=20precedente=20cd46985?= =?UTF-8?q?7)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit D12 (#641): strict-mode violation (2 triggers idênticos) só em runs completos sob carga. Estado estável tem 1 match (verificado em browser); o 2º match é duplicação transitória RSC-pending, mesma classe de cd469857. Mesmo alvo em campaignActivity:52 (único outro uso do label em e2e). --- ...2-getbylabel-atividade-relacionada-impl.md | 73 +++++++++++++++++++ tests/e2e/campaignActivity.e2e.spec.ts | 4 +- tests/e2e/campaignRegisterDemand.e2e.spec.ts | 4 +- 3 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 docs/plans/d12-getbylabel-atividade-relacionada-impl.md diff --git a/docs/plans/d12-getbylabel-atividade-relacionada-impl.md b/docs/plans/d12-getbylabel-atividade-relacionada-impl.md new file mode 100644 index 00000000..3d6656d0 --- /dev/null +++ b/docs/plans/d12-getbylabel-atividade-relacionada-impl.md @@ -0,0 +1,73 @@ +# Impl: E2E — flake do `getByLabel('Atividade relacionada')` (strict mode) sob carga + +Status: aprovado +Atualizado em: 2026-08-11 +Issue: #641 (D12) +Intenção: docs/plans/e2e-combobox-label-strict-mode-flake.md +Appetite restante: herdado (P3, kind: defect, sem nova superfície) + +## Leitura da intenção + +- **Outcome:** `campaignRegisterDemand` verde em run completo e isolado; sem mudança de DOM/labels no estado fechado que afete outros specs. +- **O que NÃO negociar:** copy visível e comportamento da combobox intactos; nenhuma regressão de a11y do dialog. +- **O que reavaliar:** a hipótese "o conteúdo do CommandDialog fica sempre montado" da intenção **não se sustenta** — ver caracterização abaixo. + +## Caracterização (completada na sessão, browser real + código) + +1. **Estado estável (fechado): exatamente 1 match.** O trigger (`aria-label="Atividade relacionada: Nenhuma atividade"`) é o único nó. O `CommandDialog` usa `DialogPortal` **sem** `forceMount` (src/components/ui/dialog.tsx): o conteúdo (incl. `CommandInput` com `aria-label={label}`) só monta com `open` ou na animação de saída (~200ms, `duration-200`). +2. **Dialog aberto: 2 matches** — trigger + `CommandInput` (`role="combobox"`, `aria-label="Atividade relacionada"`). Medido ao vivo. +3. **O sr-only `DialogHeader` (título + descrição) fica montado permanentemente** — é filho direto de `Dialog.Root` (render inline, fora da porta). O snapshot pós-falha do D11 ("título + descrição no DOM com o dialog fechado") é o **estado normal da página**, não evidência de dialog montado. +4. O teste que flakava (linha 38) **nunca abre o dialog** — logo o segundo match não pode ser o `CommandInput` por interação do próprio teste. +5. A intenção registra o segundo match como **"ambos botões"** com o mesmo `aria-label` do trigger → duplicação **transitória de instância** (2 comboboxes idênticos no DOM por um instante), a classe já tratada em cd469857 (2026-08-09): "transient RSC-pending duplication … strict-mode flake on loaded machines" → fix estabelecido no repo: `.first()` nos asserts. +6. Navegação dura (`page.goto`) medida ao vivo: 0→1 sem janela de 2 matches na máquina local; a duplicação é dependente de carga (máquina do pool, compiles de 6–27s). + +## Abordagem recomendada + +```mermaid +flowchart LR + A[Flake strict-mode 2 matches] --> B{2º match é...} + B -->|"CommandInput do dialog"| C[Descartado: teste nunca abre o dialog] + B -->|"2º trigger idêntico"| D[Duplicação transitória de instância
classe cd469857] + D --> E["Fix: .first() no assert
precedente do repo"] +``` + +**Opções consideradas:** A (spec `.first()`) | B (spec `getByRole('button', …)`) | C (componente: remover label do `CommandInput` / `aria-hidden` no fechado) + +**Recomendação:** A — `.first()` nos asserts de `getByLabel('Atividade relacionada')` dos dois specs que usam o label (`campaignRegisterDemand.e2e.spec.ts:38` e `campaignActivity.e2e.spec.ts:52`), com comentário citando o precedente cd469857. + +**Rejeitadas:** + +- **B** — `getByRole('button', { name: /Atividade relacionada:/ })` resolve a duplicação trigger+input (input é `role=combobox`), mas **não** resolve o caso reportado ("ambos botões" = duplicação de instância): 2 triggers de mesmo `aria-label` → mesma strict violation. +- **C** — o portal **não está montado no fechado** (medido): remover `aria-label` do `CommandInput` não muda o estado estável e degradaria a a11y do input de busca (o dialog precisa de label próprio); `aria-hidden` no fechado não tem alvo (nada montado). Não atinge a duplicação transitória, que é artefato de render do framework (Next/React), não do componente. +- (descartada na análise) "esperar o dialog abrir/fechar": o teste não interage com o dialog. + +### Componentes / mudanças + +- **`tests/e2e/campaignRegisterDemand.e2e.spec.ts:38`**: `page.getByLabel(CAMPAIGN_DEMAND_ACTIVITY_LABEL)` → `.first()`. Semântica preservada: o teste quer provar que o campo está presente/visível; qualquer uma das instâncias transitórias é o mesmo trigger. +- **`tests/e2e/campaignActivity.e2e.spec.ts:52`**: idem (mesmo label, mesma classe de flake — página da atividade também monta `AsyncSearchCombobox` via `DemandFields`). +- **Migration:** sem migration. **Access/Consent:** não toca. **UI/componente:** nenhuma mudança (aceite "sem mudança de DOM/labels"). +- Comentário nos dois asserts no estilo cd469857 (classe + motivo, não reescrever o histórico). + +## Fases verificáveis + +1. **Baseline (reprodução)** — run completo do e2e em dev (em andamento, máquina com load real 11–22): registrar se o flake reproduz localmente. O D11 já provou pré-existência em main via stash (3/4 runs falharam no pool); a reprodução local é bônus, não gate. +2. **Fix** — `.first()` nos 2 asserts. +3. **Gates** — `campaignRegisterDemand` isolado (dev) + `campaignActivity` isolado; run completo do e2e verde; `pnpm gate:fast` (tsc, lint, format, unit+int) e `pnpm check:cycles`; knip. + +## Rabbit holes / Não escopo (engenharia) + +- Não investigar o mecanismo exato da duplicação transitória do Next/React (bug upstream, dependente de carga; o repo já decidiu a resposta pragmática em cd469857). +- Não mexer no `AsyncSearchCombobox`/`CommandDialog` (a11y do dialog é correta; portal não monta no fechado). +- Não trocar `getByLabel` por `getByRole` nos outros ~100 usos do arquivo (fora do escopo do label em questão). + +## Riscos e mitigação + +- **Flake não reproduz na máquina local** (condições do pool diferentes) → mitigação: validação = mecanismo caracterizado ao vivo (estado estável 1 match; 2 matches só em instância duplicada/open) + fix idêntico ao precedente aprovado cd469857 + runs completos verdes pós-fix (1+). +- **`.first()` mascarar regressão real** (ex.: 2 instâncias ESTÁVEIS) → mitigação: assert continua verificando visibilidade do campo; se um dia houver duplicação estável, os outros asserts do spec (contagem 0/1 em linhas vizinhas) pegam. +- **Aceite "grep getByLabel em e2e"** → verificado: os únicos usos do label `Atividade relacionada` são os 2 pontos alterados; nenhum outro spec depende do estado fechado da combobox. + +## Aceite de engenharia + +- [ ] Aceite da intenção coberto (spec verde isolado e em run completo; DOM/labels fechados intactos) +- [ ] Invariantes AGENTS/engineering-standards (sem migration/access/transação; identificadores em inglês; precedente cd469857) +- [ ] Testes: run completo e2e dev verde + specs alterados isolados; gates completos no fechamento diff --git a/tests/e2e/campaignActivity.e2e.spec.ts b/tests/e2e/campaignActivity.e2e.spec.ts index cdc88810..04e7c146 100644 --- a/tests/e2e/campaignActivity.e2e.spec.ts +++ b/tests/e2e/campaignActivity.e2e.spec.ts @@ -49,7 +49,9 @@ test.describe('Atividades — registro-fundação', () => { await page.getByRole('link', { name: 'Adicionar demanda' }).click() await expect(page.locator('input[name="activityId"]')).toHaveValue(/\d+/) - await expect(page.getByLabel('Atividade relacionada')).toContainText(activityTitle) + // `.first()`: transient RSC-pending duplication also copies the combobox + // trigger (same strict-mode flake as cd469857; observed on loaded machines). + await expect(page.getByLabel('Atividade relacionada').first()).toContainText(activityTitle) await expect(page.getByLabel('Município')).toHaveValue(String(municipality.id)) await page.goto(`${campaign.baseURL}/campanha/municipios/${municipality.slug}?tab=updates`) diff --git a/tests/e2e/campaignRegisterDemand.e2e.spec.ts b/tests/e2e/campaignRegisterDemand.e2e.spec.ts index 3d9b4407..4e0551f7 100644 --- a/tests/e2e/campaignRegisterDemand.e2e.spec.ts +++ b/tests/e2e/campaignRegisterDemand.e2e.spec.ts @@ -35,7 +35,9 @@ test.describe('Wizard "Registrar pedido" (A5/B195)', () => { // free-text field comes straight after Tipo and Atividade. await expect(page.getByRole('heading', { name: 'Registrar pedido' })).toHaveCount(0) await expect(page.getByLabel(CAMPAIGN_DEMAND_KIND_LABEL)).toBeVisible() - await expect(page.getByLabel(CAMPAIGN_DEMAND_ACTIVITY_LABEL)).toBeVisible() + // `.first()`: transient RSC-pending duplication also copies the combobox + // trigger (same strict-mode flake as cd469857; observed on loaded machines). + await expect(page.getByLabel(CAMPAIGN_DEMAND_ACTIVITY_LABEL).first()).toBeVisible() await expect(page.getByLabel(CAMPAIGN_DEMAND_BODY_LABEL)).toBeVisible() await expect(page.locator('input[name="title"]')).toHaveCount(0) await expect(page.locator('select[name="municipalityId"]')).toHaveCount(0) From 95ac668c0b6b4839f9fe60e6e2020d24843d54c5 Mon Sep 17 00:00:00 2001 From: Francisco Solla Date: Tue, 11 Aug 2026 13:32:08 -0300 Subject: [PATCH 5/6] =?UTF-8?q?ops(work-issue):=20capture-review-debts=20a?= =?UTF-8?q?ut=C3=B4nomo=20=E2=80=94=20agente=20decide=20o=20que=20registra?= =?UTF-8?q?r=20e=20descartar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .agents/skills/capture-review-debts/SKILL.md | 22 ++++++++++++----- .agents/skills/work-issue/SKILL.md | 24 ++++++++++--------- .../skills/work-issue/execution-pipeline.md | 2 +- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/.agents/skills/capture-review-debts/SKILL.md b/.agents/skills/capture-review-debts/SKILL.md index 639b207c..d2680c00 100644 --- a/.agents/skills/capture-review-debts/SKILL.md +++ b/.agents/skills/capture-review-debts/SKILL.md @@ -124,7 +124,10 @@ Mostre ao usuário **antes** de editar docs: Inclua baldes **já_resolvido**, **descartar** e **defer** com uma linha de racional/gatilho cada (transparência > silêncio). -**Pare e confirme** — exceto em **modo autônomo** (`agent-work-issue` / pool): aí aplique a seção Modo autônomo abaixo e pule o AskQuestion. Só avance ao Passo 6 com aprovação explícita (humano) ou com as regras do modo autônomo. Sem isso = não cria Issue nem toca `docs/plans/`. +**Pare e confirme** — exceto em **modo autônomo** (`work-issue` pós-plano / +`agent-work-issue` / pool): aí aplique a seção Modo autônomo abaixo e pule o +AskQuestion. Só avance ao Passo 6 com aprovação explícita (humano) ou com as +regras do modo autônomo. Sem isso = não cria Issue nem toca `docs/plans/`. ## Passo 6 — Registrar via `agent:register` / `agent:file-miss` @@ -143,14 +146,21 @@ Não implemente as fases aqui. Execução é via `work-issue` / `agent-work-issu **Próximo no fluxo de entrega:** após a triage (confirmada ou modo autônomo), o fechamento segue o Passo 6 de `agent-work-issue` / `work-issue` (`pnpm push` → PR `--base main` com `Closes #N` → auto-merge → CI). -## Modo autônomo (`agent-work-issue` / pool) +## Modo autônomo (`work-issue` pós-plano / `agent-work-issue` / pool) Quando não há humano no gate (Passo 5): 1. Faça colheita, dedup, score e tipo como de costume. -2. **Registre** só itens `expensive_lock` com score ≥4 (Issue nova + `depends` no pai se preciso; plano curto se score ≥3 eng). -3. Todo o resto → **defer** (gatilho no `*-impl.md` / Explicitamente fora) ou **descartar** — não abra AskQuestion. -4. Resuma no comentário/fechamento o que registrou vs deferiu. +2. **`work-issue`** (humano presente só no plano): decida o destino pela + triage completa (Passos 3–4) — registre o que a triage manda (score ≥3 + registrar, expensive_lock com piso 4–5), absorva em plano existente, + defira com gatilho ou descarte o resto — **sem abrir AskQuestion**. +3. **Pool (`agent-work-issue`):** registre só itens `expensive_lock` com + score ≥4 (Issue nova + `depends` no pai se preciso; plano curto se + score ≥3 eng). +4. Todo o resto → **defer** (gatilho no `*-impl.md` / Explicitamente fora) + ou **descartar** — não abra AskQuestion. +5. Resuma no comentário/fechamento o que registrou vs deferiu vs descartou. ## Anti-padrões (baseline) @@ -162,7 +172,7 @@ Quando não há humano no gate (Passo 5): | "Junto DRY e UX num FD+" | Quebra o precedente FD+/FD2; tipos separados | | "DRY com 1 call site vira escala-dry" | defer_trigger + gatilho; não registrar epic YAGNI | | "Não li o plano existente" | Grep primeiro; absorver > duplicar | -| "Registro sem perguntar" | Passo 5 é gate; docs de produto não são scratchpad | +| "Registro sem perguntar" | Passo 5 é gate (humano-gated); em modo autônomo o agente decide e registra | | "Corto access/LGPD do lote por tempo" | expensive_lock nunca é cortável por appetite | ## Resumo ao usuário diff --git a/.agents/skills/work-issue/SKILL.md b/.agents/skills/work-issue/SKILL.md index fcf4d038..6658cd3a 100644 --- a/.agents/skills/work-issue/SKILL.md +++ b/.agents/skills/work-issue/SKILL.md @@ -1,13 +1,13 @@ --- name: work-issue description: >- - Executa uma Issue já claimada de ponta a ponta com supervisão humana: - o contexto da sessão identifica a Issue (sem claim), Plan mode com plano - de implementação, pausa para confirmação humana, depois execução → - /simplify → capture-review-debts com gate → PR Ready em main com - auto-merge. Use quando o usuário pedir /work-issue, quiser supervisionar - a abordagem de engenharia, ou trabalhar uma Issue com gate humano antes - do código. + Executa uma Issue já claimada de ponta a ponta com supervisão humana no + plano: o contexto da sessão identifica a Issue (sem claim), Plan mode com + plano de implementação, pausa para confirmação humana, depois execução → + /simplify → capture-review-debts autônomo (o agente decide o que registrar + e o que descartar) → PR Ready em main com auto-merge. Use quando o usuário + pedir /work-issue, quiser supervisionar a abordagem de engenharia, ou + trabalhar uma Issue com gate humano antes do código. disable-model-invocation: true --- @@ -36,7 +36,7 @@ pular a pausa do impl plan; Draft / sem auto-merge. - [ ] 1. Contexto: Issue do prompt/`$ARGUMENTS` da sessão (ausente → UMA pergunta com validação; nunca claim) - [ ] 2. Abrir o plano de intenção do body da Issue (`Plano: docs/plans/...`; sem link → body é spec) - [ ] 3. Plan mode → escrever `docs/plans/-impl.md` → **PARAR e confirmar com o humano** -- [ ] 4. Após “ok”: Passo 4 (execução → /simplify → débitos com gate → PR, via `execution-pipeline.md`) +- [ ] 4. Após “ok”: Passo 4 (execução → /simplify → débitos autônomos → PR, via `execution-pipeline.md`) ``` ## Passo 1 — Contexto da sessão @@ -96,9 +96,11 @@ fechar em main), com os deltas do ator humano: - **Branch:** `-` do worktree — nunca crie branch nova na sessão. - **UI:** shape → craft → critique → polish. -- **`capture-review-debts`:** **com gate humano** — colha e pontue como a - skill manda, apresente ao humano antes de registrar (Issues novas com - `depends` no pai se necessário). +- **`capture-review-debts`:** **autônomo** — colha, dedupe, pontue e **decida + você mesmo o destino** (registrar/absorver/deferir/descartar) pela triage + da própria skill, **sem pausa para o humano**; registre via + `agent:register` / `agent:file-miss` (Issues novas com `depends` no pai se + necessário) e resuma o que registrou vs descartou no fechamento. ## Resumo final diff --git a/.agents/skills/work-issue/execution-pipeline.md b/.agents/skills/work-issue/execution-pipeline.md index cfbbce6e..fb745fbd 100644 --- a/.agents/skills/work-issue/execution-pipeline.md +++ b/.agents/skills/work-issue/execution-pipeline.md @@ -40,5 +40,5 @@ entrega. | Ator | Branch | UI | `capture-review-debts` | Cloud | | ---- | ------ | --- | ---------------------- | ----- | -| **Humano** (`work-issue`) | `-` (worktree; nunca crie branch nova na sessão) | shape → craft → critique → polish | **com gate humano** — colha, pontue e apresente antes de registrar | n/a (máquina do humano) | +| **Humano** (`work-issue`) | `-` (worktree; nunca crie branch nova na sessão) | shape → craft → critique → polish | **autônomo** — decide o destino dos achados (registrar/absorver/deferir/descartar) pela triage da skill; sem pausa para o humano | n/a (máquina do humano) | | **Pool** (`agent-work-issue`) | `agent/-` (worktrees Cursor podem já ter criado) | shape → craft → critique → polish (harden/optimize só sob gatilho) | **autônomo** — só `expensive_lock` com score ≥4 (Issues novas com `depends`); score ≤3 / cheap_polish / defer_trigger → defer no `*-impl.md` ou descarte | `ManagePullRequest` com `draft: false`; Prep Cloud no Passo 0 | From 70070db8dc47d4f730219b09ccb087db0819b9d2 Mon Sep 17 00:00:00 2001 From: Francisco Solla Date: Tue, 11 Aug 2026 12:30:15 -0300 Subject: [PATCH 6/6] =?UTF-8?q?feat(C114):=20agenda=20da=20campanha=20espe?= =?UTF-8?q?lhada=20no=20Google=20Calendar=20com=20notifica=C3=A7=C3=A3o=20?= =?UTF-8?q?(push=20Teqo=20=E2=86=92=20Google)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Espelho oficial da agenda em calendário Google compartilhado via API (service account, jose + fetch): reconciliação por passada com id determinístico teqo+base32hex (upsert idempotente, sem estado por evento), hooks afterChange/afterDelete na activity (só campos espelhados, nunca lança), auto-resync quando calendarId muda, retry na view da agenda, pill de estado no header + FAB + diálogo (4 estados). Cancelado no Teqo deleta o evento no Google. Sem participantes no v1 (decisão de produto). Sem migration nova de tabela (hand-written). Closes #635 --- .env.example | 8 + docs/CHANGELOG-AGENTS.md | 2 + ...oogle-calendar-push-agenda-oficial-impl.md | 135 +++++ ...22-c114-followup-testes-estados-espelho.md | 57 ++ package.json | 1 + pnpm-lock.yaml | 11 +- .../(campaign)/campanha/(app)/agenda/page.tsx | 21 +- .../campanha/actions/googleCalendarSync.ts | 96 ++++ src/collections/Activity.ts | 5 + src/collections/GoogleCalendarSync.ts | 111 ++++ .../activity/AgendaGoogleSyncChrome.tsx | 121 +++++ .../activity/GoogleCalendarSyncDialog.tsx | 291 +++++++++++ src/lib/activityDescription.ts | 21 + src/lib/activityQuickActions.ts | 21 +- src/lib/campaignQuickActionContext.ts | 2 + src/lib/googleCalendarEventMapping.ts | 189 +++++++ src/lib/googleCalendarLink.ts | 28 + ...0260811_133332_add_google_calendar_sync.ts | 43 ++ src/migrations/index.ts | 8 +- src/payload-types.ts | 42 ++ src/payload.config.ts | 2 + src/utilities/access/googleCalendarSync.ts | 46 ++ src/utilities/calendarFeed.ts | 50 +- src/utilities/googleCalendarClient.ts | 206 ++++++++ src/utilities/googleCalendarSync.ts | 494 ++++++++++++++++++ src/utilities/googleCalendarSyncHooks.ts | 67 +++ .../e2e/campaignAgendaGoogleSync.e2e.spec.ts | 33 ++ tests/int/googleCalendarSync.int.spec.ts | 334 ++++++++++++ tests/unit/codebaseConventions.unit.spec.ts | 10 + tests/unit/googleCalendarClient.unit.spec.ts | 185 +++++++ .../googleCalendarEventMapping.unit.spec.ts | 183 +++++++ tests/unit/googleCalendarLink.unit.spec.ts | 30 ++ tests/unit/googleCalendarSync.unit.spec.ts | 240 +++++++++ .../unit/googleCalendarSyncHooks.unit.spec.ts | 146 ++++++ 34 files changed, 3209 insertions(+), 30 deletions(-) create mode 100644 docs/plans/c114-google-calendar-push-agenda-oficial-impl.md create mode 100644 docs/plans/c122-c114-followup-testes-estados-espelho.md create mode 100644 src/app/(campaign)/campanha/actions/googleCalendarSync.ts create mode 100644 src/collections/GoogleCalendarSync.ts create mode 100644 src/components/campaign/activity/AgendaGoogleSyncChrome.tsx create mode 100644 src/components/campaign/activity/GoogleCalendarSyncDialog.tsx create mode 100644 src/lib/activityDescription.ts create mode 100644 src/lib/googleCalendarEventMapping.ts create mode 100644 src/lib/googleCalendarLink.ts create mode 100644 src/migrations/20260811_133332_add_google_calendar_sync.ts create mode 100644 src/utilities/access/googleCalendarSync.ts create mode 100644 src/utilities/googleCalendarClient.ts create mode 100644 src/utilities/googleCalendarSync.ts create mode 100644 src/utilities/googleCalendarSyncHooks.ts create mode 100644 tests/e2e/campaignAgendaGoogleSync.e2e.spec.ts create mode 100644 tests/int/googleCalendarSync.int.spec.ts create mode 100644 tests/unit/googleCalendarClient.unit.spec.ts create mode 100644 tests/unit/googleCalendarEventMapping.unit.spec.ts create mode 100644 tests/unit/googleCalendarLink.unit.spec.ts create mode 100644 tests/unit/googleCalendarSync.unit.spec.ts create mode 100644 tests/unit/googleCalendarSyncHooks.unit.spec.ts diff --git a/.env.example b/.env.example index f16a2ea4..2299af2a 100644 --- a/.env.example +++ b/.env.example @@ -44,3 +44,11 @@ DEEPSEEK_API_KEY=YOUR_DEEPSEEK_API_KEY_HERE # Deep Infra API key for the Sollinha voice transcription (STT, Whisper large-v3) # — cheapest speech-to-text around ($0.00045/audio min). Get one at https://deepinfra.com DEEPINFRA_API_KEY=YOUR_DEEPINFRA_API_KEY_HERE + +# C114 — Google Calendar mirror (Agenda da Campanha): base64 of the Google Cloud +# service-account JSON key (cat key.json | base64 -w0). The service account must +# be granted "make changes to events" on the campaign's public calendar; the +# calendar ID itself is configured by an admin in the `googleCalendarSync` +# collection. Absent/malformed → the agenda shows "não configurado" and the +# mirror stays off — the Teqo agenda never depends on this. +GOOGLE_CALENDAR_SERVICE_ACCOUNT_KEY= diff --git a/docs/CHANGELOG-AGENTS.md b/docs/CHANGELOG-AGENTS.md index 200b3337..3966f556 100644 --- a/docs/CHANGELOG-AGENTS.md +++ b/docs/CHANGELOG-AGENTS.md @@ -4,6 +4,8 @@ Conteúdo movido do `AGENTS.md` em 2026-07-30 (fatiamento do paradigma de agente --- +**Recently resolved (2026-08-11):** **C114** (Issue #635) — Agenda da Campanha espelhada no Google Calendar com push Teqo→Google (API oficial, service account). **Motor:** reconciliação completa por passada (`runCampaignCalendarSync`) com **id determinístico de evento** (`teqo` + base32hex do id — charset do Google) → upsert idempotente sem estado por evento; janela 90d passado/365d futuro (precedente do feed iCal), espelho cheio do escopo do staff (sem intersecção de advisor — agenda oficial), cancelado/deletado no Teqo **apaga o evento no Google** (sem estado cancelado visível a quem segue); conteúdo comparado por instante absoluto (imune a fuso do calendário) → passadas repetidas não tocam o Google; eventos estrangeiros do calendário nunca são tocados (decode completo — um `teqo3` manual não é nosso). **Triggers (sem cron — Hobby):** hook `afterChange`/`afterDelete` na `activity` (só campos espelhados, com normalização de shapes populados vs depth-0; tarefas não disparam; **nunca lança** — Teqo independe do Google; timeout 15s por request), auto-retry na view da agenda quando `paused` (uma vez por carregamento), botão "Sincronizar agora", e **auto-resync por config** (D7): trocar `calendarId`/reativar na collection `googleCalendarSync` re-conciliar sozinho (guard por comparação de campo quebra o loop de re-entrada); scan de delete com lookback de 730d (cancelado antigo não vira fantasma; atividade viva fora da janela mantém o evento). **Estado:** collection `googleCalendarSync` (uma linha; `calendarId` admin-only, `disabledAt` staff, timestamps system-write; **status derivado** `not-configured | disabled | synced | paused` em lib pura) + **chave da service account só em env** (`GOOGLE_CALENDAR_SERVICE_ACCOUNT_KEY`, base64 do JSON do GCP — sem credencial no DB); sem env → `not-configured` com runbook de ops (criar calendário na conta da campanha, público no link, SA editor, calendarId no Painel). **Transporte:** `jose` (novo dep) + fetch → Calendar API v3 REST (token JWT assertion, cache em módulo, 401 re-minta uma vez, 409 de insert idempotente; `end` obrigatório → atividade sem `endAt` vira 1h). **UI (Impeccable B, padrão C94):** pill no header da agenda (sincronizado/pausado/não configurado/desativado) + FAB mobile + diálogo com os 4 estados do canvas (link público de adição `calendar/r?cid=webcal…` + instruções + sync agora + desativar; falha de action visível). **Sem participantes no v1** (decisão de produto do gate: aviso segue as configs de quem segue; service account exigiria domain-wide delegation — segue para o C115/Google→Teqo). DRY com o feed: janela/where/nomes de município/description compartilhados (`calendarFeed.ts` como dono da janela). Migration hand-written (o gerador re-emitia no-ops pré-existentes de `campaign_user.contact_id`). Testes: unit (mapping/derivação/guards com normalização/client com fetch fake/hook wiring via `vi.mock`), int (engine com stub in-memory: cria/converge/atualiza só drift/cancela deleta/delete limpa fantasma/falha→pausado→recupera/troca de calendarId), e2e (pill não-configurado + runbook, sem dependência externa). Débito de cobertura (estados disabled/paused/synced + ação) registrado como #666 (depends #635). Intenção [`c114-google-calendar-push-agenda-oficial.md`](plans/c114-google-calendar-push-agenda-oficial.md); impl [`c114-google-calendar-push-agenda-oficial-impl.md`](plans/c114-google-calendar-push-agenda-oficial-impl.md). + **Recently resolved (2026-08-11):** **D10** (Issue #617) — auto-cura de leftovers de consent/supporter + fim do shared-write latente no lease de consent (pós-D9). **F1 (self-heal):** `purgeOrphanedConsentRenames` (3 nomes explícitos de renames temporários — `temporarily-renamed-consent`, `chave-renomeada`, `chave-temporariamente-ausente`) roda sob lease **exclusivo**, em **autocommit** (delete na transação do lease seguraria row lock e a operation do próprio fixture self-deadlockaria ao reusar a key purgada — observado na primeira implementação), nos 3 pontos que tocam a key canônica (`withMutableConsentFixture` antes do snapshot, setup do `withMissingInviteConsentFixture`, `ensureLeasedConsent`) — uma run abortada no meio de um rename de teste não envenena mais o run seguinte (UNIQUE `consent_key_idx` auto-sustentado). **F1b:** `purgeMunicipalityResidue` também deleta os **contatos** que supporters residue órfãos deixaram (reference-check em leadership/signature/subscription/supporter pós-delete — mesmo critério do `removeSupporterData`, reimplementado no helper). **F2 (refutação por medição + design final):** a hipótese "shared-writer = ABBA latente" caiu sob carga — exclusivo-durante-a-operation → fila de 17 waiters e cascata de 15s (6 workers); operation sem lease → FK `consent_id` (cleanup de fixture apagava linha canônica adotada); o ABBA não é construtível hoje (escritas disjuntas por teste; Onda0 em ordem fixa de 3 keys). As flakes reais do baseline (3/4 runs) eram: consumidores observando a ausência commitada da janela missing-consent (janela D9 era **shared**), down-test da Onda0 deletando as 4 keys **não-atomicamente**, e provision re-textando no meio de asserções de preview. **Fix:** janela do `withMissingInviteConsentFixture` → **exclusiva** (bloqueia consumidores shared durante a ausência; fila limitada ~200–500ms — a medição D9 do "15s tight" era o regime ONDA0-exclusivo, inexistente hoje); down-test da Onda0 **atômico** (delete+count+restore numa transação — FK insert espera o commit via speculative insertion); provision da Onda0 com leases **exclusivos** aninhados (ordem fixa CI→SR→VI); consumidores continuam shared. **Review /simplify (2 reviewers):** P1-1 — os renames vivos de `campaignInvite`/`campaignFixtures` migraram para chaves privadas (`consent-vacated-`/`owned-consent-vacated-`) — nenhum teste vivo produz os 3 nomes do purge; comentários "Onda0 shared" corrigidos; `withExclusiveTestDatabaseLease` colapsado (era cópia do privado); rollback no down-test quando a sessão falta; cleanup no finally nos testes F1 (auto-curáveis — um run falho do próprio teste não polui). Testes novos: contrato da janela exclusiva (waiter SHARED bloqueado), purge em ensure/setup, self-heal do canônico (repro do run abortado, 2 fixtures), F1b. **Contraprova:** 7 arquivos da key × 6 workers → **12/12 verdes** (baseline 1/4; designs intermediários: cascata 15s / FK); suíte int completa 83 arquivos/701 testes; unit 1938; e2e paridade com baseline (falhas pré-existentes de chat IA/webauthn/mic reproduzem no código original). Sem migration/access/Consent/`src/`. Intenção [`escala-dry-pos-d9.md`](plans/escala-dry-pos-d9.md); impl [`escala-dry-pos-d9-impl.md`](plans/escala-dry-pos-d9-impl.md). **Recently resolved (2026-08-11):** **C113** (Issue #631) — feed iCal da agenda **vivo**: eventos criados depois da criação do link aparecem no mesmo link. **Spike diagnóstico primeiro** (e2e novo com o aceite de dois `GET`s via fixture — roda em dev E no build de produção): o teste **passou no modo prod ANTES do fix**, provando que o Next 15 **não** congela route handlers GET (docs: "not cached by default" desde 15.0; build compila a rota como `route.js` dinâmica, sem `.body`) — **o freeze é o contrato de resposta**: `Cache-Control: public, max-age=3600` sem validadores permitia a caches compartilhados (CDN da Vercel, infra do Google, proxies) reter o snapshot do primeiro fetch. **Fix (mínimo):** `export const dynamic = 'force-dynamic'` na rota `ical/[secret]` (explícito, convenção do repo) + novo `buildICalFeedResponse` em `src/utilities/calendarFeed.ts` (helper puro): `Cache-Control: public, no-cache` (armazenar só com revalidação obrigatória — fecha também a janela de 1h servindo feed revogado), `ETag` forte (sha256 do corpo, hash sempre muda com conteúdo), `Last-Modified` informacional, `X-PUBLISHED-TTL: PT1H` (RFC 7986) e **304 só por `If-None-Match`** — nunca por `If-Modified-Since`, porque exclusão de atividade não move `Last-Modified` (linha some) e um 304 por data esconderia "evento fantasma". **Testes:** unit do helper (12/12 — headers, ETag estável/mutável, 304 sem corpo, `*`/lista/weak, ETag velho → 200, IMS nunca 304); int na rota real com payload (`route.dynamic === 'force-dynamic'` + A → GET → B → GET ambos → edição de horário → DTSTART novo → cancelamento → some → GET condicional → 304, 18/18); e2e do aceite completo no spec existente `campaignAgendaFeed` (dev 5/5, prod build 1/1). Aceite: edição/cancelamento refletem, revogação/escopo do criador fail-closed intactos, sem migration/schema/access/UI/Consent. Verificação pós-deploy pendente no elo CDN externo (link real de produção). Intenção [`c113-feed-ical-agenda-nao-atualiza.md`](plans/c113-feed-ical-agenda-nao-atualiza.md); impl [`c113-feed-ical-agenda-nao-atualiza-impl.md`](plans/c113-feed-ical-agenda-nao-atualiza-impl.md). diff --git a/docs/plans/c114-google-calendar-push-agenda-oficial-impl.md b/docs/plans/c114-google-calendar-push-agenda-oficial-impl.md new file mode 100644 index 00000000..a2cc7db7 --- /dev/null +++ b/docs/plans/c114-google-calendar-push-agenda-oficial-impl.md @@ -0,0 +1,135 @@ +# Impl: C114 — Google Calendar: agenda da campanha espelhada com notificação (push Teqo → Google) + +Status: aprovado +Atualizado em: 2026-08-11 +Issue: #635 +Intenção: docs/plans/c114-google-calendar-push-agenda-oficial.md +Appetite restante: ~2 dias eng (herdado; corte explícito: sem participantes no v1 — ver decisão aberta P1) + +## Leitura da intenção + +- **Outcome:** com um comando em `/campanha/agenda`, a campanha ativa um calendário Google compartilhado ("Agenda da Campanha"); criar/remarcar/cancelar atividades em Teqo reflete no Google em minutos (push via API oficial); quem segue recebe aviso conforme as próprias configurações; Teqo segue SoT e funciona sem Google (estado do sync visível, re-tenta sem ação manual); leader lockdown intocado. +- **O que NÃO negociar:** direção única Teqo → Google (C115 é o bidirecional); modelo = calendário compartilhado (nunca OAuth por pessoa); Teqo SoT — credencial inválida/API fora não pode quebrar o Teqo; espelho cheio do escopo do staff (recortes continuam no iCal); sem PII desnecessária no evento; líder não vê nem ativa. +- **O que reavaliar:** a "hipótese" aponta `src/utilities/calendarFeed.ts` como precedente — confirmado (janela de eventos, formatação de summary/description, nomes de município reusáveis). O que a intenção NÃO diz e a engenharia precisa decidir: **como disparar o push** (não há cron no Vercel — verificação: `vercel.json` não tem `crons`; Hobby limita cron), **onde viver o estado do sync** e **como mapear atividade→evento sem estado duplicado**. + +## Abordagem recomendada + +```mermaid +flowchart LR + A[afterChange/afterDelete hook em activity
campos relevantes mudaram?] -->|sim| S[runCampaignCalendarSync
utilities/googleCalendarSync.ts] + P[agenda page view
estado pausado → auto-retry] --> S + B["botão 'Sincronizar agora'"
actions/googleCalendarSync.ts] --> S + S --> L[load activities janela 90d+365d
espelho cheio do staff] + L --> G[Google Calendar API v3
client com service account JWT
jose + fetch] + G --> D[diff por id determinístico teqo_activity_<id>
cria/atualiza/remove] + D --> W[grava collection googleCalendarSync
lastSyncedAt/lastSuccessAt/lastError*] + W --> U[UI: pill sincronizado/pausado/não configurado] +``` + +**Opções consideradas:** ver decisões D1–D6 abaixo. +**Recomendação:** push **orientado a evento** (hook no `activity`) com **reconciliação completa por passada** e **id determinístico de evento** — sem cron, sem fila, sem collection join e sem campo novo em `activity`. Configuração e estado do sync numa **collection `googleCalendarSync`** (credencial em env; **auto-resync quando a configuração muda**). UI no padrão do link de import (C94): pill no header desktop + quick action mobile + dialog. + +### Decisões de engenharia + +**D1 — Transporte para a API do Google.** +Opções: A) `googleapis` + `google-auth-library` (SDK oficial) | B) `jose` (assina JWT RS256) + `fetch` nativo → Calendar API v3 REST | C) JWT com `node:crypto` na mão. +Recomendação: **B** — só precisamos de token (JWT service account) + 4 endpoints REST (`events.list/insert/update/delete`); SDK oficial é ~40MB de máquina para 1 chamada; C reimplementa crypto de JWT (risco de segurança). `jose` é dependência pequena e padrão (Payload usa a mesma família). +Rejeitadas: A (pesado demais para a superfície real); C (implementar JWT à mão viola "nunca implemente crypto"). + +**D2 — Mapeamento atividade → evento Google.** +Opções: A) collection join `activityCalendarEvent` (activityId→googleEventId) | B) campos `googleEventId`/hash no `activity` | C) **id determinístico** `teqo_activity_` (charset do Google permite alfanumérico + `_`). +Recomendação: **C** — upsert idempotente sem estado persistido por evento: `events.get`/`list` → 404 cria, 200 compara conteúdo e atualiza só se mudou, cancelado/deletado remove. Elimina collection nova, migration de tabela e o loop de re-entrada do hook (escrever campo no doc re-dispara afterChange). "Edit the owner": o dono do evento no Google É a atividade; o id determinístico é só a identidade externa. +Rejeitadas: A (estado duplicado desnecessário; mais uma collection/access para administrar); B (re-entrada do hook — toda escrita de `googleEventId` re-dispara afterChange; precisa guard frágil; e leader update guard já compara snapshots de campos de staff). + +**D3 — Motor de sync.** +Opções: A) upsert incremental por evento (só o evento tocado) no hook | B) **reconciliação completa por passada**: lista janela no Google + diff contra atividades do Teqo + cria/atualiza/remove | C) dois caminhos (incremental no hook + reconciliação periódica). +Recomendação: **B** — um único caminho de código (uma superfície de teste), auto-curativo (remove fantasmas de eventos deletados no Teqo, reconstrói evento apagado à mão no Google — Teqo é SoT), e a comparação de conteúdo (summary/description/start/end) faz passadas repetidas convergirem sem tocar no Google quando nada mudou. Custo: 1 chamada `list` por passada (~100ms) — irrelevante na escala (centenas de eventos). +Rejeitadas: A (não limpa fantasmas; edição manual no Google não é corrigida; dois estados de falha distintos); C (dois caminhos para manter/testar sem ganho real). + +**D4 — Trigger do push ("em minutos" sem cron).** +Opções: A) cron Vercel (`vercel.json` crons) | B) **hook afterChange/afterDelete em `activity` (campos relevantes) + auto-retry na view da agenda quando pausado + botão "Sincronizar agora"** | C) Google push channels (webhook reverso). +Recomendação: **B** — o hook dá latência de segundos (o aceite pede "minutos"); a view da agenda + o botão cobrem o "re-tenta sem ação manual" (a própria equipe abre a agenda quando opera); escrever em Teqo continua 100% local. Sem cron (Hobby limita a 2/dia), sem fila, sem endpoint público novo. +Rejeitadas: A (limite de Hobby; cadência de horas não entrega o aceite); C (endpoint público + desafios de renovação de channel — infra que este item não precisa). +Nota: corridas convergem — a comparação de conteúdo torna uma segunda passada simultânea um no-op; sem advisory lock distribuído para estado cosmético (o `pg_advisory_xact_lock` existente é transacional e soltaria no commit, inútil para chamadas HTTP pós-commit). + +**D5 — Configuração e estado do sync (collection, não global).** +Opções: A) **collection `googleCalendarSync`** (uma linha de fato; precedente de infra `supporterImportBatch`/`calendarFeed`) | B) global `googleCalendarSync` (precedente `campaignGoals`) | C) tudo em env. +Recomendação: **A** — pedido do gate e casa com o precedente de recurso de sync com credencial revogável (`calendarFeed`); o `afterChange` da própria collection dispara o **auto-resync** quando a configuração muda (D7); access por collection explícito (read staff, update staff campo a campo: `calendarId` admin-only, `disabledAt` staff, estado system-write/staff-read). Singleton de fato garantido pelo fluxo (engine faz find/create na primeira ativação) — sem unicidade DB (a linha é única por construção da ação de ativação). +Rejeitadas: B (auto-resync também funcionaria num global, mas o precedente de recurso de sync é collection e o access fica mais legível; `campaignGoals` é configuração pura sem estado rastreado); C (calendarId em env exigiria redeploy para rotacionar; sem rastreabilidade de `lastError` na UI). + +**D6 — Credencial.** +Opções: A) **chave privada da service account em env** (`GOOGLE_CALENDAR_SERVICE_ACCOUNT_KEY`, base64 do JSON do GCP) + `calendarId` na collection (admin-set) | B) JSON inteiro da service account na collection (campo admin) | C) OAuth por pessoa. +Recomendação: **A** — segredo em env (precedente `REVALIDATE_SECRET`/Blob), identidade operacional (calendarId) na collection: staff desativa/reativa sem tocar em env; admin ajusta calendarId sem redeploy; key nunca vaza em log/DB. O runbook de ops (criar calendário na conta da campanha, compartilhar "público no link", dar permissão de escrita à service account, pôr calendarId) é o caminho da questão aberta de produto Q1 (dono da conta). +Rejeitadas: B (segredo em DB — precedente do repo é env; revogação via env é mais segura); C (anti-goal explícito da intenção). + +**D7 — Auto-resync ao mudar a configuração.** +Opções: A) **afterChange na collection `googleCalendarSync`**: quando `calendarId` (ou `disabledAt`) mudou de fato (comparação `originalDoc` vs `doc`), dispara `runCampaignCalendarSync` — reconciliação completa contra o novo calendário | B) só manual ("Sincronizar agora" / view da agenda) | C) migração completa entre calendários (deletar no antigo + criar no novo). +Recomendação: **A** — o id determinístico + diff por conteúdo tornam a passada idempotente: trocar o `calendarId` recria o espelho inteiro no novo calendário na hora, sem ação manual. Guard de re-entrada: as escritas de estado (`lastSyncedAt`/`lastSuccessAt`/`lastError*`) não alteram `calendarId`/`disabledAt`, então não re-disparam o afterChange. O calendário antigo fica com o último espelho congelado (sem limpeza retroativa — documentado no runbook). +Rejeitadas: B (o aceite "re-tenta sem ação manual" se estende à reconfiguração); C (varrer e deletar N eventos de um calendário que a campanha abandonou é valor marginal — o calendarId troca só em migração de conta). + +### Componentes / mudanças + +- **`src/lib/googleCalendarEventMapping.ts`** (novo, puro, unit-testável): `googleEventIdForActivity` (`teqo_activity_`), `buildGoogleEventPayload(activity, municipalityName)` (summary `[Município] Título`; description sem PII — Município/Local/Tags/Deputado presente, mesmo espírito do iCal; `start`/`end` timed com offset America/Bahia −03:00 ou `date` all-day via `allDayCivilDateOf`/`allDayExclusiveEndDate` de `src/lib/activityAllDay.ts`; **end ausente → start + 1h** — pendente de verificação no doc oficial da API), `googleEventContentEquals(remote, payload)`. +- **`src/utilities/googleCalendarClient.ts`** (novo, `server-only`): JWT service account com `jose` (`SignJWT` RS256, scope `https://www.googleapis.com/auth/calendar`, aud `https://oauth2.googleapis.com/token`), POST token (cache em módulo, invalida em 401 e re-tenta uma vez), `listEvents` (paginação), `insertEvent`, `updateEvent`, `deleteEvent`; erros tipados (`GoogleCalendarApiError`) sem credencial em log. +- **`src/utilities/googleCalendarSync.ts`** (novo, `server-only`): `readGoogleSyncConfig`, `deriveGoogleSyncStatus` (matriz pura `not-configured | disabled | synced | paused` — key presente? calendarId? disabledAt? lastErrorAt > lastSuccessAt?), `runCampaignCalendarSync(payload, client)` — reconciliação completa (carrega atividades da janela 90d passado/365d futuro com `overrideAccess` — espelho cheio do staff, sem intersecção de escopo de advisor: é a agenda oficial da campanha; reusa o padrão de `loadFeedActivities`/`loadMunicipalityNames` de `calendarFeed.ts`), grava `lastSyncedAt` + `lastSuccessAt` ou `lastErrorAt`/`lastError`. +- **`src/collections/GoogleCalendarSync.ts`** (novo): slug `googleCalendarSync`, group `Campanha` (precedente de infra `supporterImportBatch`); campos `calendarId` (admin-write), `disabledAt` (staff-settable via action), `lastSyncedAt`/`lastSuccessAt`/`lastErrorAt`/`lastError` (system-write, staff-read para a pill); access: read staff, update staff campo a campo (field access); **afterChange com auto-resync (D7)**: `calendarId`/`disabledAt` mudaram de fato → dispara `runCampaignCalendarSync`; escritas de estado não re-disparam (guard por comparação de campo). +- **`src/collections/Activity.ts`** (editar): afterChange ganha `syncActivityToGoogleCalendar` (dynamic import — mesmo padrão do `notifyActivityNeedsAttention`; guard: só dispara quando campos relevantes mudaram — `title, status, startAt, endAt, allDay, locality, municipality, tags, deputyPresent, description` — compara `originalDoc` vs `doc`, tarefas não disparam; try/catch **nunca lança** — Teqo nunca depende do Google) e afterDelete idem (reconciliação remove o fantasma). +- **Server actions `src/app/(campaign)/campanha/actions/googleCalendarSync.ts`** (novo): `getGoogleCalendarSyncState` (status + link público quando configurado), `runGoogleCalendarSyncNow` (retry manual/automático, retorna estado fresco), `setGoogleCalendarSyncDisabled` (toggle staff); padrão `getCampaignActionContext` + `reloadStaffActor` de `actions/calendarFeed.ts`; líder nunca chega (layout `(app)` + gate staff da página). +- **`src/lib/googleCalendarLink.ts`** (novo, puro): link público de adição `https://calendar.google.com/calendar/r?cid=` + URL iCal pública `webcal://calendar.google.com/calendar/ical//public/basic.ics` (Apple/Outlook) — formato a verificar no doc oficial no impl. +- **UI** (Impeccable B — shape→craft→critique→polish; reusa shell `Dialog`/`Drawer`/`useIsMobile`/`SetCampaignHeaderAction`/`useCampaignQuickActionContext` de C94): `AgendaGoogleSyncChrome.tsx` (pill no header desktop junto do link de import + quick action mobile; recebe estado inicial do servidor; auto-retry quando `paused` e atualiza via action + `revalidatePath`) e `GoogleCalendarSyncDialog.tsx` (estados do canvas: não configurado → instruções de ops; sincronizado → link + copiar + instruções + "Sincronizar agora" + "Desativar"; pausado → último erro + re-tentar; desativado → reativar). Copy pt-BR conforme canvas. +- **`src/app/(campaign)/campanha/(app)/agenda/page.tsx`** (editar): carrega estado do sync (staff) e monta `` ao lado do `AgendaFeedChrome` (ordem do cluster C95: [Semana][Link de import][Google][Notificações][IA] — verificar colisão de `id` dos `SetCampaignHeaderAction`). +- **`.env.example`** (editar): `GOOGLE_CALENDAR_SERVICE_ACCOUNT_KEY` documentada. +- **Migration:** `migrate:create add_google_calendar_sync` (tabela da collection nova — D5; sem collection join — D2). +- **Access / Consent:** sem chave de Consent (dados internos da campanha, precedente `calendarFeed`); sem PII nova — emails de campanha não vão para o Google no v1 (corte de participantes). Fail-closed: sem env key o sync não dispara (estado `not-configured`), o hook é no-op. +- **Docs:** entrada única em `docs/CHANGELOG-AGENTS.md`; runbook de ops (criação do calendário na conta da campanha + compartilhamento + permissão da service account + envs) — na AGENTS.md ou no plano? Decisão barata: na entrada do CHANGELOG + nota no impl. + +### Dados → forma (se aplicável) + +N/A — não há apresentação de dados no Teqo (o evento é do Google Calendar; decisão de apresentação do Google). Único dado novo visível: a pill de estado do sync (derivada, matriz unit-testada). + +## Fases verificáveis + +1. **Tracer / schema+server** (dia 1): `pnpm add jose`; collection `googleCalendarSync` + migration + `generate:types` + `generate:importmap`; `googleCalendarEventMapping` + unit tests; `googleCalendarClient`; `googleCalendarSync` + int tests com stub in-memory do cliente (incl. **auto-resync ao trocar `calendarId`** e guard de re-entrada do afterChange); hook no `activity` + guard; actions; `migrate` local + `migrate:status`. +2. **UI** (dia 1–2): chrome + dialog + wiring na página; e2e do estado `not-configured` (sem env no teste → determinístico, sem dependência externa) + diálogo; Impeccable shape→craft. +3. **Gates** (dia 2): `pnpm gate:fast`; `pnpm format:check`; `pnpm exec knip`; `pnpm check:cycles`; `pnpm test` (unit+int); `pnpm test:e2e`; `pnpm build`; Aikido nos arquivos novos; `pnpm push` com CI green. + +## Rabbit holes / Não escopo (engenharia) + +- **Watch channels do Google (push reverso para nós)** — exige webhook público e renovação; o hook do Teqo já dá latência de segundos. +- **Fila/worker/cron próprio** — Hobby; hook + lazy retry cobre o aceite. +- **Um calendário por filtro/recorte** — iCal cobre (intenção). +- **Participantes/attendees (aviso garantido ao núcleo)** — corte v1: quem segue recebe conforme as configs do próprio Google (é o que o pedido assume); reabre como follow-up se a mesa quiser (decisão aberta P1). +- **Sincronizar histórico completo** — janela 90d+365d (precedente do feed). +- **Migrar eventos antigos para o Google** — fora de escopo. +- **OAuth por pessoa** — anti-goal da intenção. +- **Lock distribuído para a passada** — corridas convergem por comparação de conteúdo; D3. + +## Riscos e mitigação + +- **Semântica da API Google desconhecida** (charset do id de evento, `end` obrigatório para timed, formato do link `cid`): verificar no doc oficial durante o impl (source-driven-development); os int tests com stub travam NOSSO contrato de mapeamento, não o do Google. +- **E2E com dependência externa (Google real)** — flakiness em CI: e2e só cobre `not-configured` (env ausente); motor testado em int com stub. +- **Latência no hook** — guard de campos relevantes (tarefa não sincroniza), try/catch total, escrita nunca bloqueada. +- **Segredo vazando em log** — key só em env; client tipa erros sem corpo da credencial; Aikido nos arquivos novos. +- **Env ausente em produção (C98 lesson)** — a pill mostra `não configurado` com runbook em vez de quebrar; `NEXT_PUBLIC_SITE_URL` já é conhecida; sem env nova obrigatória para o app funcionar (sync desligado é estado válido). +- **Re-escrita de edição manual feita no Google** — direção intencional (Teqo SoT); documentar no runbook. + +## Aceite de engenharia + +- [ ] Aceite de produto da intenção coberto: push em minutos via hook; estado visível; re-tenta sem ação manual (view + botão + próximas escritas); Teqo funciona sem Google; leader lockdown intocado (gate staff da página + sem superfície para líder). +- [ ] Reconfiguração auto-sincronizada: mudar `calendarId`/`disabledAt` dispara reconciliação contra o novo calendário sem ação manual (D7), com guard de re-entrada do afterChange. +- [ ] Invariantes AGENTS/engineering-standards: identificadores em inglês, copy pt-BR; server actions com `getCampaignActionContext`; sem PII nova; segredo em env; sem migration retroativa. +- [ ] Testes de domínio previstos: unit (mapping, derivação de status, link), int (engine com stub: cria/atualiza só quando muda/cancela remove/fantasma limpo/falha→pausado→recupera/auto-resync de config; guard do hook), e2e (pill não-configurado + diálogo). +- [ ] Self-score decision-quality: 5/5 (decisões caras com rejeitadas; cabe no appetite; rabbit holes nomeados; depth check reusa `calendarFeed.ts`/`activityAllDay.ts`/shells C94; intenção preservada). + +## Decisões em aberto (produto — para o gate humano) + +- **P1 — Participantes (núcleo) no v1?** Recomendação: **não** (quem segue recebe conforme as próprias configs; é o que o pedido assume; evita e-mail PII da campanha no Google e spam de convite a cada remarcação). Reabre como follow-up. +- **Q1 — Dono da conta do calendário:** assumido A (conta Google da campanha; service account só escreve). Requer do humano: confirmação de que existe a conta e o `calendarId` (ops). +- **Q2 — Visibilidade:** assumido A (público no link — a equipe é mista Google+Apple; conteúdo já é a agenda oficial sem PII). + +## Referências + +- Intenção: `docs/plans/c114-google-calendar-push-agenda-oficial.md` (gate de produto) +- Canvas: `plan-c114-ui-draft.canvas.tsx` (4 estados da jornada + pill) +- Precedentes: `src/utilities/calendarFeed.ts`, `src/collections/CalendarFeed.ts`, `src/app/(campaign)/campanha/actions/calendarFeed.ts`, `src/components/campaign/activity/CalendarFeedDialog.tsx` + `AgendaFeedChrome.tsx` (C94), `src/collections/Activity.ts` (hooks), `src/collections/SupporterImportBatch.ts` (infra admin-hidden), `src/lib/activityAllDay.ts`, `src/utilities/postgresTransactionLocks.ts` (lido; decidido não usar) +- Testes existentes: `tests/e2e/campaignAgendaFeed.e2e.spec.ts`, `tests/unit/calendarFeed.unit.spec.ts`, `tests/int/calendarFeed.int.spec.ts` diff --git a/docs/plans/c122-c114-followup-testes-estados-espelho.md b/docs/plans/c122-c114-followup-testes-estados-espelho.md new file mode 100644 index 00000000..f459632a --- /dev/null +++ b/docs/plans/c122-c114-followup-testes-estados-espelho.md @@ -0,0 +1,57 @@ +# C122 — C114 follow-up: cobertura de testes dos estados do espelho Google + +Status: rascunho +Atualizado em: 2026-08-11 +Issue: # +Priority: P3 +Model: composer-2.5 +Impeccable: B — superfícies existentes (pill + diálogo da agenda); sem fluxo novo +Appetite: ~0,5 dia eng — fill-in de cobertura, sem mudança de comportamento + +## Intenção + +O C114 entregou o espelho Teqo→Google com três estados derivados na UI +(sincronizado / pausado / não configurado / desativado), mas a cobertura de +testes parou no estado `not-configured` (e2e) e no engine (int com stub). O +que o /simplify deixou de fora, em um lote: + +- **S14 — teste de componente dos chromes:** o auto-retry do + `AgendaGoogleSyncChrome` (uma tentativa por mount quando `paused`) e o + rótulo da pill por status não têm nenhum teste; o repo tem precedente + jsdom+RTL (`wizardExpectedVotesStep.unit.spec.tsx`). +- **S17 — e2e dos outros estados:** `disabled` (Reativar), `paused` (erro + + link "Abrir Google Calendar") e `synced` (link block + copiar + instruções) + nunca exercitados; o FAB mobile ("Agenda da Campanha") também não tem pin. +- **S18 — int da action de desativar:** `setGoogleCalendarSyncDisabled` + (grava `disabledAt`, re-enable limpa e o hook da config re-sincroniza — D7) + sem teste. + +## Aceite + +- Unit (RTL): pill renderiza o rótulo/estado correto por status; auto-retry + dispara UMA vez quando `paused` e nenhuma quando `synced`/`not-configured`. +- E2E: dialog nos estados disabled/paused/synced (seed do doc + `googleCalendarSync` + env key fake — estados determinísticos, sem rede) e + FAB mobile abrindo o sheet. +- Int: disable → `disabledAt` gravado; re-enable → limpo e o + `googleCalendarSyncConfigHook` dispara a reconciliação (spy/observável via + stub) — sem tocar na rede. + +## Direção no codebase + +- `src/components/campaign/activity/AgendaGoogleSyncChrome.tsx` + + `GoogleCalendarSyncDialog.tsx` (alvos do teste de componente). +- `src/app/(campaign)/campanha/actions/googleCalendarSync.ts` (alvo do int). +- `tests/unit/`, `tests/int/`, `tests/e2e/` — precedentes: + `wizardExpectedVotesStep.unit.spec.tsx`, `googleCalendarSync.int.spec.ts`, + `campaignAgendaGoogleSync.e2e.spec.ts`. + +## Fora de escopo + +- Serialização de passes concorrentes (advisory lock no estado) — defer: + self-healing (o próximo pass recobre), gatilho: reclamação de pill preso em + `paused` ou o C115. +- Teste dos estados no modo mobile do pill na linha de filtros (decisão C114: + FAB é a superfície mobile). +- Refactor do chrome de instruções addbyurl (2 call sites) — defer, gatilho: + 3ª superfície de calendário (C115). diff --git a/package.json b/package.json index 660d0c83..0b02849c 100644 --- a/package.json +++ b/package.json @@ -102,6 +102,7 @@ "cross-env": "^7.0.3", "dotenv": "16.4.7", "graphql": "^16.8.1", + "jose": "^6.2.8", "leaflet": "^1.9.4", "lucide-react": "^0.570.0", "next": "15.4.11", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 74e4078a..0314f2ac 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -80,6 +80,9 @@ importers: graphql: specifier: ^16.8.1 version: 16.12.0 + jose: + specifier: ^6.2.8 + version: 6.2.8 leaflet: specifier: ^1.9.4 version: 1.9.4 @@ -5509,8 +5512,8 @@ packages: jose@5.10.0: resolution: {integrity: sha512-s+3Al/p9g32Iq+oqXxkW//7jk2Vig6FF1CFqzVXoTUXt2qz89YWbL+OwS17NFYEvxC35n0FKeGO2LGYSxeM2Gg==} - jose@6.2.1: - resolution: {integrity: sha512-jUaKr1yrbfaImV7R2TN/b3IcZzsw38/chqMpo2XJ7i2F8AfM/lA4G1goC3JVEwg0H7UldTmSt3P68nt31W7/mw==} + jose@6.2.8: + resolution: {integrity: sha512-Bsdjwm3Qsd/P0jR+BHDe3LytDfY7WBq2HmCCLIwuVRHMuEC9ae7/R474GIUdF1NgCyZjzVo/A9DOiOBtXq8ZoQ==} joycon@3.1.1: resolution: {integrity: sha512-34wB/Y7MW7bzjKRjUKTa46I2Z7eV62Rkhva+KkopW7Qvv/OSWBqvkSY7vusOPrNuZcUG3tApvdVgNB8POj3SPw==} @@ -9094,7 +9097,7 @@ snapshots: express: 5.2.1 express-rate-limit: 8.3.1(express@5.2.1) hono: 4.12.7 - jose: 6.2.1 + jose: 6.2.8 json-schema-typed: 8.0.2 pkce-challenge: 5.0.1 raw-body: 3.0.2 @@ -13133,7 +13136,7 @@ snapshots: jose@5.10.0: {} - jose@6.2.1: {} + jose@6.2.8: {} joycon@3.1.1: {} diff --git a/src/app/(campaign)/campanha/(app)/agenda/page.tsx b/src/app/(campaign)/campanha/(app)/agenda/page.tsx index 17a79263..20a3f1f4 100644 --- a/src/app/(campaign)/campanha/(app)/agenda/page.tsx +++ b/src/app/(campaign)/campanha/(app)/agenda/page.tsx @@ -7,9 +7,15 @@ import { listCalendarFeeds, revokeCalendarFeed, } from '@/app/(campaign)/campanha/actions/calendarFeed' +import { + getGoogleCalendarSyncState, + runGoogleCalendarSyncNow, + setGoogleCalendarSyncDisabled, +} from '@/app/(campaign)/campanha/actions/googleCalendarSync' import { ActivityAgenda } from '@/components/campaign/activity/ActivityAgenda' import { ActivityAgendaFilters } from '@/components/campaign/activity/ActivityAgendaFilters' import { AgendaFeedChrome } from '@/components/campaign/activity/AgendaFeedChrome' +import { AgendaGoogleSyncChrome } from '@/components/campaign/activity/AgendaGoogleSyncChrome' import { AgendaViewChrome } from '@/components/campaign/activity/AgendaViewChrome' import { CampaignListPendingBoundary, @@ -41,10 +47,11 @@ export default async function AgendaPage({ searchParams }: AgendaPageProps) { requireCampaignPageActor({ gate: 'staff' }), getPayload({ config }), ]) - const [municipalityOptions, knownTags, feeds] = await Promise.all([ + const [municipalityOptions, knownTags, feeds, googleSyncState] = await Promise.all([ loadMunicipalityOptions(payload, user), loadAccessibleActivityTags(payload, user), listCalendarFeeds().catch(() => []), + getGoogleCalendarSyncState(), ]) const state = restrictActivityAgendaState( resolvedUrl.state, @@ -79,6 +86,18 @@ export default async function AgendaPage({ searchParams }: AgendaPageProps) { return revokeCalendarFeed(feedId) }} /> + {/* C114: pill Google no header (após o link de import) + FAB mobile. */} + { + 'use server' + return runGoogleCalendarSyncNow() + }} + onSetDisabled={async (disabled) => { + 'use server' + return setGoogleCalendarSyncDisabled(disabled) + }} + /> => ({ + ...view, + addLink: view.calendarId ? buildGoogleCalendarAddLink(view.calendarId) : null, +}) + +const failure = (message: string): GoogleCalendarSyncActionResult => ({ + ok: false, + message, + status: 'not-configured', + calendarId: null, + lastSyncedAt: null, + lastSuccessAt: null, + lastErrorAt: null, + lastError: null, + addLink: null, +}) + +const requireStaff = async (payload: Payload, actor: CampaignUser): Promise => + reloadStaffActor(payload, actor, GOOGLE_CALENDAR_SYNC_STAFF_ONLY_MESSAGE) + +/** Read model for the agenda pill/dialog — status derived, never stored. */ +export const getGoogleCalendarSyncState = async (): Promise => { + const { payload, actor } = await getCampaignActionContext() + + try { + await requireStaff(payload, actor) + return { ok: true, ...withLinks(await readGoogleCalendarSyncView(payload)) } + } catch (error) { + console.error('getGoogleCalendarSyncState failed', error) + return failure(GOOGLE_CALENDAR_SYNC_FAILED_MESSAGE) + } +} + +/** Manual retry / auto-retry (agenda page view when paused). */ +export const runGoogleCalendarSyncNow = async (): Promise => { + const { payload, actor } = await getCampaignActionContext() + + try { + await requireStaff(payload, actor) + await runCampaignCalendarSync(payload, { reason: 'manual' }) + return { ok: true, ...withLinks(await readGoogleCalendarSyncView(payload)) } + } catch (error) { + console.error('runGoogleCalendarSyncNow failed', error) + return failure(GOOGLE_CALENDAR_SYNC_FAILED_MESSAGE) + } +} + +/** Staff pauses/resumes the mirror — the afterChange hook reconciles on resume. */ +export const setGoogleCalendarSyncDisabled = async ( + disabled: boolean, +): Promise => { + const { payload, actor } = await getCampaignActionContext() + + try { + const currentActor = await requireStaff(payload, actor) + const doc = await loadGoogleCalendarSyncConfig(payload) + if (doc) { + await payload.update({ + collection: 'googleCalendarSync', + id: doc.id, + data: { disabledAt: disabled ? new Date().toISOString() : null }, + depth: 0, + user: currentActor, + overrideAccess: false, + }) + } + return { ok: true, ...withLinks(await readGoogleCalendarSyncView(payload)) } + } catch (error) { + console.error('setGoogleCalendarSyncDisabled failed', error) + return failure(GOOGLE_CALENDAR_SYNC_FAILED_MESSAGE) + } +} diff --git a/src/collections/Activity.ts b/src/collections/Activity.ts index 49fdc4a8..aa7474b7 100644 --- a/src/collections/Activity.ts +++ b/src/collections/Activity.ts @@ -29,6 +29,7 @@ import { eligibleCampaignStaffWhere, } from '@/utilities/campaignAccess' import { systemStampedActorField } from '@/utilities/campaignAuditFields' +import { activityGoogleCalendarSyncHook } from '@/utilities/googleCalendarSyncHooks' const isActivityMutationShortcut = (context: Record | undefined) => context?.mutationKind === 'taskToggle' || context?.mutationKind === 'appendUpdate' @@ -404,7 +405,11 @@ export const Activity: CollectionConfig = { await notifyActivityNeedsAttention(req, doc) return doc }, + // C114: pushes the official calendar mirror to Google (best-effort — + // never throws into the write path; failures land in `paused`). + activityGoogleCalendarSyncHook, ], + afterDelete: [activityGoogleCalendarSyncHook], }, fields: [ { diff --git a/src/collections/GoogleCalendarSync.ts b/src/collections/GoogleCalendarSync.ts new file mode 100644 index 00000000..b3ca252e --- /dev/null +++ b/src/collections/GoogleCalendarSync.ts @@ -0,0 +1,111 @@ +import type { CollectionConfig } from 'payload' + +import { + canCreateGoogleCalendarSync, + canDeleteGoogleCalendarSync, + canReadGoogleCalendarSync, + canSetGoogleCalendarSyncConfigField, + canSetGoogleCalendarSyncDisabled, + canSetGoogleCalendarSyncSystemField, + canUpdateGoogleCalendarSync, +} from '@/utilities/access/googleCalendarSync' +import { googleCalendarSyncConfigHook } from '@/utilities/googleCalendarSyncHooks' + +/** + * C114 — configuration and state of the campaign→Google calendar mirror + * (one row in practice, created/edited by Payload admin in ops). The service + * account private key never lives here — it is `GOOGLE_CALENDAR_SERVICE_ACCOUNT_KEY` + * in the environment. Changing `calendarId` (or re-enabling) re-runs the full + * reconciliation automatically via `googleCalendarSyncConfigHook` (D7). + */ +export const GoogleCalendarSync: CollectionConfig = { + slug: 'googleCalendarSync', + labels: { + singular: 'Sincronização Google Agenda', + plural: 'Sincronização Google Agenda', + }, + admin: { + group: 'Campanha', + useAsTitle: 'calendarId', + defaultColumns: ['calendarId', 'lastSyncedAt', 'lastSuccessAt', 'lastError'], + }, + access: { + create: canCreateGoogleCalendarSync, + read: canReadGoogleCalendarSync, + update: canUpdateGoogleCalendarSync, + delete: canDeleteGoogleCalendarSync, + }, + hooks: { + afterChange: [googleCalendarSyncConfigHook], + }, + fields: [ + { + name: 'calendarId', + type: 'text', + label: 'ID do calendário (Google)', + index: true, + access: { + create: canSetGoogleCalendarSyncConfigField, + update: canSetGoogleCalendarSyncConfigField, + }, + admin: { + description: + 'O calendarId do calendário "Agenda da Campanha" na conta Google da campanha (ex.: c_…@group.calendar.google.com). O calendário precisa estar público no link e a service account com permissão de edição.', + }, + }, + { + name: 'disabledAt', + type: 'date', + label: 'Desativado em', + index: true, + access: { + create: canSetGoogleCalendarSyncDisabled, + update: canSetGoogleCalendarSyncDisabled, + }, + admin: { + readOnly: true, + description: 'Enquanto preenchido, o Teqo não escreve no Google.', + }, + }, + { + name: 'lastSyncedAt', + type: 'date', + label: 'Última sincronização', + admin: { readOnly: true }, + access: { + create: canSetGoogleCalendarSyncSystemField, + update: canSetGoogleCalendarSyncSystemField, + }, + }, + { + name: 'lastSuccessAt', + type: 'date', + label: 'Última sincronização com sucesso', + admin: { readOnly: true }, + access: { + create: canSetGoogleCalendarSyncSystemField, + update: canSetGoogleCalendarSyncSystemField, + }, + }, + { + name: 'lastErrorAt', + type: 'date', + label: 'Último erro', + admin: { readOnly: true }, + access: { + create: canSetGoogleCalendarSyncSystemField, + update: canSetGoogleCalendarSyncSystemField, + }, + }, + { + name: 'lastError', + type: 'textarea', + label: 'Mensagem do último erro', + admin: { readOnly: true }, + access: { + create: canSetGoogleCalendarSyncSystemField, + update: canSetGoogleCalendarSyncSystemField, + }, + }, + ], +} diff --git a/src/components/campaign/activity/AgendaGoogleSyncChrome.tsx b/src/components/campaign/activity/AgendaGoogleSyncChrome.tsx new file mode 100644 index 00000000..11bafe2b --- /dev/null +++ b/src/components/campaign/activity/AgendaGoogleSyncChrome.tsx @@ -0,0 +1,121 @@ +'use client' + +import { useCallback, useEffect, useMemo, useRef, useState } from 'react' + +import type { GoogleCalendarSyncActionResult } from '@/app/(campaign)/campanha/actions/googleCalendarSync' +import { GoogleCalendarSyncDialog } from '@/components/campaign/activity/GoogleCalendarSyncDialog' +import { SetCampaignHeaderAction } from '@/components/campaign/shell/CampaignPageChromeContext' +import { useCampaignQuickActionContext } from '@/components/campaign/shell/CampaignQuickActionContext' +import type { GoogleCalendarSyncStatus } from '@/utilities/googleCalendarSync' + +type AgendaGoogleSyncChromeProps = { + initialState: GoogleCalendarSyncActionResult + onSyncNow: () => Promise + onSetDisabled: (disabled: boolean) => Promise +} + +const PILL_COPY: Record< + GoogleCalendarSyncStatus, + { label: string; dot: string; className: string } +> = { + synced: { + label: 'Google: sincronizado', + dot: 'bg-green-500', + className: 'text-green-700', + }, + paused: { + label: 'Google: pausado — re-tentando', + dot: 'bg-amber-500', + className: 'text-amber-700', + }, + 'not-configured': { + label: 'Google: não configurado', + dot: 'bg-muted-foreground', + className: 'text-muted-foreground', + }, + disabled: { + label: 'Google: desativado', + dot: 'bg-muted-foreground', + className: 'text-muted-foreground', + }, +} + +/** + * C114 — agenda page → app chrome bridge for the Google Calendar mirror + * (same pattern as AgendaFeedChrome/C94): the status pill registers in the + * desktop header cluster (after "Link de import"), the mobile FAB opens the + * same dialog, and a paused mirror auto-retries once on mount. + */ +export const AgendaGoogleSyncChrome = ({ + initialState, + onSyncNow, + onSetDisabled, +}: AgendaGoogleSyncChromeProps) => { + const { setContext } = useCampaignQuickActionContext() + const [open, setOpen] = useState(false) + const [state, setState] = useState(initialState) + const autoRetriedRef = useRef(false) + + const openSync = useCallback(() => setOpen(true), []) + + useEffect(() => { + setContext((current) => ({ ...current, openGoogleCalendarSync: openSync })) + return () => { + setContext((current) => ({ ...current, openGoogleCalendarSync: undefined })) + } + }, [setContext, openSync]) + + // "Re-tenta sem ação manual": a paused mirror tries once when the agenda + // page loads (the staff is operating — the natural retry moment). + const handleSyncNow = useCallback(async () => { + const result = await onSyncNow() + if (result.ok) setState(result) + return result + }, [onSyncNow]) + + useEffect(() => { + if (state.status === 'paused' && !autoRetriedRef.current) { + autoRetriedRef.current = true + void handleSyncNow() + } + }, [state.status, handleSyncNow]) + + const handleSetDisabled = useCallback( + async (disabled: boolean) => { + const result = await onSetDisabled(disabled) + if (result.ok) setState(result) + return result + }, + [onSetDisabled], + ) + + const pill = useMemo(() => { + const copy = PILL_COPY[state.status] + return ( + + ) + }, [state.status, openSync]) + + return ( + <> + {pill} + + + + ) +} diff --git a/src/components/campaign/activity/GoogleCalendarSyncDialog.tsx b/src/components/campaign/activity/GoogleCalendarSyncDialog.tsx new file mode 100644 index 00000000..fb5ad864 --- /dev/null +++ b/src/components/campaign/activity/GoogleCalendarSyncDialog.tsx @@ -0,0 +1,291 @@ +'use client' + +import { useState } from 'react' + +import { CopyIcon, ExternalLinkIcon, RefreshCwIcon } from 'lucide-react' + +import type { GoogleCalendarSyncActionResult } from '@/app/(campaign)/campanha/actions/googleCalendarSync' +import { Button } from '@/components/ui/button' +import { + Dialog, + DialogContent, + DialogDescription, + DialogHeader, + DialogTitle, +} from '@/components/ui/dialog' +import { + Drawer, + DrawerCloseButton, + DrawerContent, + DrawerDescription, + DrawerFooter, + DrawerHeader, + DrawerTitle, +} from '@/components/ui/Drawer' +import { Input } from '@/components/ui/input' +import { useIsMobile } from '@/hooks/use-mobile' +import { formatBahiaDateTimeLabel } from '@/lib/campaignTime' + +type GoogleCalendarSyncDialogProps = { + open: boolean + onOpenChange: (open: boolean) => void + state: GoogleCalendarSyncActionResult + onSyncNow: () => Promise + onSetDisabled: (disabled: boolean) => Promise +} + +const NOT_CONFIGURED_RUNBOOK: readonly string[] = [ + 'Crie o calendário "Agenda da Campanha" na conta Google da campanha.', + 'No Google, compartilhe o calendário como público (qualquer pessoa com o link pode ver).', + 'Dê permissão de edição ("fazer alterações em eventos") à service account do Teqo.', + 'Um administrador configura o ID do calendário no Painel e a chave da service account nas variáveis de ambiente.', +] + +const copyIcon = + +/** + * C114 — Google Calendar mirror dialog (estados da jornada do canvas: + * não configurado / sincronizado / pausado / desativado). Bottom sheet on + * mobile, dialog on desktop — same chrome as the iCal feed dialog (C94). + */ +export const GoogleCalendarSyncDialog = ({ + open, + onOpenChange, + state, + onSyncNow, + onSetDisabled, +}: GoogleCalendarSyncDialogProps) => { + const isMobile = useIsMobile() + const [isBusy, setIsBusy] = useState(false) + const [copied, setCopied] = useState(false) + const [actionError, setActionError] = useState(null) + + const resetTransientState = () => { + setCopied(false) + setActionError(null) + } + + const handleCopy = async () => { + if (!state.addLink) return + await navigator.clipboard.writeText(state.addLink) + setCopied(true) + setTimeout(() => setCopied(false), 2000) + } + + const handleSyncNow = async () => { + if (isBusy) return + setIsBusy(true) + setActionError(null) + try { + const result = await onSyncNow() + if (!result.ok) setActionError(result.message ?? 'Não foi possível sincronizar.') + } finally { + setIsBusy(false) + } + } + + const handleSetDisabled = async (disabled: boolean) => { + if (isBusy) return + setIsBusy(true) + setActionError(null) + try { + const result = await onSetDisabled(disabled) + if (!result.ok) setActionError(result.message ?? 'Não foi possível atualizar.') + } finally { + setIsBusy(false) + } + } + + const linkBlock = state.addLink ? ( +
+ +
+ + +
+ {copied && ( +

+ Link copiado! +

+ )} +

+ Quem segue o calendário recebe aviso conforme as próprias configurações do Google. +

+
+ ) : null + + const instructions = ( +
+

Como adicionar ao Google Calendar:

+
    +
  1. Abra o Google Calendar no computador
  2. +
  3. + No menu lateral, clique em {'"'} + {'+'} + {'"'} ao lado de {'"'}Outras agendas{'"'} +
  4. +
  5. + Selecione {'"'}Por URL{'"'} +
  6. +
  7. + Cole o link acima e clique em {'"'}Adicionar agenda{'"'} +
  8. +
+

+ Apple Calendar e Outlook assinam a URL iCal pública do mesmo calendário. +

+
+ ) + + const syncButton = ( + + ) + + let body: React.ReactNode + + if (state.status === 'not-configured') { + body = ( +
+
+

Ainda não configurado

+

+ O Teqo segue 100% funcional — a agenda do Teqo nunca depende do Google. Para ativar o + espelho, um administrador precisa concluir a configuração: +

+
    + {NOT_CONFIGURED_RUNBOOK.map((step) => ( +
  1. {step}
  2. + ))} +
+
+
+ ) + } else if (state.status === 'disabled') { + body = ( +
+
+ A sincronização está desativada. A agenda do Teqo continua funcionando normalmente; nada é + enviado ao Google enquanto estiver desativada. +
+ {actionError &&

{actionError}

} +
+ +
+
+ ) + } else { + const paused = state.status === 'paused' + body = ( +
+ {paused ? ( +
+

Pausado — re-tentando

+

+ A última tentativa de sincronização falhou. O Teqo continua funcionando; o espelho é + tentado de novo automaticamente ao salvar uma atividade ou ao abrir esta página. +

+ {state.lastErrorAt ? ( +

+ Última tentativa: {formatBahiaDateTimeLabel(state.lastErrorAt)} —{' '} + {state.lastError ?? 'erro desconhecido'} +

+ ) : null} +
+ ) : ( +

+ Sincronizado — as mudanças da agenda já refletiram no Google. + {state.lastSuccessAt + ? ` Última sincronização: ${formatBahiaDateTimeLabel(state.lastSuccessAt)}.` + : ''} +

+ )} + + {linkBlock} + {instructions} + + {actionError &&

{actionError}

} + +
+ {paused ? ( + + ) : null} + {syncButton} + +
+
+ ) + } + + const description = + 'Compromissos do Teqo refletem no calendário Google compartilhado em minutos. Quem segue recebe aviso conforme as próprias configurações.' + + const handleOpenChange = (next: boolean) => { + if (!next) resetTransientState() + onOpenChange(next) + } + + if (isMobile) { + return ( + + + + Agenda da Campanha no Google + {description} + +
{body}
+ + Fechar + +
+
+ ) + } + + return ( + + + + Agenda da Campanha no Google + {description} + + {body} + + + ) +} diff --git a/src/lib/activityDescription.ts b/src/lib/activityDescription.ts new file mode 100644 index 00000000..98f27b98 --- /dev/null +++ b/src/lib/activityDescription.ts @@ -0,0 +1,21 @@ +/** + * Shared description parts for campaign activities — the iCal feed and the + * C114 Google mirror render the same facts (municipality, locality, tags, + * deputy flag) with their own consumers' separators/escapes. No PII: no + * leadership names, phones or emails. + */ +import type { Activity } from '@/payload-types' + +export type ActivityDescriptionSource = Pick + +export const buildActivityDescriptionParts = ( + activity: ActivityDescriptionSource, + municipalityName?: string, +): string[] => { + const parts: string[] = [] + if (municipalityName) parts.push(`Município: ${municipalityName}`) + if (activity.locality) parts.push(`Local: ${activity.locality}`) + if (activity.tags?.length) parts.push(`Tags: ${activity.tags.join(', ')}`) + if (activity.deputyPresent) parts.push('Deputado presente') + return parts +} diff --git a/src/lib/activityQuickActions.ts b/src/lib/activityQuickActions.ts index 80cada99..957b2183 100644 --- a/src/lib/activityQuickActions.ts +++ b/src/lib/activityQuickActions.ts @@ -1,4 +1,12 @@ -import { Calendar, ClipboardList, ListChecks, MapPinned, Pencil, Plus } from 'lucide-react' +import { + Calendar, + CalendarCog, + ClipboardList, + ListChecks, + MapPinned, + Pencil, + Plus, +} from 'lucide-react' import { CAMPAIGN_WIZARD_ACTION_SLUGS, @@ -42,6 +50,17 @@ const listQuickActions = (context: CampaignQuickActionContext): readonly Campaig }, ] : []), + ...(context.openGoogleCalendarSync + ? [ + { + id: 'google-calendar-mirror', + label: 'Agenda da Campanha', + icon: CalendarCog, + description: 'Espelhar a agenda oficial no Google Calendar da campanha', + onAction: context.openGoogleCalendarSync, + }, + ] + : []), { id: 'plan-tour', label: 'Planejar giro', diff --git a/src/lib/campaignQuickActionContext.ts b/src/lib/campaignQuickActionContext.ts index 68420972..eb06ea72 100644 --- a/src/lib/campaignQuickActionContext.ts +++ b/src/lib/campaignQuickActionContext.ts @@ -16,6 +16,8 @@ export type CampaignQuickActionContext = { * crossed server→client. */ openCalendarFeed?: () => void + /** C114 — opens the Google Calendar mirror dialog (same bridge as above). */ + openGoogleCalendarSync?: () => void } export const emptyCampaignQuickActionContext = (): CampaignQuickActionContext => ({}) diff --git a/src/lib/googleCalendarEventMapping.ts b/src/lib/googleCalendarEventMapping.ts new file mode 100644 index 00000000..011b61ab --- /dev/null +++ b/src/lib/googleCalendarEventMapping.ts @@ -0,0 +1,189 @@ +/** + * C114 — pure mapping between campaign activities and Google Calendar events. + * + * The Google event id is DETERMINISTIC (`teqo` + base32hex of the activity + * id): Google ids only allow base32hex characters (lowercase a–v, digits 0–9) + * — no underscores, no uppercase — and the deterministic id makes the whole + * reconciliation stateless (upsert by id, content-compare to decide updates). + * Teqo stays the single source of truth; canceled activities are excluded by + * the engine (their Google event is deleted), never rendered in Google. + * + * This module is pure and client-safe; only the engine/client in + * `src/utilities/googleCalendarSync.ts` touches the network. + */ +import type { Activity } from '@/payload-types' + +import { allDayCivilDateOf, allDayExclusiveEndDate } from '@/lib/activityAllDay' +import { buildActivityDescriptionParts } from '@/lib/activityDescription' + +const GOOGLE_EVENT_ID_PREFIX = 'teqo' + +/** RFC 2938 base32hex alphabet (0–9, a–v) — the only charset Google event ids accept. */ +const BASE32HEX_ALPHABET = '0123456789abcdefghijklmnopqrstuv' + +/** + * Brazil standard time is fixed UTC−03:00 (no DST since 2019), so the offset + * is a constant — the same invariant `campaignTime` builds on. + */ +const BAHIA_FIXED_OFFSET_HOURS = -3 +const BAHIA_FIXED_OFFSET_SUFFIX = '-03:00' + +/** + * Google requires `end` strictly after `start` on timed events; an activity + * without `endAt` (a point-in-time commitment) becomes a 1-hour event — the + * iCal feed mirrors this as DTEND = DTSTART, which Google cannot represent. + */ +const DEFAULT_TIMED_EVENT_DURATION_MINUTES = 60 + +export const googleEventIdForActivity = (activityId: number): string => { + if (!Number.isInteger(activityId) || activityId < 0) { + throw new Error('ID de atividade inválido para evento do Google.') + } + + let remainder = activityId + let encoded = '' + do { + encoded = BASE32HEX_ALPHABET[remainder % 32] + encoded + remainder = Math.floor(remainder / 32) + } while (remainder > 0) + + return `${GOOGLE_EVENT_ID_PREFIX}${encoded}` +} + +/** Inverse of `googleEventIdForActivity` — null for foreign/invalid ids. */ +export const decodeGoogleEventActivityId = (eventId: string): number | null => { + if (!eventId.startsWith(GOOGLE_EVENT_ID_PREFIX)) return null + const encoded = eventId.slice(GOOGLE_EVENT_ID_PREFIX.length) + if (encoded.length === 0) return null + + let value = 0 + for (const char of encoded) { + const digit = BASE32HEX_ALPHABET.indexOf(char) + if (digit === -1) return null + value = value * 32 + digit + if (!Number.isSafeInteger(value)) return null + } + return value +} + +/** + * UTC ISO instant → RFC 3339 dateTime in Bahia wall time with the fixed + * −03:00 offset (what the Calendar API expects; the timeZone field stays + * unnecessary because the offset is explicit). + */ +const formatBahiaOffsetDateTime = (iso: string): string => { + const shifted = new Date(new Date(iso).getTime() + BAHIA_FIXED_OFFSET_HOURS * 3_600_000) + const parts = shifted.toISOString().slice(0, 19).split('T') + const [date, time] = parts + const [year, month, day] = date.split('-') + const [hour, minute, second] = time.split(':') + return `${year}-${month}-${day}T${hour}:${minute}:${second}${BAHIA_FIXED_OFFSET_SUFFIX}` +} + +const addMinutes = (iso: string, minutes: number): string => + new Date(new Date(iso).getTime() + minutes * 60_000).toISOString() + +export const activityMunicipalityIdOf = ( + municipality: Activity['municipality'], +): number | undefined => (typeof municipality === 'number' ? municipality : municipality?.id) + +export type GoogleCalendarEventPayload = { + id: string + summary: string + description: string + location?: string + start: { dateTime: string } | { date: string } + end: { dateTime: string } | { date: string } +} + +/** + * Event description without unnecessary PII: municipality, locality, tags and + * the deputy flag — the same parts the iCal feed renders, joined with real + * newlines for Google. No leadership names, no phones, no emails. + */ +export const buildGoogleCalendarDescription = ( + activity: Pick, + municipalityName?: string, +): string => buildActivityDescriptionParts(activity, municipalityName).join('\n') + +export const buildGoogleEventPayload = ( + activity: Pick< + Activity, + 'id' | 'title' | 'startAt' | 'endAt' | 'allDay' | 'locality' | 'tags' | 'deputyPresent' + >, + municipalityName?: string, +): GoogleCalendarEventPayload => { + // C14 guarantees startAt beyond draft; a null start has no Google shape — + // fail loudly in the mapping (the engine never feeds one: the window query + // only returns dated activities). + if (!activity.startAt) { + throw new Error('Atividade sem data de início não pode virar evento do Google.') + } + + const summary = municipalityName ? `[${municipalityName}] ${activity.title}` : activity.title + const description = buildGoogleCalendarDescription(activity, municipalityName) + + const allDay = Boolean(activity.allDay) + const start = allDay + ? { date: allDayCivilDateOf(activity.startAt) } + : { dateTime: formatBahiaOffsetDateTime(activity.startAt) } + const end = allDay + ? { date: allDayExclusiveEndDate(activity.endAt ?? activity.startAt) } + : { + dateTime: formatBahiaOffsetDateTime( + activity.endAt ?? addMinutes(activity.startAt, DEFAULT_TIMED_EVENT_DURATION_MINUTES), + ), + } + + const locality = activity.locality?.trim() + const location = locality || municipalityName || undefined + + return { + id: googleEventIdForActivity(activity.id), + summary, + description, + ...(location ? { location } : {}), + start, + end, + } +} + +/** The remote shape the Calendar API echoes on list/insert — a narrow view of it. */ +export type GoogleRemoteEvent = { + id?: string + summary?: string + description?: string + location?: string + start?: { dateTime?: string; date?: string; timeZone?: string } + end?: { dateTime?: string; date?: string; timeZone?: string } +} + +const startEndInstantEquals = ( + remote: { dateTime?: string; date?: string } | undefined, + wanted: GoogleCalendarEventPayload['start'], +): boolean => { + if (!remote) return false + if ('dateTime' in wanted) { + // Compare absolute instants: the calendar's own timezone may echo the + // dateTime with a different offset — the instant is what we control. + const remoteMs = Date.parse(remote.dateTime ?? '') + const wantedMs = Date.parse(wanted.dateTime) + return !Number.isNaN(remoteMs) && !Number.isNaN(wantedMs) && remoteMs === wantedMs + } + return remote.date === wanted.date +} + +/** + * True when the remote event already carries exactly what we would write — + * the decision rule that makes repeated reconciliation passes converge + * without touching Google (no update churn, no modified-date spam). + */ +export const googleEventContentEquals = ( + remote: GoogleRemoteEvent, + payload: GoogleCalendarEventPayload, +): boolean => + (remote.summary ?? '') === payload.summary && + (remote.description ?? '') === payload.description && + (remote.location ?? '') === (payload.location ?? '') && + startEndInstantEquals(remote.start, payload.start) && + startEndInstantEquals(remote.end, payload.end) diff --git a/src/lib/googleCalendarLink.ts b/src/lib/googleCalendarLink.ts new file mode 100644 index 00000000..4be4d901 --- /dev/null +++ b/src/lib/googleCalendarLink.ts @@ -0,0 +1,28 @@ +/** + * C114 — public "add this calendar" links for the campaign's shared Google + * calendar. The calendar itself is created in the campaign's Google account + * (never by a service account — Google's recommendation) and shared publicly + * by link (ops runbook); these URLs are what the team receives: + * + * - Google Calendar: the `cid` webcal URL (the established add-by-URL flow the + * iCal feed dialog already uses, `calendar/r/settings/addbyurl`). + * - Apple Calendar / Outlook: subscribe straight to the webcal URL the Google + * calendar exposes for public calendars. + * + * Both require the calendar to be public in the owner's sharing settings — + * the link IS the credential, same model as the iCal feed secret. + */ +const GOOGLE_CALENDAR_ADD_BASE = 'https://calendar.google.com/calendar/r' +const GOOGLE_CALENDAR_ICAL_BASE = 'https://calendar.google.com/calendar/ical' + +export const buildGoogleCalendarWebcalUrl = (calendarId: string): string => + `webcal://calendar.google.com/calendar/ical/${encodeURIComponent(calendarId)}/public/basic.ics` + +export const buildGoogleCalendarAddLink = (calendarId: string): string => { + const webcal = buildGoogleCalendarWebcalUrl(calendarId) + return `${GOOGLE_CALENDAR_ADD_BASE}?cid=${encodeURIComponent(webcal)}` +} + +/** The public iCal URL of the shared calendar — same `cid` target, plain HTTPS. */ +export const buildGoogleCalendarPublicIcalUrl = (calendarId: string): string => + `${GOOGLE_CALENDAR_ICAL_BASE}/${encodeURIComponent(calendarId)}/public/basic.ics` diff --git a/src/migrations/20260811_133332_add_google_calendar_sync.ts b/src/migrations/20260811_133332_add_google_calendar_sync.ts new file mode 100644 index 00000000..0593b765 --- /dev/null +++ b/src/migrations/20260811_133332_add_google_calendar_sync.ts @@ -0,0 +1,43 @@ +import { MigrateDownArgs, MigrateUpArgs, sql } from '@payloadcms/db-postgres' + +/** + * C114 — `googleCalendarSync` collection: the configuration/state row of the + * campaign→Google calendar mirror. Hand-written because `migrate:create` + * re-emitted pre-existing `campaign_user.contact_id` / + * `leadership_rels.campaign_user_id` statements (already applied by + * `20260809_204728_add_campaign_user_contact_and_leadership_advisors` — those + * would fail with "column already exists" on apply; kept out deliberately). + */ +export async function up({ db, payload, req }: MigrateUpArgs): Promise { + await db.execute(sql` + CREATE TABLE "google_calendar_sync" ( + "id" serial PRIMARY KEY NOT NULL, + "calendar_id" varchar, + "disabled_at" timestamp(3) with time zone, + "last_synced_at" timestamp(3) with time zone, + "last_success_at" timestamp(3) with time zone, + "last_error_at" timestamp(3) with time zone, + "last_error" varchar, + "updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL, + "created_at" timestamp(3) with time zone DEFAULT now() NOT NULL + ); + + CREATE INDEX "google_calendar_sync_calendar_id_idx" ON "google_calendar_sync" USING btree ("calendar_id"); + CREATE INDEX "google_calendar_sync_disabled_at_idx" ON "google_calendar_sync" USING btree ("disabled_at"); + CREATE INDEX "google_calendar_sync_updated_at_idx" ON "google_calendar_sync" USING btree ("updated_at"); + CREATE INDEX "google_calendar_sync_created_at_idx" ON "google_calendar_sync" USING btree ("created_at"); + + ALTER TABLE "payload_locked_documents_rels" ADD COLUMN "google_calendar_sync_id" integer; + ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_google_calendar_sync_fk" FOREIGN KEY ("google_calendar_sync_id") REFERENCES "public"."google_calendar_sync"("id") ON DELETE cascade ON UPDATE no action; + CREATE INDEX "payload_locked_documents_rels_google_calendar_sync_id_idx" ON "payload_locked_documents_rels" USING btree ("google_calendar_sync_id"); + `) +} + +export async function down({ db, payload, req }: MigrateDownArgs): Promise { + await db.execute(sql` + ALTER TABLE "payload_locked_documents_rels" DROP CONSTRAINT "payload_locked_documents_rels_google_calendar_sync_fk"; + DROP INDEX "payload_locked_documents_rels_google_calendar_sync_id_idx"; + ALTER TABLE "payload_locked_documents_rels" DROP COLUMN "google_calendar_sync_id"; + DROP TABLE "google_calendar_sync" CASCADE; + `) +} diff --git a/src/migrations/index.ts b/src/migrations/index.ts index a6d78dfe..b2f81972 100644 --- a/src/migrations/index.ts +++ b/src/migrations/index.ts @@ -48,6 +48,7 @@ import * as migration_20260808_184113_remodel_activity_responsible from './20260 import * as migration_20260809_204728_add_campaign_user_contact_and_leadership_advisors from './20260809_204728_add_campaign_user_contact_and_leadership_advisors'; import * as migration_20260810_010844_add_activity_all_day from './20260810_010844_add_activity_all_day'; import * as migration_20260811_111828_add_contact_phones_array from './20260811_111828_add_contact_phones_array'; +import * as migration_20260811_133332_add_google_calendar_sync from './20260811_133332_add_google_calendar_sync'; export const migrations = [ { @@ -298,6 +299,11 @@ export const migrations = [ { up: migration_20260811_111828_add_contact_phones_array.up, down: migration_20260811_111828_add_contact_phones_array.down, - name: '20260811_111828_add_contact_phones_array' + name: '20260811_111828_add_contact_phones_array', + }, + { + up: migration_20260811_133332_add_google_calendar_sync.up, + down: migration_20260811_133332_add_google_calendar_sync.down, + name: '20260811_133332_add_google_calendar_sync', }, ]; diff --git a/src/payload-types.ts b/src/payload-types.ts index 3b72eaa0..37bf8da5 100644 --- a/src/payload-types.ts +++ b/src/payload-types.ts @@ -87,6 +87,7 @@ export interface Config { municipalityUpdate: MunicipalityUpdate; activity: Activity; calendarFeed: CalendarFeed; + googleCalendarSync: GoogleCalendarSync; electionTally: ElectionTally; electionCandidateVote: ElectionCandidateVote; electionCandidate: ElectionCandidate; @@ -127,6 +128,7 @@ export interface Config { municipalityUpdate: MunicipalityUpdateSelect | MunicipalityUpdateSelect; activity: ActivitySelect | ActivitySelect; calendarFeed: CalendarFeedSelect | CalendarFeedSelect; + googleCalendarSync: GoogleCalendarSyncSelect | GoogleCalendarSyncSelect; electionTally: ElectionTallySelect | ElectionTallySelect; electionCandidateVote: ElectionCandidateVoteSelect | ElectionCandidateVoteSelect; electionCandidate: ElectionCandidateSelect | ElectionCandidateSelect; @@ -890,6 +892,27 @@ export interface CalendarFeed { updatedAt: string; createdAt: string; } +/** + * This interface was referenced by `Config`'s JSON-Schema + * via the `definition` "googleCalendarSync". + */ +export interface GoogleCalendarSync { + id: number; + /** + * O calendarId do calendário "Agenda da Campanha" na conta Google da campanha (ex.: c_…@group.calendar.google.com). O calendário precisa estar público no link e a service account com permissão de edição. + */ + calendarId?: string | null; + /** + * Enquanto preenchido, o Teqo não escreve no Google. + */ + disabledAt?: string | null; + lastSyncedAt?: string | null; + lastSuccessAt?: string | null; + lastErrorAt?: string | null; + lastError?: string | null; + updatedAt: string; + createdAt: string; +} /** * Totais oficiais TSE por município e zona (dado público). * @@ -1359,6 +1382,10 @@ export interface PayloadLockedDocument { relationTo: 'calendarFeed'; value: number | CalendarFeed; } | null) + | ({ + relationTo: 'googleCalendarSync'; + value: number | GoogleCalendarSync; + } | null) | ({ relationTo: 'electionTally'; value: number | ElectionTally; @@ -1858,6 +1885,20 @@ export interface CalendarFeedSelect { updatedAt?: T; createdAt?: T; } +/** + * This interface was referenced by `Config`'s JSON-Schema + * via the `definition` "googleCalendarSync_select". + */ +export interface GoogleCalendarSyncSelect { + calendarId?: T; + disabledAt?: T; + lastSyncedAt?: T; + lastSuccessAt?: T; + lastErrorAt?: T; + lastError?: T; + updatedAt?: T; + createdAt?: T; +} /** * This interface was referenced by `Config`'s JSON-Schema * via the `definition` "electionTally_select". @@ -2423,6 +2464,7 @@ export interface TaskCreateCollectionExport { | 'municipalityUpdate' | 'activity' | 'calendarFeed' + | 'googleCalendarSync' | 'electionTally' | 'electionCandidateVote' | 'electionCandidate' diff --git a/src/payload.config.ts b/src/payload.config.ts index e0878d68..91297270 100644 --- a/src/payload.config.ts +++ b/src/payload.config.ts @@ -22,6 +22,7 @@ import { Contact } from './collections/Contact' import { ElectionCandidate } from './collections/ElectionCandidate' import { ElectionCandidateVote } from './collections/ElectionCandidateVote' import { ElectionTally } from './collections/ElectionTally' +import { GoogleCalendarSync } from './collections/GoogleCalendarSync' import { Leadership } from './collections/Leadership' import { Media } from './collections/Media' import { Municipality } from './collections/Municipality' @@ -107,6 +108,7 @@ export default buildConfig({ MunicipalityUpdate, Activity, CalendarFeed, + GoogleCalendarSync, ElectionTally, ElectionCandidateVote, ElectionCandidate, diff --git a/src/utilities/access/googleCalendarSync.ts b/src/utilities/access/googleCalendarSync.ts new file mode 100644 index 00000000..f7479b56 --- /dev/null +++ b/src/utilities/access/googleCalendarSync.ts @@ -0,0 +1,46 @@ +import type { Access, FieldAccess } from 'payload' + +import { + getFreshCampaignUser, + isCampaignStaff, + isPayloadAdmin, + payloadAdminOnly, +} from '@/utilities/access/shared' + +/** + * C114 — access for the `googleCalendarSync` configuration/state collection. + * Staff (coordinator/advisor/candidate) read the sync state (pill) and toggle + * `disabledAt`; only Payload admin sets the operational `calendarId`; the + * system state fields are written solely by the sync engine through an + * admin bypass — never by a user path. + */ + +export const canReadGoogleCalendarSync: Access = async ({ req }) => { + if (isPayloadAdmin(req.user)) return true + return isCampaignStaff(await getFreshCampaignUser(req)) +} + +export const canCreateGoogleCalendarSync: Access = async ({ req }) => { + if (isPayloadAdmin(req.user)) return true + return isCampaignStaff(await getFreshCampaignUser(req)) +} + +export const canUpdateGoogleCalendarSync: Access = async ({ req }) => { + if (isPayloadAdmin(req.user)) return true + return isCampaignStaff(await getFreshCampaignUser(req)) +} + +export const canDeleteGoogleCalendarSync: Access = payloadAdminOnly + +/** `calendarId` — operational identity, admin-only (env-level trust). */ +export const canSetGoogleCalendarSyncConfigField: FieldAccess = ({ req }) => + isPayloadAdmin(req.user) + +/** `disabledAt` — staff may pause/resume the sync from the agenda. */ +export const canSetGoogleCalendarSyncDisabled: FieldAccess = async ({ req }) => { + if (isPayloadAdmin(req.user)) return true + return isCampaignStaff(await getFreshCampaignUser(req)) +} + +/** System state fields — only the sync engine writes them (overrideAccess). */ +export const canSetGoogleCalendarSyncSystemField: FieldAccess = () => false diff --git a/src/utilities/calendarFeed.ts b/src/utilities/calendarFeed.ts index d7cc4540..13d86455 100644 --- a/src/utilities/calendarFeed.ts +++ b/src/utilities/calendarFeed.ts @@ -3,6 +3,7 @@ import 'server-only' import { createHash } from 'node:crypto' import { allDayCivilDateOf, allDayExclusiveEndDate } from '@/lib/activityAllDay' +import { buildActivityDescriptionParts } from '@/lib/activityDescription' import type { Activity, CalendarFeed } from '@/payload-types' import { advisorMunicipalityScopeWhere } from '@/utilities/access/shared' import type { Payload, Where } from 'payload' @@ -24,15 +25,9 @@ const escapeICalText = (value: string): string => const formatICalDate = (isoString: string): string => isoString.replace(/[-:]/g, '').replace(/\.\d{3}/, '') -const buildActivityDescription = (activity: Activity, municipalityName?: string): string => { - const parts: string[] = [] - if (municipalityName) parts.push(`Município: ${municipalityName}`) - if (activity.locality) parts.push(`Local: ${activity.locality}`) - if (activity.tags?.length) parts.push(`Tags: ${activity.tags.join(', ')}`) - if (activity.deputyPresent) parts.push('Deputado presente') - if (activity.status === 'cancelado') parts.push('CANCELADO') - return parts.join('\\n') -} +const buildActivityDescription = (activity: Activity, municipalityName?: string): string => + // iCal escapes newlines as literal `\n` (backslash + n). + buildActivityDescriptionParts(activity, municipalityName).join('\\n') export const generateICalFeed = ( activities: Activity[], @@ -144,7 +139,8 @@ export const buildICalFeedResponse = ( }) } -const buildFeedDateRange = (): { rangeStart: string; rangeEnd: string } => { +/** The shared activity window (past lookback + future lookahead) of the feed and the Google mirror. */ +export const buildActivityWindowRange = (): { rangeStart: string; rangeEnd: string } => { const now = new Date() const rangeStart = new Date(now) rangeStart.setDate(rangeStart.getDate() - FEED_LOOKBACK_DAYS) @@ -157,24 +153,32 @@ const buildFeedDateRange = (): { rangeStart: string; rangeEnd: string } => { } } +/** + * The activity window (past lookback + future lookahead) and its base where + * clauses — shared by the iCal feed and the C114 Google mirror so both + * surfaces keep the SAME horizon by definition. The feed adds its per-feed + * filters (municipality/deputy/tag + advisor scope) on top. + */ +export const buildActivityWindowWhereClauses = (rangeStart: string, rangeEnd: string): Where[] => [ + { startAt: { less_than: rangeEnd } }, + { + or: [ + { endAt: { greater_than: rangeStart } }, + { + and: [{ endAt: { exists: false } }, { startAt: { greater_than_equal: rangeStart } }], + }, + ], + }, + { status: { not_equals: 'cancelado' } }, +] + const buildFeedWhere = ( feed: CalendarFeed, rangeStart: string, rangeEnd: string, accessibleMunicipalityIds: number[] | null, ): Where => { - const filters: Where[] = [ - { startAt: { less_than: rangeEnd } }, - { - or: [ - { endAt: { greater_than: rangeStart } }, - { - and: [{ endAt: { exists: false } }, { startAt: { greater_than_equal: rangeStart } }], - }, - ], - }, - { status: { not_equals: 'cancelado' } }, - ] + const filters: Where[] = buildActivityWindowWhereClauses(rangeStart, rangeEnd) if (feed.filterMunicipality) { const municipalityId = @@ -208,7 +212,7 @@ export const loadFeedActivities = async ( feed: CalendarFeed, accessibleMunicipalityIds: number[] | null, ): Promise => { - const { rangeStart, rangeEnd } = buildFeedDateRange() + const { rangeStart, rangeEnd } = buildActivityWindowRange() const where = buildFeedWhere(feed, rangeStart, rangeEnd, accessibleMunicipalityIds) // Intentional admin bypass: access is already enforced at the feed level diff --git a/src/utilities/googleCalendarClient.ts b/src/utilities/googleCalendarClient.ts new file mode 100644 index 00000000..fa928c87 --- /dev/null +++ b/src/utilities/googleCalendarClient.ts @@ -0,0 +1,206 @@ +import 'server-only' + +import { importPKCS8, SignJWT } from 'jose' + +import type { GoogleRemoteEvent } from '@/lib/googleCalendarEventMapping' + +/** + * C114 — thin Calendar API v3 client authenticated as the campaign's service + * account (JWT assertion → OAuth2 token → REST). Only the four event endpoints + * the reconciliation engine needs; the token is cached per runtime instance + * and re-minted on 401. The `fetch` implementation is injectable so tests can + * stub the transport without network. + */ + +export const GOOGLE_CALENDAR_SCOPE = 'https://www.googleapis.com/auth/calendar' + +const GOOGLE_TOKEN_ENDPOINT = 'https://oauth2.googleapis.com/token' +const GOOGLE_CALENDAR_API_BASE = 'https://www.googleapis.com/calendar/v3' +const GOOGLE_TOKEN_TTL_SECONDS = 3600 +const TOKEN_REFRESH_LEAD_SECONDS = 60 +const MAX_LIST_PAGE_SIZE = 2500 +/** + * Every outbound call carries a hard timeout so a hanging Google never holds + * the activity write path (the afterChange hook awaits the sync) — Teqo's + * availability cannot depend on Google's. + */ +const REQUEST_TIMEOUT_MS = 15_000 + +export type { GoogleRemoteEvent } from '@/lib/googleCalendarEventMapping' + +export type GoogleCalendarCredentials = { + clientEmail: string + privateKey: string +} + +/** Typed transport failure — safe message, never echoes credentials or bodies. */ +export class GoogleCalendarApiError extends Error { + readonly status: number + + constructor(message: string, status: number) { + super(message) + this.name = 'GoogleCalendarApiError' + this.status = status + } +} + +export type GoogleCalendarClient = { + listEvents: ( + calendarId: string, + range: { timeMin: string; timeMax: string }, + ) => Promise + insertEvent: (calendarId: string, event: GoogleRemoteEvent) => Promise + updateEvent: (calendarId: string, eventId: string, event: GoogleRemoteEvent) => Promise + deleteEvent: (calendarId: string, eventId: string) => Promise +} + +export type FetchLike = typeof fetch + +/** + * The signed JWT assertion for the OAuth2 token exchange (RFC 7523). Exported + * for tests — `createGoogleCalendarClient` mints and exchanges it internally. + */ +export const buildServiceAccountAssertion = async ( + credentials: GoogleCalendarCredentials, + nowSeconds: number, +): Promise => { + const key = await importPKCS8(credentials.privateKey, 'RS256') + return new SignJWT({ scope: GOOGLE_CALENDAR_SCOPE }) + .setProtectedHeader({ alg: 'RS256', typ: 'JWT' }) + .setIssuer(credentials.clientEmail) + .setAudience(GOOGLE_TOKEN_ENDPOINT) + .setIssuedAt(nowSeconds) + .setExpirationTime(nowSeconds + GOOGLE_TOKEN_TTL_SECONDS) + .sign(key) +} + +export const createGoogleCalendarClient = ( + credentials: GoogleCalendarCredentials, + fetchImpl: FetchLike = fetch, +): GoogleCalendarClient => { + let cachedToken: { value: string; expiresAtMs: number } | null = null + + const requestAccessToken = async (): Promise => { + const nowSeconds = Math.floor(Date.now() / 1000) + const assertion = await buildServiceAccountAssertion(credentials, nowSeconds) + + const response = await fetchImpl(GOOGLE_TOKEN_ENDPOINT, { + method: 'POST', + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + body: new URLSearchParams({ + grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer', + assertion, + }).toString(), + signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS), + }) + if (!response.ok) { + throw new GoogleCalendarApiError( + `Não foi possível autenticar no Google (HTTP ${response.status}).`, + response.status, + ) + } + + const body = (await response.json()) as { access_token?: string; expires_in?: number } + if (!body.access_token) { + throw new GoogleCalendarApiError('Resposta de autenticação do Google sem token.', 502) + } + + cachedToken = { + value: body.access_token, + expiresAtMs: Date.now() + (body.expires_in ?? GOOGLE_TOKEN_TTL_SECONDS) * 1000, + } + return cachedToken.value + } + + const getAccessToken = async (): Promise => { + if (cachedToken && cachedToken.expiresAtMs > Date.now() + TOKEN_REFRESH_LEAD_SECONDS * 1000) { + return cachedToken.value + } + return requestAccessToken() + } + + const apiFetch = async ( + url: string, + init: RequestInit = {}, + retried = false, + ): Promise => { + const token = await getAccessToken() + const response = await fetchImpl(url, { + ...init, + headers: { ...init.headers, Authorization: `Bearer ${token}` }, + signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS), + }) + + // A 401 usually means the cached token expired server-side — re-mint once. + if (response.status === 401 && !retried) { + cachedToken = null + return apiFetch(url, init, true) + } + if (!response.ok) { + throw new GoogleCalendarApiError( + `Google Calendar API respondeu HTTP ${response.status}.`, + response.status, + ) + } + return response + } + + const eventsUrl = (calendarId: string, ...parts: string[]): string => + `${GOOGLE_CALENDAR_API_BASE}/calendars/${encodeURIComponent(calendarId)}/events${parts + .map((part) => `/${encodeURIComponent(part)}`) + .join('')}` + + const listEvents: GoogleCalendarClient['listEvents'] = async (calendarId, range) => { + const events: GoogleRemoteEvent[] = [] + let pageToken: string | undefined + + do { + const params = new URLSearchParams({ + timeMin: range.timeMin, + timeMax: range.timeMax, + maxResults: String(MAX_LIST_PAGE_SIZE), + singleEvents: 'true', + }) + if (pageToken) params.set('pageToken', pageToken) + + const response = await apiFetch(`${eventsUrl(calendarId)}?${params.toString()}`) + const body = (await response.json()) as { + items?: GoogleRemoteEvent[] + nextPageToken?: string + } + events.push(...(body.items ?? [])) + pageToken = body.nextPageToken + } while (pageToken) + + return events + } + + const insertEvent: GoogleCalendarClient['insertEvent'] = async (calendarId, event) => { + try { + await apiFetch(eventsUrl(calendarId), { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(event), + }) + } catch (error) { + // 409 = the deterministic id already exists (a concurrent pass inserted + // it between our list and this insert) — the idempotent end state, not + // a failure; the next pass converges on the content. + if (!(error instanceof GoogleCalendarApiError && error.status === 409)) throw error + } + } + + const updateEvent: GoogleCalendarClient['updateEvent'] = async (calendarId, eventId, event) => { + await apiFetch(eventsUrl(calendarId, eventId), { + method: 'PUT', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(event), + }) + } + + const deleteEvent: GoogleCalendarClient['deleteEvent'] = async (calendarId, eventId) => { + await apiFetch(eventsUrl(calendarId, eventId), { method: 'DELETE' }) + } + + return { listEvents, insertEvent, updateEvent, deleteEvent } +} diff --git a/src/utilities/googleCalendarSync.ts b/src/utilities/googleCalendarSync.ts new file mode 100644 index 00000000..17529d35 --- /dev/null +++ b/src/utilities/googleCalendarSync.ts @@ -0,0 +1,494 @@ +import 'server-only' + +import type { Activity, GoogleCalendarSync as GoogleCalendarSyncDoc } from '@/payload-types' +import type { Payload, PayloadRequest } from 'payload' + +import { + activityMunicipalityIdOf, + buildGoogleEventPayload, + decodeGoogleEventActivityId, + googleEventContentEquals, + googleEventIdForActivity, +} from '@/lib/googleCalendarEventMapping' +import { + buildActivityWindowRange, + buildActivityWindowWhereClauses, + loadMunicipalityNames, +} from '@/utilities/calendarFeed' +import { + createGoogleCalendarClient, + GoogleCalendarApiError, + type GoogleCalendarClient, + type GoogleCalendarCredentials, +} from '@/utilities/googleCalendarClient' + +/** + * C114 — the campaign→Google calendar reconciliation engine. + * + * One full pass per run (D3): load the staff-scope activities in the window, + * list the calendar's events in a slightly wider window, diff by the + * deterministic event id (`teqo` + base32hex(activityId)) and create/update/ + * delete — updates only when the content differs, so repeated passes converge + * without touching Google. Canceled and hard-deleted activities drop out of + * the wanted set, which deletes their Google event: Teqo keeps the SoT, + * Google keeps no "canceled" state visible to followers. + * + * Triggers (D4): activity afterChange/afterDelete (relevant fields only), + * agenda-page auto-retry and the manual "Sincronizar agora", plus the + * `googleCalendarSync` config afterChange (D7: calendarId change / re-enable). + * The engine NEVER throws into the caller — failures land in the state fields + * and the status derives to `paused`. + */ + +export const GOOGLE_CALENDAR_SERVICE_ACCOUNT_KEY_ENV = 'GOOGLE_CALENDAR_SERVICE_ACCOUNT_KEY' + +/** + * The list window scans further into the past than the push window so a + * canceled/deleted event older than the 90-day lookback is still found and + * deleted (a realistic case: canceling an activity that happened months ago). + */ +const SYNC_DELETE_LOOKBACK_DAYS = 730 +const SYNC_LIST_WINDOW_BUFFER_DAYS = 30 +/** + * The fields the Google event mirrors (title, schedule, location, tags, + * deputy flag, municipality). Task toggles, updates and result records are + * deliberately absent — those never trigger a pass. + */ +const SYNC_RELEVANT_ACTIVITY_FIELDS = [ + 'title', + 'status', + 'startAt', + 'endAt', + 'allDay', + 'locality', + 'municipality', + 'tags', + 'deputyPresent', +] as const + +export type GoogleCalendarSyncStatus = 'not-configured' | 'disabled' | 'synced' | 'paused' + +export type GoogleCalendarSyncView = { + status: GoogleCalendarSyncStatus + calendarId: string | null + lastSyncedAt: string | null + lastSuccessAt: string | null + lastErrorAt: string | null + lastError: string | null +} + +/** + * Pure derivation (never stored): the pill state is a function of the + * credential presence, the configured calendar and the last attempt outcomes. + * `paused` wins while the last attempt failed; any later success recovers. + */ +export const deriveGoogleCalendarSyncStatus = (input: { + hasCredential: boolean + calendarId: string | null | undefined + disabledAt?: string | null | undefined + lastSuccessAt?: string | null | undefined + lastErrorAt?: string | null | undefined +}): GoogleCalendarSyncStatus => { + if (!input.hasCredential || !input.calendarId) return 'not-configured' + if (input.disabledAt) return 'disabled' + if (input.lastErrorAt && (!input.lastSuccessAt || input.lastErrorAt > input.lastSuccessAt)) { + return 'paused' + } + return 'synced' +} + +/** + * Reads the service account credential from the environment (base64 of the + * Google Cloud JSON key). Fail-closed: malformed/absent → null, which + * disables the sync entirely without breaking anything else. + */ +export const readGoogleServiceAccountCredentials = (): GoogleCalendarCredentials | null => { + const raw = process.env[GOOGLE_CALENDAR_SERVICE_ACCOUNT_KEY_ENV] + if (!raw) return null + + try { + const parsed = JSON.parse(Buffer.from(raw, 'base64').toString('utf8')) as Record< + string, + unknown + > + if ( + typeof parsed.client_email === 'string' && + typeof parsed.private_key === 'string' && + parsed.private_key.length > 0 + ) { + return { clientEmail: parsed.client_email, privateKey: parsed.private_key } + } + } catch { + // malformed credential — treated as absent (fail-closed, no sync attempts) + } + return null +} + +export const loadGoogleCalendarSyncConfig = async ( + payload: Payload, + req?: PayloadRequest, +): Promise => { + // Intentional admin bypass: config/state of the mirror is read by the + // engine and the agenda pill for any staff — access is enforced at the + // action layer, never per-read. + const result = await payload.find({ + collection: 'googleCalendarSync', + limit: 1, + pagination: false, + depth: 0, + overrideAccess: true, + req, + }) + return result.docs[0] ?? null +} + +const docToView = ( + doc: GoogleCalendarSyncDoc | null, + hasCredential: boolean, +): GoogleCalendarSyncView => ({ + status: deriveGoogleCalendarSyncStatus({ + hasCredential, + calendarId: doc?.calendarId ?? null, + disabledAt: doc?.disabledAt ?? null, + lastSuccessAt: doc?.lastSuccessAt ?? null, + lastErrorAt: doc?.lastErrorAt ?? null, + }), + calendarId: doc?.calendarId ?? null, + lastSyncedAt: doc?.lastSyncedAt ?? null, + lastSuccessAt: doc?.lastSuccessAt ?? null, + lastErrorAt: doc?.lastErrorAt ?? null, + lastError: doc?.lastError ?? null, +}) + +/** The read model the agenda UI renders — status derived, never stored. */ +export const readGoogleCalendarSyncView = async ( + payload: Payload, +): Promise => { + const [doc, credentials] = await Promise.all([ + loadGoogleCalendarSyncConfig(payload), + readGoogleServiceAccountCredentials(), + ]) + return docToView(doc, credentials !== null) +} + +/** + * The official calendar mirrors the FULL staff scope (espelho cheio): no + * advisor intersection — a coordinator-managed calendar, unlike per-feed + * creator scopes. Canceled activities are excluded from the PUSH set (their + * events get deleted); the window is the feed's own (shared contract). + */ +const loadSyncActivities = async ( + payload: Payload, + req?: PayloadRequest, +): Promise<{ activities: Activity[]; municipalityNames: Map }> => { + const { rangeStart, rangeEnd } = buildActivityWindowRange() + + const result = await payload.find({ + collection: 'activity', + depth: 0, + limit: 0, + pagination: false, + sort: 'startAt', + where: { and: buildActivityWindowWhereClauses(rangeStart, rangeEnd) }, + // Intentional admin bypass: the official mirror is the whole campaign + // agenda (espelho cheio) — the collection access already gates writers. + overrideAccess: true, + req, + }) + + const activities = result.docs + const municipalityIds = [ + ...new Set( + activities + .map((activity) => activityMunicipalityIdOf(activity.municipality)) + .filter((id): id is number => id !== undefined), + ), + ] + const municipalityNames = await loadMunicipalityNames(payload, municipalityIds) + + return { activities, municipalityNames } +} + +/** + * Ids of activities alive over the WIDER past window — the delete guard set. + * An event older than the push lookback whose activity is still alive (e.g. a + * past `realizado` commitment) must NOT be deleted; canceled/hard-deleted + * activities drop out of this set and their events are cleaned up wherever + * they are in the extended window. + */ +const loadAliveActivityIds = async ( + payload: Payload, + req: PayloadRequest | undefined, + timeMin: string, + timeMax: string, +): Promise> => { + const result = await payload.find({ + collection: 'activity', + depth: 0, + limit: 0, + pagination: false, + where: { + and: [ + { startAt: { less_than: timeMax } }, + { + or: [ + { endAt: { greater_than: timeMin } }, + { + and: [{ endAt: { exists: false } }, { startAt: { greater_than_equal: timeMin } }], + }, + ], + }, + { status: { not_equals: 'cancelado' } }, + ], + }, + // Intentional admin bypass: same espelho-cheio rationale as above. + overrideAccess: true, + req, + }) + return new Set(result.docs.map((activity) => activity.id)) +} + +/** + * The list window scans the extended past so canceled/deleted events older + * than the 90-day push lookback are still found and removed (canceling an + * activity that happened months ago is a realistic operation). + */ +const buildListWindow = ( + rangeStart: string, + rangeEnd: string, +): { timeMin: string; timeMax: string } => { + const bufferMs = SYNC_LIST_WINDOW_BUFFER_DAYS * 86_400_000 + const deleteLookbackMs = SYNC_DELETE_LOOKBACK_DAYS * 86_400_000 + const nowMs = Date.now() + return { + timeMin: new Date( + Math.min(new Date(rangeStart).getTime() - bufferMs, nowMs - deleteLookbackMs), + ).toISOString(), + timeMax: new Date(new Date(rangeEnd).getTime() + bufferMs).toISOString(), + } +} + +const deleteRemoteEvent = async ( + client: GoogleCalendarClient, + calendarId: string, + eventId: string, +): Promise => { + try { + await client.deleteEvent(calendarId, eventId) + } catch (error) { + // Already gone is the desired end state. + if (!(error instanceof GoogleCalendarApiError && error.status === 404)) throw error + } +} + +type SyncCounts = { created: number; updated: number; deleted: number } + +const runSyncPass = async ( + payload: Payload, + req: PayloadRequest | undefined, + client: GoogleCalendarClient, + calendarId: string, +): Promise => { + const { rangeStart, rangeEnd } = buildActivityWindowRange() + const { activities, municipalityNames } = await loadSyncActivities(payload, req) + const listWindow = buildListWindow(rangeStart, rangeEnd) + const remoteEvents = await client.listEvents(calendarId, listWindow) + const aliveActivityIds = await loadAliveActivityIds( + payload, + req, + listWindow.timeMin, + listWindow.timeMax, + ) + + const wantedById = new Map() + for (const activity of activities) { + wantedById.set(activity.id, activity) + } + + let created = 0 + let updated = 0 + let deleted = 0 + + const remoteEventIds = new Set() + + for (const event of remoteEvents) { + // Only OUR deterministic ids are reconciled; foreign events on the + // campaign calendar (e.g. added manually by the owner) stay untouched — + // the full decode, never a prefix match (a manual `teqo3` is foreign). + const activityId = event.id ? decodeGoogleEventActivityId(event.id) : null + if (activityId === null) continue + remoteEventIds.add(event.id!) + + const activity = wantedById.get(activityId) + if (!activity) { + // Not in the push window: alive past/future commitments keep their + // event; anything else (canceled, hard-deleted) leaves Google. + if (!aliveActivityIds.has(activityId)) { + await deleteRemoteEvent(client, calendarId, event.id!) + deleted += 1 + } + continue + } + + const municipalityId = activityMunicipalityIdOf(activity.municipality) + const municipalityName = municipalityId ? municipalityNames.get(municipalityId) : undefined + const googleEvent = buildGoogleEventPayload(activity, municipalityName) + + if (!googleEventContentEquals(event, googleEvent)) { + await client.updateEvent(calendarId, event.id!, googleEvent) + updated += 1 + } + } + + for (const [activityId, activity] of wantedById) { + const eventId = googleEventIdForActivity(activityId) + if (remoteEventIds.has(eventId)) continue + + const municipalityId = activityMunicipalityIdOf(activity.municipality) + const municipalityName = municipalityId ? municipalityNames.get(municipalityId) : undefined + await client.insertEvent(calendarId, buildGoogleEventPayload(activity, municipalityName)) + created += 1 + } + + return { created, updated, deleted } +} + +const recordSyncState = async ( + payload: Payload, + req: PayloadRequest | undefined, + patch: Partial< + Pick + >, +): Promise => { + const doc = await loadGoogleCalendarSyncConfig(payload, req) + if (!doc) return // nothing to record before the first activation + + await payload.update({ + collection: 'googleCalendarSync', + id: doc.id, + data: patch, + depth: 0, + // Intentional admin bypass: the state fields are system-only by design. + overrideAccess: true, + req, + }) +} + +export type CampaignCalendarSyncOutcome = SyncCounts & { + status: GoogleCalendarSyncStatus + at: string +} + +export type CampaignCalendarSyncOptions = { + reason: 'create' | 'update' | 'delete' | 'config-change' | 'manual' + client?: GoogleCalendarClient + req?: PayloadRequest +} + +export const runCampaignCalendarSync = async ( + payload: Payload, + options: CampaignCalendarSyncOptions, +): Promise => { + const credentials = readGoogleServiceAccountCredentials() + const config = await loadGoogleCalendarSyncConfig(payload, options.req) + const at = new Date().toISOString() + + if (!credentials || !config?.calendarId || config.disabledAt) { + return { + status: deriveGoogleCalendarSyncStatus({ + hasCredential: credentials !== null, + calendarId: config?.calendarId ?? null, + disabledAt: config?.disabledAt ?? null, + lastSuccessAt: config?.lastSuccessAt ?? null, + lastErrorAt: config?.lastErrorAt ?? null, + }), + created: 0, + updated: 0, + deleted: 0, + at, + } + } + + const client = options.client ?? createGoogleCalendarClient(credentials) + + try { + const counts = await runSyncPass(payload, options.req, client, config.calendarId) + await recordSyncState(payload, options.req, { lastSyncedAt: at, lastSuccessAt: at }) + return { status: 'synced', ...counts, at } + } catch (error) { + const message = + error instanceof Error ? error.message : 'Erro desconhecido ao sincronizar com o Google.' + await recordSyncState(payload, options.req, { + lastSyncedAt: at, + lastErrorAt: at, + lastError: message.slice(0, 500), + }) + return { status: 'paused', created: 0, updated: 0, deleted: 0, at } + } +} + +/** + * Normalizes a mirrored field for change detection. The afterChange `doc` + * carries populated relationships (municipality object) while `previousDoc` + * comes depth-0 (numeric id) — comparing raw values would flag EVERY update + * as changed and sync on every task toggle. + */ +const normalizeMirroredField = (doc: Activity | undefined, field: string): unknown => { + const raw = (doc as unknown as Record | undefined)?.[field] + if (field === 'municipality') { + return activityMunicipalityIdOf(raw as unknown as Activity['municipality']) ?? null + } + if (field === 'tags') { + return Array.isArray(raw) ? [...raw].sort() : raw + } + return raw +} + +/** + * Pure guard — the activity hook only runs a sync pass when a field the + * Google event mirrors changed (task toggles, updates and result records + * never trigger). Create/delete always sync. + */ +export const shouldSyncActivityOperation = ({ + operation, + doc, + previousDoc, +}: { + operation: 'create' | 'update' | 'delete' + doc: Activity + previousDoc?: Activity +}): boolean => { + if (operation !== 'update') return true + return SYNC_RELEVANT_ACTIVITY_FIELDS.some( + (field) => + JSON.stringify(normalizeMirroredField(doc, field)) !== + JSON.stringify(normalizeMirroredField(previousDoc, field)), + ) +} + +/** + * Pure guard — the config hook reconciles on calendarId change or re-enable + * (D7); state-only writes (the engine recording outcomes) never re-trigger, + * which is what breaks the would-be loop. + */ +export const shouldSyncConfigChange = ({ + operation, + doc, + previousDoc, +}: { + operation: 'create' | 'update' + doc: GoogleCalendarSyncDoc + previousDoc?: GoogleCalendarSyncDoc +}): boolean => { + if (operation !== 'update') return true + const calendarChanged = (doc.calendarId ?? '') !== (previousDoc?.calendarId ?? '') + const disabledCleared = Boolean(previousDoc?.disabledAt) && !doc.disabledAt + return calendarChanged || disabledCleared +} + +/** + * Activity hook (afterChange + afterDelete) and the `googleCalendarSync` + * config hook (D7 auto-resync) live in `googleCalendarSyncHooks.ts` — they + * delegate to the guards above and the engine, and are kept in a separate + * module so tests can mock the engine at the import boundary. + */ diff --git a/src/utilities/googleCalendarSyncHooks.ts b/src/utilities/googleCalendarSyncHooks.ts new file mode 100644 index 00000000..ec79ee6b --- /dev/null +++ b/src/utilities/googleCalendarSyncHooks.ts @@ -0,0 +1,67 @@ +import 'server-only' + +import type { Activity, GoogleCalendarSync as GoogleCalendarSyncDoc } from '@/payload-types' +import type { PayloadRequest } from 'payload' + +import { + runCampaignCalendarSync, + shouldSyncActivityOperation, + shouldSyncConfigChange, +} from '@/utilities/googleCalendarSync' + +/** + * C114 — the two Payload hooks that trigger the Google mirror (in a module of + * their own so tests can mock the engine at the import boundary). + * + * Activity hook (afterChange + afterDelete): delegates the decision to the + * pure guard — task toggles, updates and result records never trigger a pass. + * afterDelete hooks carry no `operation`; its absence means delete. + * Config hook (afterChange on `googleCalendarSync`): D7 auto-resync — a + * changed `calendarId` or a re-enable runs the full reconciliation against + * the new calendar; state-only writes never re-trigger (no loop). + * + * Both NEVER throw: the Teqo write paths are independent of Google + * (fail-closed at the trigger; engine failures land in the `paused` state). + */ +export const activityGoogleCalendarSyncHook = async ({ + doc, + previousDoc, + operation, + req, +}: { + doc: Activity + previousDoc?: Activity + operation?: 'create' | 'update' + req: PayloadRequest +}): Promise => { + try { + const resolvedOperation = operation ?? 'delete' + if (shouldSyncActivityOperation({ operation: resolvedOperation, doc, previousDoc })) { + await runCampaignCalendarSync(req.payload, { reason: resolvedOperation, req }) + } + } catch { + // recorded as paused inside the engine; the write path stays intact + } + return doc +} + +export const googleCalendarSyncConfigHook = async ({ + doc, + previousDoc, + operation, + req, +}: { + doc: GoogleCalendarSyncDoc + previousDoc?: GoogleCalendarSyncDoc + operation: 'create' | 'update' + req: PayloadRequest +}): Promise => { + try { + if (shouldSyncConfigChange({ operation, doc, previousDoc })) { + await runCampaignCalendarSync(req.payload, { reason: 'config-change', req }) + } + } catch { + // never throw into admin writes + } + return doc +} diff --git a/tests/e2e/campaignAgendaGoogleSync.e2e.spec.ts b/tests/e2e/campaignAgendaGoogleSync.e2e.spec.ts new file mode 100644 index 00000000..20fd5911 --- /dev/null +++ b/tests/e2e/campaignAgendaGoogleSync.e2e.spec.ts @@ -0,0 +1,33 @@ +import { campaignPageChrome, expect, test } from './fixtures/campaignE2EFixtures.js' + +/** + * C114 — Google Calendar mirror surface on the agenda. The e2e only pins the + * deterministic "não configurado" state (no credential env in the test + * runtime, no sync doc in the seeded DB): engine behavior (create/update/ + * delete/recover) is covered by the int tests with an in-memory calendar + * stub — this spec must never depend on the real Google API. + */ +test.describe('Agenda — sincronização Google (C114)', () => { + test('staff vê a pill "não configurado" e o diálogo explica o passo de operação', async ({ + campaign, + page, + }) => { + const { fixtures } = campaign + const coordinator = await fixtures.createCampaignUser('coordinator') + + await campaign.login(page, coordinator.email!, coordinator.password) + await page.goto(`${campaign.baseURL}/campanha/agenda`) + + await expect(campaignPageChrome(page, 'Agenda')).toBeVisible() + + const pill = page.getByRole('button', { name: 'Google: não configurado' }) + await expect(pill).toBeVisible({ timeout: 15_000 }) + await pill.click() + + const dialog = page.getByRole('dialog', { name: /Agenda da Campanha no Google/ }) + await expect(dialog).toBeVisible() + await expect(dialog.getByText('Ainda não configurado')).toBeVisible() + // O runbook de operação aparece (a ativação depende da conta Google da campanha). + await expect(dialog.getByText(/service account do Teqo/)).toBeVisible() + }) +}) diff --git a/tests/int/googleCalendarSync.int.spec.ts b/tests/int/googleCalendarSync.int.spec.ts new file mode 100644 index 00000000..055a2b6c --- /dev/null +++ b/tests/int/googleCalendarSync.int.spec.ts @@ -0,0 +1,334 @@ +// @vitest-environment node + +import { getPayload, type Payload } from 'payload' +import { afterEach, beforeAll, describe, expect, it } from 'vitest' + +import { googleEventIdForActivity } from '@/lib/googleCalendarEventMapping' +import config from '@/payload.config' +import type { GoogleCalendarClient, GoogleRemoteEvent } from '@/utilities/googleCalendarClient' +import { + GOOGLE_CALENDAR_SERVICE_ACCOUNT_KEY_ENV, + loadGoogleCalendarSyncConfig, + readGoogleCalendarSyncView, + runCampaignCalendarSync, +} from '@/utilities/googleCalendarSync' +import { hookFilledCreateData } from '@/utilities/hookFilledData' + +import { installCampaignFixtures } from '../helpers/campaignFixtures' + +let payload: Payload +const campaignFixtures = installCampaignFixtures({ + getPayload: () => payload, + setPayload: (nextPayload) => { + payload = nextPayload + }, +}) + +const FAKE_KEY = Buffer.from( + JSON.stringify({ + client_email: 'teqo-sa@projeto.iam.gserviceaccount.com', + private_key: '-----BEGIN PRIVATE KEY-----\nMOCK\n-----END PRIVATE KEY-----\n', + }), + 'utf8', +).toString('base64') + +const originalKey = process.env[GOOGLE_CALENDAR_SERVICE_ACCOUNT_KEY_ENV] + +const withCredential = async (run: () => Promise): Promise => { + process.env[GOOGLE_CALENDAR_SERVICE_ACCOUNT_KEY_ENV] = FAKE_KEY + try { + return await run() + } finally { + if (originalKey === undefined) { + delete process.env[GOOGLE_CALENDAR_SERVICE_ACCOUNT_KEY_ENV] + } else { + process.env[GOOGLE_CALENDAR_SERVICE_ACCOUNT_KEY_ENV] = originalKey + } + } +} + +/** In-memory Google calendar stub — list/insert/update/delete over an array. */ +const createStubClient = (store: GoogleRemoteEvent[] = []): GoogleCalendarClient => ({ + listEvents: async (_calendarId, range) => + store.filter((event) => { + const start = event.start?.dateTime ?? event.start?.date ?? '' + return start >= range.timeMin && start <= range.timeMax + }), + insertEvent: async (_calendarId, event) => { + store.push(event) + }, + updateEvent: async (_calendarId, eventId, event) => { + const index = store.findIndex((entry) => entry.id === eventId) + if (index >= 0) store[index] = event + }, + deleteEvent: async (_calendarId, eventId) => { + const index = store.findIndex((entry) => entry.id === eventId) + if (index >= 0) store.splice(index, 1) + }, +}) + +const calendarA = 'c_campanha_a@group.calendar.google.com' +const calendarB = 'c_campanha_b@group.calendar.google.com' + +describe('campaign Google calendar sync engine (C114)', () => { + beforeAll(async () => { + payload = await getPayload({ config: await config }) + }) + + afterEach(async () => { + // Teqo never depends on Google: the engine must never throw into callers. + // Cleanup happens per test; the env is restored by withCredential. + await payload.delete({ + collection: 'activity', + where: { title: { like: 'C114%' } }, + overrideAccess: true, + }) + await payload.delete({ + collection: 'googleCalendarSync', + where: { id: { exists: true } }, + overrideAccess: true, + }) + }) + + const createActivity = async (overrides: Record = {}) => { + const municipality = await campaignFixtures().getMunicipality() + return payload.create({ + collection: 'activity', + data: hookFilledCreateData<'activity'>({ + title: `C114 ${crypto.randomUUID().slice(0, 8)}`, + tags: ['Caminhada'], + status: 'confirmado', + startAt: new Date(Date.now() + 86_400_000).toISOString(), + municipality: municipality.id, + ...overrides, + }), + depth: 0, + overrideAccess: true, + }) + } + + const createConfig = async (calendarId: string) => + payload.create({ + collection: 'googleCalendarSync', + data: { calendarId }, + depth: 0, + overrideAccess: true, + }) + + it('creates the full mirror on the first pass (timed + all-day, municipality summary)', async () => { + const municipality = await campaignFixtures().getMunicipality() + const timed = await createActivity({ municipality: municipality.id, locality: 'Centro' }) + const allDay = await createActivity({ + municipality: municipality.id, + allDay: true, + startAt: '2026-08-10T03:00:00.000Z', + endAt: '2026-08-12T03:00:00.000Z', + }) + await createConfig(calendarA) + const store: GoogleRemoteEvent[] = [] + const client = createStubClient(store) + + const outcome = await withCredential(() => + runCampaignCalendarSync(payload, { reason: 'manual', client }), + ) + + expect(outcome.status).toBe('synced') + expect(outcome.created).toBe(2) + expect(outcome.updated).toBe(0) + expect(outcome.deleted).toBe(0) + + const timedEvent = store.find((event) => event.id === googleEventIdForActivity(timed.id)) + expect(timedEvent?.summary).toBe(`[${municipality.name}] ${timed.title}`) + expect(timedEvent?.location).toBe('Centro') + expect(timedEvent?.start).toEqual({ dateTime: expect.stringMatching(/-03:00$/) }) + + const allDayEvent = store.find((event) => event.id === googleEventIdForActivity(allDay.id)) + expect(allDayEvent?.start).toEqual({ date: '2026-08-10' }) + expect(allDayEvent?.end).toEqual({ date: '2026-08-13' }) + + const doc = await loadGoogleCalendarSyncConfig(payload) + expect(doc?.lastSuccessAt).toBeTruthy() + expect(doc?.lastError).toBeNull() + }) + + it('converges: a second pass with no changes touches nothing', async () => { + await createActivity() + await createConfig(calendarA) + const store: GoogleRemoteEvent[] = [] + const client = createStubClient(store) + + const first = await withCredential(() => + runCampaignCalendarSync(payload, { reason: 'manual', client }), + ) + expect(first.created).toBe(1) + + const second = await withCredential(() => + runCampaignCalendarSync(payload, { reason: 'manual', client }), + ) + expect(second).toMatchObject({ created: 0, updated: 0, deleted: 0, status: 'synced' }) + }) + + it('updates only the drifted event and ignores foreign calendar events', async () => { + const activity = await createActivity() + await createConfig(calendarA) + const store: GoogleRemoteEvent[] = [ + { id: 'foreign-manual-event', summary: 'Mantido pela conta da campanha' }, + ] + const client = createStubClient(store) + await withCredential(() => runCampaignCalendarSync(payload, { reason: 'manual', client })) + + // Title is immutable after creation (canonical slug rule) — drift a + // mutable mirrored field instead (locality is in the mirror surface). + await payload.update({ + collection: 'activity', + id: activity.id, + data: { locality: 'Centro (atualizado)' }, + depth: 0, + overrideAccess: true, + }) + + const outcome = await withCredential(() => + runCampaignCalendarSync(payload, { reason: 'manual', client }), + ) + expect(outcome.updated).toBe(1) + expect(outcome.deleted).toBe(0) + expect(store.find((event) => event.id === 'foreign-manual-event')).toBeTruthy() + expect( + store.find((event) => event.id === googleEventIdForActivity(activity.id))?.location, + ).toBe('Centro (atualizado)') + }) + + it('canceled activities leave Google: their events are deleted, none created', async () => { + const activity = await createActivity() + await createConfig(calendarA) + const store: GoogleRemoteEvent[] = [] + const client = createStubClient(store) + await withCredential(() => runCampaignCalendarSync(payload, { reason: 'manual', client })) + expect(store).toHaveLength(1) + + await payload.update({ + collection: 'activity', + id: activity.id, + data: { status: 'cancelado' }, + depth: 0, + overrideAccess: true, + }) + + const outcome = await withCredential(() => + runCampaignCalendarSync(payload, { reason: 'manual', client }), + ) + expect(outcome.deleted).toBe(1) + expect(store).toHaveLength(0) + }) + + it('hard-deleted activities leave no ghost in Google', async () => { + const activity = await createActivity() + await createConfig(calendarA) + const store: GoogleRemoteEvent[] = [] + const client = createStubClient(store) + await withCredential(() => runCampaignCalendarSync(payload, { reason: 'manual', client })) + expect(store).toHaveLength(1) + + await payload.delete({ collection: 'activity', id: activity.id, overrideAccess: true }) + + const outcome = await withCredential(() => + runCampaignCalendarSync(payload, { reason: 'manual', client }), + ) + expect(outcome.deleted).toBe(1) + expect(store).toHaveLength(0) + }) + + it('activities outside the window never reach Google', async () => { + await createActivity({ startAt: '2029-01-01T12:00:00.000Z' }) + await createConfig(calendarA) + const store: GoogleRemoteEvent[] = [] + const client = createStubClient(store) + + const outcome = await withCredential(() => + runCampaignCalendarSync(payload, { reason: 'manual', client }), + ) + expect(outcome.created).toBe(0) + expect(store).toHaveLength(0) + }) + + it('failures land in paused and the next success recovers to synced', async () => { + await createActivity() + await createConfig(calendarA) + + const failingClient: GoogleCalendarClient = { + ...createStubClient(), + listEvents: async () => { + throw new Error('Google fora do ar (simulado)') + }, + } + const failed = await withCredential(() => + runCampaignCalendarSync(payload, { reason: 'manual', client: failingClient }), + ) + expect(failed.status).toBe('paused') + + const doc = await loadGoogleCalendarSyncConfig(payload) + expect(doc?.lastErrorAt).toBeTruthy() + expect(doc?.lastError).toContain('Google fora do ar') + + // The pill reads with the credential present (same context the run had). + const failedView = await withCredential(() => readGoogleCalendarSyncView(payload)) + expect(failedView.status).toBe('paused') + + const store: GoogleRemoteEvent[] = [] + const recovered = await withCredential(() => + runCampaignCalendarSync(payload, { reason: 'manual', client: createStubClient(store) }), + ) + expect(recovered.status).toBe('synced') + expect(recovered.created).toBe(1) + const recoveredView = await withCredential(() => readGoogleCalendarSyncView(payload)) + expect(recoveredView.status).toBe('synced') + }) + + it('without a credential or calendar the engine is a no-op (fail-closed)', async () => { + await createActivity() + const store: GoogleRemoteEvent[] = [] + const client = createStubClient(store) + + // No config doc, no env key. + const outcome = await runCampaignCalendarSync(payload, { reason: 'manual', client }) + expect(outcome.status).toBe('not-configured') + expect(store).toHaveLength(0) + + await createConfig(calendarA) + // Config present but env key absent (outside withCredential). + const withoutKey = await runCampaignCalendarSync(payload, { reason: 'manual', client }) + expect(withoutKey.status).toBe('not-configured') + expect(store).toHaveLength(0) + }) + + it('changing the calendarId reconciles into the new calendar (D7 engine side)', async () => { + await createActivity() + await createConfig(calendarA) + const storeA: GoogleRemoteEvent[] = [] + await withCredential(() => + runCampaignCalendarSync(payload, { reason: 'manual', client: createStubClient(storeA) }), + ) + expect(storeA).toHaveLength(1) + + const config = await loadGoogleCalendarSyncConfig(payload) + await payload.update({ + collection: 'googleCalendarSync', + id: config!.id, + data: { calendarId: calendarB }, + depth: 0, + overrideAccess: true, + }) + + const storeB: GoogleRemoteEvent[] = [] + const outcome = await withCredential(() => + runCampaignCalendarSync(payload, { + reason: 'config-change', + client: createStubClient(storeB), + }), + ) + expect(outcome.created).toBe(1) + expect(storeB).toHaveLength(1) + // The abandoned calendar keeps its last mirror (documented, no retro-cleanup). + expect(storeA).toHaveLength(1) + }) +}) diff --git a/tests/unit/codebaseConventions.unit.spec.ts b/tests/unit/codebaseConventions.unit.spec.ts index 34587e54..7c27af90 100644 --- a/tests/unit/codebaseConventions.unit.spec.ts +++ b/tests/unit/codebaseConventions.unit.spec.ts @@ -87,6 +87,9 @@ describe('banned campaign terminology', () => { 'src/lib/projectionSheetParse.ts', // Fixture entry deliberately exercises legacy visits written pre-rename. 'tests/unit/recentVisits.unit.spec.ts', + // English prose ("one row in practice") trips the accent-tolerant + // /pra[çc]/ — genuine code comment, not the retired term. + 'src/collections/GoogleCalendarSync.ts', ], }, { @@ -400,6 +403,13 @@ describe('src/utilities top-level is pinned', () => { 'campaignAccess.ts', 'campaignActionContext.ts', 'campaignAuditFields.ts', + // C114 — Google Calendar mirror: thin Calendar API v3 transport (jose+fetch). + 'googleCalendarClient.ts', + // C114 — Google Calendar mirror: reconciliation engine + hook guards. + 'googleCalendarSync.ts', + // C114 — Google Calendar mirror: activity/config hooks (separate module so + // the engine is mockable at the import boundary in tests). + 'googleCalendarSyncHooks.ts', 'campaignAuth.ts', 'campaignBiometricsPrompt.ts', 'campaignColumnVisibilityCookie.ts', diff --git a/tests/unit/googleCalendarClient.unit.spec.ts b/tests/unit/googleCalendarClient.unit.spec.ts new file mode 100644 index 00000000..e77858f6 --- /dev/null +++ b/tests/unit/googleCalendarClient.unit.spec.ts @@ -0,0 +1,185 @@ +// @vitest-environment node + +import { exportPKCS8, exportSPKI, generateKeyPair, importSPKI, jwtVerify } from 'jose' +import { beforeAll, describe, expect, it } from 'vitest' + +import { + buildServiceAccountAssertion, + createGoogleCalendarClient, + GOOGLE_CALENDAR_SCOPE, + GoogleCalendarApiError, + type FetchLike, + type GoogleCalendarCredentials, +} from '@/utilities/googleCalendarClient' + +describe('buildServiceAccountAssertion', () => { + let credentials: GoogleCalendarCredentials + let publicKeyPem: string + + beforeAll(async () => { + const { publicKey, privateKey } = await generateKeyPair('RS256', { extractable: true }) + credentials = { + clientEmail: 'teqo-sa@projeto.iam.gserviceaccount.com', + privateKey: await exportPKCS8(privateKey), + } + publicKeyPem = await exportSPKI(publicKey) + }) + + it('signs an RS256 JWT with the service-account claims the token endpoint expects', async () => { + const assertion = await buildServiceAccountAssertion(credentials, 1_800_000_000) + const { payload } = await jwtVerify(assertion, await importSPKI(publicKeyPem, 'RS256')) + + expect(payload.iss).toBe(credentials.clientEmail) + expect(payload.aud).toBe('https://oauth2.googleapis.com/token') + expect(payload.scope).toBe(GOOGLE_CALENDAR_SCOPE) + expect(payload.iat).toBe(1_800_000_000) + expect((payload.exp as number) - (payload.iat as number)).toBe(3600) + }) +}) + +describe('createGoogleCalendarClient', () => { + let credentials: GoogleCalendarCredentials + + beforeAll(async () => { + // The token endpoint is stubbed, but the client still SIGNS the JWT + // assertion before posting — a real key keeps the tests honest. + const { privateKey } = await generateKeyPair('RS256', { extractable: true }) + credentials = { + clientEmail: 'teqo-sa@projeto.iam.gserviceaccount.com', + privateKey: await exportPKCS8(privateKey), + } + }) + + const calendarId = 'c_abc@group.calendar.google.com' + + /** Minimal fake transport: token endpoint + calendar REST in memory. */ + const stubTransport = (events: Array> = []) => { + let tokenRequests = 0 + const calls: Array<{ url: string; method: string; body?: string; auth?: string }> = [] + + const fetchImpl: FetchLike = (async (input: RequestInfo | URL, _init?: RequestInit) => { + const url = String(input) + const method = (_init?.method ?? 'GET').toUpperCase() + const body = typeof _init?.body === 'string' ? _init.body : undefined + const auth = (_init?.headers as Record | undefined)?.['Authorization'] + calls.push({ url, method, body, auth }) + + if (url === 'https://oauth2.googleapis.com/token') { + tokenRequests += 1 + return new Response( + JSON.stringify({ access_token: `token-${tokenRequests}`, expires_in: 3600 }), + { + status: 200, + headers: { 'Content-Type': 'application/json' }, + }, + ) + } + + if (url.includes('/events') && method === 'POST') { + const event = JSON.parse(body ?? '{}') as Record + events.push(event) + return new Response(JSON.stringify(event), { + status: 200, + headers: { 'Content-Type': 'application/json' }, + }) + } + if (url.includes('/events') && method === 'PUT') { + const event = JSON.parse(body ?? '{}') as Record + const index = events.findIndex((e) => e.id === event.id) + if (index >= 0) events[index] = event + return new Response(JSON.stringify(event), { + status: 200, + headers: { 'Content-Type': 'application/json' }, + }) + } + if (url.includes('/events') && method === 'DELETE') { + const eventId = url.split('/').pop() + const index = events.findIndex((e) => e.id === eventId) + if (index >= 0) events.splice(index, 1) + return new Response(null, { status: 204 }) + } + if (url.includes('/events')) { + return new Response(JSON.stringify({ items: events }), { + status: 200, + headers: { 'Content-Type': 'application/json' }, + }) + } + return new Response('not found', { status: 404 }) + }) as FetchLike + + return { fetchImpl, calls, events, getTokenRequests: () => tokenRequests } + } + + it('inserts with the deterministic id, bearer auth and JSON body', async () => { + const { fetchImpl, calls, events } = stubTransport() + const client = createGoogleCalendarClient(credentials, fetchImpl) + + await client.insertEvent(calendarId, { id: 'teqo1a', summary: 'S', description: 'D' }) + + expect(events).toHaveLength(1) + expect(events[0].id).toBe('teqo1a') + const insertCall = calls.find((call) => call.method === 'POST' && call.url.includes('/events')) + expect(insertCall?.url).toContain(`/calendars/${encodeURIComponent(calendarId)}/events`) + expect(insertCall?.auth).toBe('Bearer token-1') + }) + + it('lists with the window params and pagination', async () => { + const { fetchImpl, calls } = stubTransport() + const client = createGoogleCalendarClient(credentials, fetchImpl) + + await client.listEvents(calendarId, { + timeMin: '2026-01-01T00:00:00Z', + timeMax: '2026-12-31T00:00:00Z', + }) + + const listCall = calls.find((call) => call.method === 'GET') + expect(listCall?.url).toContain('timeMin=2026-01-01T00%3A00%3A00Z') + expect(listCall?.url).toContain('timeMax=2026-12-31T00%3A00%3A00Z') + expect(listCall?.url).toContain('maxResults=2500') + }) + + it('re-mints the token once on a 401 and retries the call', async () => { + let calendarCalls = 0 + const fetchImpl: FetchLike = (async (input: RequestInfo | URL, _init?: RequestInit) => { + const url = String(input) + if (url === 'https://oauth2.googleapis.com/token') { + return new Response(JSON.stringify({ access_token: 'fresh-token', expires_in: 3600 }), { + status: 200, + headers: { 'Content-Type': 'application/json' }, + }) + } + calendarCalls += 1 + if (calendarCalls === 1) { + return new Response('unauthorized', { status: 401 }) + } + return new Response(JSON.stringify({ items: [] }), { + status: 200, + headers: { 'Content-Type': 'application/json' }, + }) + }) as FetchLike + + const client = createGoogleCalendarClient(credentials, fetchImpl) + const events = await client.listEvents(calendarId, { + timeMin: '2026-01-01T00:00:00Z', + timeMax: '2026-12-31T00:00:00Z', + }) + + expect(events).toEqual([]) + expect(calendarCalls).toBe(2) + }) + + it('throws a typed error on a non-401 failure without leaking the body', async () => { + const fetchImpl: FetchLike = (async (input: RequestInfo | URL) => { + const url = String(input) + if (url === 'https://oauth2.googleapis.com/token') { + return new Response(JSON.stringify({ error: 'invalid_grant' }), { status: 403 }) + } + return new Response('ok', { status: 200 }) + }) as FetchLike + + const client = createGoogleCalendarClient(credentials, fetchImpl) + await expect( + client.listEvents(calendarId, { timeMin: 'a', timeMax: 'b' }), + ).rejects.toBeInstanceOf(GoogleCalendarApiError) + }) +}) diff --git a/tests/unit/googleCalendarEventMapping.unit.spec.ts b/tests/unit/googleCalendarEventMapping.unit.spec.ts new file mode 100644 index 00000000..d633f7fa --- /dev/null +++ b/tests/unit/googleCalendarEventMapping.unit.spec.ts @@ -0,0 +1,183 @@ +import { describe, expect, it } from 'vitest' + +import { + buildGoogleCalendarDescription, + buildGoogleEventPayload, + decodeGoogleEventActivityId, + googleEventContentEquals, + googleEventIdForActivity, + type GoogleRemoteEvent, +} from '@/lib/googleCalendarEventMapping' + +type MappingSource = Parameters[0] + +const baseActivity = (overrides: Partial = {}): MappingSource => + ({ + id: 42, + title: 'Caminhada no Cabula', + startAt: '2026-08-15T19:00:00.000Z', + endAt: '2026-08-15T22:00:00.000Z', + allDay: false, + locality: 'Centro', + tags: ['Caminhada'], + deputyPresent: true, + ...overrides, + }) as MappingSource + +describe('googleEventIdForActivity / decodeGoogleEventActivityId', () => { + it('encodes ids in base32hex under the teqo prefix (charset do Google)', () => { + expect(googleEventIdForActivity(0)).toBe('teqo0') + expect(googleEventIdForActivity(1)).toBe('teqo1') + expect(googleEventIdForActivity(31)).toBe('teqov') + expect(googleEventIdForActivity(32)).toBe('teqo10') + expect(googleEventIdForActivity(1000)).toBe('teqov8') + }) + + it('round-trips any positive id', () => { + for (const id of [1, 7, 42, 255, 1024, 123_456, 999_999_999]) { + const encoded = googleEventIdForActivity(id) + expect(encoded).toMatch(/^teqo[a-v0-9]+$/) + expect(decodeGoogleEventActivityId(encoded)).toBe(id) + } + }) + + it('rejects foreign and malformed ids', () => { + expect(decodeGoogleEventActivityId('meu-evento-42')).toBeNull() + expect(decodeGoogleEventActivityId('teqo')).toBeNull() + expect(decodeGoogleEventActivityId('teqoz')).toBeNull() + expect(decodeGoogleEventActivityId('teqoA1')).toBeNull() + }) + + it('rejects invalid activity ids', () => { + expect(() => googleEventIdForActivity(-1)).toThrow() + expect(() => googleEventIdForActivity(1.5)).toThrow() + }) +}) + +describe('buildGoogleEventPayload', () => { + it('maps a timed activity to RFC3339 dateTimes with the fixed -03:00 offset', () => { + const payload = buildGoogleEventPayload(baseActivity(), 'Salvador') + + expect(payload.id).toBe('teqo1a') + expect(payload.summary).toBe('[Salvador] Caminhada no Cabula') + expect(payload.start).toEqual({ dateTime: '2026-08-15T16:00:00-03:00' }) + expect(payload.end).toEqual({ dateTime: '2026-08-15T19:00:00-03:00' }) + expect(payload.location).toBe('Centro') + }) + + it('builds the description without unnecessary PII', () => { + const payload = buildGoogleEventPayload(baseActivity(), 'Salvador') + + expect(payload.description).toContain('Município: Salvador') + expect(payload.description).toContain('Local: Centro') + expect(payload.description).toContain('Tags: Caminhada') + expect(payload.description).toContain('Deputado presente') + }) + + it('synthesizes end = start + 1h when the activity has no endAt (Google exige end > start)', () => { + const payload = buildGoogleEventPayload( + baseActivity({ endAt: null, startAt: '2026-08-15T19:00:00.000Z' }), + 'Salvador', + ) + + expect(payload.start).toEqual({ dateTime: '2026-08-15T16:00:00-03:00' }) + expect(payload.end).toEqual({ dateTime: '2026-08-15T17:00:00-03:00' }) + }) + + it('maps all-day activities to civil dates with the exclusive end (C104 convention)', () => { + const payload = buildGoogleEventPayload( + baseActivity({ + allDay: true, + startAt: '2026-08-10T03:00:00.000Z', + endAt: '2026-08-12T03:00:00.000Z', + locality: '', + }), + 'Feira de Santana', + ) + + expect(payload.start).toEqual({ date: '2026-08-10' }) + expect(payload.end).toEqual({ date: '2026-08-13' }) + }) + + it('falls back to the municipality name as the location when locality is empty', () => { + const payload = buildGoogleEventPayload(baseActivity({ locality: '' }), 'Salvador') + expect(payload.location).toBe('Salvador') + }) + + it('omits location entirely when neither locality nor municipality name exist', () => { + const payload = buildGoogleEventPayload(baseActivity({ locality: '' })) + expect(payload.location).toBeUndefined() + expect(payload.summary).toBe('Caminhada no Cabula') + }) +}) + +describe('buildGoogleCalendarDescription', () => { + it('is empty when there is nothing to say', () => { + expect(buildGoogleCalendarDescription({ locality: '', tags: [], deputyPresent: false })).toBe( + '', + ) + }) +}) + +describe('googleEventContentEquals', () => { + const payload = buildGoogleEventPayload(baseActivity(), 'Salvador') + + it('treats the same instant under a different timezone echo as equal', () => { + const remote: GoogleRemoteEvent = { + id: payload.id, + summary: payload.summary, + description: payload.description, + location: payload.location, + // Calendar in America/Sao_Paulo echoes the same instant with its own offset. + start: { dateTime: '2026-08-15T16:00:00-03:00', timeZone: 'America/Sao_Paulo' }, + end: { dateTime: '2026-08-15T19:00:00-03:00', timeZone: 'America/Sao_Paulo' }, + } + expect(googleEventContentEquals(remote, payload)).toBe(true) + }) + + it('flags content drift', () => { + const remote: GoogleRemoteEvent = { ...payload, summary: '[Salvador] Outro título' } + expect(googleEventContentEquals(remote, payload)).toBe(false) + }) + + it('flags a moved instant', () => { + const remote: GoogleRemoteEvent = { + ...payload, + start: { dateTime: '2026-08-16T16:00:00-03:00' }, + end: { dateTime: '2026-08-16T19:00:00-03:00' }, + } + expect(googleEventContentEquals(remote, payload)).toBe(false) + }) + + it('compares all-day events by civil date', () => { + const allDayPayload = buildGoogleEventPayload( + baseActivity({ + allDay: true, + startAt: '2026-08-10T03:00:00.000Z', + endAt: '2026-08-12T03:00:00.000Z', + }), + 'Feira de Santana', + ) + const remote: GoogleRemoteEvent = { + id: allDayPayload.id, + summary: allDayPayload.summary, + description: allDayPayload.description, + location: allDayPayload.location, + start: { date: '2026-08-10' }, + end: { date: '2026-08-13' }, + } + expect(googleEventContentEquals(remote, allDayPayload)).toBe(true) + }) + + it('treats a missing remote description as empty (not drift)', () => { + const remote: GoogleRemoteEvent = { + id: payload.id, + summary: payload.summary, + start: payload.start, + end: payload.end, + } + expect( + googleEventContentEquals(remote, { ...payload, description: '', location: undefined }), + ).toBe(true) + }) +}) diff --git a/tests/unit/googleCalendarLink.unit.spec.ts b/tests/unit/googleCalendarLink.unit.spec.ts new file mode 100644 index 00000000..6acde48c --- /dev/null +++ b/tests/unit/googleCalendarLink.unit.spec.ts @@ -0,0 +1,30 @@ +import { describe, expect, it } from 'vitest' + +import { + buildGoogleCalendarAddLink, + buildGoogleCalendarPublicIcalUrl, + buildGoogleCalendarWebcalUrl, +} from '@/lib/googleCalendarLink' + +describe('googleCalendarLink', () => { + const calendarId = 'c_abc123@group.calendar.google.com' + + it('builds the webcal subscription URL Apple/Outlook consume', () => { + expect(buildGoogleCalendarWebcalUrl(calendarId)).toBe( + 'webcal://calendar.google.com/calendar/ical/c_abc123%40group.calendar.google.com/public/basic.ics', + ) + }) + + it('builds the Google add-by-URL link with the webcal URL as cid', () => { + const link = buildGoogleCalendarAddLink(calendarId) + expect(link).toBe( + 'https://calendar.google.com/calendar/r?cid=webcal%3A%2F%2Fcalendar.google.com%2Fcalendar%2Fical%2Fc_abc123%2540group.calendar.google.com%2Fpublic%2Fbasic.ics', + ) + }) + + it('builds the public HTTPS iCal URL of the same calendar', () => { + expect(buildGoogleCalendarPublicIcalUrl(calendarId)).toBe( + 'https://calendar.google.com/calendar/ical/c_abc123%40group.calendar.google.com/public/basic.ics', + ) + }) +}) diff --git a/tests/unit/googleCalendarSync.unit.spec.ts b/tests/unit/googleCalendarSync.unit.spec.ts new file mode 100644 index 00000000..569786bf --- /dev/null +++ b/tests/unit/googleCalendarSync.unit.spec.ts @@ -0,0 +1,240 @@ +import { afterEach, describe, expect, it } from 'vitest' + +import type { Activity, GoogleCalendarSync as GoogleCalendarSyncDoc } from '@/payload-types' + +import { + deriveGoogleCalendarSyncStatus, + GOOGLE_CALENDAR_SERVICE_ACCOUNT_KEY_ENV, + readGoogleServiceAccountCredentials, + shouldSyncActivityOperation, + shouldSyncConfigChange, +} from '@/utilities/googleCalendarSync' + +describe('deriveGoogleCalendarSyncStatus', () => { + const configured = { hasCredential: true, calendarId: 'c_abc@group.calendar.google.com' } + + it('is not-configured without credential or calendar id (fail-closed)', () => { + expect(deriveGoogleCalendarSyncStatus({ hasCredential: false, calendarId: null })).toBe( + 'not-configured', + ) + expect(deriveGoogleCalendarSyncStatus({ hasCredential: true, calendarId: null })).toBe( + 'not-configured', + ) + expect(deriveGoogleCalendarSyncStatus({ hasCredential: false, calendarId: 'x' })).toBe( + 'not-configured', + ) + }) + + it('is disabled while disabledAt is set', () => { + expect( + deriveGoogleCalendarSyncStatus({ ...configured, disabledAt: '2026-08-11T10:00:00.000Z' }), + ).toBe('disabled') + }) + + it('is paused while the last attempt failed', () => { + expect( + deriveGoogleCalendarSyncStatus({ + ...configured, + lastSuccessAt: '2026-08-11T09:00:00.000Z', + lastErrorAt: '2026-08-11T10:00:00.000Z', + }), + ).toBe('paused') + expect( + deriveGoogleCalendarSyncStatus({ + ...configured, + lastErrorAt: '2026-08-11T10:00:00.000Z', + }), + ).toBe('paused') + }) + + it('recovers to synced after the last success', () => { + expect( + deriveGoogleCalendarSyncStatus({ + ...configured, + lastSuccessAt: '2026-08-11T10:30:00.000Z', + lastErrorAt: '2026-08-11T10:00:00.000Z', + }), + ).toBe('synced') + expect(deriveGoogleCalendarSyncStatus(configured)).toBe('synced') + }) +}) + +describe('readGoogleServiceAccountCredentials', () => { + const originalKey = process.env[GOOGLE_CALENDAR_SERVICE_ACCOUNT_KEY_ENV] + + afterEach(() => { + if (originalKey === undefined) { + delete process.env[GOOGLE_CALENDAR_SERVICE_ACCOUNT_KEY_ENV] + } else { + process.env[GOOGLE_CALENDAR_SERVICE_ACCOUNT_KEY_ENV] = originalKey + } + }) + + it('parses the base64 JSON key into credentials', () => { + const payload = { + client_email: 'teqo-sa@projeto.iam.gserviceaccount.com', + private_key: '-----BEGIN PRIVATE KEY-----\nMOCK\n-----END PRIVATE KEY-----\n', + } + process.env[GOOGLE_CALENDAR_SERVICE_ACCOUNT_KEY_ENV] = Buffer.from( + JSON.stringify(payload), + 'utf8', + ).toString('base64') + + expect(readGoogleServiceAccountCredentials()).toEqual({ + clientEmail: payload.client_email, + privateKey: payload.private_key, + }) + }) + + it('returns null when the env is absent or malformed (fail-closed)', () => { + delete process.env[GOOGLE_CALENDAR_SERVICE_ACCOUNT_KEY_ENV] + expect(readGoogleServiceAccountCredentials()).toBeNull() + + process.env[GOOGLE_CALENDAR_SERVICE_ACCOUNT_KEY_ENV] = 'not-base64-json' + expect(readGoogleServiceAccountCredentials()).toBeNull() + + process.env[GOOGLE_CALENDAR_SERVICE_ACCOUNT_KEY_ENV] = Buffer.from( + JSON.stringify({ client_email: 'x' }), + 'utf8', + ).toString('base64') + expect(readGoogleServiceAccountCredentials()).toBeNull() + }) +}) + +describe('shouldSyncActivityOperation', () => { + const activity = (overrides: Record = {}) => + ({ id: 1, title: 'Caminhada', status: 'confirmado', ...overrides }) as unknown as Activity + + it('always syncs on create and delete', () => { + expect(shouldSyncActivityOperation({ operation: 'create', doc: activity() })).toBe(true) + expect(shouldSyncActivityOperation({ operation: 'delete', doc: activity() })).toBe(true) + }) + + it('skips updates that only touch non-mirrored fields (tasks, derived state)', () => { + expect( + shouldSyncActivityOperation({ + operation: 'update', + doc: activity({ tasks: [{ title: 'Novo' }], taskDoneCount: 1 }), + previousDoc: activity({ tasks: [{ title: 'Antigo' }], taskDoneCount: 0 }), + }), + ).toBe(false) + }) + + it.each([ + 'title', + 'status', + 'startAt', + 'endAt', + 'allDay', + 'locality', + 'tags', + 'deputyPresent', + ] as const)('syncs when the mirrored field %s changed', (field) => { + expect( + shouldSyncActivityOperation({ + operation: 'update', + doc: activity({ [field]: 'changed' }), + previousDoc: activity({ [field]: 'original' }), + }), + ).toBe(true) + }) + + it('syncs when the municipality relationship changed', () => { + expect( + shouldSyncActivityOperation({ + operation: 'update', + doc: activity({ municipality: 2 }), + previousDoc: activity({ municipality: 1 }), + }), + ).toBe(true) + }) + + it('does NOT sync when only non-mirrored fields changed (tasks, description, updates, result)', () => { + expect( + shouldSyncActivityOperation({ + operation: 'update', + doc: activity({ description: 'novo texto', tasks: [{ title: 'Nova tarefa' }] }), + previousDoc: activity({ description: 'texto antigo', tasks: [{ title: 'Antiga' }] }), + }), + ).toBe(false) + }) + + it('normalizes populated vs depth-0 relationship shapes (afterChange doc vs previousDoc)', () => { + // The afterChange `doc` carries the populated municipality object; the + // `previousDoc` is loaded depth-0 (numeric id) — same id must NOT change. + expect( + shouldSyncActivityOperation({ + operation: 'update', + doc: activity({ municipality: { id: 7, name: 'Salvador' } }), + previousDoc: activity({ municipality: 7 }), + }), + ).toBe(false) + expect( + shouldSyncActivityOperation({ + operation: 'update', + doc: activity({ tags: ['B', 'A'] }), + previousDoc: activity({ tags: ['A', 'B'] }), + }), + ).toBe(false) + expect( + shouldSyncActivityOperation({ + operation: 'update', + doc: activity({ tags: ['A'] }), + previousDoc: activity({ tags: ['B'] }), + }), + ).toBe(true) + }) +}) + +describe('shouldSyncConfigChange', () => { + const config = (overrides: Record = {}) => + ({ + id: 1, + calendarId: 'c_a@group.calendar.google.com', + ...overrides, + }) as unknown as GoogleCalendarSyncDoc + + it('always syncs on create', () => { + expect(shouldSyncConfigChange({ operation: 'create', doc: config() })).toBe(true) + }) + + it('skips state-only writes (engine recording outcomes) — no re-entry loop', () => { + expect( + shouldSyncConfigChange({ + operation: 'update', + doc: config({ lastSyncedAt: '2026-08-11T10:00:00.000Z', lastSuccessAt: 'x' }), + previousDoc: config(), + }), + ).toBe(false) + }) + + it('skips disabling (no sync needed while off)', () => { + expect( + shouldSyncConfigChange({ + operation: 'update', + doc: config({ disabledAt: '2026-08-11T10:00:00.000Z' }), + previousDoc: config(), + }), + ).toBe(false) + }) + + it('syncs when the calendarId changed (reconcile into the new calendar)', () => { + expect( + shouldSyncConfigChange({ + operation: 'update', + doc: config({ calendarId: 'c_b@group.calendar.google.com' }), + previousDoc: config(), + }), + ).toBe(true) + }) + + it('syncs when re-enabled (disabledAt cleared)', () => { + expect( + shouldSyncConfigChange({ + operation: 'update', + doc: config(), + previousDoc: config({ disabledAt: '2026-08-11T10:00:00.000Z' }), + }), + ).toBe(true) + }) +}) diff --git a/tests/unit/googleCalendarSyncHooks.unit.spec.ts b/tests/unit/googleCalendarSyncHooks.unit.spec.ts new file mode 100644 index 00000000..55654d0b --- /dev/null +++ b/tests/unit/googleCalendarSyncHooks.unit.spec.ts @@ -0,0 +1,146 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' + +import type { Activity, GoogleCalendarSync as GoogleCalendarSyncDoc } from '@/payload-types' +import type { PayloadRequest } from 'payload' + +/** + * Hook → engine wiring (the guards themselves are unit-tested in + * `googleCalendarSync.unit.spec.ts`): the hooks must call the engine exactly + * when the guard says so, and NEVER throw into the write path. The hooks live + * in their own module (`googleCalendarSyncHooks.ts`), so mocking the engine + * at the import boundary reliably intercepts the wiring. + */ +vi.mock('@/utilities/googleCalendarSync', async (importOriginal) => { + const original = await importOriginal() + return { ...original, runCampaignCalendarSync: vi.fn() } +}) + +import { runCampaignCalendarSync } from '@/utilities/googleCalendarSync' +import { + activityGoogleCalendarSyncHook, + googleCalendarSyncConfigHook, +} from '@/utilities/googleCalendarSyncHooks' + +const mockedRun = vi.mocked(runCampaignCalendarSync) + +const fakeReq = { payload: {} } as unknown as PayloadRequest +const activity = (overrides: Record = {}) => + ({ id: 1, title: 'Caminhada', status: 'confirmado', ...overrides }) as unknown as Activity +const config = (overrides: Record = {}) => + ({ + id: 1, + calendarId: 'c_a@group.calendar.google.com', + ...overrides, + }) as unknown as GoogleCalendarSyncDoc + +const settledOutcome = { status: 'synced' as const, created: 0, updated: 0, deleted: 0, at: '' } + +describe('activityGoogleCalendarSyncHook wiring', () => { + beforeEach(() => { + mockedRun.mockReset() + mockedRun.mockResolvedValue(settledOutcome) + }) + + it('calls the engine on create, delete and relevant updates; skips task-only updates', async () => { + await activityGoogleCalendarSyncHook({ + doc: activity(), + operation: 'create', + req: fakeReq, + }) + expect(mockedRun).toHaveBeenCalledTimes(1) + + await activityGoogleCalendarSyncHook({ + doc: activity({ tasks: [{ title: 'Novo' }] }), + previousDoc: activity({ tasks: [{ title: 'Antigo' }] }), + operation: 'update', + req: fakeReq, + }) + expect(mockedRun).toHaveBeenCalledTimes(1) + + await activityGoogleCalendarSyncHook({ + doc: activity({ title: 'Caminhada (remarcada)' }), + previousDoc: activity(), + operation: 'update', + req: fakeReq, + }) + expect(mockedRun).toHaveBeenCalledTimes(2) + + // Delete: afterDelete hooks carry no `operation` — absence means delete. + await activityGoogleCalendarSyncHook({ + doc: activity(), + req: fakeReq, + }) + expect(mockedRun).toHaveBeenCalledTimes(3) + }) + + it('never throws into the activity write path', async () => { + mockedRun.mockRejectedValue(new Error('boom')) + await expect( + activityGoogleCalendarSyncHook({ + doc: activity(), + operation: 'create', + req: fakeReq, + }), + ).resolves.toBeDefined() + }) +}) + +describe('googleCalendarSyncConfigHook wiring', () => { + beforeEach(() => { + mockedRun.mockReset() + mockedRun.mockResolvedValue(settledOutcome) + }) + + it('calls the engine on create, calendarId change and re-enable; skips state writes and disable', async () => { + await googleCalendarSyncConfigHook({ + doc: config(), + operation: 'create', + req: fakeReq, + }) + expect(mockedRun).toHaveBeenCalledTimes(1) + + await googleCalendarSyncConfigHook({ + doc: config({ lastSyncedAt: '2026-08-11T10:00:00.000Z' }), + previousDoc: config(), + operation: 'update', + req: fakeReq, + }) + expect(mockedRun).toHaveBeenCalledTimes(1) + + await googleCalendarSyncConfigHook({ + doc: config({ disabledAt: '2026-08-11T10:00:00.000Z' }), + previousDoc: config(), + operation: 'update', + req: fakeReq, + }) + expect(mockedRun).toHaveBeenCalledTimes(1) + + await googleCalendarSyncConfigHook({ + doc: config({ calendarId: 'c_b@group.calendar.google.com' }), + previousDoc: config(), + operation: 'update', + req: fakeReq, + }) + expect(mockedRun).toHaveBeenCalledTimes(2) + + await googleCalendarSyncConfigHook({ + doc: config(), + previousDoc: config({ disabledAt: '2026-08-11T10:00:00.000Z' }), + operation: 'update', + req: fakeReq, + }) + expect(mockedRun).toHaveBeenCalledTimes(3) + }) + + it('never throws into admin writes', async () => { + mockedRun.mockRejectedValue(new Error('boom')) + await expect( + googleCalendarSyncConfigHook({ + doc: config({ calendarId: 'c_b@group.calendar.google.com' }), + previousDoc: config(), + operation: 'update', + req: fakeReq, + }), + ).resolves.toBeDefined() + }) +})