Files
pn-new-crm/src/lib/validators/invoices.ts
Matt Ciaccio 1fb3aa3aeb fix(regressions): client-bundle ioredis + Drizzle ANY() array bindings
Two regressions from yesterday's audit-tier-0 work that broke the dev
server and every clients API call.

- baseListQuerySchema lived in route-helpers.ts, which was made
  server-only by the rate-limit import. Every validator imported it,
  pulling ioredis (and dns/net/tls/fs/node:async_hooks) into the client
  bundle — every form/detail page returned 500 in dev. Extracted the
  schema to src/lib/api/list-query.ts and updated all 14 validators.
- clients.service.listClients and email-compose used raw SQL
  ANY(\${jsArray}) which Drizzle binds as JSON — Postgres rejects with
  42809 "op ANY/ALL (array) requires array on right side". Switched to
  the inArray helper.

GET /api/v1/clients now returns 200 again. Affects every form/detail
page that imports a validator.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-06 14:56:59 +02:00

90 lines
3.3 KiB
TypeScript

import { z } from 'zod';
import { baseListQuerySchema } from '@/lib/api/list-query';
export const INVOICE_KINDS = ['general', 'deposit'] as const;
export type InvoiceKind = (typeof INVOICE_KINDS)[number];
export const createInvoiceSchema = z
.object({
billingEntity: z.object({
type: z.enum(['client', 'company']),
id: z.string().min(1),
}),
billingEmail: z.string().email().optional(),
billingAddress: z.string().max(500).optional(),
dueDate: z.string().min(1),
paymentTerms: z
.enum(['immediate', 'net10', 'net15', 'net30', 'net45', 'net60'])
.default('net30'),
currency: z.string().length(3).default('USD'),
notes: z.string().max(2000).optional(),
/** Optional link to a sales interest. Required when kind === 'deposit'. */
interestId: z.string().min(1).optional(),
kind: z.enum(INVOICE_KINDS).default('general'),
lineItems: z
.array(
z.object({
description: z.string().min(1),
quantity: z.coerce.number().positive().default(1),
unitPrice: z.coerce.number().min(0),
}),
)
.optional(),
expenseIds: z.array(z.string()).optional(),
})
.refine((data) => data.kind !== 'deposit' || !!data.interestId, {
message: 'Deposit invoices must be linked to an interest',
path: ['interestId'],
})
.refine(
(data) =>
(data.lineItems && data.lineItems.length > 0) ||
(data.expenseIds && data.expenseIds.length > 0),
{ message: 'Invoice must have at least one line item or linked expense' },
);
export const updateInvoiceSchema = z.object({
clientName: z.string().min(1).max(200).optional(),
billingEmail: z.string().email().optional(),
billingAddress: z.string().max(500).optional(),
dueDate: z.string().min(1).optional(),
paymentTerms: z.enum(['immediate', 'net10', 'net15', 'net30', 'net45', 'net60']).optional(),
currency: z.string().length(3).optional(),
notes: z.string().max(2000).optional(),
interestId: z.string().min(1).nullable().optional(),
kind: z.enum(INVOICE_KINDS).optional(),
lineItems: z
.array(
z.object({
description: z.string().min(1),
quantity: z.coerce.number().positive().default(1),
unitPrice: z.coerce.number().min(0),
}),
)
.optional(),
expenseIds: z.array(z.string()).optional(),
});
export const recordPaymentSchema = z.object({
paymentDate: z.string().min(1),
paymentMethod: z.string().optional(),
paymentReference: z.string().optional(),
});
export const listInvoicesSchema = baseListQuerySchema.extend({
status: z.string().optional(),
clientName: z.string().optional(),
dateFrom: z.string().optional(),
dateTo: z.string().optional(),
billingEntityType: z.enum(['client', 'company']).optional(),
billingEntityId: z.string().optional(),
});
// `z.input` keeps fields with `.default()` (paymentTerms, currency, kind)
// optional from the caller's perspective. The schema parser still fills in
// the defaults, so the service body can rely on them being present at runtime.
export type CreateInvoiceInput = z.input<typeof createInvoiceSchema>;
export type UpdateInvoiceInput = z.input<typeof updateInvoiceSchema>;
export type RecordPaymentInput = z.infer<typeof recordPaymentSchema>;
export type ListInvoicesInput = z.infer<typeof listInvoicesSchema>;