Phase 7 of the berth-recommender refactor (plan §3.3, §4.8, §4.9, §5.7,
§5.8, §5.9, §11.1, §14.7, §14.9). Adds the rep-driven send-out path for
per-berth PDFs and port-wide brochures, the per-port sales SMTP/IMAP
config + body templates, and the supporting admin UI.
Migration: 0031_brochures_and_document_sends.sql
Schema additions:
- brochures (port-wide, with isDefault marker + archive)
- brochure_versions (versioned uploads, storageKey per §4.7a)
- document_sends (audit log of every rep-initiated send; failures
captured with failedAt + errorReason). berthPdfVersionId is a plain
text column (no FK) — loose-coupled to Phase 6b's berth_pdf_versions
so the two phases stay independent.
§14.7 critical mitigations:
- Body XSS: rep-authored markdown goes through renderEmailBody()
(HTML-escape first, then a tight allowlist of bold/italic/code/link
rules). https:// + mailto: only — javascript:/data: URLs stripped.
Tested against script/img/iframe/svg/onerror polyglots.
- Recipient typo: strict email regex + two-step confirm modal that
shows the exact recipient before send.
- Unresolved merge fields: pre-send dry-run /preview endpoint blocks
submission until findUnresolvedTokens() returns empty.
- SMTP failure: every transport rejection writes a document_sends row
with failedAt + errorReason; UI surfaces the message.
- Hourly per-user rate limit: 50 sends/user/hour via existing
checkRateLimit().
- Size threshold fallback (§11.1): files above
email_attach_threshold_mb (default 15) ship as a 24h signed-URL
download link in the body instead of an attachment. Storage stream
flows directly to nodemailer to avoid buffering 20MB+.
§14.10 critical mitigation:
- SMTP/IMAP passwords encrypted at rest via the existing
EMAIL_CREDENTIAL_KEY (AES-256-GCM). The /api/v1/admin/email/
sales-config GET endpoint never returns the decrypted value — only
a *PassIsSet boolean. PATCH treats empty string as "leave unchanged"
and explicit null as "clear", so the masked-placeholder UI round-
trips without forcing re-entry on every save.
system_settings keys (per-port unless noted):
- sales_from_address, sales_smtp_{host,port,secure,user,pass_encrypted}
- sales_imap_{host,port,user,pass_encrypted}
- sales_auth_method (default app_password)
- noreply_from_address
- email_template_send_berth_pdf_body, email_template_send_brochure_body
- brochure_max_upload_mb (default 50)
- email_attach_threshold_mb (default 15)
UI surfaces (per §5.7, §5.8, §5.9):
- <SendDocumentDialog> shared 2-step compose+confirm flow.
- <SendBerthPdfDialog>, <SendDocumentsDialog>, <SendFromInterestButton>
wrappers per detail page.
- /[portSlug]/admin/brochures: list, upload (direct-to-storage
presigned PUT for the 20MB+ files per §11.1), default toggle,
archive.
- /[portSlug]/admin/email extended with <SalesEmailConfigCard>:
SMTP + IMAP creds, body templates, threshold/max settings.
Storage: every upload + download goes through getStorageBackend() —
no direct minio imports, per Phase 6a contract.
Tests: 1145 vitest passing (+ 50 new in
markdown-email-sanitization.test.ts, document-sends-validators.test.ts,
sales-email-config-validators.test.ts).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
935 lines
27 KiB
TypeScript
935 lines
27 KiB
TypeScript
import { relations } from 'drizzle-orm';
|
|
|
|
// Ports
|
|
import { ports } from './ports';
|
|
|
|
// Users
|
|
import { userProfiles, roles, portRoleOverrides, userPortRoles } from './users';
|
|
|
|
// Clients
|
|
import {
|
|
clients,
|
|
clientContacts,
|
|
clientRelationships,
|
|
clientNotes,
|
|
clientTags,
|
|
clientMergeLog,
|
|
clientAddresses,
|
|
} from './clients';
|
|
|
|
// Interests
|
|
import { interests, interestNotes, interestTags, interestBerths } from './interests';
|
|
|
|
// Yachts
|
|
import { yachts, yachtOwnershipHistory, yachtNotes, yachtTags } from './yachts';
|
|
|
|
// Companies
|
|
import {
|
|
companies,
|
|
companyMemberships,
|
|
companyAddresses,
|
|
companyNotes,
|
|
companyTags,
|
|
} from './companies';
|
|
|
|
// Berths
|
|
import {
|
|
berths,
|
|
berthMapData,
|
|
berthRecommendations,
|
|
berthWaitingList,
|
|
berthMaintenanceLog,
|
|
berthTags,
|
|
berthPdfVersions,
|
|
} from './berths';
|
|
|
|
// Reservations
|
|
import { berthReservations } from './reservations';
|
|
|
|
// Documents
|
|
import {
|
|
files,
|
|
documents,
|
|
documentSigners,
|
|
documentEvents,
|
|
documentTemplates,
|
|
documentWatchers,
|
|
formTemplates,
|
|
formSubmissions,
|
|
} from './documents';
|
|
|
|
// Brochures + send-outs (Phase 7)
|
|
import { brochures, brochureVersions, documentSends } from './brochures';
|
|
|
|
// Financial
|
|
import { expenses, invoices, invoiceLineItems, invoiceExpenses } from './financial';
|
|
|
|
// Email
|
|
import { emailAccounts, emailThreads, emailMessages } from './email';
|
|
|
|
// Operations
|
|
import {
|
|
reminders,
|
|
googleCalendarCache,
|
|
googleCalendarTokens,
|
|
notifications,
|
|
scheduledReports,
|
|
reportRecipients,
|
|
generatedReports,
|
|
} from './operations';
|
|
|
|
// System
|
|
import {
|
|
auditLogs,
|
|
tags,
|
|
webhooks,
|
|
webhookDeliveries,
|
|
systemSettings,
|
|
savedViews,
|
|
scratchpadNotes,
|
|
userNotificationPreferences,
|
|
customFieldDefinitions,
|
|
customFieldValues,
|
|
} from './system';
|
|
import { residentialClients, residentialInterests } from './residential';
|
|
|
|
// ─── Ports ────────────────────────────────────────────────────────────────────
|
|
|
|
export const portsRelations = relations(ports, ({ many }) => ({
|
|
userPortRoles: many(userPortRoles),
|
|
portRoleOverrides: many(portRoleOverrides),
|
|
clients: many(clients),
|
|
interests: many(interests),
|
|
yachts: many(yachts),
|
|
companies: many(companies),
|
|
berths: many(berths),
|
|
berthReservations: many(berthReservations),
|
|
documents: many(documents),
|
|
documentTemplates: many(documentTemplates),
|
|
formTemplates: many(formTemplates),
|
|
expenses: many(expenses),
|
|
invoices: many(invoices),
|
|
emailAccounts: many(emailAccounts),
|
|
emailThreads: many(emailThreads),
|
|
reminders: many(reminders),
|
|
notifications: many(notifications),
|
|
scheduledReports: many(scheduledReports),
|
|
auditLogs: many(auditLogs),
|
|
tags: many(tags),
|
|
files: many(files),
|
|
webhooks: many(webhooks),
|
|
systemSettings: many(systemSettings),
|
|
savedViews: many(savedViews),
|
|
userNotificationPreferences: many(userNotificationPreferences),
|
|
customFieldDefinitions: many(customFieldDefinitions),
|
|
residentialClients: many(residentialClients),
|
|
residentialInterests: many(residentialInterests),
|
|
berthMaintenanceLogs: many(berthMaintenanceLog),
|
|
clientMergeLogs: many(clientMergeLog),
|
|
clientRelationships: many(clientRelationships),
|
|
clientAddresses: many(clientAddresses),
|
|
}));
|
|
|
|
// ─── Users ────────────────────────────────────────────────────────────────────
|
|
|
|
export const userProfilesRelations = relations(userProfiles, ({ many }) => ({
|
|
userPortRoles: many(userPortRoles),
|
|
}));
|
|
|
|
export const rolesRelations = relations(roles, ({ many }) => ({
|
|
userPortRoles: many(userPortRoles),
|
|
portRoleOverrides: many(portRoleOverrides),
|
|
}));
|
|
|
|
export const portRoleOverridesRelations = relations(portRoleOverrides, ({ one }) => ({
|
|
port: one(ports, {
|
|
fields: [portRoleOverrides.portId],
|
|
references: [ports.id],
|
|
}),
|
|
role: one(roles, {
|
|
fields: [portRoleOverrides.roleId],
|
|
references: [roles.id],
|
|
}),
|
|
}));
|
|
|
|
export const userPortRolesRelations = relations(userPortRoles, ({ one }) => ({
|
|
port: one(ports, {
|
|
fields: [userPortRoles.portId],
|
|
references: [ports.id],
|
|
}),
|
|
role: one(roles, {
|
|
fields: [userPortRoles.roleId],
|
|
references: [roles.id],
|
|
}),
|
|
}));
|
|
|
|
// ─── Clients ──────────────────────────────────────────────────────────────────
|
|
|
|
export const clientsRelations = relations(clients, ({ one, many }) => ({
|
|
port: one(ports, {
|
|
fields: [clients.portId],
|
|
references: [ports.id],
|
|
}),
|
|
contacts: many(clientContacts),
|
|
notes: many(clientNotes),
|
|
tags: many(clientTags),
|
|
interests: many(interests),
|
|
relationships_a: many(clientRelationships, { relationName: 'client_a' }),
|
|
relationships_b: many(clientRelationships, { relationName: 'client_b' }),
|
|
mergeLogsAsSurvivor: many(clientMergeLog),
|
|
documents: many(documents),
|
|
emailThreads: many(emailThreads),
|
|
reminders: many(reminders),
|
|
files: many(files),
|
|
waitingListEntries: many(berthWaitingList),
|
|
scratchpadNotes: many(scratchpadNotes),
|
|
formSubmissions: many(formSubmissions),
|
|
addresses: many(clientAddresses),
|
|
companyMemberships: many(companyMemberships),
|
|
berthReservations: many(berthReservations),
|
|
}));
|
|
|
|
export const clientContactsRelations = relations(clientContacts, ({ one }) => ({
|
|
client: one(clients, {
|
|
fields: [clientContacts.clientId],
|
|
references: [clients.id],
|
|
}),
|
|
}));
|
|
|
|
export const clientAddressesRelations = relations(clientAddresses, ({ one }) => ({
|
|
client: one(clients, {
|
|
fields: [clientAddresses.clientId],
|
|
references: [clients.id],
|
|
}),
|
|
port: one(ports, {
|
|
fields: [clientAddresses.portId],
|
|
references: [ports.id],
|
|
}),
|
|
}));
|
|
|
|
export const clientRelationshipsRelations = relations(clientRelationships, ({ one }) => ({
|
|
port: one(ports, {
|
|
fields: [clientRelationships.portId],
|
|
references: [ports.id],
|
|
}),
|
|
clientA: one(clients, {
|
|
fields: [clientRelationships.clientAId],
|
|
references: [clients.id],
|
|
relationName: 'client_a',
|
|
}),
|
|
clientB: one(clients, {
|
|
fields: [clientRelationships.clientBId],
|
|
references: [clients.id],
|
|
relationName: 'client_b',
|
|
}),
|
|
}));
|
|
|
|
export const clientNotesRelations = relations(clientNotes, ({ one }) => ({
|
|
client: one(clients, {
|
|
fields: [clientNotes.clientId],
|
|
references: [clients.id],
|
|
}),
|
|
}));
|
|
|
|
export const clientTagsRelations = relations(clientTags, ({ one }) => ({
|
|
client: one(clients, {
|
|
fields: [clientTags.clientId],
|
|
references: [clients.id],
|
|
}),
|
|
tag: one(tags, {
|
|
fields: [clientTags.tagId],
|
|
references: [tags.id],
|
|
}),
|
|
}));
|
|
|
|
export const clientMergeLogRelations = relations(clientMergeLog, ({ one }) => ({
|
|
port: one(ports, {
|
|
fields: [clientMergeLog.portId],
|
|
references: [ports.id],
|
|
}),
|
|
survivingClient: one(clients, {
|
|
fields: [clientMergeLog.survivingClientId],
|
|
references: [clients.id],
|
|
}),
|
|
}));
|
|
|
|
// ─── Interests ────────────────────────────────────────────────────────────────
|
|
|
|
export const interestsRelations = relations(interests, ({ one, many }) => ({
|
|
port: one(ports, {
|
|
fields: [interests.portId],
|
|
references: [ports.id],
|
|
}),
|
|
client: one(clients, {
|
|
fields: [interests.clientId],
|
|
references: [clients.id],
|
|
}),
|
|
yacht: one(yachts, {
|
|
fields: [interests.yachtId],
|
|
references: [yachts.id],
|
|
}),
|
|
notes: many(interestNotes),
|
|
tags: many(interestTags),
|
|
documents: many(documents),
|
|
reminders: many(reminders),
|
|
berthRecommendations: many(berthRecommendations),
|
|
formSubmissions: many(formSubmissions),
|
|
interestBerths: many(interestBerths),
|
|
}));
|
|
|
|
export const interestBerthsRelations = relations(interestBerths, ({ one }) => ({
|
|
interest: one(interests, {
|
|
fields: [interestBerths.interestId],
|
|
references: [interests.id],
|
|
}),
|
|
berth: one(berths, {
|
|
fields: [interestBerths.berthId],
|
|
references: [berths.id],
|
|
}),
|
|
}));
|
|
|
|
export const interestNotesRelations = relations(interestNotes, ({ one }) => ({
|
|
interest: one(interests, {
|
|
fields: [interestNotes.interestId],
|
|
references: [interests.id],
|
|
}),
|
|
}));
|
|
|
|
export const interestTagsRelations = relations(interestTags, ({ one }) => ({
|
|
interest: one(interests, {
|
|
fields: [interestTags.interestId],
|
|
references: [interests.id],
|
|
}),
|
|
tag: one(tags, {
|
|
fields: [interestTags.tagId],
|
|
references: [tags.id],
|
|
}),
|
|
}));
|
|
|
|
// ─── Yachts ───────────────────────────────────────────────────────────────────
|
|
|
|
export const yachtsRelations = relations(yachts, ({ one, many }) => ({
|
|
port: one(ports, {
|
|
fields: [yachts.portId],
|
|
references: [ports.id],
|
|
}),
|
|
ownershipHistory: many(yachtOwnershipHistory),
|
|
notes: many(yachtNotes),
|
|
tags: many(yachtTags),
|
|
interests: many(interests),
|
|
reservations: many(berthReservations),
|
|
documents: many(documents),
|
|
}));
|
|
|
|
export const yachtOwnershipHistoryRelations = relations(yachtOwnershipHistory, ({ one }) => ({
|
|
yacht: one(yachts, {
|
|
fields: [yachtOwnershipHistory.yachtId],
|
|
references: [yachts.id],
|
|
}),
|
|
}));
|
|
|
|
export const yachtNotesRelations = relations(yachtNotes, ({ one }) => ({
|
|
yacht: one(yachts, {
|
|
fields: [yachtNotes.yachtId],
|
|
references: [yachts.id],
|
|
}),
|
|
}));
|
|
|
|
export const yachtTagsRelations = relations(yachtTags, ({ one }) => ({
|
|
yacht: one(yachts, {
|
|
fields: [yachtTags.yachtId],
|
|
references: [yachts.id],
|
|
}),
|
|
tag: one(tags, {
|
|
fields: [yachtTags.tagId],
|
|
references: [tags.id],
|
|
}),
|
|
}));
|
|
|
|
// ─── Companies ────────────────────────────────────────────────────────────────
|
|
|
|
export const companiesRelations = relations(companies, ({ one, many }) => ({
|
|
port: one(ports, {
|
|
fields: [companies.portId],
|
|
references: [ports.id],
|
|
}),
|
|
memberships: many(companyMemberships),
|
|
addresses: many(companyAddresses),
|
|
notes: many(companyNotes),
|
|
tags: many(companyTags),
|
|
documents: many(documents),
|
|
files: many(files),
|
|
}));
|
|
|
|
export const companyMembershipsRelations = relations(companyMemberships, ({ one }) => ({
|
|
company: one(companies, {
|
|
fields: [companyMemberships.companyId],
|
|
references: [companies.id],
|
|
}),
|
|
client: one(clients, {
|
|
fields: [companyMemberships.clientId],
|
|
references: [clients.id],
|
|
}),
|
|
}));
|
|
|
|
export const companyAddressesRelations = relations(companyAddresses, ({ one }) => ({
|
|
company: one(companies, {
|
|
fields: [companyAddresses.companyId],
|
|
references: [companies.id],
|
|
}),
|
|
port: one(ports, {
|
|
fields: [companyAddresses.portId],
|
|
references: [ports.id],
|
|
}),
|
|
}));
|
|
|
|
export const companyNotesRelations = relations(companyNotes, ({ one }) => ({
|
|
company: one(companies, {
|
|
fields: [companyNotes.companyId],
|
|
references: [companies.id],
|
|
}),
|
|
}));
|
|
|
|
export const companyTagsRelations = relations(companyTags, ({ one }) => ({
|
|
company: one(companies, {
|
|
fields: [companyTags.companyId],
|
|
references: [companies.id],
|
|
}),
|
|
tag: one(tags, {
|
|
fields: [companyTags.tagId],
|
|
references: [tags.id],
|
|
}),
|
|
}));
|
|
|
|
// ─── Berths ───────────────────────────────────────────────────────────────────
|
|
|
|
export const berthsRelations = relations(berths, ({ one, many }) => ({
|
|
port: one(ports, {
|
|
fields: [berths.portId],
|
|
references: [ports.id],
|
|
}),
|
|
mapData: one(berthMapData),
|
|
recommendations: many(berthRecommendations),
|
|
waitingList: many(berthWaitingList),
|
|
maintenanceLogs: many(berthMaintenanceLog),
|
|
tags: many(berthTags),
|
|
interestBerths: many(interestBerths),
|
|
reminders: many(reminders),
|
|
pdfVersions: many(berthPdfVersions),
|
|
currentPdfVersion: one(berthPdfVersions, {
|
|
fields: [berths.currentPdfVersionId],
|
|
references: [berthPdfVersions.id],
|
|
relationName: 'berthCurrentPdfVersion',
|
|
}),
|
|
}));
|
|
|
|
export const berthPdfVersionsRelations = relations(berthPdfVersions, ({ one }) => ({
|
|
berth: one(berths, {
|
|
fields: [berthPdfVersions.berthId],
|
|
references: [berths.id],
|
|
}),
|
|
}));
|
|
|
|
export const berthMapDataRelations = relations(berthMapData, ({ one }) => ({
|
|
berth: one(berths, {
|
|
fields: [berthMapData.berthId],
|
|
references: [berths.id],
|
|
}),
|
|
}));
|
|
|
|
export const berthRecommendationsRelations = relations(berthRecommendations, ({ one }) => ({
|
|
interest: one(interests, {
|
|
fields: [berthRecommendations.interestId],
|
|
references: [interests.id],
|
|
}),
|
|
berth: one(berths, {
|
|
fields: [berthRecommendations.berthId],
|
|
references: [berths.id],
|
|
}),
|
|
}));
|
|
|
|
export const berthWaitingListRelations = relations(berthWaitingList, ({ one }) => ({
|
|
berth: one(berths, {
|
|
fields: [berthWaitingList.berthId],
|
|
references: [berths.id],
|
|
}),
|
|
client: one(clients, {
|
|
fields: [berthWaitingList.clientId],
|
|
references: [clients.id],
|
|
}),
|
|
}));
|
|
|
|
export const berthMaintenanceLogRelations = relations(berthMaintenanceLog, ({ one }) => ({
|
|
berth: one(berths, {
|
|
fields: [berthMaintenanceLog.berthId],
|
|
references: [berths.id],
|
|
}),
|
|
port: one(ports, {
|
|
fields: [berthMaintenanceLog.portId],
|
|
references: [ports.id],
|
|
}),
|
|
}));
|
|
|
|
export const berthTagsRelations = relations(berthTags, ({ one }) => ({
|
|
berth: one(berths, {
|
|
fields: [berthTags.berthId],
|
|
references: [berths.id],
|
|
}),
|
|
tag: one(tags, {
|
|
fields: [berthTags.tagId],
|
|
references: [tags.id],
|
|
}),
|
|
}));
|
|
|
|
// ─── Berth Reservations ───────────────────────────────────────────────────────
|
|
|
|
export const berthReservationsRelations = relations(berthReservations, ({ one, many }) => ({
|
|
berth: one(berths, {
|
|
fields: [berthReservations.berthId],
|
|
references: [berths.id],
|
|
}),
|
|
port: one(ports, {
|
|
fields: [berthReservations.portId],
|
|
references: [ports.id],
|
|
}),
|
|
client: one(clients, {
|
|
fields: [berthReservations.clientId],
|
|
references: [clients.id],
|
|
}),
|
|
yacht: one(yachts, {
|
|
fields: [berthReservations.yachtId],
|
|
references: [yachts.id],
|
|
}),
|
|
interest: one(interests, {
|
|
fields: [berthReservations.interestId],
|
|
references: [interests.id],
|
|
}),
|
|
contractFile: one(files, {
|
|
fields: [berthReservations.contractFileId],
|
|
references: [files.id],
|
|
}),
|
|
documents: many(documents),
|
|
}));
|
|
|
|
// ─── Documents ────────────────────────────────────────────────────────────────
|
|
|
|
export const filesRelations = relations(files, ({ one, many }) => ({
|
|
port: one(ports, {
|
|
fields: [files.portId],
|
|
references: [ports.id],
|
|
}),
|
|
client: one(clients, {
|
|
fields: [files.clientId],
|
|
references: [clients.id],
|
|
}),
|
|
yacht: one(yachts, {
|
|
fields: [files.yachtId],
|
|
references: [yachts.id],
|
|
}),
|
|
company: one(companies, {
|
|
fields: [files.companyId],
|
|
references: [companies.id],
|
|
}),
|
|
documentAsFile: many(documents, { relationName: 'file' }),
|
|
documentAsSignedFile: many(documents, { relationName: 'signed_file' }),
|
|
}));
|
|
|
|
export const documentsRelations = relations(documents, ({ one, many }) => ({
|
|
port: one(ports, {
|
|
fields: [documents.portId],
|
|
references: [ports.id],
|
|
}),
|
|
interest: one(interests, {
|
|
fields: [documents.interestId],
|
|
references: [interests.id],
|
|
}),
|
|
client: one(clients, {
|
|
fields: [documents.clientId],
|
|
references: [clients.id],
|
|
}),
|
|
file: one(files, {
|
|
fields: [documents.fileId],
|
|
references: [files.id],
|
|
relationName: 'file',
|
|
}),
|
|
signedFile: one(files, {
|
|
fields: [documents.signedFileId],
|
|
references: [files.id],
|
|
relationName: 'signed_file',
|
|
}),
|
|
yacht: one(yachts, {
|
|
fields: [documents.yachtId],
|
|
references: [yachts.id],
|
|
}),
|
|
company: one(companies, {
|
|
fields: [documents.companyId],
|
|
references: [companies.id],
|
|
}),
|
|
reservation: one(berthReservations, {
|
|
fields: [documents.reservationId],
|
|
references: [berthReservations.id],
|
|
}),
|
|
signers: many(documentSigners),
|
|
events: many(documentEvents),
|
|
watchers: many(documentWatchers),
|
|
}));
|
|
|
|
export const documentSignersRelations = relations(documentSigners, ({ one, many }) => ({
|
|
document: one(documents, {
|
|
fields: [documentSigners.documentId],
|
|
references: [documents.id],
|
|
}),
|
|
events: many(documentEvents),
|
|
}));
|
|
|
|
export const documentEventsRelations = relations(documentEvents, ({ one }) => ({
|
|
document: one(documents, {
|
|
fields: [documentEvents.documentId],
|
|
references: [documents.id],
|
|
}),
|
|
signer: one(documentSigners, {
|
|
fields: [documentEvents.signerId],
|
|
references: [documentSigners.id],
|
|
}),
|
|
}));
|
|
|
|
export const documentTemplatesRelations = relations(documentTemplates, ({ one }) => ({
|
|
port: one(ports, {
|
|
fields: [documentTemplates.portId],
|
|
references: [ports.id],
|
|
}),
|
|
sourceFile: one(files, {
|
|
fields: [documentTemplates.sourceFileId],
|
|
references: [files.id],
|
|
}),
|
|
}));
|
|
|
|
export const documentWatchersRelations = relations(documentWatchers, ({ one }) => ({
|
|
document: one(documents, {
|
|
fields: [documentWatchers.documentId],
|
|
references: [documents.id],
|
|
}),
|
|
}));
|
|
|
|
export const formTemplatesRelations = relations(formTemplates, ({ one, many }) => ({
|
|
port: one(ports, {
|
|
fields: [formTemplates.portId],
|
|
references: [ports.id],
|
|
}),
|
|
submissions: many(formSubmissions),
|
|
}));
|
|
|
|
export const formSubmissionsRelations = relations(formSubmissions, ({ one }) => ({
|
|
formTemplate: one(formTemplates, {
|
|
fields: [formSubmissions.formTemplateId],
|
|
references: [formTemplates.id],
|
|
}),
|
|
client: one(clients, {
|
|
fields: [formSubmissions.clientId],
|
|
references: [clients.id],
|
|
}),
|
|
interest: one(interests, {
|
|
fields: [formSubmissions.interestId],
|
|
references: [interests.id],
|
|
}),
|
|
}));
|
|
|
|
// ─── Financial ────────────────────────────────────────────────────────────────
|
|
|
|
export const expensesRelations = relations(expenses, ({ one, many }) => ({
|
|
port: one(ports, {
|
|
fields: [expenses.portId],
|
|
references: [ports.id],
|
|
}),
|
|
invoiceExpenses: many(invoiceExpenses),
|
|
}));
|
|
|
|
export const invoicesRelations = relations(invoices, ({ one, many }) => ({
|
|
port: one(ports, {
|
|
fields: [invoices.portId],
|
|
references: [ports.id],
|
|
}),
|
|
pdfFile: one(files, {
|
|
fields: [invoices.pdfFileId],
|
|
references: [files.id],
|
|
}),
|
|
lineItems: many(invoiceLineItems),
|
|
invoiceExpenses: many(invoiceExpenses),
|
|
}));
|
|
|
|
export const invoiceLineItemsRelations = relations(invoiceLineItems, ({ one }) => ({
|
|
invoice: one(invoices, {
|
|
fields: [invoiceLineItems.invoiceId],
|
|
references: [invoices.id],
|
|
}),
|
|
}));
|
|
|
|
export const invoiceExpensesRelations = relations(invoiceExpenses, ({ one }) => ({
|
|
invoice: one(invoices, {
|
|
fields: [invoiceExpenses.invoiceId],
|
|
references: [invoices.id],
|
|
}),
|
|
expense: one(expenses, {
|
|
fields: [invoiceExpenses.expenseId],
|
|
references: [expenses.id],
|
|
}),
|
|
}));
|
|
|
|
// ─── Email ────────────────────────────────────────────────────────────────────
|
|
|
|
export const emailAccountsRelations = relations(emailAccounts, ({ one }) => ({
|
|
port: one(ports, {
|
|
fields: [emailAccounts.portId],
|
|
references: [ports.id],
|
|
}),
|
|
}));
|
|
|
|
export const emailThreadsRelations = relations(emailThreads, ({ one, many }) => ({
|
|
port: one(ports, {
|
|
fields: [emailThreads.portId],
|
|
references: [ports.id],
|
|
}),
|
|
client: one(clients, {
|
|
fields: [emailThreads.clientId],
|
|
references: [clients.id],
|
|
}),
|
|
messages: many(emailMessages),
|
|
}));
|
|
|
|
export const emailMessagesRelations = relations(emailMessages, ({ one }) => ({
|
|
thread: one(emailThreads, {
|
|
fields: [emailMessages.threadId],
|
|
references: [emailThreads.id],
|
|
}),
|
|
rawFile: one(files, {
|
|
fields: [emailMessages.rawFileId],
|
|
references: [files.id],
|
|
}),
|
|
}));
|
|
|
|
// ─── Operations ───────────────────────────────────────────────────────────────
|
|
|
|
export const remindersRelations = relations(reminders, ({ one, many }) => ({
|
|
port: one(ports, {
|
|
fields: [reminders.portId],
|
|
references: [ports.id],
|
|
}),
|
|
client: one(clients, {
|
|
fields: [reminders.clientId],
|
|
references: [clients.id],
|
|
}),
|
|
interest: one(interests, {
|
|
fields: [reminders.interestId],
|
|
references: [interests.id],
|
|
}),
|
|
berth: one(berths, {
|
|
fields: [reminders.berthId],
|
|
references: [berths.id],
|
|
}),
|
|
calendarCacheEntries: many(googleCalendarCache),
|
|
}));
|
|
|
|
export const googleCalendarTokensRelations = relations(googleCalendarTokens, ({ many }) => ({
|
|
cacheEntries: many(googleCalendarCache),
|
|
}));
|
|
|
|
export const googleCalendarCacheRelations = relations(googleCalendarCache, ({ one }) => ({
|
|
reminder: one(reminders, {
|
|
fields: [googleCalendarCache.reminderId],
|
|
references: [reminders.id],
|
|
}),
|
|
}));
|
|
|
|
export const notificationsRelations = relations(notifications, ({ one }) => ({
|
|
port: one(ports, {
|
|
fields: [notifications.portId],
|
|
references: [ports.id],
|
|
}),
|
|
}));
|
|
|
|
export const scheduledReportsRelations = relations(scheduledReports, ({ one, many }) => ({
|
|
port: one(ports, {
|
|
fields: [scheduledReports.portId],
|
|
references: [ports.id],
|
|
}),
|
|
recipients: many(reportRecipients),
|
|
generatedReports: many(generatedReports),
|
|
}));
|
|
|
|
export const reportRecipientsRelations = relations(reportRecipients, ({ one }) => ({
|
|
report: one(scheduledReports, {
|
|
fields: [reportRecipients.reportId],
|
|
references: [scheduledReports.id],
|
|
}),
|
|
}));
|
|
|
|
export const generatedReportsRelations = relations(generatedReports, ({ one }) => ({
|
|
port: one(ports, {
|
|
fields: [generatedReports.portId],
|
|
references: [ports.id],
|
|
}),
|
|
scheduledReport: one(scheduledReports, {
|
|
fields: [generatedReports.scheduledReportId],
|
|
references: [scheduledReports.id],
|
|
}),
|
|
file: one(files, {
|
|
fields: [generatedReports.fileId],
|
|
references: [files.id],
|
|
}),
|
|
}));
|
|
|
|
// ─── System ───────────────────────────────────────────────────────────────────
|
|
|
|
export const auditLogsRelations = relations(auditLogs, ({ one }) => ({
|
|
port: one(ports, {
|
|
fields: [auditLogs.portId],
|
|
references: [ports.id],
|
|
}),
|
|
revertOfLog: one(auditLogs, {
|
|
fields: [auditLogs.revertOf],
|
|
references: [auditLogs.id],
|
|
relationName: 'revert_of',
|
|
}),
|
|
}));
|
|
|
|
export const tagsRelations = relations(tags, ({ one, many }) => ({
|
|
port: one(ports, {
|
|
fields: [tags.portId],
|
|
references: [ports.id],
|
|
}),
|
|
clientTags: many(clientTags),
|
|
interestTags: many(interestTags),
|
|
berthTags: many(berthTags),
|
|
}));
|
|
|
|
export const webhooksRelations = relations(webhooks, ({ one, many }) => ({
|
|
port: one(ports, {
|
|
fields: [webhooks.portId],
|
|
references: [ports.id],
|
|
}),
|
|
deliveries: many(webhookDeliveries),
|
|
}));
|
|
|
|
export const webhookDeliveriesRelations = relations(webhookDeliveries, ({ one }) => ({
|
|
webhook: one(webhooks, {
|
|
fields: [webhookDeliveries.webhookId],
|
|
references: [webhooks.id],
|
|
}),
|
|
}));
|
|
|
|
export const systemSettingsRelations = relations(systemSettings, ({ one }) => ({
|
|
port: one(ports, {
|
|
fields: [systemSettings.portId],
|
|
references: [ports.id],
|
|
}),
|
|
}));
|
|
|
|
export const savedViewsRelations = relations(savedViews, ({ one }) => ({
|
|
port: one(ports, {
|
|
fields: [savedViews.portId],
|
|
references: [ports.id],
|
|
}),
|
|
}));
|
|
|
|
export const scratchpadNotesRelations = relations(scratchpadNotes, ({ one }) => ({
|
|
linkedClient: one(clients, {
|
|
fields: [scratchpadNotes.linkedClientId],
|
|
references: [clients.id],
|
|
}),
|
|
}));
|
|
|
|
export const userNotificationPreferencesRelations = relations(
|
|
userNotificationPreferences,
|
|
({ one }) => ({
|
|
port: one(ports, {
|
|
fields: [userNotificationPreferences.portId],
|
|
references: [ports.id],
|
|
}),
|
|
}),
|
|
);
|
|
|
|
export const customFieldDefinitionsRelations = relations(
|
|
customFieldDefinitions,
|
|
({ one, many }) => ({
|
|
port: one(ports, {
|
|
fields: [customFieldDefinitions.portId],
|
|
references: [ports.id],
|
|
}),
|
|
values: many(customFieldValues),
|
|
}),
|
|
);
|
|
|
|
export const customFieldValuesRelations = relations(customFieldValues, ({ one }) => ({
|
|
definition: one(customFieldDefinitions, {
|
|
fields: [customFieldValues.fieldId],
|
|
references: [customFieldDefinitions.id],
|
|
}),
|
|
}));
|
|
|
|
// ─── Residential ──────────────────────────────────────────────────────────────
|
|
|
|
export const residentialClientsRelations = relations(residentialClients, ({ one, many }) => ({
|
|
port: one(ports, {
|
|
fields: [residentialClients.portId],
|
|
references: [ports.id],
|
|
}),
|
|
interests: many(residentialInterests),
|
|
}));
|
|
|
|
export const residentialInterestsRelations = relations(residentialInterests, ({ one }) => ({
|
|
port: one(ports, {
|
|
fields: [residentialInterests.portId],
|
|
references: [ports.id],
|
|
}),
|
|
client: one(residentialClients, {
|
|
fields: [residentialInterests.residentialClientId],
|
|
references: [residentialClients.id],
|
|
}),
|
|
}));
|
|
|
|
// ─── Brochures + send-outs (Phase 7) ──────────────────────────────────────────
|
|
|
|
export const brochuresRelations = relations(brochures, ({ one, many }) => ({
|
|
port: one(ports, {
|
|
fields: [brochures.portId],
|
|
references: [ports.id],
|
|
}),
|
|
versions: many(brochureVersions),
|
|
sends: many(documentSends),
|
|
}));
|
|
|
|
export const brochureVersionsRelations = relations(brochureVersions, ({ one, many }) => ({
|
|
brochure: one(brochures, {
|
|
fields: [brochureVersions.brochureId],
|
|
references: [brochures.id],
|
|
}),
|
|
sends: many(documentSends),
|
|
}));
|
|
|
|
export const documentSendsRelations = relations(documentSends, ({ one }) => ({
|
|
port: one(ports, {
|
|
fields: [documentSends.portId],
|
|
references: [ports.id],
|
|
}),
|
|
client: one(clients, {
|
|
fields: [documentSends.clientId],
|
|
references: [clients.id],
|
|
}),
|
|
interest: one(interests, {
|
|
fields: [documentSends.interestId],
|
|
references: [interests.id],
|
|
}),
|
|
berth: one(berths, {
|
|
fields: [documentSends.berthId],
|
|
references: [berths.id],
|
|
}),
|
|
brochure: one(brochures, {
|
|
fields: [documentSends.brochureId],
|
|
references: [brochures.id],
|
|
}),
|
|
brochureVersion: one(brochureVersions, {
|
|
fields: [documentSends.brochureVersionId],
|
|
references: [brochureVersions.id],
|
|
}),
|
|
}));
|