feat(invoices): remove client-facing PDF generation
Phase 1 / commit 11 of 14 — invoices are client-facing documents, and
per the new "no CRM-generated client-facing PDFs" rule (see the design
spec), the in-app pdfme rendering is removed entirely.
Future invoice rendering will use the deferred AcroForm-fill admin-
template feature: admin uploads a PDF template with named form fields,
CRM fills them with invoice data via pdf-lib. Same pattern as the
in-app EOI pathway. Tracked in BACKLOG.md.
Deleted:
- src/lib/services/invoices.ts:generateInvoicePdf (60 LOC)
- src/lib/pdf/templates/invoice-template.ts (entire pdfme template)
- src/app/api/v1/invoices/[id]/generate-pdf/route.ts
- src/components/invoices/invoice-pdf-preview.tsx (regenerate UI)
- "PDF Preview" tab on invoice detail page
- 5 now-unused imports in invoices.ts (files, ports, buildStoragePath,
getStorageBackend, env)
sendInvoice() retained: still queues the send-invoice email job, still
flips status to "sent", still emits the socket event. The PDF-attach
step is gone — downstream consumers either render externally or wait
for the AcroForm-fill feature. The `pdfFileId` column on invoices stays
so existing rows don't break, just never gets written by this code path.
1319/1319 vitest green.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,21 +0,0 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
import { withAuth, withPermission } from '@/lib/api/helpers';
|
||||
import { errorResponse } from '@/lib/errors';
|
||||
import { generateInvoicePdf } from '@/lib/services/invoices';
|
||||
|
||||
export const POST = withAuth(
|
||||
withPermission('invoices', 'edit', async (_req, ctx, params) => {
|
||||
try {
|
||||
const fileRecord = await generateInvoicePdf(params.id!, ctx.portId, {
|
||||
userId: ctx.userId,
|
||||
portId: ctx.portId,
|
||||
ipAddress: ctx.ipAddress,
|
||||
userAgent: ctx.userAgent,
|
||||
});
|
||||
return NextResponse.json({ data: fileRecord });
|
||||
} catch (error) {
|
||||
return errorResponse(error);
|
||||
}
|
||||
}),
|
||||
);
|
||||
@@ -23,7 +23,6 @@ import {
|
||||
SelectValue,
|
||||
} from '@/components/ui/select';
|
||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
|
||||
import { InvoicePdfPreview } from './invoice-pdf-preview';
|
||||
import { apiFetch } from '@/lib/api/client';
|
||||
import { toastError } from '@/lib/api/toast-error';
|
||||
import { useMobileChrome } from '@/components/layout/mobile/mobile-layout-provider';
|
||||
@@ -218,7 +217,6 @@ export function InvoiceDetail({ invoiceId }: InvoiceDetailProps) {
|
||||
<TabsList>
|
||||
<TabsTrigger value="overview">Overview</TabsTrigger>
|
||||
<TabsTrigger value="expenses">Linked Expenses</TabsTrigger>
|
||||
<TabsTrigger value="pdf">PDF Preview</TabsTrigger>
|
||||
<TabsTrigger value="payment">Payment</TabsTrigger>
|
||||
</TabsList>
|
||||
|
||||
@@ -360,11 +358,6 @@ export function InvoiceDetail({ invoiceId }: InvoiceDetailProps) {
|
||||
)}
|
||||
</TabsContent>
|
||||
|
||||
{/* PDF Preview */}
|
||||
<TabsContent value="pdf" className="pt-4">
|
||||
<InvoicePdfPreview invoiceId={invoiceId} pdfFileId={invoice.pdfFileId} />
|
||||
</TabsContent>
|
||||
|
||||
{/* Payment */}
|
||||
<TabsContent value="payment" className="pt-4">
|
||||
{invoice.status === 'paid' ? (
|
||||
|
||||
@@ -1,104 +0,0 @@
|
||||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { Loader2, RefreshCw, FileText } from 'lucide-react';
|
||||
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { apiFetch } from '@/lib/api/client';
|
||||
|
||||
interface InvoicePdfPreviewProps {
|
||||
invoiceId: string;
|
||||
pdfFileId?: string | null;
|
||||
}
|
||||
|
||||
export function InvoicePdfPreview({
|
||||
invoiceId,
|
||||
pdfFileId: initialPdfFileId,
|
||||
}: InvoicePdfPreviewProps) {
|
||||
const queryClient = useQueryClient();
|
||||
const [pdfFileId, setPdfFileId] = useState(initialPdfFileId);
|
||||
|
||||
const { data: previewData, isLoading: previewLoading } = useQuery<{
|
||||
url: string;
|
||||
mimeType: string;
|
||||
}>({
|
||||
queryKey: ['file-preview', pdfFileId],
|
||||
queryFn: () => apiFetch(`/api/v1/files/${pdfFileId}/preview`),
|
||||
enabled: !!pdfFileId,
|
||||
});
|
||||
|
||||
const regenerateMutation = useMutation({
|
||||
mutationFn: () =>
|
||||
apiFetch<{ data?: { id?: string } }>(`/api/v1/invoices/${invoiceId}/generate-pdf`, {
|
||||
method: 'POST',
|
||||
}),
|
||||
onSuccess: (data) => {
|
||||
const fileId = data?.data?.id;
|
||||
if (fileId) {
|
||||
setPdfFileId(fileId);
|
||||
queryClient.invalidateQueries({ queryKey: ['invoices', invoiceId] });
|
||||
queryClient.invalidateQueries({ queryKey: ['file-preview', fileId] });
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
if (!pdfFileId) {
|
||||
return (
|
||||
<div className="flex flex-col items-center justify-center gap-4 py-12 border border-dashed rounded-md">
|
||||
<FileText className="h-10 w-10 text-muted-foreground" />
|
||||
<p className="text-sm text-muted-foreground">No PDF generated yet</p>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => regenerateMutation.mutate()}
|
||||
disabled={regenerateMutation.isPending}
|
||||
>
|
||||
{regenerateMutation.isPending ? (
|
||||
<Loader2 className="mr-1.5 h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
<RefreshCw className="mr-1.5 h-4 w-4" />
|
||||
)}
|
||||
Generate PDF
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
<div className="flex items-center justify-end">
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => regenerateMutation.mutate()}
|
||||
disabled={regenerateMutation.isPending}
|
||||
>
|
||||
{regenerateMutation.isPending ? (
|
||||
<Loader2 className="mr-1.5 h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
<RefreshCw className="mr-1.5 h-4 w-4" />
|
||||
)}
|
||||
Regenerate PDF
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{previewLoading ? (
|
||||
<div className="flex items-center justify-center py-12">
|
||||
<Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
|
||||
</div>
|
||||
) : previewData?.url ? (
|
||||
<iframe
|
||||
src={previewData.url}
|
||||
className="w-full rounded border"
|
||||
style={{ height: '600px' }}
|
||||
title="Invoice PDF"
|
||||
/>
|
||||
) : (
|
||||
<div className="flex items-center justify-center py-12 border rounded">
|
||||
<p className="text-sm text-muted-foreground">Unable to load PDF preview</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,124 +0,0 @@
|
||||
import type { Template } from '@pdfme/common';
|
||||
|
||||
import { formatCurrency } from '@/lib/utils/currency';
|
||||
|
||||
export const invoiceTemplate: Template = {
|
||||
basePdf: 'BLANK_PDF' as unknown as string,
|
||||
schemas: [
|
||||
[
|
||||
// Header fields
|
||||
{
|
||||
name: 'portName',
|
||||
type: 'text',
|
||||
position: { x: 20, y: 15 },
|
||||
width: 100,
|
||||
height: 10,
|
||||
fontSize: 16,
|
||||
},
|
||||
{
|
||||
name: 'invoiceTitle',
|
||||
type: 'text',
|
||||
position: { x: 140, y: 15 },
|
||||
width: 50,
|
||||
height: 10,
|
||||
fontSize: 16,
|
||||
},
|
||||
{
|
||||
name: 'invoiceNumber',
|
||||
type: 'text',
|
||||
position: { x: 140, y: 27 },
|
||||
width: 50,
|
||||
height: 6,
|
||||
fontSize: 10,
|
||||
},
|
||||
{
|
||||
name: 'invoiceDate',
|
||||
type: 'text',
|
||||
position: { x: 140, y: 35 },
|
||||
width: 50,
|
||||
height: 6,
|
||||
fontSize: 10,
|
||||
},
|
||||
{
|
||||
name: 'dueDate',
|
||||
type: 'text',
|
||||
position: { x: 140, y: 43 },
|
||||
width: 50,
|
||||
height: 6,
|
||||
fontSize: 10,
|
||||
},
|
||||
// Client info
|
||||
{
|
||||
name: 'clientInfo',
|
||||
type: 'text',
|
||||
position: { x: 20, y: 55 },
|
||||
width: 100,
|
||||
height: 20,
|
||||
fontSize: 10,
|
||||
},
|
||||
// Line items as text block
|
||||
{
|
||||
name: 'lineItems',
|
||||
type: 'text',
|
||||
position: { x: 20, y: 85 },
|
||||
width: 170,
|
||||
height: 120,
|
||||
fontSize: 9,
|
||||
},
|
||||
// Totals
|
||||
{
|
||||
name: 'totals',
|
||||
type: 'text',
|
||||
position: { x: 110, y: 215 },
|
||||
width: 80,
|
||||
height: 30,
|
||||
fontSize: 10,
|
||||
},
|
||||
// Notes
|
||||
{
|
||||
name: 'notes',
|
||||
type: 'text',
|
||||
position: { x: 20, y: 250 },
|
||||
width: 170,
|
||||
height: 20,
|
||||
fontSize: 8,
|
||||
},
|
||||
],
|
||||
],
|
||||
};
|
||||
|
||||
export function buildInvoiceInputs(
|
||||
invoice: Record<string, unknown>,
|
||||
lineItems: Record<string, unknown>[],
|
||||
port: Record<string, unknown>,
|
||||
): Record<string, string> {
|
||||
const currency = (invoice.currency as string) ?? 'USD';
|
||||
const itemLines = lineItems
|
||||
.map(
|
||||
(li, i) =>
|
||||
`${i + 1}. ${li.description} | Qty: ${li.quantity} | Unit: ${formatCurrency(Number(li.unitPrice), currency)} | Total: ${formatCurrency(Number(li.total), currency)}`,
|
||||
)
|
||||
.join('\n');
|
||||
|
||||
let totalsText = `Subtotal: ${formatCurrency(Number(invoice.subtotal), currency)}`;
|
||||
if (Number(invoice.discountAmount) > 0) {
|
||||
totalsText += `\nDiscount (${invoice.discountPct}%): -${formatCurrency(Number(invoice.discountAmount), currency)}`;
|
||||
}
|
||||
if (Number(invoice.feeAmount) > 0) {
|
||||
totalsText += `\nFee (${invoice.feePct}%): +${formatCurrency(Number(invoice.feeAmount), currency)}`;
|
||||
}
|
||||
totalsText += `\n─────────────\nTOTAL: ${formatCurrency(Number(invoice.total), currency)}`;
|
||||
|
||||
return {
|
||||
portName: (port?.name as string) ?? 'Port Nimara',
|
||||
invoiceTitle: 'INVOICE',
|
||||
invoiceNumber: invoice.invoiceNumber as string,
|
||||
invoiceDate: `Date: ${new Date(invoice.createdAt as string | Date).toLocaleDateString('en-GB')}`,
|
||||
dueDate: `Due: ${invoice.dueDate}`,
|
||||
clientInfo:
|
||||
`${invoice.clientName}\n${invoice.billingEmail ?? ''}\n${invoice.billingAddress ?? ''}`.trim(),
|
||||
lineItems: itemLines || 'No line items',
|
||||
totals: totalsText,
|
||||
notes: invoice.notes ? `Notes: ${invoice.notes}` : '',
|
||||
};
|
||||
}
|
||||
@@ -3,8 +3,6 @@ import type { PgColumn } from 'drizzle-orm/pg-core';
|
||||
|
||||
import { db } from '@/lib/db';
|
||||
import { invoices, invoiceLineItems, invoiceExpenses, expenses } from '@/lib/db/schema/financial';
|
||||
import { files } from '@/lib/db/schema/documents';
|
||||
import { ports } from '@/lib/db/schema/ports';
|
||||
import { systemSettings } from '@/lib/db/schema/system';
|
||||
import { clients, clientAddresses } from '@/lib/db/schema/clients';
|
||||
import { companies, companyAddresses } from '@/lib/db/schema/companies';
|
||||
@@ -17,12 +15,7 @@ import { getCountryName } from '@/lib/i18n/countries';
|
||||
import { getSubdivisionName } from '@/lib/i18n/subdivisions';
|
||||
import { emitToRoom } from '@/lib/socket/server';
|
||||
import { logger } from '@/lib/logger';
|
||||
import { generatePdf } from '@/lib/pdf/generate';
|
||||
import { invoiceTemplate, buildInvoiceInputs } from '@/lib/pdf/templates/invoice-template';
|
||||
import { buildStoragePath } from '@/lib/minio';
|
||||
import { getStorageBackend } from '@/lib/storage';
|
||||
import { getQueue } from '@/lib/queue';
|
||||
import { env } from '@/lib/env';
|
||||
import type {
|
||||
CreateInvoiceInput,
|
||||
UpdateInvoiceInput,
|
||||
@@ -588,83 +581,17 @@ export async function deleteInvoice(id: string, portId: string, meta: AuditMeta)
|
||||
});
|
||||
}
|
||||
|
||||
// ─── Generate PDF ─────────────────────────────────────────────────────────
|
||||
|
||||
export async function generateInvoicePdf(id: string, portId: string, meta: AuditMeta) {
|
||||
const invoice = await getInvoiceById(id, portId);
|
||||
|
||||
const [port] = await db
|
||||
.select({ id: ports.id, name: ports.name, slug: ports.slug })
|
||||
.from(ports)
|
||||
.where(eq(ports.id, portId))
|
||||
.limit(1);
|
||||
|
||||
const inputs = buildInvoiceInputs(invoice, invoice.lineItems, port ?? {});
|
||||
|
||||
const pdfBytes = await generatePdf(invoiceTemplate, [inputs]);
|
||||
|
||||
const fileId = crypto.randomUUID();
|
||||
const storagePath = buildStoragePath(port?.slug ?? portId, 'invoices', id, fileId, 'pdf');
|
||||
|
||||
const buffer = Buffer.from(pdfBytes);
|
||||
const backend = await getStorageBackend();
|
||||
await backend.put(storagePath, buffer, {
|
||||
contentType: 'application/pdf',
|
||||
sizeBytes: buffer.length,
|
||||
});
|
||||
|
||||
const [fileRecord] = await db
|
||||
.insert(files)
|
||||
.values({
|
||||
portId,
|
||||
filename: `invoice-${invoice.invoiceNumber}.pdf`,
|
||||
originalName: `invoice-${invoice.invoiceNumber}.pdf`,
|
||||
mimeType: 'application/pdf',
|
||||
sizeBytes: String(pdfBytes.length),
|
||||
storagePath,
|
||||
storageBucket: env.MINIO_BUCKET,
|
||||
category: 'invoice',
|
||||
uploadedBy: meta.userId,
|
||||
})
|
||||
.returning();
|
||||
|
||||
if (!fileRecord)
|
||||
throw new CodedError('INSERT_RETURNING_EMPTY', {
|
||||
internalMessage: 'Invoice PDF file record insert returned no row',
|
||||
});
|
||||
|
||||
await db
|
||||
.update(invoices)
|
||||
.set({ pdfFileId: fileRecord.id, updatedAt: new Date() })
|
||||
.where(and(eq(invoices.id, id), eq(invoices.portId, portId)));
|
||||
|
||||
void createAuditLog({
|
||||
userId: meta.userId,
|
||||
portId,
|
||||
action: 'update',
|
||||
entityType: 'invoice',
|
||||
entityId: id,
|
||||
metadata: { action: 'pdf_generated', fileId: fileRecord.id },
|
||||
ipAddress: meta.ipAddress,
|
||||
userAgent: meta.userAgent,
|
||||
});
|
||||
|
||||
return fileRecord;
|
||||
}
|
||||
|
||||
// ─── Send invoice ─────────────────────────────────────────────────────────
|
||||
|
||||
export async function sendInvoice(id: string, portId: string, meta: AuditMeta) {
|
||||
const invoice = await getInvoiceById(id, portId);
|
||||
|
||||
// Generate PDF if not exists
|
||||
let pdfFileId = invoice.pdfFileId;
|
||||
if (!pdfFileId) {
|
||||
const fileRecord = await generateInvoicePdf(id, portId, meta);
|
||||
pdfFileId = fileRecord.id;
|
||||
}
|
||||
|
||||
// Queue email job
|
||||
// Invoice PDF generation has been removed (the CRM no longer renders
|
||||
// client-facing PDFs from scratch — see the PDF stack overhaul spec).
|
||||
// The "send" event still fires so the queue + audit + socket flow
|
||||
// remains intact; downstream consumers can decide whether to render
|
||||
// an external document, link to the in-app view, or wait for the
|
||||
// admin-uploaded AcroForm-fill feature to ship.
|
||||
await getQueue('email').add('send-invoice', { invoiceId: id, portId });
|
||||
|
||||
// Update status to 'sent'
|
||||
|
||||
Reference in New Issue
Block a user