# מובייל: הצטרפות ובקשת מחיקה

> תיעוד מקור. מקור: docs/mobile-p8-onboarding-deletion.md · 6a466bf.

## Approved change scope

- Reuse the existing personal Clerk identity for every plan; **never require an organization to sign in**.
- Personal / Family: authenticated Clerk user + an individual/family Supabase tenant; no Clerk Organization.
- Business / Enterprise: the same personal Clerk user + a business Supabase tenant **and** a dedicated Clerk Organization. Organization is created by that user, tagged with immutable tenant ID in Clerk private metadata, and persisted in the already-existing `ev1_tenants.clerk_org_id` column using server-side service credentials after verification.
- Reuse the existing `lne_create_account` RPC with per-plan UUID idempotency. Only the authenticated user can provision for their identity. Never accept a client-supplied owner or org ID.
- Enterprise Organization memberships are not arbitrarily capped by the existing five-person default; actual Supabase tenant role permissions and commercial entitlements remain independent.
- Existing UI screens, layout, assets, colors, fonts, navigation and payments are locked. Two narrowly scoped account-action cards are added **after** the approved Account screen content, using existing `AppScreen` tokens. The obsolete empty-state copy is corrected without rearranging content.

## Technical flow

1. User signs into Clerk via existing native sign-up/login; account creation / verification and token cache are unchanged.
2. In Account screen, select Personal / Family / Business / Enterprise, supply valid name and optional organization tax/legal fields, consent to the existing terms/privacy, and submit.
3. Mobile sends `POST /api/client/v1/accounts` with Bearer session and a persisted request UUID. No service-role key or payment key ever goes into the mobile bundle.
4. API verifies Clerk session via `clientIdentity()`, uses existing user JWT-backed Supabase client and RLS, invokes `lne_create_account` and re-reads the tenant for **exact owner+plan**.
5. For Business / Enterprise, backend creates or recovers an org from Clerk **private** metadata and owner ID, and then server-links it to the tenant via the pre-existing `clerk_org_id` column. Existing registered orgs never get overwritten or silently re-associated.
6. On success, `WorkspaceProvider.refresh(preferredTenantId)` checks the new ID exists in authorized `context.tenants` and selects it. No client-chosen tenant role is trusted.

**Concurrency/recovery:** Clerk Production intentionally has Organization slugs disabled; the implementation does not change that setting or require a slug. The unique, service-only `lne_clerk_org_provision_claims(tenant_id)` row is reserved before the first external create attempt. Only an exact owner/tenant match in Clerk private metadata is eligible for recovery. If a creation is uncertain and cannot be verified, the claim remains locked and the user receives a safe pending/review response — **never a blind second organization**. Concurrency and signed-device QA are still explicit release gates.

## In-app account deletion request

- User can select `בקשה למחיקת החשבון` from Account screen, read the warning and confirm explicitly.
- API `POST /api/client/v1/account/deletion` only accepts authenticated Clerk Bearer tokens and a strict `DELETE_MY_ACCOUNT` confirmation. `GET` on the same path returns only the authenticated user's pending request status for subsequent app launches.
- DB queue `lne_account_deletion_requests` has RLS enabled, no `anon` or `authenticated` direct privileges or policies, and one pending request per Clerk user. Its data is read/written by **service-role backend** only. Repeated requests return the same pending record, not duplicates.
- This action **does not immediately erase or suspend the identity**, because ownership of organizations and financial/legal retention must be resolved first.
- A verified operator must: confirm user ownership and identity; list all memberships/owned tenants and other organization admins; transfer ownership or obtain closure consent; stop relevant future subscriptions; export and preserve only legally required billing records; erase/scrub non-required user and device data across Supabase, Clerk and processors; revoke tokens/delete Clerk user; mark request completed; notify the verified contact address. Financial accounting/audit data must not be silently destroyed. This operator workflow has **NOT** been automated or executed.
- Apple permits manual erasure processing if users receive a clear expectation and confirmation. Before public App Review, the organization must define and actually staff a time-bound SLA, complete end-to-end test and notification delivery. Storing a request is not equivalent to completing account deletion.

## External deletion request — Google Play

- Public route: `https://linknodesenergy.com/account-deletion` (company website only), built using existing `CompanyShell` and style system. Links directly to the existing secure company contact form, where users can submit a deletion request without reinstalling or logging into the app. Contact form processing and delivery must be tested with an authorized synthetic request, and privacy retention policy must be reviewed.
- Do not force app reinstall or require another user's workspace. Link the direct deletion URL in Google Play Data Safety / account deletion fields, **after verifying Production route HTTP 200 and form delivery**.
- Public terms/privacy URLs remain unchanged.

## QA acceptance matrix (not all verified)

| Case                        | Intended result                                                                    | Gate                                                  |
| --------------------------- | ---------------------------------------------------------------------------------- | ----------------------------------------------------- |
| Personal                    | Own individual tenant, no Clerk Organization                                       | Signed-device/real Supabase test pending              |
| Family                      | Own personal-kind family tenant, no Clerk Organization                             | Signed-device test pending                            |
| Business                    | Individual Clerk user + business tenant + Clerk org bound by private metadata      | Real sandbox service test pending                     |
| Enterprise                  | Individual Clerk user + enterprise tenant + dedicated org; seats/role verification | Real multi-member test pending                        |
| Existing personal workspace | Do not create a second personal-kind tenant                                        | RLS and idempotency tests pending                     |
| Concurrent requests         | One Supabase tenant / no duplicate Clerk org                                       | Chaos test pending                                    |
| Tenant crossover            | Wrong-user/wrong-tenant organization cannot be linked                              | Unit policy tests, live negative tests pending        |
| Deletion request            | Authenticated confirm; repeat request idempotent; no unsafe deletion               | DB privilege/read-only checks and API tests pending   |
| Legal retention             | Keep required financial documents, remove all unnecessary personal data            | Compliance decision + operator implementation pending |
| External web request        | Works without app; lands in operator queue                                         | Live contact form QA pending                          |

## No deployment without gates

- Additive SQL migrations are recorded in Supabase as `20261111155528_mobile_onboarding_and_deletion_requests` and `20261111161830_clerk_org_provision_claims`. Both queue tables have RLS ON and **zero** anon/authenticated policies; service role only. Check this again before a public launch.
- Staging/mobile TypeScript, Next.js production preview, and tests must pass.
- No real customer deletion or organization creation during development/QA without specific authorization and disposable test identities.
- Clerk Organizations are enabled **with personal membership optional**; do not change that to membership required.
- Corporate publisher ownership, Apple/Google signing and digital-subscription payment policy remain separate P8 store gates.

## Rollback

- If QA fails or any unauthorized visual change occurs, revert the isolated PR and restore the last approved main commit. Do not edit approved app layout/assets; do not delete persisted customer accounts or migration records merely to roll back UI code.
