-
Notifications
You must be signed in to change notification settings - Fork 55
fix(PM-1650): Paginate copilot request API #848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2474181
e65a766
e439cc8
e30591f
d4a88f4
902e2b3
44047b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import _ from 'lodash'; | ||
import { Op, Sequelize } from 'sequelize'; | ||
|
||
import models from '../../models'; | ||
import util from '../../util'; | ||
import { PERMISSION } from '../../permissions/constants'; | ||
import { DEFAULT_PAGE_SIZE } from '../../constants'; | ||
|
||
module.exports = [ | ||
(req, res, next) => { | ||
|
@@ -15,6 +17,10 @@ module.exports = [ | |
return next(err); | ||
} | ||
|
||
const page = parseInt(req.query.page, 10) || 1; | ||
const pageSize = parseInt(req.query.pageSize, 10) || DEFAULT_PAGE_SIZE; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding validation for |
||
const offset = (page - 1) * pageSize; | ||
|
||
const projectId = _.parseInt(req.params.projectId); | ||
|
||
let sort = req.query.sort ? decodeURIComponent(req.query.sort) : 'createdAt desc'; | ||
|
@@ -29,19 +35,22 @@ module.exports = [ | |
|
||
const whereCondition = projectId ? { projectId } : {}; | ||
|
||
return models.CopilotRequest.findAll({ | ||
return models.CopilotRequest.findAndCountAll({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider checking if |
||
where: whereCondition, | ||
include: [ | ||
{ | ||
model: models.CopilotOpportunity, | ||
as: 'copilotOpportunity', | ||
}, | ||
{ model: models.CopilotOpportunity, as: 'copilotOpportunity', required: false }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
{ model: models.Project, as: 'project', required: false }, | ||
], | ||
order: [[sortParams[0], sortParams[1]]], | ||
}) | ||
.then(copilotRequests => res.json(copilotRequests)) | ||
.catch((err) => { | ||
util.handleError('Error fetching copilot requests', err, req, next); | ||
}); | ||
limit: pageSize, | ||
offset, | ||
distinct: true, | ||
subQuery: false, | ||
}).then(({rows: copilotRequests, count}) => util.setPaginationHeaders(req, res, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function |
||
count: count, | ||
rows: copilotRequests, | ||
page, | ||
pageSize, | ||
})); | ||
}, | ||
]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding validation for
req.query.page
to ensure it is a positive integer. This will prevent potential issues if a non-numeric or negative value is provided.