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

@@ -0,0 +1,29 @@
CREATE TABLE "berth_reservations" (
"id" text PRIMARY KEY NOT NULL,
"berth_id" text NOT NULL,
"port_id" text NOT NULL,
"client_id" text NOT NULL,
"yacht_id" text NOT NULL,
"interest_id" text,
"status" text NOT NULL,
"start_date" timestamp with time zone NOT NULL,
"end_date" timestamp with time zone,
"tenure_type" text DEFAULT 'permanent' NOT NULL,
"contract_file_id" text,
"notes" text,
"created_by" text NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "berth_reservations" ADD CONSTRAINT "berth_reservations_berth_id_berths_id_fk" FOREIGN KEY ("berth_id") REFERENCES "public"."berths"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "berth_reservations" ADD CONSTRAINT "berth_reservations_port_id_ports_id_fk" FOREIGN KEY ("port_id") REFERENCES "public"."ports"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "berth_reservations" ADD CONSTRAINT "berth_reservations_client_id_clients_id_fk" FOREIGN KEY ("client_id") REFERENCES "public"."clients"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "berth_reservations" ADD CONSTRAINT "berth_reservations_yacht_id_yachts_id_fk" FOREIGN KEY ("yacht_id") REFERENCES "public"."yachts"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "berth_reservations" ADD CONSTRAINT "berth_reservations_interest_id_interests_id_fk" FOREIGN KEY ("interest_id") REFERENCES "public"."interests"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "berth_reservations" ADD CONSTRAINT "berth_reservations_contract_file_id_files_id_fk" FOREIGN KEY ("contract_file_id") REFERENCES "public"."files"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "idx_br_berth" ON "berth_reservations" USING btree ("berth_id");--> statement-breakpoint
CREATE INDEX "idx_br_client" ON "berth_reservations" USING btree ("client_id");--> statement-breakpoint
CREATE INDEX "idx_br_yacht" ON "berth_reservations" USING btree ("yacht_id");--> statement-breakpoint
CREATE INDEX "idx_br_port" ON "berth_reservations" USING btree ("port_id");--> statement-breakpoint
CREATE UNIQUE INDEX "idx_br_active" ON "berth_reservations" USING btree ("berth_id") WHERE "berth_reservations"."status" = 'active';

File diff suppressed because it is too large Load Diff

View File

@@ -29,6 +29,13 @@
"when": 1776959610819, "when": 1776959610819,
"tag": "0003_opposite_lucky_pierre", "tag": "0003_opposite_lucky_pierre",
"breakpoints": true "breakpoints": true
},
{
"idx": 4,
"version": "7",
"when": 1776959707066,
"tag": "0004_nasty_warstar",
"breakpoints": true
} }
] ]
} }

View File

@@ -19,6 +19,9 @@ export * from './interests';
// Berths // Berths
export * from './berths'; export * from './berths';
// Reservations
export * from './reservations';
// Documents & Files // Documents & Files
export * from './documents'; 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;