-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generic JSON based pagination, filter, sort for data tables (#522)
Co-authored-by: goemen <[email protected]>
- Loading branch information
1 parent
e1650f4
commit 5ecdf41
Showing
8 changed files
with
842 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import express, { Application } from 'express'; | ||
import router from './admin-report-routes'; | ||
import request from 'supertest'; | ||
|
||
const mockSearchReport = jest.fn(); | ||
jest.mock('../services/report-search-service', () => ({ | ||
reportSearchService: { | ||
searchReport: (...args) => mockSearchReport(...args), | ||
}, | ||
})); | ||
|
||
let app: Application; | ||
describe('admin-report-routes', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
app = express(); | ||
app.use(router); | ||
}); | ||
|
||
it('should default offset=0 and limit=20', async () => { | ||
await request(app).get('').expect(200); | ||
expect(mockSearchReport).toHaveBeenCalledWith( | ||
0, | ||
20, | ||
undefined, | ||
undefined | ||
) | ||
}); | ||
|
||
it('should resolve query params', async () => { | ||
await request(app).get('') | ||
.query({offset: 1, limit: 50, filter: 'naics_code=11', sort: 'field=naics_code,direction=asc'}).expect(200); | ||
expect(mockSearchReport).toHaveBeenCalledWith( | ||
1, | ||
50, | ||
'field=naics_code,direction=asc', | ||
'naics_code=11', | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import express, { Request, Response } from 'express'; | ||
import { logger } from '../../logger'; | ||
import { utils } from '../services/utils-service'; | ||
import { reportSearchService } from '../services/report-search-service'; | ||
|
||
const router = express.Router(); | ||
/** | ||
* /admin-api/v1/reports | ||
* Search reports with pagination and sort. | ||
* flexible endpoint to support filtering and sorting without any modification required to this endpoint. | ||
*/ | ||
router.get( | ||
'/', | ||
utils.asyncHandler(async (req: Request, res: Response) => { | ||
logger.info('Pagination endpoint called'); | ||
logger.silly(req.query); //log the query params at silly level | ||
const offset = req.query.offset ? parseInt(req.query.offset as string) : 0; | ||
const limit = req.query.limit ? parseInt(req.query.limit as string) : 20; | ||
const paginatedReportsResponse = await reportSearchService.searchReport( | ||
offset, | ||
limit, | ||
req.query.sort as string, | ||
req.query.filter as string, | ||
); | ||
return res.status(200).json(paginatedReportsResponse); | ||
}), | ||
); | ||
|
||
export default router; |
Oops, something went wrong.