Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ workflows:
context : org-global
filters:
branches:
only: ['develop', 'migration-setup', 'pm-1613']
only: ['develop', 'migration-setup', 'paginate-copilot-request']
- deployProd:
context : org-global
filters:
Expand Down
1 change: 1 addition & 0 deletions src/models/copilotRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module.exports = function defineCopilotRequest(sequelize, DataTypes) {

CopliotRequest.associate = (models) => {
CopliotRequest.hasMany(models.CopilotOpportunity, { as: 'copilotOpportunity', foreignKey: 'copilotRequestId' });
CopliotRequest.belongsTo(models.Project, { as: 'project', foreignKey: 'projectId' });
};

return CopliotRequest;
Expand Down
29 changes: 19 additions & 10 deletions src/routes/copilotRequest/list.js
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) => {
Expand All @@ -15,6 +17,10 @@ module.exports = [
return next(err);
}

const page = parseInt(req.query.page, 10) || 1;

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.

const pageSize = parseInt(req.query.pageSize, 10) || DEFAULT_PAGE_SIZE;

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.pageSize to ensure it is a positive integer. This will prevent potential issues if a non-numeric or negative value is provided.

const offset = (page - 1) * pageSize;

const projectId = _.parseInt(req.params.projectId);

let sort = req.query.sort ? decodeURIComponent(req.query.sort) : 'createdAt desc';
Expand All @@ -29,19 +35,22 @@ module.exports = [

const whereCondition = projectId ? { projectId } : {};

return models.CopilotRequest.findAll({
return models.CopilotRequest.findAndCountAll({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider checking if projectId is valid before using it in the whereCondition to prevent potential errors.

where: whereCondition,
include: [
{
model: models.CopilotOpportunity,
as: 'copilotOpportunity',
},
{ model: models.CopilotOpportunity, as: 'copilotOpportunity', required: false },

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The required: false option in the include statement for CopilotOpportunity and Project models might lead to unexpected results if the associations are not optional. Ensure that this behavior is intended.

{ 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, {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function util.setPaginationHeaders is used to set pagination headers. Ensure that this function correctly handles edge cases, such as when count is zero or pageSize is larger than the total number of items.

count: count,
rows: copilotRequests,
page,
pageSize,
}));
},
];