Skip to content
This repository has been archived by the owner on May 5, 2023. It is now read-only.

Commit

Permalink
fix(Pagination): Handle case when cursor filter overlaps entry filter…
Browse files Browse the repository at this point in the history
… (LLC-1909) (#399)
  • Loading branch information
crazy-grizzly authored Jul 5, 2022
1 parent a61d7f8 commit 1b762ab
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 44 deletions.
113 changes: 94 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"rimraf": "2.7.1",
"supertest": "3.4.2",
"travis-cov": "0.2.5",
"ts-mocha": "^10.0.0",
"tslint": "5.20.1",
"tslint-consistent-codestyle": "1.16.0",
"tslint-immutable": "4.9.1",
Expand Down
28 changes: 19 additions & 9 deletions src/mongoModelsRepo/utils/pagination.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { first, last } from 'lodash';
import { first, isEmpty, isUndefined, last } from 'lodash';
import { ObjectID } from 'mongodb';
import NoCursorBackwardsDirection from '../../errors/NoCursorBackwardsDirection';
import BaseModel from '../../models/BaseModel';
Expand Down Expand Up @@ -26,18 +26,28 @@ export default (config: Config, collectionName: string) => {

const collection = (await config.db).collection(collectionName);

const theFilter = {
...filter,
...cursorToFilter({
cursor,
direction,
sort,
}),
const cursorFilter = cursorToFilter({
cursor,
direction,
sort,
});

const filters = [
filter,
cursorFilter,
].filter((item) => !isUndefined(item) && !isEmpty(item));

const combinedFilters = filters.length <= 1
? filters[0]
: { $and: filters };

const scopedFilter = {
...combinedFilters,
organisation: new ObjectID(organisation),
};

const mongoCursor = collection.find()
.filter(theFilter)
.filter(scopedFilter)
.sort(sort)
.project(project)
.limit(limit + 1)
Expand Down
Loading

0 comments on commit 1b762ab

Please sign in to comment.