fix(tenancies): unblock first-tenancy chicken-and-egg in webhook

Webhook auto-create on signed Reservation Agreement was gating itself on
isTenanciesModuleEnabled, but autoCreatePendingTenancies never enabled
the module — so the very first tenancy on a fresh port was unreachable
even though the row-exists fallback in isTenanciesModuleEnabled was
designed exactly for this lazy auto-surface case. Drop the gate; the
inserted row now flips the module on automatically via the fallback.

docs/tenancies-design.md §"When disabled" and the P3 PR-table row
updated to reflect the new contract.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-26 18:48:15 +02:00
parent c8869338e8
commit 353a31323e
3 changed files with 21 additions and 22 deletions

View File

@@ -33,7 +33,7 @@ A sold berth stays sold without any tenancy data — the platform does not assum
- Client / Yacht / Berth `Tenancies` tab hidden - Client / Yacht / Berth `Tenancies` tab hidden
- All four reporting widgets hidden from dashboard registry - All four reporting widgets hidden from dashboard registry
- Top-level `/{portSlug}/tenancies` page returns 404 - Top-level `/{portSlug}/tenancies` page returns 404
- `handleDocumentCompleted` skips the auto-create branch on signed `reservation_agreement` (the document still progresses the interest stage and flips `reservationDocStatus` — only the tenancy mint is gated) - `handleDocumentCompleted` still mints pending tenancies on a signed `reservation_agreement` — we intentionally do NOT gate the auto-create branch on the module flag, because the resulting row is what lazily surfaces the module on a fresh port (rule (a) above). The CRM surface stays hidden until that first insert lands; from then on, both rules (a) and (b) are satisfied.
### When enabled ### When enabled
@@ -279,10 +279,10 @@ All routes gated on `tenancies.view` (read) or `tenancies.manage` / `tenancies.c
## Phased PR plan ## Phased PR plan
| PR | Scope | Effort | Ships independently | | PR | Scope | Effort | Ships independently |
| ------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------ | ------------------------------------------------------------------------------ | | ------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------ | ------------------------------------------------------------------------------ |
| **P1: Rename migration + perms + setting** | `008X_rename_reservations_to_tenancies.sql` + self-FKs + seed `tenancies.view`/`.manage`/`.cancel` + `tenancies_module_enabled` registry entry. Schema files renamed. ALL imports updated. **No behaviour change** — module starts disabled, so reps don't see anything new. | ~6 h | Yes (silent rename; existing consumers keep working through the renamed table) | | **P1: Rename migration + perms + setting** | `008X_rename_reservations_to_tenancies.sql` + self-FKs + seed `tenancies.view`/`.manage`/`.cancel` + `tenancies_module_enabled` registry entry. Schema files renamed. ALL imports updated. **No behaviour change** — module starts disabled, so reps don't see anything new. | ~6 h | Yes (silent rename; existing consumers keep working through the renamed table) |
| **P2: Module-enabled gating infra** | `tenancies-module.service.ts` + admin Operations page Switch + lazy-flip logic + permission helper that combines `tenancies.view` AND module-enabled. | ~4 h | Yes (admin can toggle; rest of app honors the flag) | | **P2: Module-enabled gating infra** | `tenancies-module.service.ts` + admin Operations page Switch + lazy-flip logic + permission helper that combines `tenancies.view` AND module-enabled. | ~4 h | Yes (admin can toggle; rest of app honors the flag) |
| **P3: Webhook auto-create branch** | `autoCreatePendingTenancies` + branch in `handleDocumentCompleted` + first-insert flip. Vitest covering: module on → row created; module off → no row + stage still advances. | ~5 h | Yes (back-compat — pre-existing reservation flows keep working) | | **P3: Webhook auto-create branch** | `autoCreatePendingTenancies` + unconditional branch in `handleDocumentCompleted` (no module gate — the inserted row is what surfaces the module via the row-exists fallback in `isTenanciesModuleEnabled`). Vitest covering: first signing on a fresh port surfaces the module; replay is idempotent; stage still advances regardless. | ~5 h | Yes (back-compat — pre-existing reservation flows keep working) |
| **P4: Public-map status flip rules** | Status resolver in `berths.service.ts` honors active permanent-class tenancies. Vitest for precedence + module-off behaviour. | ~3 h | Yes | | **P4: Public-map status flip rules** | Status resolver in `berths.service.ts` honors active permanent-class tenancies. Vitest for precedence + module-off behaviour. | ~3 h | Yes |
| **P5: Sidebar entry + top-level page** | Sidebar mounts the Tenancies entry behind both gates. New `/{portSlug}/tenancies/page.tsx` with the listing table + filters. 404 when module disabled. | ~6 h | Yes (visible to super_admin first; sales reps see it once perms seed) | | **P5: Sidebar entry + top-level page** | Sidebar mounts the Tenancies entry behind both gates. New `/{portSlug}/tenancies/page.tsx` with the listing table + filters. 404 when module disabled. | ~6 h | Yes (visible to super_admin first; sales reps see it once perms seed) |
| **P6: Entity tab refresh + Create dialog** | Friendly empty state + "Create tenancy" CTA on Client / Yacht / Berth tabs. `<TenancyCreateDialog>` pre-fills from parent context. Edit / Renew / Transfer / End dialogs follow the same idiom. | ~8 h | Yes | | **P6: Entity tab refresh + Create dialog** | Friendly empty state + "Create tenancy" CTA on Client / Yacht / Berth tabs. `<TenancyCreateDialog>` pre-fills from parent context. Edit / Renew / Transfer / End dialogs follow the same idiom. | ~8 h | Yes |

View File

@@ -680,11 +680,12 @@ export interface AutoCreateOptions {
* then confirms start date + tenure type via the entity-tab UI to flip * then confirms start date + tenure type via the entity-tab UI to flip
* `pending → active`. * `pending → active`.
* *
* Caller is responsible for gating on `isTenanciesModuleEnabled(portId)` — * The webhook caller does not gate on `isTenanciesModuleEnabled` — the
* this function does NOT check the flag itself (per design line 132: the * row-exists fallback in that helper lazily surfaces the module from the
* webhook caller short-circuits when the module is off). Service-level * first successful insert onwards, which is what unlocks the very first
* idempotency: skips any berth that already has a non-terminal tenancy in * tenancy on a fresh port. Service-level idempotency: skips any berth that
* `(pending, active)` so a webhook re-delivery never double-mints. * already has a non-terminal tenancy in `(pending, active)` so a webhook
* re-delivery never double-mints.
* *
* Returns the newly-inserted rows (empty array when nothing to mint). * Returns the newly-inserted rows (empty array when nothing to mint).
*/ */

View File

@@ -1683,15 +1683,13 @@ export async function handleDocumentCompleted(eventData: { documentId: string; p
evaluateRule('contract_signed', doc.interestId!, doc.portId, systemMeta), evaluateRule('contract_signed', doc.interestId!, doc.portId, systemMeta),
); );
// Tenancies P3 — auto-create pending tenancies (one per in-bundle berth) // Tenancies P3 — auto-create pending tenancies (one per in-bundle berth).
// when the module is enabled for this port. Gating is at the call site: // No module gate here: the row-exists fallback in `isTenanciesModuleEnabled`
// disabled module = stage + docStatus updates still fire, only the // lazily surfaces the module after the first signing, which is exactly what
// tenancy mint is skipped (per docs/tenancies-design.md §"When disabled"). // we want for the first-ever Reservation Agreement on a fresh port. Admins
// can still pre-enable the module to make widgets/sidebar appear earlier.
void (async () => { void (async () => {
try { try {
const { isTenanciesModuleEnabled } =
await import('@/lib/services/tenancies-module.service');
if (!(await isTenanciesModuleEnabled(doc.portId))) return;
const { autoCreatePendingTenancies } = const { autoCreatePendingTenancies } =
await import('@/lib/services/berth-tenancies.service'); await import('@/lib/services/berth-tenancies.service');
// Re-read signedFileId from the post-commit row; the in-tx update // Re-read signedFileId from the post-commit row; the in-tx update