11import _ from 'lodash' ;
2+ import { Op , Sequelize } from 'sequelize' ;
23
34import models from '../../models' ;
45import util from '../../util' ;
56import { PERMISSION } from '../../permissions/constants' ;
7+ import { DEFAULT_PAGE_SIZE } from '../../constants' ;
68
79module . 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