feat(docs): nested-entity 'This deal' / 'From client' split (B4 #8 phase 4)
Finishes B4 #8 by completing the UI half of the per-interest filing model. Backend foundations (files.interest_id column, ensureEntityFolder for 'interest', upload-zone scope radio, outcome rename hook, backfill) shipped earlier in this audit cycle. - listFiles validator + service: optional interestId filter - listFilesAggregatedByEntity: routes entityType='interest' to a new helper that returns "THIS DEAL" + "FROM CLIENT" + symmetric-reach company/yacht groups - InterestDocumentsTab: Attachments section now renders two cohorts via two paginated queries, with client-side de-duplication so files filed under this deal don't double-count under "From client" - FileRow type exposes the optional interestId so the de-dupe filter doesn't need a re-fetch
This commit is contained in:
@@ -32,6 +32,10 @@ export interface FileRow {
|
||||
category: string | null;
|
||||
createdAt: string | Date;
|
||||
uploadedBy: string;
|
||||
/** Optional — present when the file is scoped to a sales deal.
|
||||
* Consumers like InterestDocumentsTab filter on this to split the
|
||||
* "This deal" vs "From client" cohorts. */
|
||||
interestId?: string | null;
|
||||
}
|
||||
|
||||
interface FileGridProps {
|
||||
|
||||
@@ -45,24 +45,34 @@ export function InterestDocumentsTab({ interestId }: InterestDocumentsTabProps)
|
||||
apiFetch<{ data: InterestData }>(`/api/v1/interests/${interestId}`).then((r) => r.data),
|
||||
});
|
||||
|
||||
// Files attach at the client level (the schema has no interest_id
|
||||
// FK on `files`). For an interest, surface every file that belongs
|
||||
// to its parent client - covers the realistic case where a rep
|
||||
// uploaded a passport / scan / photo while working a deal.
|
||||
// Until the interest record loads we pass a sentinel clientId so the
|
||||
// server returns empty rather than the unscoped port-wide file list.
|
||||
// Two cohorts: files tagged with this interestId ("This deal") +
|
||||
// files belonging to the parent client without an interestId, or
|
||||
// tagged to another interest of the same client ("From client").
|
||||
// Until the interest record loads we pass a sentinel clientId so
|
||||
// the server returns empty rather than the unscoped port-wide list.
|
||||
const clientId = interest?.clientId ?? '__pending__';
|
||||
const filesQueryKey = ['files', { clientId }] as const;
|
||||
const { data: files, isLoading: filesLoading } = usePaginatedQuery<FileRow>({
|
||||
queryKey: filesQueryKey,
|
||||
const thisDealQueryKey = ['files', { interestId }] as const;
|
||||
const fromClientQueryKey = ['files', { clientId }] as const;
|
||||
|
||||
const { data: thisDealFiles, isLoading: thisDealLoading } = usePaginatedQuery<FileRow>({
|
||||
queryKey: thisDealQueryKey,
|
||||
endpoint: `/api/v1/files?interestId=${encodeURIComponent(interestId)}`,
|
||||
filterDefinitions: [],
|
||||
});
|
||||
|
||||
const { data: clientFiles, isLoading: clientFilesLoading } = usePaginatedQuery<FileRow>({
|
||||
queryKey: fromClientQueryKey,
|
||||
endpoint: `/api/v1/files?clientId=${encodeURIComponent(clientId)}`,
|
||||
filterDefinitions: [],
|
||||
});
|
||||
|
||||
// "From client" excludes anything we already surface under "This deal".
|
||||
const fromClientFiles = clientFiles.filter((f) => !f.interestId || f.interestId !== interestId);
|
||||
|
||||
useRealtimeInvalidation({
|
||||
'file:uploaded': [filesQueryKey],
|
||||
'file:updated': [filesQueryKey],
|
||||
'file:deleted': [filesQueryKey],
|
||||
'file:uploaded': [thisDealQueryKey, fromClientQueryKey],
|
||||
'file:updated': [thisDealQueryKey, fromClientQueryKey],
|
||||
'file:deleted': [thisDealQueryKey, fromClientQueryKey],
|
||||
});
|
||||
|
||||
const handleDownload = async (file: FileRow) => {
|
||||
@@ -85,13 +95,16 @@ export function InterestDocumentsTab({ interestId }: InterestDocumentsTabProps)
|
||||
if (!ok) return;
|
||||
try {
|
||||
await apiFetch(`/api/v1/files/${file.id}`, { method: 'DELETE' });
|
||||
queryClient.invalidateQueries({ queryKey: filesQueryKey });
|
||||
queryClient.invalidateQueries({ queryKey: thisDealQueryKey });
|
||||
queryClient.invalidateQueries({ queryKey: fromClientQueryKey });
|
||||
} catch {
|
||||
// silent
|
||||
}
|
||||
};
|
||||
|
||||
const hasAttachments = files.length > 0;
|
||||
const hasThisDeal = thisDealFiles.length > 0;
|
||||
const hasFromClient = fromClientFiles.length > 0;
|
||||
const hasAttachments = hasThisDeal || hasFromClient;
|
||||
|
||||
return (
|
||||
<div className="space-y-8">
|
||||
@@ -129,7 +142,8 @@ export function InterestDocumentsTab({ interestId }: InterestDocumentsTabProps)
|
||||
<h3 className="text-sm font-medium text-muted-foreground">Attachments</h3>
|
||||
{hasAttachments ? (
|
||||
<span className="text-xs text-muted-foreground">
|
||||
{files.length} file{files.length === 1 ? '' : 's'}
|
||||
{thisDealFiles.length + fromClientFiles.length} file
|
||||
{thisDealFiles.length + fromClientFiles.length === 1 ? '' : 's'}
|
||||
</span>
|
||||
) : null}
|
||||
</div>
|
||||
@@ -148,22 +162,44 @@ export function InterestDocumentsTab({ interestId }: InterestDocumentsTabProps)
|
||||
// client-level.
|
||||
interestId={interestId}
|
||||
onUploadComplete={() => {
|
||||
queryClient.invalidateQueries({ queryKey: filesQueryKey });
|
||||
queryClient.invalidateQueries({ queryKey: thisDealQueryKey });
|
||||
queryClient.invalidateQueries({ queryKey: fromClientQueryKey });
|
||||
}}
|
||||
/>
|
||||
) : null}
|
||||
</PermissionGate>
|
||||
|
||||
{hasAttachments ? (
|
||||
<FileGrid
|
||||
files={files}
|
||||
onDownload={handleDownload}
|
||||
onPreview={setPreviewFile}
|
||||
onRename={() => {}}
|
||||
onDelete={handleDelete}
|
||||
isLoading={filesLoading}
|
||||
/>
|
||||
) : null}
|
||||
{(hasThisDeal || thisDealLoading) && (
|
||||
<div className="space-y-2">
|
||||
<h4 className="text-xs font-semibold uppercase tracking-wide text-muted-foreground">
|
||||
This deal
|
||||
</h4>
|
||||
<FileGrid
|
||||
files={thisDealFiles}
|
||||
onDownload={handleDownload}
|
||||
onPreview={setPreviewFile}
|
||||
onRename={() => {}}
|
||||
onDelete={handleDelete}
|
||||
isLoading={thisDealLoading}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{(hasFromClient || clientFilesLoading) && (
|
||||
<div className="space-y-2">
|
||||
<h4 className="text-xs font-semibold uppercase tracking-wide text-muted-foreground">
|
||||
From client
|
||||
</h4>
|
||||
<FileGrid
|
||||
files={fromClientFiles}
|
||||
onDownload={handleDownload}
|
||||
onPreview={setPreviewFile}
|
||||
onRename={() => {}}
|
||||
onDelete={handleDelete}
|
||||
isLoading={clientFilesLoading}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</section>
|
||||
|
||||
<EoiGenerateDialog
|
||||
|
||||
@@ -22,6 +22,7 @@ import { documentFolders } from '@/lib/db/schema/documents';
|
||||
import { clients } from '@/lib/db/schema/clients';
|
||||
import { companies, companyMemberships } from '@/lib/db/schema/companies';
|
||||
import { yachts } from '@/lib/db/schema/yachts';
|
||||
import { interests } from '@/lib/db/schema/interests';
|
||||
import type { EntityType } from '@/lib/services/document-folders.service';
|
||||
|
||||
// ─── Types ────────────────────────────────────────────────────────────────────
|
||||
@@ -267,7 +268,8 @@ export async function deleteFile(id: string, portId: string, meta: AuditMeta) {
|
||||
// ─── List ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
export async function listFiles(portId: string, query: ListFilesInput) {
|
||||
const { page, limit, sort, order, search, clientId, yachtId, companyId, category } = query;
|
||||
const { page, limit, sort, order, search, clientId, yachtId, companyId, interestId, category } =
|
||||
query;
|
||||
|
||||
const filters = [];
|
||||
|
||||
@@ -280,6 +282,9 @@ export async function listFiles(portId: string, query: ListFilesInput) {
|
||||
if (companyId) {
|
||||
filters.push(eq(files.companyId, companyId));
|
||||
}
|
||||
if (interestId) {
|
||||
filters.push(eq(files.interestId, interestId));
|
||||
}
|
||||
if (category) {
|
||||
filters.push(eq(files.category, category));
|
||||
}
|
||||
@@ -333,7 +338,7 @@ export type AggregatedFileRow = Omit<typeof files.$inferSelect, 'storagePath' |
|
||||
|
||||
export interface AggregatedFileGroup {
|
||||
label: string;
|
||||
source: 'direct' | 'client' | 'company' | 'yacht';
|
||||
source: 'direct' | 'client' | 'company' | 'yacht' | 'this_deal' | 'other_deals';
|
||||
files: AggregatedFileRow[];
|
||||
total: number;
|
||||
}
|
||||
@@ -351,6 +356,74 @@ const GROUP_LIMIT = 20;
|
||||
* Source of truth: each file's snapshotted entity FKs.
|
||||
* Defense-in-depth: port_id at every entity / membership / yacht / file join.
|
||||
*/
|
||||
async function listFilesAggregatedForInterest(
|
||||
portId: string,
|
||||
interestId: string,
|
||||
clientId: string | null,
|
||||
): Promise<AggregatedFilesResult> {
|
||||
const groups: AggregatedFileGroup[] = [];
|
||||
|
||||
// "THIS DEAL" — files explicitly tagged with this interest_id.
|
||||
const thisDeal = await fetchGroupRows(portId, eq(files.interestId, interestId), GROUP_LIMIT);
|
||||
if (thisDeal.rows.length > 0) {
|
||||
groups.push({
|
||||
label: 'THIS DEAL',
|
||||
source: 'this_deal',
|
||||
files: thisDeal.rows,
|
||||
total: thisDeal.total,
|
||||
});
|
||||
}
|
||||
|
||||
if (clientId) {
|
||||
// "FROM CLIENT" — files on the same client that aren't tagged to
|
||||
// this interest. NULL interest_id = client-level, non-deal-specific.
|
||||
// A different interest id = another deal's docs, still useful to
|
||||
// surface so the rep can pull from history without leaving the
|
||||
// current detail page.
|
||||
const fromClient = await fetchGroupRows(
|
||||
portId,
|
||||
and(
|
||||
eq(files.clientId, clientId),
|
||||
or(isNull(files.interestId), sql`${files.interestId} <> ${interestId}`),
|
||||
)!,
|
||||
GROUP_LIMIT,
|
||||
);
|
||||
if (fromClient.rows.length > 0) {
|
||||
groups.push({
|
||||
label: 'FROM CLIENT',
|
||||
source: 'other_deals',
|
||||
files: fromClient.rows,
|
||||
total: fromClient.total,
|
||||
});
|
||||
}
|
||||
|
||||
// Symmetric reach via the client's related companies + yachts.
|
||||
const related = await collectRelatedEntities(portId, 'client', clientId);
|
||||
for (const { id, name } of related.companies) {
|
||||
const g = await fetchGroupRows(portId, eq(files.companyId, id), GROUP_LIMIT);
|
||||
if (g.rows.length === 0) continue;
|
||||
groups.push({
|
||||
label: `FROM COMPANY: ${name.toUpperCase()}`,
|
||||
source: 'company',
|
||||
files: g.rows,
|
||||
total: g.total,
|
||||
});
|
||||
}
|
||||
for (const { id, name } of related.yachts) {
|
||||
const g = await fetchGroupRows(portId, eq(files.yachtId, id), GROUP_LIMIT);
|
||||
if (g.rows.length === 0) continue;
|
||||
groups.push({
|
||||
label: `FROM YACHT: ${name.toUpperCase()}`,
|
||||
source: 'yacht',
|
||||
files: g.rows,
|
||||
total: g.total,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return { groups };
|
||||
}
|
||||
|
||||
export async function listFilesAggregatedByEntity(
|
||||
portId: string,
|
||||
entityType: EntityType,
|
||||
@@ -359,6 +432,20 @@ export async function listFilesAggregatedByEntity(
|
||||
const entityExists = await assertEntityInPort(portId, entityType, entityId);
|
||||
if (!entityExists) return { groups: [] };
|
||||
|
||||
// Interest view: split direct-attached files into "this deal" (files
|
||||
// explicitly tagged with this interestId) + "from client" (files on
|
||||
// the same client but no interestId OR a different interest). The
|
||||
// client-level companies/yachts walk is reused via the underlying
|
||||
// client.
|
||||
if (entityType === 'interest') {
|
||||
const interest = await db.query.interests.findFirst({
|
||||
where: and(eq(interests.id, entityId), eq(interests.portId, portId)),
|
||||
columns: { id: true, clientId: true },
|
||||
});
|
||||
if (!interest) return { groups: [] };
|
||||
return listFilesAggregatedForInterest(portId, interest.id, interest.clientId ?? null);
|
||||
}
|
||||
|
||||
const related = await collectRelatedEntities(portId, entityType, entityId);
|
||||
const groups: AggregatedFileGroup[] = [];
|
||||
|
||||
@@ -434,6 +521,13 @@ export async function assertEntityInPort(
|
||||
});
|
||||
return Boolean(c);
|
||||
}
|
||||
if (entityType === 'interest') {
|
||||
const i = await db.query.interests.findFirst({
|
||||
where: and(eq(interests.id, entityId), eq(interests.portId, portId)),
|
||||
columns: { id: true },
|
||||
});
|
||||
return Boolean(i);
|
||||
}
|
||||
const y = await db.query.yachts.findFirst({
|
||||
where: and(eq(yachts.id, entityId), eq(yachts.portId, portId)),
|
||||
columns: { id: true },
|
||||
|
||||
@@ -23,14 +23,18 @@ export const listFilesSchema = baseListQuerySchema
|
||||
clientId: z.string().optional(),
|
||||
yachtId: z.string().optional(),
|
||||
companyId: z.string().optional(),
|
||||
interestId: z.string().optional(),
|
||||
category: z.string().optional(),
|
||||
folderId: z
|
||||
.string()
|
||||
.uuid()
|
||||
.optional()
|
||||
.transform((v) => (v === '' ? null : v)),
|
||||
/** Entity-aggregated projection params - mutually exclusive with folderId. */
|
||||
entityType: z.enum(['client', 'company', 'yacht']).optional(),
|
||||
/** Entity-aggregated projection params - mutually exclusive with folderId.
|
||||
* 'interest' splits direct-attached into "This deal" (files.interestId
|
||||
* matches) and "From client" (client-level + other deals); the other
|
||||
* values fall through to the legacy symmetric-reach aggregator. */
|
||||
entityType: z.enum(['client', 'company', 'yacht', 'interest']).optional(),
|
||||
entityId: z.string().uuid().optional(),
|
||||
})
|
||||
.refine(
|
||||
|
||||
Reference in New Issue
Block a user