feat(orgs): organization invitations and members, slice B (#37) #108

Merged
amiryahaya merged 22 commits from feat/37-org-invitations into main 2026-10-27 15:05:10 +01:00
Owner

Slice B of #37 (does not close it). A tour agent company's Organization Administrator invites its own staff by email; the invited person accepts after signing in; the administrator lists, re-roles and removes the company's partner-portal users. Builds on slice A (#107). Plan: docs/superpowers/plans/2026-10-27-org-invitations.md; rulings and limits: ADR 0021 "Slice B" (o)-(t).

What is in it

  • Migration 0026 organization_invitations: SHA-256 token hash (secret column, never audited), guarded new -> used|expired|revoked, one open invitation per (organization, email), 7-day default expiry.
  • Roles: Business Entry Clerk and Business Portal Payment Clerk as system roles, with exactly the legacy permission lists (pinned by a test).
  • Invite / list / revoke (/v1/orgs/{id}/invitations): business_portal.users.invite, Idempotency-Key required; only an Organization Administrator may grant that role; at most 50 open; re-invite revokes the old link; email queued in the same transaction (notify.invitation); the link carries the token in a URL fragment; the response never contains the token.
  • Accept (POST /v1/me/invitations/accept, token in the body): verified email, exact email match, active organization, and the inviter must still be an Organization Administrator; every refusal is one identical 404. Roles granted are the built-in system roles only.
  • Members (GET /v1/orgs/{id}/members, PUT .../{user_id}/roles, DELETE .../{user_id}): last Organization Administrator can never be removed or demoted (organization row FOR NO KEY UPDATE, then count).
  • orgs.invitations_expire job (every 5 min); notify.invitation handler registered in the worker with SMTP.
  • Shared-code changes: dead jobs drop a top-level payload key token (scalar-safe); rbac/pgstore gets HasRoleTx and GrantSystemRoleTx (system roles only); slice A's approval now grants through GrantSystemRoleTx (the name-only GrantRoleTx is deleted).
  • OpenAPI: 7 operations + live contract calls; CLAUDE.md "Organizations" and ADR 0021 extended.

Verification

  • go test ./... -race -count=1 green, make lint 0 issues, make contract ok, clients (TS/Dart) generate.
  • make cover: 90.8% (minimum 80%), run after the last code change.
  • Review: per task spec + quality (sonnet/opus) with fix rounds, then the dual-pass whole-branch review: Pass 1 (ponytail) no Must fix; Pass 2 (opus) found one Important (a removed administrator's open invitation stayed valid) which is fixed with tests; re-review and Pass 1 re-check clean. Not done (ruled, recorded): shared lock-wait helper and shared function types refactors (~-50 lines).

Deploy notes

Run migration 0026 (it re-applies system roles on KWS_MODE=migrate). Needs partner_portal_url (https) set, SMTP on API and worker, and the worker running (invitation emails, expiry). Without SMTP, inviting returns 503.

[verify] with the owner

7-day expiry (legacy 2); clerks hold eWallet top-up (legacy lists); only administrators grant Organization Administrator; already-member = 409; removal deletes the membership; the registrant is an ordinary member after approval.

Known limits

No staff-on-behalf invitations, custom organization roles, member email change, switch-organization endpoint or removal notice; no per-organization send cap (#62); the 50-open cap is soft under racing invites; accept timing differs slightly between 404 cases; a repeat accept is 404 once the inviter loses administrator; concurrent removal of one user from two organizations can leave no default membership.

🤖 Generated with Claude Code

Slice B of #37 (does not close it). A tour agent company's Organization Administrator invites its own staff by email; the invited person accepts after signing in; the administrator lists, re-roles and removes the company's partner-portal users. Builds on slice A (#107). Plan: `docs/superpowers/plans/2026-10-27-org-invitations.md`; rulings and limits: ADR 0021 "Slice B" (o)-(t). ## What is in it - **Migration 0026** `organization_invitations`: SHA-256 token hash (secret column, never audited), guarded `new -> used|expired|revoked`, one open invitation per (organization, email), 7-day default expiry. - **Roles:** `Business Entry Clerk` and `Business Portal Payment Clerk` as system roles, with exactly the legacy permission lists (pinned by a test). - **Invite / list / revoke** (`/v1/orgs/{id}/invitations`): `business_portal.users.invite`, `Idempotency-Key` required; only an Organization Administrator may grant that role; at most 50 open; re-invite revokes the old link; email queued in the same transaction (`notify.invitation`); the link carries the token in a URL fragment; the response never contains the token. - **Accept** (`POST /v1/me/invitations/accept`, token in the body): verified email, exact email match, active organization, and the inviter must still be an Organization Administrator; every refusal is one identical 404. Roles granted are the built-in system roles only. - **Members** (`GET /v1/orgs/{id}/members`, `PUT .../{user_id}/roles`, `DELETE .../{user_id}`): last Organization Administrator can never be removed or demoted (organization row `FOR NO KEY UPDATE`, then count). - `orgs.invitations_expire` job (every 5 min); `notify.invitation` handler registered in the worker with SMTP. - Shared-code changes: dead jobs drop a top-level payload key `token` (scalar-safe); `rbac/pgstore` gets `HasRoleTx` and `GrantSystemRoleTx` (system roles only); slice A's approval now grants through `GrantSystemRoleTx` (the name-only `GrantRoleTx` is deleted). - OpenAPI: 7 operations + live contract calls; CLAUDE.md "Organizations" and ADR 0021 extended. ## Verification - `go test ./... -race -count=1` green, `make lint` 0 issues, `make contract` ok, clients (TS/Dart) generate. - **`make cover`: 90.8%** (minimum 80%), run after the last code change. - Review: per task spec + quality (sonnet/opus) with fix rounds, then the dual-pass whole-branch review: Pass 1 (ponytail) no Must fix; Pass 2 (opus) found one Important (a removed administrator's open invitation stayed valid) which is fixed with tests; re-review and Pass 1 re-check clean. Not done (ruled, recorded): shared lock-wait helper and shared function types refactors (~-50 lines). ## Deploy notes Run migration 0026 (it re-applies system roles on `KWS_MODE=migrate`). Needs `partner_portal_url` (https) set, SMTP on **API and worker**, and the worker running (invitation emails, expiry). Without SMTP, inviting returns 503. ## [verify] with the owner 7-day expiry (legacy 2); clerks hold eWallet top-up (legacy lists); only administrators grant Organization Administrator; already-member = 409; removal deletes the membership; the registrant is an ordinary member after approval. ## Known limits No staff-on-behalf invitations, custom organization roles, member email change, switch-organization endpoint or removal notice; no per-organization send cap (#62); the 50-open cap is soft under racing invites; accept timing differs slightly between 404 cases; a repeat accept is 404 once the inviter loses administrator; concurrent removal of one user from two organizations can leave no default membership. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A custom organization role with the same name is never picked; GrantRole
and GrantRoleTx keep their behaviour.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
POST /v1/me/invitations/accept: verified email (403 first), token by hash
FOR UPDATE, organization FOR SHARE and active, email match; every refusal
is one 404. Grants the invitation's system roles (merging into an existing
membership), marks it used, invalidates the user's access after commit.
The same user again replays 200. MarkUsed refuses an overdue invitation.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Members service (List, SetRoles, Remove) under the organization row's
FOR NO KEY UPDATE lock: only an Organization Administrator adds or takes
away that role, and no change leaves the organization without one (409
last_administrator). Removal promotes another default membership. Routes
GET/PUT/DELETE /v1/orgs/{id}/members... with users.view/update/delete.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Accept requires, under the organization's FOR SHARE, that the inviter
still holds the built-in Organization Administrator role there
(rbac/pgstore.HasRoleTx); otherwise the same 404. Member changes take the
organization FOR NO KEY UPDATE, so they cannot race the check.
Docs: CLAUDE.md (and the invitable-roles wording), ADR 0021 (r) and (s).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
- rbac/pgstore.GrantRoleTx (prefers an organization role of the same name)
  had no caller left: removed with its test; review.go names
  GrantSystemRoleTx.
- RevokeOpenByEmail and CountOpen map a lock or statement timeout to
  ErrBusy (503) instead of a 500.
- The invitation expiry job logs only when it expired something.
- OpenAPI: 413 on POST /v1/orgs/{id}/invitations and
  PUT /v1/orgs/{id}/members/{user_id}/roles.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
docs(orgs): approval names GrantSystemRoleTx; accept exceptions and dead-on-arrival invitations (#37)
All checks were successful
ci / changes (pull_request) Successful in 24s
ci / clients (pull_request) Successful in 2m12s
ci / api (pull_request) Successful in 10m57s
ci / ci-gate (pull_request) Successful in 3s
f8874d243b
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
primatekuntech/kws!108
No description provided.