feat(tenancies-p6-followup): generic create dialog + edit dialog + self-FKs

- Migration 0086: berth_tenancies.previous_tenancy_id +
  transferred_from_tenancy_id self-FKs + partial indexes. Per
  docs/tenancies-design.md these chain renewal / transfer successors
  to predecessors for fixed-term and seasonal lineage. Schema mirrored
  in tenancies.ts with AnyPgColumn typed-import.
- POST /api/v1/tenancies (generic create): accepts berthId in the
  body so client + yacht tab entry points don't have to bounce through
  /api/v1/berths/[id]/tenancies. Same createPending service helper.
- TenancyCreateDialog: <TenancyCreateDialog open clientId? yachtId?
  berthId? /> with all three pickers; pre-fills the carrier from the
  parent entity. POSTs to /api/v1/tenancies; "Create" and
  "Create and activate" CTAs both wire to the new endpoint.
- Mounted on ClientTenanciesTab + YachtTenanciesTab behind
  <PermissionGate resource="tenancies" action="manage"> so reps can
  mint tenancies directly from those tabs without bouncing through
  the berth page.
- TenancyEditDialog: edit metadata only (start/end dates, tenure type,
  notes) via the new action='update' branch on the [id] PATCH route.
  Status transitions stay on activate/end/cancel. Wired into the
  tenancy detail page header. Outer wrapper unmounts on close so the
  form re-initialises from current row data without setState-in-effect.
- updateTenancy service helper + PATCH action='update' branch added.
  Audit-logged + emits berth_tenancy:activated to invalidate detail
  query caches.

Renew + Transfer dialogs deferred — both need lineage UX decisions
(tenure-aware mutate-in-place vs new-row spawn; client/yacht swap
semantics) and the self-FK columns this commit lands are the
underpinning. Next sub-task.

Verified: tsc clean, 1493/1493 vitest, migration applied.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-25 17:10:06 +02:00
parent c4450dd852
commit 911b51a669
12 changed files with 689 additions and 13 deletions

View File

@@ -0,0 +1,14 @@
-- 0086_tenancies_self_fks.sql
-- Add self-FKs for renewals + transfers per docs/tenancies-design.md
-- §"Data model > Rename migration". `previous_tenancy_id` chains a new
-- row to the row it succeeds (fixed-term + seasonal renewals mint a new
-- row; permanent renewals mutate in place). `transferred_from_tenancy_id`
-- chains a new row to the predecessor when an active tenancy is
-- handed over to a different client/yacht.
ALTER TABLE berth_tenancies
ADD COLUMN IF NOT EXISTS previous_tenancy_id text REFERENCES berth_tenancies(id) ON DELETE SET NULL,
ADD COLUMN IF NOT EXISTS transferred_from_tenancy_id text REFERENCES berth_tenancies(id) ON DELETE SET NULL;
CREATE INDEX IF NOT EXISTS idx_bt_previous ON berth_tenancies(previous_tenancy_id) WHERE previous_tenancy_id IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_bt_transferred_from ON berth_tenancies(transferred_from_tenancy_id) WHERE transferred_from_tenancy_id IS NOT NULL;

View File

@@ -1,4 +1,11 @@
import { pgTable, text, timestamp, index, uniqueIndex } from 'drizzle-orm/pg-core';
import {
pgTable,
text,
timestamp,
index,
uniqueIndex,
type AnyPgColumn,
} from 'drizzle-orm/pg-core';
import { sql } from 'drizzle-orm';
import { ports } from './ports';
import { berths } from './berths';
@@ -43,6 +50,22 @@ export const berthTenancies = pgTable(
tenureType: text('tenure_type').notNull().default('permanent'),
contractFileId: text('contract_file_id').references(() => files.id, { onDelete: 'set null' }),
notes: text('notes'),
// Renewal + transfer self-FKs (migration 0086). `previousTenancyId`
// chains a successor row to the row it succeeds (fixed-term + seasonal
// renewals mint a new row; permanent renewals mutate in place so the
// FK stays null). `transferredFromTenancyId` chains a new row to the
// predecessor when an active tenancy is handed over to a different
// client / yacht. SET NULL on delete keeps the lineage best-effort.
previousTenancyId: text('previous_tenancy_id').references(
(): AnyPgColumn => berthTenancies.id,
{
onDelete: 'set null',
},
),
transferredFromTenancyId: text('transferred_from_tenancy_id').references(
(): AnyPgColumn => berthTenancies.id,
{ onDelete: 'set null' },
),
createdBy: text('created_by').notNull(),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),

View File

@@ -18,6 +18,7 @@ import type {
EndTenancyInput,
CancelInput,
ListTenanciesInput,
UpdateTenancyInput,
} from '@/lib/validators/tenancies';
// Use z.input so callers (including tests) can omit fields with
@@ -354,6 +355,55 @@ export async function cancel(
return updated;
}
// ─── Update (metadata only — non-status fields) ──────────────────────────────
export async function updateTenancy(
tenancyId: string,
portId: string,
data: UpdateTenancyInput,
meta: AuditMeta,
): Promise<BerthTenancy> {
const existing = await loadScoped(tenancyId, portId);
const patch: Partial<typeof berthTenancies.$inferInsert> = { updatedAt: new Date() };
if (data.startDate !== undefined) patch.startDate = data.startDate;
if (data.endDate !== undefined) patch.endDate = data.endDate;
if (data.tenureType !== undefined) patch.tenureType = data.tenureType;
if (data.notes !== undefined) patch.notes = data.notes;
const [updated] = await db
.update(berthTenancies)
.set(patch)
.where(and(eq(berthTenancies.id, tenancyId), eq(berthTenancies.portId, portId)))
.returning();
if (!updated) throw new NotFoundError('Tenancy');
void createAuditLog({
userId: meta.userId,
portId,
action: 'update',
entityType: 'berth_tenancy',
entityId: tenancyId,
oldValue: {
startDate: existing.startDate,
endDate: existing.endDate,
tenureType: existing.tenureType,
notes: existing.notes,
},
newValue: patch,
ipAddress: meta.ipAddress,
userAgent: meta.userAgent,
});
emitToRoom(`port:${portId}`, 'berth_tenancy:activated', {
tenancyId,
berthId: updated.berthId,
});
return updated;
}
// ─── Get ─────────────────────────────────────────────────────────────────────
export async function getById(id: string, portId: string): Promise<BerthTenancy> {

View File

@@ -28,6 +28,20 @@ export const cancelSchema = z.object({
reason: z.string().optional(),
});
/** PATCH body for the "edit metadata" idiom — touches notes / dates /
* tenure type without crossing a status boundary. Status transitions
* flow through activate / endTenancy / cancel; this is non-transition
* metadata only. */
export const updateTenancySchema = z
.object({
startDate: z.coerce.date().optional(),
endDate: z.coerce.date().nullable().optional(),
tenureType: z.enum(TENURE_TYPES).optional(),
notes: z.string().nullable().optional(),
})
.refine((d) => Object.keys(d).length > 0, { message: 'At least one field must be provided' });
export type UpdateTenancyInput = z.infer<typeof updateTenancySchema>;
export const listTenanciesSchema = baseListQuerySchema.extend({
status: z.enum(TENANCY_STATUSES).optional(),
berthId: z.string().optional(),