diff --git a/src/app/api/v1/orders/backfill-duplicate-checks/route.test.ts b/src/app/api/v1/orders/backfill-duplicate-checks/route.test.ts index 9d2534e..bf1e358 100644 --- a/src/app/api/v1/orders/backfill-duplicate-checks/route.test.ts +++ b/src/app/api/v1/orders/backfill-duplicate-checks/route.test.ts @@ -100,6 +100,26 @@ describe("POST /api/v1/orders/backfill-duplicate-checks", () => { expect(body.error).toMatch(/limit/i); }); + it("uses the skip param to advance the page (regression: skip was ignored)", async () => { + const { getAdminPocketBase } = await import("@/lib/server/pocketbase-admin"); + const getListSpy = vi.fn().mockResolvedValue({ + items: [{ id: "x" }], + totalItems: 1, + page: 5, + perPage: 3, + }); + vi.mocked(getAdminPocketBase).mockResolvedValueOnce({ + autoCancellation: vi.fn(), + collection: () => ({ getList: getListSpy }), + } as unknown as Awaited>); + + const res = await POST(backfillRequest("?limit=2&skip=8")); + expect(res.status).toBe(200); + // skip=8, limit=2 → page = floor(8/2) + 1 = 5 + expect(getListSpy).toHaveBeenCalledWith(5, 3, expect.any(Object)); + expect((await res.json()).nextSkip).toBe(9); + }); + it("returns 401 when AGENT_API_KEY is missing", async () => { delete process.env.AGENT_API_KEY; const res = await POST(backfillRequest()); 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 de58b86..6f67658 100644 --- a/src/app/api/v1/orders/backfill-duplicate-checks/route.ts +++ b/src/app/api/v1/orders/backfill-duplicate-checks/route.ts @@ -89,10 +89,14 @@ export async function POST(req: NextRequest) { const pb = await getAdminPocketBase(); pb.autoCancellation(false); + // Convert `skip` to a 1-based page number. PB's getList uses page, + // not offset, so we compute `page = floor(skip/limit) + 1`. + const page = Math.floor(skip / limit) + 1; + // Fetch `limit + 1` so we can detect whether more rows remain after // this chunk without a second `count` query. const orders = await pb.collection("orders").getList<{ id: string }>( - 1, + page, limit + 1, { fields: "id",