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:
@@ -17,7 +17,7 @@ import { clients } from './clients';
|
||||
import { yachts } from './yachts';
|
||||
import { companies } from './companies';
|
||||
import { interests } from './interests';
|
||||
import { berthReservations } from './reservations';
|
||||
import { berthTenancies } from './tenancies';
|
||||
|
||||
export const files = pgTable(
|
||||
'files',
|
||||
@@ -90,7 +90,7 @@ export const documents = pgTable(
|
||||
clientId: text('client_id').references(() => clients.id, { onDelete: 'set null' }),
|
||||
yachtId: text('yacht_id').references(() => yachts.id, { onDelete: 'set null' }),
|
||||
companyId: text('company_id').references(() => companies.id, { onDelete: 'set null' }),
|
||||
reservationId: text('reservation_id').references(() => berthReservations.id, {
|
||||
tenancyId: text('tenancy_id').references(() => berthTenancies.id, {
|
||||
onDelete: 'set null',
|
||||
}),
|
||||
folderId: text('folder_id').references((): AnyPgColumn => documentFolders.id, {
|
||||
@@ -155,7 +155,7 @@ export const documents = pgTable(
|
||||
index('idx_docs_client').on(table.clientId),
|
||||
index('idx_documents_yacht').on(table.yachtId),
|
||||
index('idx_documents_company').on(table.companyId),
|
||||
index('idx_docs_reservation').on(table.reservationId),
|
||||
index('idx_docs_tenancy').on(table.tenancyId),
|
||||
index('idx_docs_type').on(table.portId, table.documentType),
|
||||
index('idx_docs_status_port').on(table.portId, table.status),
|
||||
// Cover the file FKs Postgres doesn't auto-index. Without these,
|
||||
|
||||
@@ -19,8 +19,8 @@ export * from './interests';
|
||||
// Berths
|
||||
export * from './berths';
|
||||
|
||||
// Reservations
|
||||
export * from './reservations';
|
||||
// Tenancies (formerly berth_reservations)
|
||||
export * from './tenancies';
|
||||
|
||||
// Documents & Files
|
||||
export * from './documents';
|
||||
|
||||
@@ -43,8 +43,8 @@ import {
|
||||
berthPdfVersions,
|
||||
} from './berths';
|
||||
|
||||
// Reservations
|
||||
import { berthReservations } from './reservations';
|
||||
// Tenancies (formerly berth_reservations)
|
||||
import { berthTenancies } from './tenancies';
|
||||
|
||||
// Documents
|
||||
import {
|
||||
@@ -104,7 +104,7 @@ export const portsRelations = relations(ports, ({ many }) => ({
|
||||
yachts: many(yachts),
|
||||
companies: many(companies),
|
||||
berths: many(berths),
|
||||
berthReservations: many(berthReservations),
|
||||
berthTenancies: many(berthTenancies),
|
||||
documents: many(documents),
|
||||
documentTemplates: many(documentTemplates),
|
||||
formTemplates: many(formTemplates),
|
||||
@@ -187,7 +187,7 @@ export const clientsRelations = relations(clients, ({ one, many }) => ({
|
||||
formSubmissions: many(formSubmissions),
|
||||
addresses: many(clientAddresses),
|
||||
companyMemberships: many(companyMemberships),
|
||||
berthReservations: many(berthReservations),
|
||||
berthTenancies: many(berthTenancies),
|
||||
}));
|
||||
|
||||
export const clientContactsRelations = relations(clientContacts, ({ one }) => ({
|
||||
@@ -318,7 +318,7 @@ export const yachtsRelations = relations(yachts, ({ one, many }) => ({
|
||||
notes: many(yachtNotes),
|
||||
tags: many(yachtTags),
|
||||
interests: many(interests),
|
||||
reservations: many(berthReservations),
|
||||
tenancies: many(berthTenancies),
|
||||
documents: many(documents),
|
||||
}));
|
||||
|
||||
@@ -482,31 +482,31 @@ export const berthTagsRelations = relations(berthTags, ({ one }) => ({
|
||||
}),
|
||||
}));
|
||||
|
||||
// ─── Berth Reservations ───────────────────────────────────────────────────────
|
||||
// ─── Berth Tenancies ──────────────────────────────────────────────────────────
|
||||
|
||||
export const berthReservationsRelations = relations(berthReservations, ({ one, many }) => ({
|
||||
export const berthTenanciesRelations = relations(berthTenancies, ({ one, many }) => ({
|
||||
berth: one(berths, {
|
||||
fields: [berthReservations.berthId],
|
||||
fields: [berthTenancies.berthId],
|
||||
references: [berths.id],
|
||||
}),
|
||||
port: one(ports, {
|
||||
fields: [berthReservations.portId],
|
||||
fields: [berthTenancies.portId],
|
||||
references: [ports.id],
|
||||
}),
|
||||
client: one(clients, {
|
||||
fields: [berthReservations.clientId],
|
||||
fields: [berthTenancies.clientId],
|
||||
references: [clients.id],
|
||||
}),
|
||||
yacht: one(yachts, {
|
||||
fields: [berthReservations.yachtId],
|
||||
fields: [berthTenancies.yachtId],
|
||||
references: [yachts.id],
|
||||
}),
|
||||
interest: one(interests, {
|
||||
fields: [berthReservations.interestId],
|
||||
fields: [berthTenancies.interestId],
|
||||
references: [interests.id],
|
||||
}),
|
||||
contractFile: one(files, {
|
||||
fields: [berthReservations.contractFileId],
|
||||
fields: [berthTenancies.contractFileId],
|
||||
references: [files.id],
|
||||
}),
|
||||
documents: many(documents),
|
||||
@@ -566,9 +566,9 @@ export const documentsRelations = relations(documents, ({ one, many }) => ({
|
||||
fields: [documents.companyId],
|
||||
references: [companies.id],
|
||||
}),
|
||||
reservation: one(berthReservations, {
|
||||
fields: [documents.reservationId],
|
||||
references: [berthReservations.id],
|
||||
tenancy: one(berthTenancies, {
|
||||
fields: [documents.tenancyId],
|
||||
references: [berthTenancies.id],
|
||||
}),
|
||||
folder: one(documentFolders, {
|
||||
fields: [documents.folderId],
|
||||
|
||||
@@ -7,17 +7,17 @@ import { yachts } from './yachts';
|
||||
import { interests } from './interests';
|
||||
import { files } from './documents';
|
||||
|
||||
export const berthReservations = pgTable(
|
||||
'berth_reservations',
|
||||
export const berthTenancies = pgTable(
|
||||
'berth_tenancies',
|
||||
{
|
||||
id: text('id')
|
||||
.primaryKey()
|
||||
.$defaultFn(() => crypto.randomUUID()),
|
||||
// H-01: reservations are the canonical "who occupies a berth right
|
||||
// H-01: tenancies are the canonical "who occupies a berth right
|
||||
// now" record; RESTRICT on every parent FK keeps an ad-hoc DB-side
|
||||
// hard-delete from leaving a reservation pointing at a missing
|
||||
// hard-delete from leaving a tenancy pointing at a missing
|
||||
// berth/client/yacht. Interest is nullable + SET NULL because a
|
||||
// reservation legitimately outlives the originating deal.
|
||||
// tenancy legitimately outlives the originating deal.
|
||||
berthId: text('berth_id')
|
||||
.notNull()
|
||||
.references(() => berths.id, { onDelete: 'restrict' }),
|
||||
@@ -36,7 +36,7 @@ export const berthReservations = pgTable(
|
||||
endDate: timestamp('end_date', { withTimezone: true, mode: 'date' }),
|
||||
// M-L01: canonical tenure_type union is
|
||||
// `permanent | fixed_term | fee_simple | strata_lot | seasonal`
|
||||
// (kept in sync with berths.tenure_type). 'seasonal' is reservation-
|
||||
// (kept in sync with berths.tenure_type). 'seasonal' is tenancy-
|
||||
// specific (winter haul-out etc.); the others mirror the berth's
|
||||
// own tenure shape. Configurable via the per-port vocabulary at
|
||||
// /admin/vocabularies (key: berth_tenure_types).
|
||||
@@ -48,21 +48,17 @@ export const berthReservations = pgTable(
|
||||
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
|
||||
},
|
||||
(table) => [
|
||||
index('idx_br_berth').on(table.berthId),
|
||||
index('idx_br_client').on(table.clientId),
|
||||
index('idx_br_yacht').on(table.yachtId),
|
||||
index('idx_br_port').on(table.portId),
|
||||
// Cover the FKs Postgres doesn't auto-index. Without these, deleting
|
||||
// (or restrict-checking) the parent interest / contract file row
|
||||
// requires a full scan of berth_reservations. (idx_br_interest is
|
||||
// already used by berth_recommendations - namespace this one.)
|
||||
index('idx_brr_interest').on(table.interestId),
|
||||
index('idx_brr_contract_file').on(table.contractFileId),
|
||||
uniqueIndex('idx_br_active')
|
||||
index('idx_bt_berth').on(table.berthId),
|
||||
index('idx_bt_client').on(table.clientId),
|
||||
index('idx_bt_yacht').on(table.yachtId),
|
||||
index('idx_bt_port').on(table.portId),
|
||||
index('idx_bt_interest').on(table.interestId),
|
||||
index('idx_bt_contract_file').on(table.contractFileId),
|
||||
uniqueIndex('idx_bt_active')
|
||||
.on(table.berthId)
|
||||
.where(sql`${table.status} = 'active'`),
|
||||
],
|
||||
);
|
||||
|
||||
export type BerthReservation = typeof berthReservations.$inferSelect;
|
||||
export type NewBerthReservation = typeof berthReservations.$inferInsert;
|
||||
export type BerthTenancy = typeof berthTenancies.$inferSelect;
|
||||
export type NewBerthTenancy = typeof berthTenancies.$inferInsert;
|
||||
@@ -130,10 +130,9 @@ export type RolePermissions = {
|
||||
view: boolean;
|
||||
manage: boolean;
|
||||
};
|
||||
reservations: {
|
||||
tenancies: {
|
||||
view: boolean;
|
||||
create: boolean;
|
||||
activate: boolean;
|
||||
manage: boolean;
|
||||
cancel: boolean;
|
||||
};
|
||||
admin: {
|
||||
|
||||
Reference in New Issue
Block a user