feat(reservations): add berth_reservations schema with partial unique exclusivity

This commit is contained in:
Matt Ciaccio
2026-04-23 17:55:53 +02:00
parent 299e893e2b
commit 88a87afa77
5 changed files with 8547 additions and 0 deletions

View File

@@ -19,6 +19,9 @@ export * from './interests';
// Berths
export * from './berths';
// Reservations
export * from './reservations';
// Documents & Files
export * from './documents';

View File

@@ -0,0 +1,51 @@
import { pgTable, text, timestamp, index, uniqueIndex } from 'drizzle-orm/pg-core';
import { sql } from 'drizzle-orm';
import { ports } from './ports';
import { berths } from './berths';
import { clients } from './clients';
import { yachts } from './yachts';
import { interests } from './interests';
import { files } from './documents';
export const berthReservations = pgTable(
'berth_reservations',
{
id: text('id')
.primaryKey()
.$defaultFn(() => crypto.randomUUID()),
berthId: text('berth_id')
.notNull()
.references(() => berths.id),
portId: text('port_id')
.notNull()
.references(() => ports.id),
clientId: text('client_id')
.notNull()
.references(() => clients.id),
yachtId: text('yacht_id')
.notNull()
.references(() => yachts.id),
interestId: text('interest_id').references(() => interests.id),
status: text('status').notNull(), // 'pending' | 'active' | 'ended' | 'cancelled'
startDate: timestamp('start_date', { withTimezone: true, mode: 'date' }).notNull(),
endDate: timestamp('end_date', { withTimezone: true, mode: 'date' }),
tenureType: text('tenure_type').notNull().default('permanent'), // 'permanent' | 'fixed_term' | 'seasonal'
contractFileId: text('contract_file_id').references(() => files.id),
notes: text('notes'),
createdBy: text('created_by').notNull(),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
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),
uniqueIndex('idx_br_active')
.on(table.berthId)
.where(sql`${table.status} = 'active'`),
],
);
export type BerthReservation = typeof berthReservations.$inferSelect;
export type NewBerthReservation = typeof berthReservations.$inferInsert;