feat(admin): repair a tenant's sign-in, delete an archived tenant #42

Merged
sharang merged 2 commits from feat/tenant-purge into main 2026-09-26 11:00:15 +00:00
Owner

Found on staging (sharna): the Keycloak organization and user were deleted by hand. The registry kept the tenant (archived), so the name couldn't be reused, and setting it back to trial didn't recreate the sign-in.

POST /v1/admin/tenants/{id}/reprovision {email?}. The admin defaults to the tenant's first admin.

  • If the org is missing: CreateOrgAndInvite.
  • If the org exists: InviteMember(IT_ADMIN), or ResendInvite when the admin is already a member.
  • Then SyncTenantStatus.
  • Audit tenant.signin_reprovisioned. An archived tenant gets 409; an address that belongs to another tenant gets 409 email_taken.

DELETE /v1/admin/tenants/{id}?confirm=<slug>, archived tenants only:

  • Order: S3 document files → Keycloak (PurgeTenant: the org, plus the accounts among its members and the first admin whose tenant_id names this tenant, never someone else's) → DELETE FROM tenants (FKs cascade; audit_log is SET NULL).
  • A failure at any step stops before the DB delete.
  • The ERP customer stays. tenant.purged (tenant-less, so it survives) records the staff member, the slug, and counts of removed accounts and documents.

Tests: server (memory + Postgres) for invited/resent/invite-into-org, rejections, purge guards, Keycloak failure leaving the tenant intact, name reusable afterwards; a stub-Keycloak test that only this tenant's accounts are deleted; OpenAPI.

The staff UI is in portal#56. Merge this one first.

🤖 Generated with Claude Code

https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB

**Found on staging (sharna):** the Keycloak organization and user were deleted by hand. The registry kept the tenant (archived), so the name couldn't be reused, and setting it back to trial didn't recreate the sign-in. **`POST /v1/admin/tenants/{id}/reprovision`** `{email?}`. The admin defaults to the tenant's first admin. - If the org is missing: `CreateOrgAndInvite`. - If the org exists: `InviteMember(IT_ADMIN)`, or `ResendInvite` when the admin is already a member. - Then `SyncTenantStatus`. - Audit `tenant.signin_reprovisioned`. An archived tenant gets 409; an address that belongs to another tenant gets 409 `email_taken`. **`DELETE /v1/admin/tenants/{id}?confirm=<slug>`**, archived tenants only: - Order: S3 document files → Keycloak (`PurgeTenant`: the org, plus the accounts among its members and the first admin whose `tenant_id` names this tenant, never someone else's) → `DELETE FROM tenants` (FKs cascade; `audit_log` is SET NULL). - A failure at any step stops before the DB delete. - The ERP customer stays. `tenant.purged` (tenant-less, so it survives) records the staff member, the slug, and counts of removed accounts and documents. Tests: server (memory + Postgres) for invited/resent/invite-into-org, rejections, purge guards, Keycloak failure leaving the tenant intact, name reusable afterwards; a stub-Keycloak test that only this tenant's accounts are deleted; OpenAPI. The staff UI is in portal#56. Merge this one first. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
sharang added 1 commit 2026-09-26 10:07:51 +00:00
feat(admin): repair a tenant's sign-in, delete an archived tenant
ci / shared (pull_request) Successful in 17s
ci / test (pull_request) Failing after 5m34s
ci / image (pull_request) Skipped
6f3451272c
Found on staging: a workspace whose Keycloak organization and admin
were deleted by hand stayed in the registry. Its name could not be
used again, and setting it back to trial did not bring the sign-in
back, because nothing re-creates the Keycloak side.

- POST /v1/admin/tenants/{id}/reprovision creates the organization with
  the admin invited when it is missing, invites the admin into an
  existing one, or sends the invitation again; then syncs the status.
- DELETE /v1/admin/tenants/{id}?confirm=<slug> removes an archived
  tenant: document files, then the Keycloak organization and the
  accounts whose tenant_id names this tenant, then the tenant row, whose
  foreign keys take everything attached. The ERP customer and the audit
  trail stay; tenant.purged records the staff member. The name is free.
- Store.DeleteTenant (memory and Postgres), Adapter.PurgeTenant (HTTP
  and mock), OpenAPI, tests against both stores and a stub Keycloak.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
sharang added 1 commit 2026-09-26 10:23:40 +00:00
ci: rerun after runner disk cleanup
ci / shared (pull_request) Successful in 33s
ci / test (pull_request) Successful in 23m56s
ci / image (pull_request) Skipped
1a57025bcf
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
sharang merged commit e8dcd1d259 into main 2026-09-26 11:00:15 +00:00
Sign in to join this conversation.