Skip to content

Commit ea6cd97

Browse files
committed
debug
2 parents c55231a + 44047b6 commit ea6cd97

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/models/copilotRequest.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module.exports = function defineCopilotRequest(sequelize, DataTypes) {
3232

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

3738
return CopliotRequest;

src/routes/copilotRequest/list.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import _ from 'lodash';
2+
import { Op, Sequelize } from 'sequelize';
23

34
import models from '../../models';
45
import util from '../../util';
56
import { PERMISSION } from '../../permissions/constants';
7+
import { DEFAULT_PAGE_SIZE } from '../../constants';
68

79
module.exports = [
810
(req, res, next) => {
@@ -15,6 +17,10 @@ module.exports = [
1517
return next(err);
1618
}
1719

20+
const page = parseInt(req.query.page, 10) || 1;
21+
const pageSize = parseInt(req.query.pageSize, 10) || DEFAULT_PAGE_SIZE;
22+
const offset = (page - 1) * pageSize;
23+
1824
const projectId = _.parseInt(req.params.projectId);
1925

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

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

32-
return models.CopilotRequest.findAll({
38+
return models.CopilotRequest.findAndCountAll({
3339
where: whereCondition,
3440
include: [
35-
{
36-
model: models.CopilotOpportunity,
37-
as: 'copilotOpportunity',
38-
},
41+
{ model: models.CopilotOpportunity, as: 'copilotOpportunity', required: false },
42+
{ model: models.Project, as: 'project', required: false },
3943
],
4044
order: [[sortParams[0], sortParams[1]]],
41-
})
42-
.then(copilotRequests => res.json(copilotRequests))
43-
.catch((err) => {
44-
util.handleError('Error fetching copilot requests', err, req, next);
45-
});
45+
limit: pageSize,
46+
offset,
47+
distinct: true,
48+
subQuery: false,
49+
}).then(({rows: copilotRequests, count}) => util.setPaginationHeaders(req, res, {
50+
count: count,
51+
rows: copilotRequests,
52+
page,
53+
pageSize,
54+
}));
4655
},
4756
];

0 commit comments

Comments
 (0)