fix(audit-tier-1): timeouts, lifecycle, per-port Documenso, FK constraints
Closes the second wave of HIGH-priority audit findings: * fetchWithTimeout helper (new src/lib/fetch-with-timeout.ts) wraps Documenso, OCR, currency, Umami, IMAP, etc. — a hung upstream can no longer pin a worker concurrency slot indefinitely. OpenAI client passes timeout: 30_000. ImapFlow gets socket / greeting / connection timeouts. * SIGTERM / SIGINT handler in src/server.ts drains in-flight HTTP, closes Socket.io, and disconnects Redis before exit; compose stop_grace_period bumped to 30s. Adds closeSocketServer() helper. * env.ts gains zod-validated PORT and MULTI_NODE_DEPLOYMENT, and filesystem.ts now reads from env (a typo can no longer silently disable the multi-node guard). * Per-port Documenso template + recipient IDs land in system_settings with env fallback (PortDocumensoConfig now exposes eoiTemplateId, clientRecipientId, developerRecipientId, approvalRecipientId). document-templates.ts uses the per-port config and threads portId into documensoGenerateFromTemplate(). * Migration 0042 wires the eleven HIGH-tier missing FK constraints (documents/files/interests/reminders/berth_waiting_list/ form_submissions) plus polymorphic CHECK round 2 (yacht_ownership_history.owner_type, document_sends.document_kind), invoices.billing_entity_id NOT EMPTY, and clients.merged_into self-FK. Drizzle schema columns updated to .references(...) where possible so the misleading "FK wired in relations.ts" comments are gone. Test status: 1168/1168 vitest, tsc clean. Refs: docs/audit-comprehensive-2026-05-05.md HIGH §§5,6,7,8,9,10 + MED §§14,15,16,18. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,21 +1,17 @@
|
||||
import {
|
||||
pgTable,
|
||||
text,
|
||||
boolean,
|
||||
timestamp,
|
||||
jsonb,
|
||||
index,
|
||||
uniqueIndex,
|
||||
} from 'drizzle-orm/pg-core';
|
||||
import { pgTable, text, boolean, timestamp, jsonb, index, uniqueIndex } from 'drizzle-orm/pg-core';
|
||||
import { sql } from 'drizzle-orm';
|
||||
import { ports } from './ports';
|
||||
import { clients } from './clients';
|
||||
import { files } from './documents';
|
||||
import { interests } from './interests';
|
||||
import { berths } from './berths';
|
||||
|
||||
export const reminders = pgTable(
|
||||
'reminders',
|
||||
{
|
||||
id: text('id').primaryKey().$defaultFn(() => crypto.randomUUID()),
|
||||
id: text('id')
|
||||
.primaryKey()
|
||||
.$defaultFn(() => crypto.randomUUID()),
|
||||
portId: text('port_id')
|
||||
.notNull()
|
||||
.references(() => ports.id),
|
||||
@@ -27,8 +23,8 @@ export const reminders = pgTable(
|
||||
assignedTo: text('assigned_to'), // user ID
|
||||
createdBy: text('created_by').notNull(),
|
||||
clientId: text('client_id').references(() => clients.id),
|
||||
interestId: text('interest_id'), // references interests.id
|
||||
berthId: text('berth_id'), // references berths.id
|
||||
interestId: text('interest_id').references(() => interests.id, { onDelete: 'set null' }),
|
||||
berthId: text('berth_id').references(() => berths.id, { onDelete: 'set null' }),
|
||||
autoGenerated: boolean('auto_generated').notNull().default(false),
|
||||
googleCalendarEventId: text('google_calendar_event_id'),
|
||||
googleCalendarSynced: boolean('google_calendar_synced').notNull().default(false),
|
||||
@@ -40,16 +36,18 @@ export const reminders = pgTable(
|
||||
(table) => [
|
||||
index('idx_reminders_port').on(table.portId),
|
||||
index('idx_reminders_assigned').on(table.assignedTo, table.status),
|
||||
index('idx_reminders_due').on(table.portId, table.dueAt).where(
|
||||
sql`${table.status} IN ('pending', 'snoozed')`
|
||||
),
|
||||
index('idx_reminders_due')
|
||||
.on(table.portId, table.dueAt)
|
||||
.where(sql`${table.status} IN ('pending', 'snoozed')`),
|
||||
],
|
||||
);
|
||||
|
||||
export const googleCalendarTokens = pgTable(
|
||||
'google_calendar_tokens',
|
||||
{
|
||||
id: text('id').primaryKey().$defaultFn(() => crypto.randomUUID()),
|
||||
id: text('id')
|
||||
.primaryKey()
|
||||
.$defaultFn(() => crypto.randomUUID()),
|
||||
userId: text('user_id').notNull().unique(),
|
||||
accessToken: text('access_token').notNull(), // encrypted
|
||||
refreshToken: text('refresh_token').notNull(), // encrypted
|
||||
@@ -67,7 +65,9 @@ export const googleCalendarTokens = pgTable(
|
||||
export const googleCalendarCache = pgTable(
|
||||
'google_calendar_cache',
|
||||
{
|
||||
id: text('id').primaryKey().$defaultFn(() => crypto.randomUUID()),
|
||||
id: text('id')
|
||||
.primaryKey()
|
||||
.$defaultFn(() => crypto.randomUUID()),
|
||||
userId: text('user_id').notNull(),
|
||||
eventId: text('event_id').notNull(), // Google Calendar event ID
|
||||
title: text('title').notNull(),
|
||||
@@ -88,7 +88,9 @@ export const googleCalendarCache = pgTable(
|
||||
export const notifications = pgTable(
|
||||
'notifications',
|
||||
{
|
||||
id: text('id').primaryKey().$defaultFn(() => crypto.randomUUID()),
|
||||
id: text('id')
|
||||
.primaryKey()
|
||||
.$defaultFn(() => crypto.randomUUID()),
|
||||
portId: text('port_id')
|
||||
.notNull()
|
||||
.references(() => ports.id),
|
||||
@@ -114,7 +116,9 @@ export const notifications = pgTable(
|
||||
export const scheduledReports = pgTable(
|
||||
'scheduled_reports',
|
||||
{
|
||||
id: text('id').primaryKey().$defaultFn(() => crypto.randomUUID()),
|
||||
id: text('id')
|
||||
.primaryKey()
|
||||
.$defaultFn(() => crypto.randomUUID()),
|
||||
portId: text('port_id')
|
||||
.notNull()
|
||||
.references(() => ports.id),
|
||||
@@ -135,7 +139,9 @@ export const scheduledReports = pgTable(
|
||||
export const reportRecipients = pgTable(
|
||||
'report_recipients',
|
||||
{
|
||||
id: text('id').primaryKey().$defaultFn(() => crypto.randomUUID()),
|
||||
id: text('id')
|
||||
.primaryKey()
|
||||
.$defaultFn(() => crypto.randomUUID()),
|
||||
reportId: text('report_id')
|
||||
.notNull()
|
||||
.references(() => scheduledReports.id, { onDelete: 'cascade' }),
|
||||
@@ -151,7 +157,9 @@ export const reportRecipients = pgTable(
|
||||
export const generatedReports = pgTable(
|
||||
'generated_reports',
|
||||
{
|
||||
id: text('id').primaryKey().$defaultFn(() => crypto.randomUUID()),
|
||||
id: text('id')
|
||||
.primaryKey()
|
||||
.$defaultFn(() => crypto.randomUUID()),
|
||||
portId: text('port_id')
|
||||
.notNull()
|
||||
.references(() => ports.id),
|
||||
@@ -171,9 +179,9 @@ export const generatedReports = pgTable(
|
||||
(table) => [
|
||||
index('idx_gr_port_created').on(table.portId, table.createdAt),
|
||||
index('idx_gr_port_status').on(table.portId, table.status),
|
||||
index('idx_gr_scheduled').on(table.scheduledReportId).where(
|
||||
sql`${table.scheduledReportId} IS NOT NULL`
|
||||
),
|
||||
index('idx_gr_scheduled')
|
||||
.on(table.scheduledReportId)
|
||||
.where(sql`${table.scheduledReportId} IS NOT NULL`),
|
||||
],
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user