feat(eoi): template-aware generate-EOI dialog

The EOI dialog now lists "Documenso Standard EOI" (default) plus any
seeded in-app EOI templates and routes the submit to the dual-path
generate-and-sign endpoint with the correct pathway:

  - "documenso-template" sentinel id → pathway: documenso-template
  - any other template id → pathway: inapp

Signers are derived server-side from EoiContext for both pathways when
the template type is EOI (interest's client + hardcoded developer +
approver), so the dialog doesn't collect them. Non-EOI templates still
require explicit signers.

Drops the legacy `client.yachtLengthFt` prerequisite check (yacht is now
a first-class entity) and replaces it with hasYacht based on
interest.yachtId. Tests updated; 646/646 green.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Matt Ciaccio
2026-04-26 13:42:08 +02:00
parent 2ff24a7132
commit f4ec51002c
4 changed files with 171 additions and 58 deletions

View File

@@ -1,7 +1,7 @@
'use client'; 'use client';
import { useState } from 'react'; import { useMemo, useState } from 'react';
import { useQueryClient } from '@tanstack/react-query'; import { useQuery, useQueryClient } from '@tanstack/react-query';
import { import {
Dialog, Dialog,
@@ -12,12 +12,19 @@ import {
DialogTitle, DialogTitle,
} from '@/components/ui/dialog'; } from '@/components/ui/dialog';
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import { Label } from '@/components/ui/label';
import { apiFetch } from '@/lib/api/client'; import { apiFetch } from '@/lib/api/client';
interface EoiPrerequisites { interface EoiPrerequisites {
hasName: boolean; hasName: boolean;
hasEmail: boolean; hasYacht: boolean;
hasYachtDims: boolean;
hasBerth: boolean; hasBerth: boolean;
} }
@@ -30,11 +37,23 @@ interface EoiGenerateDialogProps {
const PREREQUISITE_LABELS: { key: keyof EoiPrerequisites; label: string }[] = [ const PREREQUISITE_LABELS: { key: keyof EoiPrerequisites; label: string }[] = [
{ key: 'hasName', label: 'Client has full name' }, { key: 'hasName', label: 'Client has full name' },
{ key: 'hasEmail', label: 'Client has email address' }, { key: 'hasYacht', label: 'Yacht linked to interest' },
{ key: 'hasYachtDims', label: 'Yacht dimensions set' },
{ key: 'hasBerth', label: 'Berth linked to interest' }, { key: 'hasBerth', label: 'Berth linked to interest' },
]; ];
const DOCUMENSO_TEMPLATE_VALUE = 'documenso-template';
interface InAppTemplate {
id: string;
name: string;
description?: string | null;
templateType: string;
}
interface ListResponse {
data: InAppTemplate[];
}
export function EoiGenerateDialog({ export function EoiGenerateDialog({
interestId, interestId,
open, open,
@@ -44,9 +63,21 @@ export function EoiGenerateDialog({
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const [isGenerating, setIsGenerating] = useState(false); const [isGenerating, setIsGenerating] = useState(false);
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
const [selectedTemplate, setSelectedTemplate] = useState<string>(DOCUMENSO_TEMPLATE_VALUE);
const allMet = Object.values(prerequisites).every(Boolean); const allMet = Object.values(prerequisites).every(Boolean);
// Load in-app EOI templates so the operator can pick one as an alternative
// to the Documenso external-signing flow.
const { data: templatesRes } = useQuery<ListResponse>({
queryKey: ['document-templates', { templateType: 'eoi', isActive: true }],
queryFn: () =>
apiFetch<ListResponse>('/api/v1/document-templates?templateType=eoi&isActive=true'),
enabled: open,
});
const inAppTemplates = useMemo(() => templatesRes?.data ?? [], [templatesRes]);
const handleGenerate = async () => { const handleGenerate = async () => {
if (!allMet) return; if (!allMet) return;
@@ -54,9 +85,17 @@ export function EoiGenerateDialog({
setError(null); setError(null);
try { try {
await apiFetch('/api/v1/documents/generate-eoi', { const isDocumensoPath = selectedTemplate === DOCUMENSO_TEMPLATE_VALUE;
const url = `/api/v1/document-templates/${encodeURIComponent(selectedTemplate)}/generate-and-sign`;
await apiFetch(url, {
method: 'POST', method: 'POST',
body: { interestId }, body: {
interestId,
pathway: isDocumensoPath ? 'documenso-template' : 'inapp',
// Signers are derived server-side from EOI context for both pathways
// when the template type is EOI, so the dialog doesn't collect them.
signers: [],
},
}); });
queryClient.invalidateQueries({ queryKey: ['documents', { interestId }] }); queryClient.invalidateQueries({ queryKey: ['documents', { interestId }] });
@@ -74,39 +113,58 @@ export function EoiGenerateDialog({
<DialogHeader> <DialogHeader>
<DialogTitle>Generate Expression of Interest</DialogTitle> <DialogTitle>Generate Expression of Interest</DialogTitle>
<DialogDescription> <DialogDescription>
The following prerequisites must be met before generating the EOI document. Pick how to render the EOI. Documenso is the primary path; in-app templates use the same
source PDF but render and store the PDF locally before sending for signing.
</DialogDescription> </DialogDescription>
</DialogHeader> </DialogHeader>
<div className="space-y-2 py-2"> <div className="space-y-4 py-2">
{PREREQUISITE_LABELS.map(({ key, label }) => ( <div className="space-y-2">
<div key={key} className="flex items-center gap-3"> <Label htmlFor="eoi-template">Template</Label>
<span <Select value={selectedTemplate} onValueChange={setSelectedTemplate}>
className={`flex h-5 w-5 items-center justify-center rounded-full text-xs font-bold ${ <SelectTrigger id="eoi-template">
prerequisites[key] <SelectValue />
? 'bg-green-100 text-green-700' </SelectTrigger>
: 'bg-red-100 text-red-700' <SelectContent>
}`} <SelectItem value={DOCUMENSO_TEMPLATE_VALUE}>
> Documenso Standard EOI (recommended)
{prerequisites[key] ? '✓' : '✗'} </SelectItem>
</span> {inAppTemplates.map((t) => (
<span className={prerequisites[key] ? 'text-foreground' : 'text-muted-foreground'}> <SelectItem key={t.id} value={t.id}>
{label} {t.name}
</span> </SelectItem>
</div> ))}
))} </SelectContent>
</Select>
</div>
<div className="space-y-2">
<p className="text-xs font-medium text-muted-foreground">Prerequisites</p>
{PREREQUISITE_LABELS.map(({ key, label }) => (
<div key={key} className="flex items-center gap-3">
<span
className={`flex h-5 w-5 items-center justify-center rounded-full text-xs font-bold ${
prerequisites[key] ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700'
}`}
>
{prerequisites[key] ? '✓' : '✗'}
</span>
<span className={prerequisites[key] ? 'text-foreground' : 'text-muted-foreground'}>
{label}
</span>
</div>
))}
</div>
</div> </div>
{error && ( {error && <p className="text-sm text-destructive">{error}</p>}
<p className="text-sm text-destructive">{error}</p>
)}
<DialogFooter> <DialogFooter>
<Button variant="outline" onClick={() => onOpenChange(false)}> <Button variant="outline" onClick={() => onOpenChange(false)}>
Cancel Cancel
</Button> </Button>
<Button onClick={handleGenerate} disabled={!allMet || isGenerating}> <Button onClick={handleGenerate} disabled={!allMet || isGenerating}>
{isGenerating ? 'Generating...' : 'Generate EOI'} {isGenerating ? 'Generating' : 'Generate EOI'}
</Button> </Button>
</DialogFooter> </DialogFooter>
</DialogContent> </DialogContent>

View File

@@ -14,13 +14,9 @@ interface InterestDocumentsTabProps {
interface InterestData { interface InterestData {
id: string; id: string;
yachtId?: string | null;
berthId?: string | null; berthId?: string | null;
client?: { clientName?: string | null;
fullName?: string | null;
yachtLengthFt?: string | null;
yachtLengthM?: string | null;
contacts?: Array<{ channel: string; value: string }>;
};
} }
export function InterestDocumentsTab({ interestId }: InterestDocumentsTabProps) { export function InterestDocumentsTab({ interestId }: InterestDocumentsTabProps) {
@@ -28,20 +24,14 @@ export function InterestDocumentsTab({ interestId }: InterestDocumentsTabProps)
const { data: interestRes } = useQuery({ const { data: interestRes } = useQuery({
queryKey: ['interests', interestId], queryKey: ['interests', interestId],
queryFn: () => queryFn: () => apiFetch<{ data: InterestData }>(`/api/v1/interests/${interestId}`),
apiFetch<{ data: InterestData }>(`/api/v1/interests/${interestId}`),
}); });
const interest = interestRes?.data; const interest = interestRes?.data;
const prerequisites = { const prerequisites = {
hasName: Boolean(interest?.client?.fullName), hasName: Boolean(interest?.clientName),
hasEmail: Boolean( hasYacht: Boolean(interest?.yachtId),
interest?.client?.contacts?.some((c) => c.channel === 'email' && c.value),
),
hasYachtDims: Boolean(
interest?.client?.yachtLengthFt || interest?.client?.yachtLengthM,
),
hasBerth: Boolean(interest?.berthId), hasBerth: Boolean(interest?.berthId),
}; };

View File

@@ -817,10 +817,34 @@ async function generateAndSignViaInApp(
signers: GenerateAndSignInput['signers'], signers: GenerateAndSignInput['signers'],
meta: AuditMeta, meta: AuditMeta,
) { ) {
if (!signers || signers.length === 0) { const template = await getTemplateById(templateId, portId);
// For EOI templates, signers default to the same set the Documenso template
// pathway uses (interest's client + hardcoded developer + approver), so the
// UI doesn't need to collect them. Non-EOI templates still require explicit
// signers since they have no canonical recipient list.
let resolvedSigners = signers;
if ((!resolvedSigners || resolvedSigners.length === 0) && template.templateType === 'eoi') {
if (!context.interestId) {
throw new ValidationError(
'interestId is required when generating an EOI without explicit signers',
);
}
const eoiCtx = await buildEoiContext(context.interestId, portId);
resolvedSigners = [
{
name: eoiCtx.client.fullName,
email: eoiCtx.client.primaryEmail ?? '',
role: 'signer',
signingOrder: 1,
},
{ name: 'David Mizrahi', email: 'dm@portnimara.com', role: 'signer', signingOrder: 2 },
{ name: 'Abbie May', email: 'sales@portnimara.com', role: 'approver', signingOrder: 3 },
];
}
if (!resolvedSigners || resolvedSigners.length === 0) {
throw new ValidationError('signers are required for inapp pathway'); throw new ValidationError('signers are required for inapp pathway');
} }
const template = await getTemplateById(templateId, portId);
// EOI templates fill the same source PDF as the Documenso template (so both // EOI templates fill the same source PDF as the Documenso template (so both
// pathways yield the same document). Other template types stay on the // pathways yield the same document). Other template types stay on the
@@ -845,7 +869,7 @@ async function generateAndSignViaInApp(
const documensoDoc = await documensoCreate( const documensoDoc = await documensoCreate(
template.name, template.name,
pdfBase64, pdfBase64,
signers.map((s) => ({ resolvedSigners.map((s) => ({
name: s.name, name: s.name,
email: s.email, email: s.email,
role: s.role, role: s.role,
@@ -873,7 +897,11 @@ async function generateAndSignViaInApp(
entityType: 'document', entityType: 'document',
entityId: documentRecord.id, entityId: documentRecord.id,
newValue: { status: 'sent', documensoId: documensoDoc.id }, newValue: { status: 'sent', documensoId: documensoDoc.id },
metadata: { action: 'generate_and_sign', pathway: 'inapp', signerCount: signers.length }, metadata: {
action: 'generate_and_sign',
pathway: 'inapp',
signerCount: resolvedSigners.length,
},
ipAddress: meta.ipAddress, ipAddress: meta.ipAddress,
userAgent: meta.userAgent, userAgent: meta.userAgent,
}); });

View File

@@ -206,16 +206,53 @@ describe('generateAndSign — inapp pathway', () => {
expect(docRow?.fileId).toBeTruthy(); expect(docRow?.fileId).toBeTruthy();
}); });
it('throws ValidationError when signers array is empty', async () => { it('auto-derives signers for EOI templates when none are provided', async () => {
const client = await import('@/lib/services/documenso-client');
vi.mocked(client.createDocument).mockResolvedValue({
id: 'doc-auto-signers',
status: 'PENDING',
recipients: [],
});
vi.mocked(client.sendDocument).mockResolvedValue({
id: 'doc-auto-signers',
status: 'PENDING',
recipients: [],
});
await generateAndSign(
setup.inAppTemplateId,
setup.portId,
{ clientId: setup.clientId, interestId: setup.interestId },
[],
'inapp',
{ ...meta, portId: setup.portId },
);
expect(client.createDocument).toHaveBeenCalledOnce();
const recipients = vi.mocked(client.createDocument).mock.calls[0]![2];
expect(recipients).toHaveLength(3);
expect(recipients[0]?.name).toBe('Dual Path Client');
expect(recipients[1]?.name).toBe('David Mizrahi');
expect(recipients[2]?.role).toBe('approver');
});
it('throws ValidationError when non-EOI template has no signers', async () => {
const [other] = await db
.insert(documentTemplates)
.values({
portId: setup.portId,
name: 'Plain Letter',
templateType: 'welcome_letter',
bodyHtml: '<p>x</p>',
createdBy: 'test',
})
.returning();
await expect( await expect(
generateAndSign( generateAndSign(other!.id, setup.portId, { clientId: setup.clientId }, [], 'inapp', {
setup.inAppTemplateId, ...meta,
setup.portId, portId: setup.portId,
{ clientId: setup.clientId, interestId: setup.interestId }, }),
[],
'inapp',
{ ...meta, portId: setup.portId },
),
).rejects.toThrow(ValidationError); ).rejects.toThrow(ValidationError);
}); });