@@ -27,11 +27,38 @@ module.exports = [
27
27
if ( sort . indexOf ( ' ' ) === - 1 ) {
28
28
sort += ' asc' ;
29
29
}
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
+ 'status asc' ,
40
+ 'status desc' ,
41
+ ] ;
31
42
if ( _ . indexOf ( sortableProps , sort ) < 0 ) {
32
43
return util . handleError ( 'Invalid sort criteria' , null , req , next ) ;
33
44
}
34
- const sortParams = sort . split ( ' ' ) ;
45
+ let sortParams = sort . split ( ' ' ) ;
46
+ let order = [ [ sortParams [ 0 ] , sortParams [ 1 ] ] ] ;
47
+ const relationBasedSortParams = [ 'projectName' ] ;
48
+ const jsonBasedSortParams = [ 'opportunityTitle' , 'projectType' ] ;
49
+ if ( relationBasedSortParams . includes ( sortParams [ 0 ] ) ) {
50
+ order = [
51
+ [ { model : models . Project , as : 'project' } , 'name' , sortParams [ 1 ] ] ,
52
+ [ 'id' , 'DESC' ]
53
+ ]
54
+ }
55
+
56
+ if ( jsonBasedSortParams . includes ( sortParams [ 0 ] ) ) {
57
+ order = [
58
+ [ models . sequelize . literal ( `("CopilotRequest"."data"->>'${ sortParams [ 0 ] } ')` ) , sortParams [ 1 ] ] ,
59
+ [ 'id' , 'DESC' ] ,
60
+ ]
61
+ }
35
62
36
63
const whereCondition = projectId ? { projectId } : { } ;
37
64
@@ -41,7 +68,7 @@ module.exports = [
41
68
{ model : models . CopilotOpportunity , as : 'copilotOpportunity' , required : false } ,
42
69
{ model : models . Project , as : 'project' , required : false } ,
43
70
] ,
44
- order : [ [ sortParams [ 0 ] , sortParams [ 1 ] ] ] ,
71
+ order,
45
72
limit : pageSize ,
46
73
offset,
47
74
distinct : true ,
0 commit comments