From 3320928a6d06f0d3f2087d3cf74916d720e757d6 Mon Sep 17 00:00:00 2001 From: Felix Kotschenreuther Date: Mon, 9 Nov 2026 11:24:12 +0100 Subject: [PATCH 01/24] feat(dupes): interactive duplicate-person review (scan, review, export-finance) Adds the ct dupes command family: a detector with match classes and tiers, relationship-based suppression, an interactive review with dynamic and static group handling, a local audit log, and an Optigem finance export. Runs against protected envs only in a human terminal session. v1 opens the ChurchTools merge page instead of calling the merge API. --- CONTRIBUTING.md | 8 +- README.md | 12 +- docs/handbuch/dupes.md | 103 ++++++++++ docs/handbuch/index.md | 3 + docs/handbuch/mkdocs.yml | 1 + package.json | 2 +- scripts/seed-dupes-dev.ts | 245 ++++++++++++++++++++++++ src/commands/dupes.ts | 213 +++++++++++++++++++++ src/dupes/ct.ts | 272 +++++++++++++++++++++++++++ src/dupes/detector.ts | 321 ++++++++++++++++++++++++++++++++ src/dupes/finance.ts | 97 ++++++++++ src/dupes/guard.ts | 71 +++++++ src/dupes/memberships.ts | 97 ++++++++++ src/dupes/normalize.ts | 75 ++++++++ src/dupes/redact.ts | 38 ++++ src/dupes/review.ts | 298 +++++++++++++++++++++++++++++ src/dupes/store.ts | 140 ++++++++++++++ src/index.ts | 5 +- tests/dupes-data.test.ts | 164 ++++++++++++++++ tests/dupes-detector.test.ts | 117 ++++++++++++ tests/dupes-guard-store.test.ts | 141 ++++++++++++++ 21 files changed, 2414 insertions(+), 9 deletions(-) create mode 100644 docs/handbuch/dupes.md create mode 100644 scripts/seed-dupes-dev.ts create mode 100644 src/commands/dupes.ts create mode 100644 src/dupes/ct.ts create mode 100644 src/dupes/detector.ts create mode 100644 src/dupes/finance.ts create mode 100644 src/dupes/guard.ts create mode 100644 src/dupes/memberships.ts create mode 100644 src/dupes/normalize.ts create mode 100644 src/dupes/redact.ts create mode 100644 src/dupes/review.ts create mode 100644 src/dupes/store.ts create mode 100644 tests/dupes-data.test.ts create mode 100644 tests/dupes-detector.test.ts create mode 100644 tests/dupes-guard-store.test.ts diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index fc17be2..4dff737 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -28,9 +28,11 @@ export `CT_LOGINTOKEN` (and `CT_HOST`) instead. Never commit a token — see tool can have. Anything touching diffing needs a test that pins the rendered plan. - **A clean apply must round-trip to a no-op.** If applying a config and re-planning shows a diff, the resource's `managedFields` are wrong — that is a bug, not a quirk. -- **People are out of scope, permanently.** `assertNotPeople` guards every write path. - PRs that manage people will be declined regardless of quality; it is a boundary, not a - gap. +- **People are out of scope for the structure engine, permanently.** `assertNotPeople` + guards every engine write path. PRs that make `plan` / `apply` manage people will be + declined regardless of quality; it is a boundary, not a gap. The only code that touches + person records is `ct dupes` (`src/dupes/`, `src/commands/dupes.ts`): interactive, + operator-driven, with its own personal-data guard. Keep it that way. - **Live-API tests are opt-in, and writes are triple-gated.** Reads need `CT_LIVE=1`. Writes additionally need `CT_LIVE_WRITE=1` **and** `CT_LIVE_WRITE_HOST` set to exactly the authenticated host — a deliberate barrier against a live write landing on prod. diff --git a/README.md b/README.md index 9dbd397..33868b4 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,13 @@ group types and roles, permissions, auto-groups — as versioned desired-state code, and reconcile it against the ChurchTools API with Terraform-style **`plan` / `apply`**. -> **People are never managed.** This tool touches only the scaffold, and only -> resources that are _explicitly_ declared or adopted. Everything else is -> invisible: never shown, never changed, never proposed for deletion. +> **The structure commands never touch people.** `plan` / `apply` / `destroy` +> work only on the scaffold, and only on resources that are _explicitly_ declared +> or adopted. Everything else is invisible: never shown, never changed, never +> proposed for deletion. The one exception is the separate, interactive-only +> [`ct dupes`](docs/handbuch/dupes.md) command family, which helps an operator +> review duplicate person records. It never runs unattended and refuses to run +> in a non-interactive session against a protected environment. > **Frozen.** The TypeScript config DSL receives bugfixes only and is removed > in ct-cli 5.0. Its successor is `terraform-provider-churchtools`, an @@ -330,7 +334,7 @@ ct apply --env prod # protected env: type the env name to confirm - Protected environments: apply/destroy always require typed confirmation of the env name — `--auto-approve` / `--force` never bypass it. - Per-env state files are host-bound: `--env prod` can never touch a dev-bound state. -- People and memberships are never touched (a hard boundary in code). +- `plan` / `apply` / `destroy` never touch people or memberships (a hard boundary in code). Only `ct dupes` does, explicitly and interactively. - Backup/export before every `apply` and `destroy`. - Rate-limit + retry on API calls (writes are never blindly re-sent on 5xx). - Tokens live in the OS keychain / `.env`, never in git. diff --git a/docs/handbuch/dupes.md b/docs/handbuch/dupes.md new file mode 100644 index 0000000..097ab6f --- /dev/null +++ b/docs/handbuch/dupes.md @@ -0,0 +1,103 @@ +--- +sources_hash: 4d866bf0bcecebbc +title: Duplicate review (ct dupes) +sources: + - src/commands/dupes.ts + - src/dupes/detector.ts + - src/dupes/guard.ts + - src/dupes/store.ts + - src/dupes/review.ts + - src/dupes/finance.ts + - src/dupes/memberships.ts + - src/dupes/redact.ts +reviewed: 2026-11-09 +--- + +# Duplicate review (`ct dupes`) + +`ct dupes` helps an operator find and merge duplicate person records. It is the one part +of ct-cli that reads and writes person data, which is why it is **interactive only** and +sits behind a personal-data guard. The structure commands (`plan`, `apply`, `destroy`) +never touch people. + +Version 1 does **not** call ChurchTools' merge API. It prepares the merge, then opens +ChurchTools' own merge page in the browser and waits for you to confirm. + +## Commands + +| Command | What it does | +| ---------------------------------------- | ------------------------------------------------------------------------------------------------- | +| `ct dupes scan [--env]` | Loads non-archived persons and ChurchTools' own duplicate suggestions, scores pairs, stores them. | +| `ct dupes review [--limit N]` | One pair at a time: merge, "keine Dublette", skip or quit. | +| `ct dupes export-finance [--since DATE]` | Writes `optigem-merges-.csv` and `.json` for merges where both persons had an Optigem ID. | + +Common options: `-e, --env`, `--redact`, `--optigem-field ` (or +`CT_DUPES_OPTIGEM_FIELD`, default `optigemId`). `review` also takes `--merge-url