Skip to content

Commit

Permalink
Merge pull request #314 from UN-OCHA/HPC-10019
Browse files Browse the repository at this point in the history
Refactor: remove pagination cursor
  • Loading branch information
manelcecs authored Jan 17, 2025
2 parents 7d1f8be + 02e2ec7 commit 250250c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 24 deletions.
22 changes: 8 additions & 14 deletions src/domain-services/flows/flow-search-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ export class FlowSearchService {
): Promise<FlowSearchResult> {
const {
limit,
nextPageCursor,
prevPageCursor,
page,
sortField,
sortOrder,
includeChildrenOfParkedFlows: shouldIncludeChildrenOfParkedFlows,
Expand Down Expand Up @@ -138,7 +137,7 @@ export class FlowSearchService {
orderBy
);

const offset = nextPageCursor ?? prevPageCursor ?? 0;
const offset = page && limit ? page * limit : 0;

// We add 1 to the limit to check if there is a next page
const searchLimit = limit + 1;
Expand Down Expand Up @@ -296,13 +295,8 @@ export class FlowSearchService {
return {
flows: items,
hasNextPage: hasNextPage,
hasPreviousPage: nextPageCursor !== undefined,
prevPageCursor: nextPageCursor ? nextPageCursor - limit : 0,
nextPageCursor: hasNextPage
? nextPageCursor
? nextPageCursor + limit
: limit
: 0,
hasPreviousPage: page > 0,
page,
pageSize: flows.length,
sortField: `${orderBy.entity}.${orderBy.column}` as FlowSortField,
sortOrder: sortOrder ?? 'desc',
Expand Down Expand Up @@ -532,19 +526,19 @@ export class FlowSearchService {

let hasNextPage = flowSearchResponse.hasNextPage;

let cursor = flowSearchResponse.nextPageCursor;
let nextArgs: SearchFlowsArgs = { ...args, nextPageCursor: cursor };
let page = flowSearchResponse.page;
let nextArgs: SearchFlowsArgs = { ...args, page: page + 1 };

let nextFlowSearchResponse: FlowSearchResult;
while (hasNextPage) {
nextFlowSearchResponse = await this.search(models, nextArgs);
flows.push(...nextFlowSearchResponse.flows);

hasNextPage = nextFlowSearchResponse.hasNextPage;
cursor = nextFlowSearchResponse.nextPageCursor;
page = nextFlowSearchResponse.page + 1;

// Update the cursor for the next iteration
nextArgs = { ...args, nextPageCursor: cursor };
nextArgs = { ...args, page };
}

return { flows, flowsCount: flows.length };
Expand Down
10 changes: 2 additions & 8 deletions src/utils/graphql/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ export class PageInfo<TSortFields extends string> {
hasPreviousPage: boolean;

@Field(() => Number, { nullable: false })
prevPageCursor: number;

@Field(() => Number, { nullable: false })
nextPageCursor: number;
page: number;

@Field({ nullable: false })
pageSize: number;
Expand All @@ -39,10 +36,7 @@ export class PaginationArgs<TSortFields extends string> {
limit: number;

@Field(() => Number, { nullable: true })
nextPageCursor: number;

@Field(() => Number, { nullable: true })
prevPageCursor: number;
page: number;

@Field(() => String, { nullable: true })
sortField: TSortFields;
Expand Down
3 changes: 1 addition & 2 deletions tests/resolvers/flows.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,7 @@ describe('Query should return Flow search', () => {
expect(searchFlowsResponse.pageSize).toBe(defaultPageSize);
expect(searchFlowsResponse.hasPreviousPage).toBeDefined();
expect(searchFlowsResponse.hasNextPage).toBeDefined();
expect(searchFlowsResponse.nextPageCursor).toBeDefined();
expect(searchFlowsResponse.prevPageCursor).toBeDefined();
expect(searchFlowsResponse.page).toBeDefined();
expect(searchFlowsResponse.total).toBeDefined();
expect(searchFlowsResponse.flows).toBeDefined();
}
Expand Down

0 comments on commit 250250c

Please sign in to comment.