Massive multi-area push driven by docs/admin-ux-backlog.md. Every byte
path now goes through getStorageBackend() so signed EOIs, contracts,
brochures, berth PDFs, files, avatars, branding logos, and DB backups
all work identically on S3 and filesystem backends.
USER SETTINGS (rebuild)
- Country + Timezone selectors with cross-defaulting
- Browser-detected timezone banner ("Looks like you're in Europe/Paris…")
- Email change with verification flow (user_email_changes table,
OLD-address cancel link + NEW-address confirm link)
+ EMAIL_CHANGE_INSTANT=true dev shortcut
- Password reset triggered via better-auth requestPasswordReset
- Profile photo upload + crop (square 256×256) via shared
<ImageCropperDialog> + /api/v1/me/avatar
BRANDING
- Shared <ImageCropperDialog> using react-easy-crop
- Logo upload + crop in /admin/branding (writes via
/api/v1/admin/settings/image -> storage backend)
- Email header/footer HTML defaults injectable via "Insert default"
- SettingsFormCard new field types: timezone (combobox), image-upload
STORAGE ADMIN OVERHAUL
- S3 config form FIRST, swap action SECOND
- Test connection before any switch
- Two-button switch: "Switch + migrate" vs "Switch only" with
warning modals
- runMigration() honours skipMigration flag
- /api/ready + system-monitoring health check use the active
storage backend instead of always probing MinIO
- Filesystem backend already had full feature parity — verified
BACKUP MANAGEMENT (real)
- New backup_jobs table (id / status / trigger / size / storage_path)
- runBackup() service spawns pg_dump --format=custom, streams to
active storage backend via getStorageBackend().put()
- /admin/backup page: trigger, history, download .dump for restore
- Super-admin gated
AI ADMIN PANEL
- /admin/ai consolidates master switch + monthly token cap +
provider credentials
- Per-feature settings (OCR, berth-PDF parser, recommender)
linked from the same page
ONBOARDING WIZARD
- /admin/onboarding now real with auto-checked steps
- Reads each setting key + lists endpoint (roles/users/tags) to
decide completion
- Manual checkboxes for steps without an auto-detect signal
- Progress bar + Mark done/Mark incomplete buttons
- State persisted in system_settings.onboarding_manual_status
RESIDENTIAL PARITY (full)
- New residential_client_notes + residential_interest_notes tables
(mirror marina-side shape)
- Polymorphic notes.service.ts extended (verifyParent, listForEntity,
create, update, delete) for residential_clients/_interests
- <NotesList> component accepts the new entity types
- 4 new note endpoints (GET/POST/PATCH/DELETE for clients + interests)
- 2 new activity endpoints (residential clients + interests)
- residential-client-tabs.tsx + residential-interest-tabs.tsx use
DetailLayout (Overview / Interests / Notes / Activity)
- residential-client-detail-header.tsx mirrors marina-side strip
- useBreadcrumbHint wired into both detail components
- Configurable Assigned-to dropdown (residential_interests.view perm)
CONFIGURABLE RESIDENTIAL STAGES
- residential-stages.service.ts with list / save / orphan-check
- /api/v1/residential/stages GET/PUT
- /admin/residential-stages admin UI with reassign-on-remove modal
- Validators relaxed from z.enum to z.string
DOCUMENSO PHASE 1
- Schema: document_signers.invited_at / opened_at /
last_reminder_sent_at / signing_token (+ idx_ds_signing_token)
- Schema: documents.completion_cc_emails (text[]) +
auto_reminder_interval_days (int)
- transformSigningUrl() now maps SignerRole -> URL segment via
ROLE_TO_URL_SEGMENT (approver->cc, witness->witness) — fixes
Risk #5 where approver invites landed on /sign/error
- POST /api/v1/documents/[id]/send-invitation with auto-pick of
next pending signer
- Per-port settings: documenso_developer_label / _approver_label
+ documenso_developer_user_id / _approver_user_id (Phase 7
Project Director RBAC binding fields)
ADMIN UX RAPID-FIRE
- Sidebar collapse removed (always-expanded design)
- Audit log: input sizes (h-9), date pickers w-44, action cell
sub-label so single-row entries aren't blank
- Sales email config: token list <details> + tooltips on
threshold + body fields
- Custom Settings card: long-form description
- Reminder digest timezone uses TimezoneCombobox
- Port form: currency dropdown (10 common currencies) + timezone
combobox + brand color picker
- Permissions count badge opens modal with granted/denied per
resource
- Role names display-normalized via prettifyRoleName
- Tag form: native input type=color
- Custom Fields page: amber heads-up about non-integration
- Settings manager: select field type + fallthrough_policy as dropdown
- Storage admin S3 fields ship as proper password + boolean
LIST PAGES
- Residential client list: clickable email/phone (mailto/tel/wa.me)
- Residential interests + Documents Hub search inputs sized h-9
CURRENCY API
- scripts/test-currency-api.ts verifies live Frankfurter fetch
-> DB upsert -> getRate -> convert. Inverse-rate drift <=0.001
TESTS
- 1185/1185 vitest passing
- tsc clean
- eslint 0 errors (16 pre-existing warnings)
Note: WEBSITE_INTAKE_SECRET added to .env.example but committed
separately due to pre-commit hook policy on .env* files.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
281 lines
12 KiB
TypeScript
281 lines
12 KiB
TypeScript
import {
|
|
pgTable,
|
|
primaryKey,
|
|
text,
|
|
boolean,
|
|
integer,
|
|
timestamp,
|
|
jsonb,
|
|
index,
|
|
uniqueIndex,
|
|
} from 'drizzle-orm/pg-core';
|
|
import { sql } from 'drizzle-orm';
|
|
import { ports } from './ports';
|
|
import { clients } from './clients';
|
|
import { yachts } from './yachts';
|
|
import { companies } from './companies';
|
|
import { interests } from './interests';
|
|
import { berthReservations } from './reservations';
|
|
|
|
export const files = pgTable(
|
|
'files',
|
|
{
|
|
id: text('id')
|
|
.primaryKey()
|
|
.$defaultFn(() => crypto.randomUUID()),
|
|
portId: text('port_id')
|
|
.notNull()
|
|
.references(() => ports.id),
|
|
clientId: text('client_id').references(() => clients.id),
|
|
yachtId: text('yacht_id').references(() => yachts.id, { onDelete: 'set null' }),
|
|
companyId: text('company_id').references(() => companies.id, { onDelete: 'set null' }),
|
|
filename: text('filename').notNull(),
|
|
originalName: text('original_name').notNull(),
|
|
mimeType: text('mime_type'),
|
|
sizeBytes: text('size_bytes'), // stored as text to avoid bigint issues; parse as number in app
|
|
storagePath: text('storage_path').notNull(),
|
|
storageBucket: text('storage_bucket').notNull().default('crm-files'),
|
|
category: text('category'), // eoi, contract, image, receipt, correspondence, misc
|
|
uploadedBy: text('uploaded_by').notNull(),
|
|
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
|
},
|
|
(table) => [
|
|
index('idx_files_port').on(table.portId),
|
|
index('idx_files_client').on(table.clientId),
|
|
index('idx_files_yacht').on(table.yachtId),
|
|
index('idx_files_company').on(table.companyId),
|
|
],
|
|
);
|
|
|
|
export const documents = pgTable(
|
|
'documents',
|
|
{
|
|
id: text('id')
|
|
.primaryKey()
|
|
.$defaultFn(() => crypto.randomUUID()),
|
|
portId: text('port_id')
|
|
.notNull()
|
|
.references(() => ports.id),
|
|
interestId: text('interest_id').references(() => interests.id, { onDelete: 'set null' }),
|
|
clientId: text('client_id').references(() => clients.id),
|
|
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, {
|
|
onDelete: 'set null',
|
|
}),
|
|
documentType: text('document_type').notNull(), // eoi, contract, nda, reservation_agreement, other
|
|
title: text('title').notNull(),
|
|
status: text('status').notNull().default('draft'), // draft, sent, partially_signed, completed, expired, cancelled
|
|
documensoId: text('documenso_id'),
|
|
fileId: text('file_id').references(() => files.id),
|
|
signedFileId: text('signed_file_id').references(() => files.id),
|
|
isManualUpload: boolean('is_manual_upload').notNull().default(false),
|
|
/** Email addresses CC'd on the completion notification (the
|
|
* passive Documenso CC concept — see plan Q4). Per-document set
|
|
* by the rep; doesn't gate signing. */
|
|
completionCcEmails: text('completion_cc_emails').array().default([]),
|
|
/** Optional auto-reminder cadence — when set, a daily worker
|
|
* fires `sendSigningReminder()` for unsigned signers every
|
|
* N days until they complete. Null = manual reminders only. */
|
|
autoReminderIntervalDays: integer('auto_reminder_interval_days'),
|
|
notes: text('notes'),
|
|
remindersDisabled: boolean('reminders_disabled').notNull().default(false),
|
|
reminderCadenceOverride: integer('reminder_cadence_override'),
|
|
createdBy: text('created_by').notNull(),
|
|
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
|
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
|
|
},
|
|
(table) => [
|
|
index('idx_docs_port').on(table.portId),
|
|
index('idx_docs_interest').on(table.interestId),
|
|
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_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,
|
|
// deleting (or RESTRICT-checking) a referenced files row scans
|
|
// the documents table fully.
|
|
index('idx_docs_file_id').on(table.fileId),
|
|
index('idx_docs_signed_file_id').on(table.signedFileId),
|
|
],
|
|
);
|
|
|
|
export const documentSigners = pgTable(
|
|
'document_signers',
|
|
{
|
|
id: text('id')
|
|
.primaryKey()
|
|
.$defaultFn(() => crypto.randomUUID()),
|
|
documentId: text('document_id')
|
|
.notNull()
|
|
.references(() => documents.id, { onDelete: 'cascade' }),
|
|
signerName: text('signer_name').notNull(),
|
|
signerEmail: text('signer_email').notNull(),
|
|
signerRole: text('signer_role').notNull(), // client, developer, sales, approver, other
|
|
signingOrder: integer('signing_order').notNull(),
|
|
status: text('status').notNull().default('pending'), // pending, signed, declined
|
|
signedAt: timestamp('signed_at', { withTimezone: true }),
|
|
signingUrl: text('signing_url'),
|
|
embeddedUrl: text('embedded_url'),
|
|
/** Phase 1+2 lifecycle tracking — set by the send-invitation endpoint
|
|
* and the Documenso webhook handler respectively. */
|
|
invitedAt: timestamp('invited_at', { withTimezone: true }),
|
|
openedAt: timestamp('opened_at', { withTimezone: true }),
|
|
lastReminderSentAt: timestamp('last_reminder_sent_at', { withTimezone: true }),
|
|
/** Documenso recipient token — used for token-based lookup when the
|
|
* webhook fires (more robust than email match when one address
|
|
* serves multiple roles). */
|
|
signingToken: text('signing_token'),
|
|
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
|
},
|
|
(table) => [
|
|
index('idx_ds_doc').on(table.documentId),
|
|
index('idx_ds_signing_token').on(table.signingToken),
|
|
],
|
|
);
|
|
|
|
export const documentEvents = pgTable(
|
|
'document_events',
|
|
{
|
|
id: text('id')
|
|
.primaryKey()
|
|
.$defaultFn(() => crypto.randomUUID()),
|
|
documentId: text('document_id')
|
|
.notNull()
|
|
.references(() => documents.id, { onDelete: 'cascade' }),
|
|
eventType: text('event_type').notNull(), // created, sent, viewed, signed, completed, expired, reminder_sent
|
|
signerId: text('signer_id').references(() => documentSigners.id),
|
|
eventData: jsonb('event_data').default({}),
|
|
signatureHash: text('signature_hash'), // deduplication
|
|
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
|
},
|
|
(table) => [
|
|
index('idx_de_doc').on(table.documentId),
|
|
// Reverse-lookup signer→events without scanning the events table.
|
|
index('idx_de_signer').on(table.signerId),
|
|
uniqueIndex('idx_de_dedup')
|
|
.on(table.documentId, table.signatureHash)
|
|
.where(sql`${table.signatureHash} IS NOT NULL`),
|
|
],
|
|
);
|
|
|
|
export const documentTemplates = pgTable(
|
|
'document_templates',
|
|
{
|
|
id: text('id')
|
|
.primaryKey()
|
|
.$defaultFn(() => crypto.randomUUID()),
|
|
portId: text('port_id')
|
|
.notNull()
|
|
.references(() => ports.id),
|
|
name: text('name').notNull(),
|
|
description: text('description'),
|
|
templateType: text('template_type').notNull(), // welcome_letter, handover_checklist, acknowledgment, correspondence, custom
|
|
// Nullable: only required when template_format='html'.
|
|
bodyHtml: text('body_html'),
|
|
mergeFields: jsonb('merge_fields').notNull().default([]),
|
|
// 'html' | 'pdf_form' | 'pdf_overlay' | 'documenso_render'
|
|
templateFormat: text('template_format').notNull().default('html'),
|
|
sourceFileId: text('source_file_id').references(() => files.id),
|
|
documensoTemplateId: text('documenso_template_id'),
|
|
// pdf_form: { acroFieldName: mergeToken }
|
|
fieldMapping: jsonb('field_mapping').notNull().default({}),
|
|
// pdf_overlay: [{ token, page, x, y, fontSize }]
|
|
overlayPositions: jsonb('overlay_positions').notNull().default([]),
|
|
// null = no auto-reminders
|
|
reminderCadenceDays: integer('reminder_cadence_days'),
|
|
isActive: boolean('is_active').notNull().default(true),
|
|
createdBy: text('created_by').notNull(),
|
|
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
|
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
|
|
},
|
|
(table) => [
|
|
index('idx_dt_port').on(table.portId),
|
|
index('idx_dt_type').on(table.portId, table.templateType),
|
|
index('idx_dt_source_file').on(table.sourceFileId),
|
|
],
|
|
);
|
|
|
|
export const documentWatchers = pgTable(
|
|
'document_watchers',
|
|
{
|
|
documentId: text('document_id')
|
|
.notNull()
|
|
.references(() => documents.id, { onDelete: 'cascade' }),
|
|
userId: text('user_id').notNull(),
|
|
addedBy: text('added_by').notNull(),
|
|
addedAt: timestamp('added_at', { withTimezone: true }).notNull().defaultNow(),
|
|
},
|
|
(table) => [
|
|
primaryKey({ columns: [table.documentId, table.userId] }),
|
|
index('idx_doc_watchers_doc').on(table.documentId),
|
|
index('idx_doc_watchers_user').on(table.userId),
|
|
],
|
|
);
|
|
|
|
export const formTemplates = pgTable(
|
|
'form_templates',
|
|
{
|
|
id: text('id')
|
|
.primaryKey()
|
|
.$defaultFn(() => crypto.randomUUID()),
|
|
portId: text('port_id')
|
|
.notNull()
|
|
.references(() => ports.id),
|
|
name: text('name').notNull(),
|
|
description: text('description'),
|
|
fields: jsonb('fields').notNull(),
|
|
branding: jsonb('branding').default({}),
|
|
isActive: boolean('is_active').notNull().default(true),
|
|
createdBy: text('created_by').notNull(),
|
|
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
|
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
|
|
},
|
|
(table) => [index('idx_ft_port').on(table.portId)],
|
|
);
|
|
|
|
export const formSubmissions = pgTable(
|
|
'form_submissions',
|
|
{
|
|
id: text('id')
|
|
.primaryKey()
|
|
.$defaultFn(() => crypto.randomUUID()),
|
|
formTemplateId: text('form_template_id')
|
|
.notNull()
|
|
.references(() => formTemplates.id),
|
|
clientId: text('client_id').references(() => clients.id),
|
|
interestId: text('interest_id').references(() => interests.id, { onDelete: 'set null' }),
|
|
token: text('token').notNull().unique(),
|
|
prefilledData: jsonb('prefilled_data').default({}),
|
|
submittedData: jsonb('submitted_data'),
|
|
status: text('status').notNull().default('pending'), // pending, submitted, expired
|
|
expiresAt: timestamp('expires_at', { withTimezone: true }),
|
|
submittedAt: timestamp('submitted_at', { withTimezone: true }),
|
|
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
|
},
|
|
(table) => [
|
|
uniqueIndex('idx_fs_token').on(table.token),
|
|
index('idx_fs_template').on(table.formTemplateId),
|
|
index('idx_fs_client').on(table.clientId),
|
|
],
|
|
);
|
|
|
|
export type File = typeof files.$inferSelect;
|
|
export type NewFile = typeof files.$inferInsert;
|
|
export type Document = typeof documents.$inferSelect;
|
|
export type NewDocument = typeof documents.$inferInsert;
|
|
export type DocumentSigner = typeof documentSigners.$inferSelect;
|
|
export type NewDocumentSigner = typeof documentSigners.$inferInsert;
|
|
export type DocumentEvent = typeof documentEvents.$inferSelect;
|
|
export type NewDocumentEvent = typeof documentEvents.$inferInsert;
|
|
export type DocumentTemplate = typeof documentTemplates.$inferSelect;
|
|
export type NewDocumentTemplate = typeof documentTemplates.$inferInsert;
|
|
export type DocumentWatcher = typeof documentWatchers.$inferSelect;
|
|
export type NewDocumentWatcher = typeof documentWatchers.$inferInsert;
|
|
export type FormTemplate = typeof formTemplates.$inferSelect;
|
|
export type NewFormTemplate = typeof formTemplates.$inferInsert;
|
|
export type FormSubmission = typeof formSubmissions.$inferSelect;
|
|
export type NewFormSubmission = typeof formSubmissions.$inferInsert;
|