diff --git a/src/lib/server/quotes-service.ts b/src/lib/server/quotes-service.ts index 1b5f2a2..43bb45b 100644 --- a/src/lib/server/quotes-service.ts +++ b/src/lib/server/quotes-service.ts @@ -3,7 +3,7 @@ import { PLACEHOLDER_CUSTOMER_IDS, PLACEHOLDER_CUSTOMER_NAMES, } from "@/lib/placeholder-customers"; -import type { Quote, Customer, Company } from "@/lib/types"; +import type { Quote, QuoteItem, Customer, Company } from "@/lib/types"; /** * Server-side quote pipeline used by the MCP layer (n8n) and internal API routes. @@ -1057,7 +1057,9 @@ export async function createQuoteFromEmail( // ── 1. Idempotency: if we already have a quote for this Message-ID, return it. const existing = await findQuoteByMessageId(pb, effectiveMessageId); if (existing) { - const itemsCount = await countQuoteItems(pb, existing.id); + const existingItems = await pb + .collection(QUOTE_ITEMS_COLLECTION) + .getFullList({ filter: `quote = "${existing.id}"` }); const attsCount = await countQuoteAttachments(pb, existing.id); const customer = existing.customer ? ((await pb.collection(CUSTOMERS_COLLECTION).getOne(existing.customer)) as Customer) @@ -1069,7 +1071,8 @@ export async function createQuoteFromEmail( quote: existing, customer: customer as Customer, company: company as Company | null, - items: itemsCount, + items: existingItems, + items_count: existingItems.length, attachments: attsCount, deduped: true, quote_number: existing.quote_number,