سامانه صدور فاکتور و حسابداری
Plan enterprise licensing ELI-1 implementation
davoodmehraban89 committed
Sep 30, 2026 at 08:19 UTC
665c2d6e2a71026ad171e234786a4495e2211d0a
1 file changed
+206
docs/superpowers/plans/2026-09-30-enterprise-licensing-eli1.md
new
+206
@@ -0,0 +1,206 @@
1
+# Enterprise Licensing ELI-1 Implementation Plan
2
+
3
+> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
4
+
5
+**Goal:** Add hard named-user seat capacity and delegated organization administration to the existing organization/RBAC model without breaking existing tenants or accounting history.
6
+
7
+**Architecture:** Extend the current organization-linked license with `max_users` and future-safe `limits` metadata, enforce active named-user seats and legal-company capacity inside Postgres, and make `core/configure` the explicit delegated customer-admin capability. Preserve existing direct member permissions and owner semantics; ELI-2 roles and ELI-3 invitations remain separate later slices.
8
+
9
+**Tech Stack:** PostgreSQL 17 / Supabase RLS+RPC, vanilla JavaScript, Supabase JS, Node 22 invariant scripts, Playwright 1.55 Chromium.
10
+
11
+**Spec:** `docs/superpowers/specs/2026-09-30-enterprise-licensing-organization-identity-design.md`
12
+
13
+## Global Constraints
14
+
15
+- Existing tenant IDs, posted accounting history, organization ownership and direct member grants must not be rewritten or deleted.
16
+- Named-user seats count active `organization_members`, including the owner; `suspended` and `left` do not consume seats.
17
+- Existing tenants are grandfathered so `max_users >= current active-member count` after migration.
18
+- Seat/company limits are server-enforced; UI counters are informational only.
19
+- Customer administrators cannot change commercial license dates/status/modules/company/user capacity.
20
+- `core` is commercially always present, but `core/configure` is an explicit authorization grant for delegated organization administration.
21
+- A module administrator cannot grant another module or an unlicensed module.
22
+- All production SQL must be archived exactly under `supabase/migrations/` and `PROJECT_STATE.md` updated in the same durable implementation commit.
23
+- Existing accounting Phase 2–6, Golden Accounting Journey, modular-office, Slice-B and Real-DOM gates must remain green.
24
+
25
+## Review Focus
26
+
27
+- Concurrent activation of the last available seat: exactly one activation succeeds and the next is rejected atomically.
28
+- Reducing `max_users` or `max_companies` below current usage: server rejects without partial mutation.
29
+- Delegated `core/configure`: can manage tenant members/units but cannot mutate commercial license fields.
30
+- Re-activating a suspended/left member at capacity: rejected; suspending an active member immediately frees one seat without deleting identity/audit.
31
+- One legal company with many regions/units: only `companies` records consume company capacity; organization units never consume company capacity.
32
+
33
+---
34
+
35
+### Task 1: Database entitlement and delegated-admin contract
36
+
37
+**Files:**
38
+- Create: `supabase/migrations/20260930050000_enterprise_license_seats_delegated_admin.sql`
39
+- Create: `scripts/enterprise-license-check.mjs`
40
+- Modify: `.github/workflows/quality.yml`
41
+- Modify: `PROJECT_STATE.md`
42
+
43
+**Interfaces:**
44
+- Produces: `licenses.max_users integer`, `licenses.limits jsonb`, `private.organization_active_member_count(uuid) -> integer`, `private.organization_has_seat(uuid, uuid default null) -> boolean`, `public.admin_set_user_limit(uuid, integer) -> void`, updated `public.admin_set_company_limit(uuid, integer)`, updated `public.organization_add_member(uuid,text,uuid,text) -> uuid`, `public.organization_set_member_status(uuid,uuid,text) -> void`, and `core` as a valid `member_module_permissions.module_key`.
45
+- Produces authorization rule: owner OR `private.has_org_capability(org,'core','configure')` may manage organization members/units; only platform `is_admin()` may change commercial limits.
46
+- Consumes: existing `organizations`, `organization_members`, `member_module_permissions`, `licenses`, `records`, `private.has_org_capability`, `private.organization_has_module_entitlement`.
47
+
48
+- [ ] **Step 1: Write the failing invariant script**
49
+
50
+Create `scripts/enterprise-license-check.mjs` with assertions that the new migration contains: `max_users`, grandfathering via active-member count, `limits jsonb`, `core` in the permission constraint, seat enforcement on activation, organization-scoped company counting, `admin_set_user_limit`, delegated `core/configure` management, and audit capture for membership/status/permission changes. Assert `js/organization-rbac.js`, `js/admin.js`/`js/platform-modules.js`, `index.html` and the Quality workflow are wired to the new contract only after later tasks.
51
+
52
+- [ ] **Step 2: Run the new invariant script and verify RED**
53
+
54
+Run: `node scripts/enterprise-license-check.mjs`
55
+Expected: FAIL because the ELI-1 migration/UI contract does not yet exist.
56
+
57
+- [ ] **Step 3: Implement the additive SQL migration**
58
+
59
+In `supabase/migrations/20260930050000_enterprise_license_seats_delegated_admin.sql`:
60
+- add `licenses.max_users integer not null default 1` with a bounded positive check and grandfather every license to at least current active-member count;
61
+- add `licenses.limits jsonb not null default '{}'::jsonb` with object-type check;
62
+- replace the `member_module_permissions.module_key` check so `core` is valid while retaining all existing module keys;
63
+- add `private.organization_active_member_count(p_organization_id uuid) returns integer`;
64
+- add `private.organization_has_seat(p_organization_id uuid,p_excluding_member uuid default null) returns boolean`;
65
+- add a BEFORE INSERT/UPDATE membership trigger that only performs a seat check when the resulting row becomes active and prevents race-overbooking by locking the organization license row before counting;
66
+- add `public.admin_set_user_limit(target uuid,new_max_users integer)` guarded by `public.is_admin()` and reject values below current active usage;
67
+- replace `public.admin_set_company_limit` and `private/public` company-limit enforcement so counts and entitlement lookup use `organization_id`, not `owner_id`;
68
+- replace `public.organization_add_member` so owner or `core/configure` may add/reactivate a pre-existing Finora user and the seat trigger remains authoritative;
69
+- add `public.organization_set_member_status` for `active/suspended/left`, owner-protection and delegated administration;
70
+- allow owner or `core/configure` to manage organization units and non-owner membership; ordinary members remain denied;
71
+- update `organization_set_member_permission` so `core` is assignable only by owner or existing `core/configure`, while module configurators remain restricted to their own licensed module;
72
+- extend immutable audit capture to member status/unit/position and delegated permission changes.
73
+
74
+- [ ] **Step 4: Run static SQL-contract test**
75
+
76
+Run: `node scripts/enterprise-license-check.mjs`
77
+Expected: migration assertions PASS; UI assertions may remain intentionally failing until Tasks 2–3, so gate the script by explicit task-stage fixtures or implement only the DB assertions in this first commit.
78
+
79
+- [ ] **Step 5: Run existing non-browser gates**
80
+
81
+Run: `node scripts/syntax-check.mjs && node scripts/quality-check.mjs && node scripts/phase2-check.mjs && node scripts/phase3-check.mjs && node scripts/phase3-behavior-check.mjs && node scripts/phase4-behavior-check.mjs && node scripts/phase5-behavior-check.mjs && node scripts/enterprise-behavior-check.mjs && node scripts/phase6-behavior-check.mjs && node scripts/golden-accounting-core-check.mjs && node scripts/platform-office-check.mjs && node scripts/slice-b-rbac-check.mjs`
82
+Expected: all PASS.
83
+
84
+- [ ] **Step 6: Commit Task 1 with state update**
85
+
86
+Commit migration, invariant script, Quality workflow entry and `PROJECT_STATE.md` together. State must say migration is repository-prepared only until Production apply/test actually succeeds.
87
+
88
+### Task 2: License admin UI and runtime seat context
89
+
90
+**Files:**
91
+- Modify: `index.html`
92
+- Modify: `js/admin.js`
93
+- Modify: `js/platform-modules.js`
94
+- Modify: `js/organization-rbac.js`
95
+- Modify: `scripts/enterprise-license-check.mjs`
96
+- Modify: `PROJECT_STATE.md`
97
+
98
+**Interfaces:**
99
+- Consumes: `licenses.max_users`, `admin_set_user_limit`, existing `admin_set_company_limit`, `admin_set_license_modules`, organization memberships.
100
+- Produces runtime fields: `currentUser.maxUsers`, `currentUser.activeSeatCount`, `currentUser.remainingSeats`, `currentUser.canConfigureOrganization`.
101
+- Produces UI: platform-admin license editor for named-user capacity; organization access manager usage counter and delegated-admin controls.
102
+
103
+- [ ] **Step 1: Extend failing invariant assertions for UI wiring**
104
+
105
+Assert the license modal has `modal-license-user-limit`; admin list shows user capacity; the license save path calls `admin_set_user_limit`; organization context loads active membership usage; and `core/configure` is used to show delegated organization-management UI.
106
+
107
+- [ ] **Step 2: Run invariant script and verify RED**
108
+
109
+Run: `node scripts/enterprise-license-check.mjs`
110
+Expected: FAIL on missing UI/runtime hooks.
111
+
112
+- [ ] **Step 3: Implement platform-admin capacity UI**
113
+
114
+In `index.html` add a named-user capacity field next to company capacity and add a user-capacity column to the admin license table. In `js/admin.js` render `max_users` safely with backward-compatible fallback. In `js/platform-modules.js` extend the existing license edit override so it validates a positive user limit and calls `admin_set_user_limit` after plan/company/module updates; do not claim success if any RPC fails.
115
+
116
+- [ ] **Step 4: Implement organization runtime/admin context**
117
+
118
+In `js/organization-rbac.js` load enough membership/license context to calculate active seat usage and expose `maxUsers`, `activeSeatCount`, `remainingSeats`. Change organization-manager visibility from owner-or-any-module-configure to: owner or `core/configure` for member/unit administration; module configurators keep only their module permission editor. Add member suspend/reactivate actions through `organization_set_member_status`; prevent owner suspension in UI but rely on server enforcement as authoritative.
119
+
120
+- [ ] **Step 5: Run invariant + syntax checks**
121
+
122
+Run: `node scripts/enterprise-license-check.mjs && node scripts/syntax-check.mjs && node scripts/slice-b-rbac-check.mjs && node scripts/platform-office-check.mjs`
123
+Expected: all PASS.
124
+
125
+- [ ] **Step 6: Commit Task 2 with state update**
126
+
127
+Commit UI/runtime changes and `PROJECT_STATE.md` together; keep Production behavior marked NOT VERIFIED until migration apply and transactional checks.
128
+
129
+### Task 3: Real-DOM coverage for license seats and delegated administration
130
+
131
+**Files:**
132
+- Create: `scripts/enterprise-license.spec.js`
133
+- Modify: `.github/workflows/quality.yml`
134
+- Modify: `scripts/enterprise-license-check.mjs`
135
+- Modify: `PROJECT_STATE.md`
136
+
137
+**Interfaces:**
138
+- Consumes: ELI-1 DOM IDs/functions from Task 2.
139
+- Produces: Chromium evidence for platform-admin capacity editing, seat counters, delegated-admin visibility and ordinary/module-admin denial in rendered UI.
140
+
141
+- [ ] **Step 1: Write Playwright tests before wiring them into CI**
142
+
143
+Create `scripts/enterprise-license.spec.js` that stubs Supabase responses deterministically and verifies: `300` named-user capacity renders; active usage/remaining seats render; platform admin can enter a changed capacity; delegated `core/configure` sees member management; a module-only configurator does not gain organization-wide administration; an unlicensed module remains unavailable.
144
+
145
+- [ ] **Step 2: Run the Playwright file and verify RED where behavior is missing**
146
+
147
+Run: `python3 -m http.server 4173 >/tmp/finora-http.log 2>&1 & npx playwright test scripts/enterprise-license.spec.js --reporter=line --workers=1`
148
+Expected: initial failing assertion before final DOM wiring, then PASS after correction.
149
+
150
+- [ ] **Step 3: Add the spec to the existing Real DOM CI command**
151
+
152
+Modify `.github/workflows/quality.yml` so the Real DOM step runs `scripts/dom-smoke.spec.js`, `scripts/platform-office.spec.js`, and `scripts/enterprise-license.spec.js` together.
153
+
154
+- [ ] **Step 4: Run the full local quality suite**
155
+
156
+Run the complete commands from `.github/workflows/quality.yml` including Chromium.
157
+Expected: all PASS, including Golden Accounting Journey and all three Real-DOM specs.
158
+
159
+- [ ] **Step 5: Commit Task 3 with state update**
160
+
161
+Record exact local tests run; do not call Production seat/RLS behavior verified yet.
162
+
163
+### Task 4: Production apply, adversarial SQL verification, and release checkpoint
164
+
165
+**Files:**
166
+- Modify if actual Supabase version differs: `supabase/migrations/20260930050000_enterprise_license_seats_delegated_admin.sql` so repository SQL exactly matches applied SQL/version.
167
+- Create: `docs/ELI_1_ENTERPRISE_LICENSE_VERIFICATION.md`
168
+- Modify: `PROJECT_STATE.md`
169
+
170
+**Interfaces:**
171
+- Consumes: reviewed migration and app changes from Tasks 1–3.
172
+- Produces: Production schema/RLS evidence and release checkpoint; no fabricated two-real-user browser claim.
173
+
174
+- [ ] **Step 1: Re-read Production before mutation**
175
+
176
+Verify project `hcsixhqbyuhpshfwqpjx` health, migration list, active-member counts per organization, current `max_companies/modules`, RLS and advisors. Abort/reconcile if Production differs from the plan assumptions.
177
+
178
+- [ ] **Step 2: Apply the exact ELI-1 migration once**
179
+
180
+Apply the reviewed SQL to Production. If Supabase assigns a different migration version, rename/archive the repository file to the exact applied version before the durable implementation commit.
181
+Expected: migration succeeds with no existing member deactivated and every `max_users >= active_member_count`.
182
+
183
+- [ ] **Step 3: Run transactional adversarial verification and ROLLBACK test data**
184
+
185
+Inside explicit transactions/rollback, prove: platform admin can set user capacity; non-admin cannot; lowering below usage fails; activation at capacity fails; suspension frees a seat; reactivation consumes it; delegated `core/configure` can manage a non-owner member/unit; ordinary member cannot; delegated admin cannot mutate license commercial fields; module configurator cannot grant another/unlicensed module; company capacity counts organization `companies` records and ignores regions/units. Use legitimate existing principals only; if a second principal is unavailable, mark the affected authenticated-browser scenario NOT VERIFIED rather than fabricating one.
186
+
187
+- [ ] **Step 4: Re-run advisors and data invariants**
188
+
189
+Verify RLS remains enabled, no orphan membership/license is introduced, active seats are within limits, and accounting data counts/journal invariants are unchanged.
190
+
191
+- [ ] **Step 5: Write verification evidence**
192
+
193
+Create `docs/ELI_1_ENTERPRISE_LICENSE_VERIFICATION.md` with applied migration version, SQL test matrix/results, CI SHAs/run IDs, advisor results and explicit NOT VERIFIED boundaries.
194
+
195
+- [ ] **Step 6: Push final durable implementation commit and observe GitHub gates**
196
+
197
+Update `PROJECT_STATE.md` in the same commit. Verify Quality, Security/CodeQL and Pages for the final head. Never mark ELI-1 COMPLETED from CI alone: Production transactional tests and Real-DOM tests must both have passed; any unavailable two-real-user browser test stays NOT VERIFIED.
198
+
199
+---
200
+
201
+## Self-review result
202
+
203
+- Spec coverage: ELI-1 is fully covered; ELI-2 reusable roles, ELI-3 invitations and ELI-4 organization-tree UX/real-user E2E are intentionally excluded and retain their own future plans.
204
+- Type/interface consistency: `max_users`, `core/configure`, organization-scoped company count and membership status RPC are used consistently across DB/runtime/UI/tests.
205
+- Review-focus coverage: all five listed failure modes are assigned to static, Playwright or transactional Production tests.
206
+- Proportion: tasks describe contracts and testable outcomes without embedding implementation bodies.