From e1323199100a7fd2a67b38b916819e0e9a493169 Mon Sep 17 00:00:00 2001 From: SJCoetz Date: Tue, 4 Aug 2026 20:36:50 +0200 Subject: [PATCH] fix(orders,quotes): null-safe sort comparator in archive functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The archive sort comparator in both getOrdersArchiveData and getQuotesArchiveData was: if (av === undefined) return 1; if (bv === undefined) return -1; return (av > bv ? 1 : -1) * sortDir; PocketBase returns `null` (not `undefined`) for unset fields like `updated`, `email_body`, etc. The strict equality check missed nulls, and the `av > bv` comparison where one side is null fails the production tsc check (TS strict mode rejects `null > string`). Production build failed with: ./src/lib/server/orders-service.ts:687:13 Type error: 'av' is possibly 'null'. Fix: use `== null` (which matches both null and undefined), and use explicit > / < comparisons that return 0 on equality rather than a fragile ternary that falls through. Also fixed in the matching function in quotes-service.ts — the same sort comparator was used in getQuotesArchiveData and would fail the same way. All 1331 tests pass; npm run build completes cleanly. Co-authored-by: Orca --- src/lib/server/orders-service.ts | 13 ++++++++++--- src/lib/server/quotes-service.ts | 13 ++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/lib/server/orders-service.ts b/src/lib/server/orders-service.ts index 14a9143..32e0cde 100644 --- a/src/lib/server/orders-service.ts +++ b/src/lib/server/orders-service.ts @@ -682,9 +682,16 @@ export async function getOrdersArchiveData( const av = (a as unknown as Record)[sortKey]; const bv = (b as unknown as Record)[sortKey]; if (av === bv) return 0; - if (av === undefined) return 1; - if (bv === undefined) return -1; - return (av > bv ? 1 : -1) * sortDir; + // PB returns `null` for unset fields (e.g. `updated`, `email_body`) + // and `undefined` for absent fields. Treat both as "missing" + // and sink them to the end of an ascending sort, the start of + // a descending sort. Avoids the `av > bv` comparison where one + // side is null and TS strict mode rejects the >. + if (av == null) return 1; + if (bv == null) return -1; + if (av > bv) return 1 * sortDir; + if (av < bv) return -1 * sortDir; + return 0; }); const totalItems = filtered.length; diff --git a/src/lib/server/quotes-service.ts b/src/lib/server/quotes-service.ts index 01f9afe..6d3a74f 100644 --- a/src/lib/server/quotes-service.ts +++ b/src/lib/server/quotes-service.ts @@ -670,9 +670,16 @@ export async function getQuotesArchiveData( const av = (a as unknown as Record)[sortKey]; const bv = (b as unknown as Record)[sortKey]; if (av === bv) return 0; - if (av === undefined) return 1; - if (bv === undefined) return -1; - return (av > bv ? 1 : -1) * sortDir; + // PB returns `null` for unset fields (e.g. `updated`, `email_body`) + // and `undefined` for absent fields. Treat both as "missing" + // and sink them to the end of an ascending sort, the start of + // a descending sort. Avoids the `av > bv` comparison where one + // side is null and TS strict mode rejects the >. + if (av == null) return 1; + if (bv == null) return -1; + if (av > bv) return 1 * sortDir; + if (av < bv) return -1 * sortDir; + return 0; }); const totalItems = filtered.length;