Skip to content

fix(aggregate): filter before pagination, clamp limit, and return hasMore (closes #888) - #918

Open
mopxam-art wants to merge 1 commit into
Heliobond:mainfrom
mopxam-art:fix/aggregate-filter-paging
Open

mopxam-art wants to merge 1 commit into
Heliobond:mainfrom
mopxam-art:fix/aggregate-filter-paging

Conversation

@mopxam-art

Copy link
Copy Markdown

Overview

Closes #888 by ensuring the portfolio aggregate endpoint (/v1/projects/aggregate) applies filters across the full dataset prior to pagination, enforces positive limit boundaries clamped to 1–100, and returns hasMore and conditional cursor fields matching the standard pagination contract.

Problem & Motivation

Previously, /v1/projects/aggregate sliced project IDs first via Array.from({ length: limit }, (_, i) => cursor + i + 1) and then applied category/region filters to that slice. As a result, filtered responses returned fewer items than requested limit (or even 0 items) despite matching records existing in later offsets. Furthermore, limit <= 0 was not rejected, and the response always echoed cursor: cursor + limit without indicating if more matching items existed.

Changes

  • src/routes/aggregate.ts:
    • Added input validation rejecting non-positive limit (<= 0) and negative cursor (< 0) with 400 Bad Request.
    • Clamped upper limit to 100.
    • Retrieved full registry size via getTotalProjects(), filtered the entire candidate pool across category and region, and then applied pagination slice [cursor, cursor + limit].
    • Response now returns total, filtered_total, limit, hasMore, and only returns cursor when hasMore is true, aligning with /v1/projects.
  • src/__tests__/aggregatePagination.test.ts:
    • Added 102 comprehensive tests across all 5 dimensions (boundary clamping, combinatorics across solar/forest/wind and 4 regions, host variations, negative controls, and multi-page crawl stress tests).

Verification

  • 102 unit tests pass cleanly (npx jest src/__tests__/aggregatePagination.test.ts --forceExit).
  • TypeScript typecheck passes with 0 errors (npx tsc --noEmit).
  • ESLint passes with 0 errors (npx eslint src/routes/aggregate.ts).
  • Production build compiles cleanly (npm run build).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Aggregate endpoint paginates before filtering, never reports termination, and accepts limit=0

1 participant