Skip to content

Commit dea96ef

Browse files
committed
fix: added sorting functionality
1 parent b8343b9 commit dea96ef

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/routes/copilotRequest/list.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,31 @@ module.exports = [
2727
if (sort.indexOf(' ') === -1) {
2828
sort += ' asc';
2929
}
30-
const sortableProps = ['createdAt asc', 'createdAt desc'];
30+
const sortableProps = [
31+
'createdAt asc',
32+
'createdAt desc',
33+
'projectName asc',
34+
'projectName desc',
35+
'opportunityTitle asc',
36+
'opportunityTitle desc',
37+
'projectType asc',
38+
'projectType desc',
39+
];
3140
if (_.indexOf(sortableProps, sort) < 0) {
3241
return util.handleError('Invalid sort criteria', null, req, next);
3342
}
3443
let sortParams = sort.split(' ');
35-
let order = [[sortParams[0], sortParams[1]]]
36-
if (sortParams[0] === 'projectName') {
44+
let order = [[sortParams[0], sortParams[1]]];
45+
const relationBasedSortParams = ['projectName'];
46+
const jsonBasedSortParams = ['opportunityTitle', 'projectType'];
47+
if (relationBasedSortParams.includes(sortParams[0])) {
3748
order = [[{model: models.Project, as: 'project'}, 'name', sortParams[1]]]
3849
}
3950

51+
if (jsonBasedSortParams.includes(sortParams[0])) {
52+
order = [[models.sequelize.literal(`("CopilotRequest"."data"->>'${sortParams[0]}')`), sortParams[1]]]
53+
}
54+
4055
const whereCondition = projectId ? { projectId } : {};
4156

4257
return models.CopilotRequest.findAndCountAll({

0 commit comments

Comments
 (0)