diff --git a/src/app/api/v1/orders/backfill-duplicate-checks/route.ts b/src/app/api/v1/orders/backfill-duplicate-checks/route.ts index ec32e9d..b41bcc0 100644 --- a/src/app/api/v1/orders/backfill-duplicate-checks/route.ts +++ b/src/app/api/v1/orders/backfill-duplicate-checks/route.ts @@ -7,6 +7,12 @@ * on new order creation (see orders-service.ts createOrder) — this * endpoint is what gets historical orders flagged too. * + * PREREQUISITE: The 7 PB admin fields documented in the per-matcher + * service files (order-number-duplicate-link-service.ts, + * order-order-duplicate-link-service.ts, order-delivery-note-link-service.ts) + * must exist on the `orders` collection before this endpoint can write + * to them. Without the fields, per-order updates will 500. + * * Intentionally sequential per order (not Promise.all) — this can run * over the full orders table, and PocketBase's admin client doesn't * love thousands of concurrent requests. This is a background/admin diff --git a/src/lib/server/order-order-duplicate-link-service.test.ts b/src/lib/server/order-order-duplicate-link-service.test.ts index 95b2dcf..41629c2 100644 --- a/src/lib/server/order-order-duplicate-link-service.test.ts +++ b/src/lib/server/order-order-duplicate-link-service.test.ts @@ -329,4 +329,18 @@ describe("suggestOrderDuplicate", () => { expect(updated.duplicate_order_match_confidence).toBe(1); expect(updated.potential_duplicate_order).toBe("order-edge-in"); }); + + it("does not stamp _matching_attempted when order has no company (early return)", async () => { + const order = await pbMock.collection("orders").create({ + id: "order-no-company", + // company deliberately omitted + }); + + const result = await suggestOrderDuplicate(order.id); + + expect(result.duplicateOrderId).toBeNull(); + expect(result.method).toBe("no_match"); + const updated = await pbMock.collection("orders").getOne("order-no-company"); + expect(updated.duplicate_order_matching_attempted).toBeFalsy(); + }); });