feat(tenancies-p2): rename berth_reservations → berth_tenancies (schema + perms + UI)

73-file atomic rename per docs/tenancies-design.md:

- Migration 0085: rename table + indexes + FK constraints; rename
  documents.reservation_id → tenancy_id; migrate jsonb permission maps
  (reservations resource → tenancies; collapse create+activate → manage);
  rewrite historical audit_logs.entity_type='berth_reservation' →
  'berth_tenancy'. FK renames wrapped in DO blocks so dev DBs that pre-date
  the FK additions don't abort.
- Schema: berthReservations → berthTenancies; BerthReservation type →
  BerthTenancy; indexes idx_br_* / idx_brr_* → idx_bt_*.
- RolePermissions: resource { view, create, activate, cancel } collapses to
  { view, manage, cancel }; all 8 default seed bundles + role-form + matrix
  updated.
- Service: berth-reservations.service.ts → berth-tenancies.service.ts;
  endReservation → endTenancy; listReservations → listTenancies.
- API: /api/v1/berth-reservations → /api/v1/tenancies (+ nested [id]);
  /api/v1/berths/[id]/reservations → /api/v1/berths/[id]/tenancies.
- Validators: reservations.ts → tenancies.ts; RESERVATION_STATUSES →
  TENANCY_STATUSES; endReservationSchema → endTenancySchema.
- Routes: /{portSlug}/berth-reservations → /{portSlug}/tenancies;
  /portal/my-reservations → /portal/my-tenancies.
- Components: src/components/reservations/* → src/components/tenancies/*;
  BerthReservationsTab → BerthTenanciesTab; ClientReservationsTab →
  ClientTenanciesTab; ReservationList → TenancyList.
- Socket events: berth_reservation:* → berth_tenancy:*; payload
  reservationId → tenancyId.
- Webhook events: berth_reservation.* → berth_tenancy.*.
- Portal: getPortalUserReservations → getPortalUserTenancies;
  PortalReservation → PortalTenancy; PortalDashboard.counts.activeReservations
  → activeTenancies; PortalNav label "Reservations" → "Tenancies".
- Dossier: DossierReservation → DossierTenancy; reservationDecisions →
  tenancyDecisions across smart-archive-dialog + bulk-archive routes.
- Documents schema: documents.reservationId → documents.tenancyId
  (TS + DB column + index + FK constraint).
- Activity feed label berth_reservation → berth_tenancy (matched against
  migrated historical audit rows).

KEPT (separate concepts):
- Reservation Agreement document type (the contract sent to clients).
- "Reservation" pipeline stage name.
- {{reservation.*}} merge tokens in template authoring.
- interest.reservationStatus / reservationDocStatus / dateReservationSent
  fields (track agreement signing on the deal).
- reservation-agreement-context.ts service (builds merge context for the
  Reservation Agreement doc; only its DB imports were renamed).

Verified: tsc clean, 1480/1480 vitest passing, migration applied.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-25 15:09:35 +02:00
parent 4f350d1fbd
commit ccc775dc66
77 changed files with 818 additions and 742 deletions

View File

@@ -18,7 +18,7 @@ import { yachts } from '@/lib/db/schema/yachts';
import { companies, companyMemberships } from '@/lib/db/schema/companies';
import { interests, interestBerths } from '@/lib/db/schema/interests';
import { berths } from '@/lib/db/schema/berths';
import { berthReservations } from '@/lib/db/schema/reservations';
import { berthTenancies } from '@/lib/db/schema/tenancies';
import { invoices } from '@/lib/db/schema/financial';
import { documents } from '@/lib/db/schema/documents';
import { activeInterestsWhere } from '@/lib/services/active-interest';
@@ -84,8 +84,8 @@ export interface DossierCompany {
membershipRole: string | null;
}
export interface DossierReservation {
reservationId: string;
export interface DossierTenancy {
tenancyId: string;
berthId: string;
mooringNumber: string;
status: string; // typically 'active'
@@ -128,13 +128,13 @@ export interface ClientArchiveDossier {
berths: DossierBerth[];
yachts: DossierYacht[];
companies: DossierCompany[];
reservations: DossierReservation[];
tenancies: DossierTenancy[];
invoices: DossierInvoice[];
documents: DossierDocument[];
hasPortalUser: boolean;
/** Hard blockers - cannot proceed with archive at all until these are
* resolved manually. Currently the only one is "active reservation
* resolved manually. Currently the only one is "active tenancy
* on a sold berth" (since you can't unsell a berth from this flow). */
blockers: string[];
}
@@ -327,23 +327,23 @@ export async function getClientArchiveDossier(
),
);
// ─── Active reservations ─────────────────────────────────────────────────
const activeReservations = await db
// ─── Active tenancies ────────────────────────────────────────────────────
const activeTenancies = await db
.select({
id: berthReservations.id,
berthId: berthReservations.berthId,
id: berthTenancies.id,
berthId: berthTenancies.berthId,
mooringNumber: berths.mooringNumber,
status: berthReservations.status,
startDate: berthReservations.startDate,
status: berthTenancies.status,
startDate: berthTenancies.startDate,
berthStatus: berths.status,
})
.from(berthReservations)
.innerJoin(berths, eq(berthReservations.berthId, berths.id))
.from(berthTenancies)
.innerJoin(berths, eq(berthTenancies.berthId, berths.id))
.where(
and(
eq(berthReservations.clientId, clientId),
eq(berthReservations.portId, portId),
eq(berthReservations.status, 'active'),
eq(berthTenancies.clientId, clientId),
eq(berthTenancies.portId, portId),
eq(berthTenancies.status, 'active'),
),
);
@@ -376,14 +376,14 @@ export async function getClientArchiveDossier(
.limit(1);
// ─── Hard blockers ───────────────────────────────────────────────────────
// The only true blocker is an active reservation on a SOLD berth - we
// The only true blocker is an active tenancy on a SOLD berth - we
// can't auto-handle this without crossing into refund territory. Force
// the operator to handle it via the existing reservation UI first.
// the operator to handle it via the existing tenancy UI first.
const blockers: string[] = [];
for (const r of activeReservations) {
for (const r of activeTenancies) {
if (r.berthStatus === 'sold') {
blockers.push(
`Active reservation on sold berth ${r.mooringNumber} (#${r.id.slice(0, 8)}). Process the refund or transfer the reservation before archiving.`,
`Active tenancy on sold berth ${r.mooringNumber} (#${r.id.slice(0, 8)}). Process the refund or transfer the tenancy before archiving.`,
);
}
}
@@ -410,8 +410,8 @@ export async function getClientArchiveDossier(
name: m.name,
membershipRole: m.role,
})),
reservations: activeReservations.map((r) => ({
reservationId: r.id,
tenancies: activeTenancies.map((r) => ({
tenancyId: r.id,
berthId: r.berthId,
mooringNumber: r.mooringNumber,
status: r.status,