fix(orders): honor skip param in backfill endpoint (was hardcoded to page 1)

The route accepted ?skip=N but always called getList(1, ...) — every
chunk re-processed the FIRST limit orders, leaving offsets > limit
unprocessed. Convert skip to page (floor(skip/limit) + 1) and add a
regression test asserting getList is called with the right page.
This commit is contained in:
Dave 2026-08-31 00:55:56 +02:00
parent ee2dcdecb7
commit 463265fe36
2 changed files with 25 additions and 1 deletions

View file

@ -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<ReturnType<typeof getAdminPocketBase>>);
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());

View file

@ -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",