Skip to content

Commit

Permalink
feat: generic JSON based pagination, filter, sort for data tables (#522)
Browse files Browse the repository at this point in the history
Co-authored-by: goemen <[email protected]>
  • Loading branch information
sukanya-rath and goemen committed Jun 12, 2024
1 parent e1650f4 commit 5ecdf41
Show file tree
Hide file tree
Showing 8 changed files with 842 additions and 2 deletions.
11 changes: 10 additions & 1 deletion backend/package-lock.json

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

3 changes: 2 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"uuid": "^9.0.1",
"wait-for-expect": "^3.0.2",
"winston": "^3.11.0",
"xml2js": "^0.6.2"
"xml2js": "^0.6.2",
"zod": "^3.23.8"
},
"devDependencies": {
"@faker-js/faker": "^8.4.1",
Expand Down
2 changes: 2 additions & 0 deletions backend/src/admin-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import adminAuthRouter from './v1/routes/admin-auth-routes';
import adminUserRouter from './v1/routes/admin-user-info-routes';
import { adminAuth } from './v1/services/admin-auth-service';
import { utils } from './v1/services/utils-service';
import adminReportRoutes from "./v1/routes/admin-report-routes";

export const OIDC_AZUREIDIR_CALLBACK_URL = `${config.get('server:adminFrontend')}/admin-api/auth/${OIDC_AZUREIDIR_CALLBACK_NAME}`;

Expand Down Expand Up @@ -261,6 +262,7 @@ apiRouter.use(
},
);
apiRouter.use('/user', adminUserRouter);
apiRouter.use('/v1/reports', adminReportRoutes);
adminApp.use(function (req: Request, res: Response, _next: NextFunction) {
return res.status(404).send({ message: 'Route' + req.url + ' Not found.' });
});
Expand Down
40 changes: 40 additions & 0 deletions backend/src/v1/routes/admin-report-routes.spec.ts
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',
);
});
});
29 changes: 29 additions & 0 deletions backend/src/v1/routes/admin-report-routes.ts
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;
Loading

0 comments on commit 5ecdf41

Please sign in to comment.