11import _ from 'lodash' ;
2- import { middleware as tcMiddleware } from 'tc-core-library-js' ;
32
43import models from '../../models' ;
54import { ADMIN_ROLES } from '../../constants' ;
65import util from '../../util' ;
76
8- const permissions = tcMiddleware . permissions ;
9-
107module . exports = [
11- permissions ( 'copilotApplications.view' ) ,
128 ( req , res , next ) => {
13- const canAccessAllApplications = util . hasRoles ( req , ADMIN_ROLES ) || util . hasProjectManagerRole ( req ) ;
14- const userId = req . authUser . userId ;
9+ const isAdminOrPM = util . hasRoles ( req , ADMIN_ROLES ) || util . hasProjectManagerRole ( req ) ;
1510 const opportunityId = _ . parseInt ( req . params . id ) ;
1611
1712 let sort = req . query . sort ? decodeURIComponent ( req . query . sort ) : 'createdAt desc' ;
@@ -24,17 +19,15 @@ module.exports = [
2419 }
2520 const sortParams = sort . split ( ' ' ) ;
2621
27- // Admin can see all requests and the PM can only see requests created by them
2822 const whereCondition = _ . assign ( {
2923 opportunityId,
3024 } ,
31- canAccessAllApplications ? { } : { createdBy : userId } ,
3225 ) ;
3326
3427 return models . CopilotOpportunity . findOne ( {
3528 where : {
3629 id : opportunityId ,
37- }
30+ } ,
3831 } ) . then ( ( opportunity ) => {
3932 if ( ! opportunity ) {
4033 const err = new Error ( 'No opportunity found' ) ;
@@ -51,13 +44,13 @@ module.exports = [
5144 ] ,
5245 order : [ [ sortParams [ 0 ] , sortParams [ 1 ] ] ] ,
5346 } )
54- . then ( copilotApplications => {
47+ . then ( ( copilotApplications ) => {
5548 req . log . debug ( `CopilotApplications ${ JSON . stringify ( copilotApplications ) } ` ) ;
5649 return models . ProjectMember . getActiveProjectMembers ( opportunity . projectId ) . then ( ( members ) => {
5750 req . log . debug ( `Fetched existing active members ${ JSON . stringify ( members ) } ` ) ;
5851 req . log . debug ( `Applications ${ JSON . stringify ( copilotApplications ) } ` ) ;
59- const enrichedApplications = copilotApplications . map ( application => {
60- const m = members . find ( m => m . userId === application . userId ) ;
52+ const enrichedApplications = copilotApplications . map ( ( application ) => {
53+ const member = members . find ( memberItem => memberItem . userId === application . userId ) ;
6154
6255 // Using spread operator fails in lint check
6356 // While Object.assign fails silently during run time
@@ -77,22 +70,30 @@ module.exports = [
7770 copilotOpportunity : application . copilotOpportunity ,
7871 } ;
7972
80- if ( m ) {
81- enriched . existingMembership = m ;
73+ if ( member ) {
74+ enriched . existingMembership = member ;
8275 }
8376
8477 req . log . debug ( `Existing member to application ${ JSON . stringify ( enriched ) } ` ) ;
8578
8679 return enriched ;
8780 } ) ;
8881
82+ const response = isAdminOrPM
83+ ? enrichedApplications
84+ : enrichedApplications . map ( app => ( {
85+ userId : app . userId ,
86+ status : app . status ,
87+ createdAt : app . createdAt ,
88+ } ) ) ;
89+
8990 req . log . debug ( `Enriched Applications ${ JSON . stringify ( enrichedApplications ) } ` ) ;
90- res . status ( 200 ) . send ( enrichedApplications ) ;
91+ res . status ( 200 ) . send ( response ) ;
9192 } ) ;
92- } )
93+ } ) ;
9394 } )
94- . catch ( ( err ) => {
95- util . handleError ( 'Error fetching copilot applications' , err , req , next ) ;
96- } ) ;
95+ . catch ( ( err ) => {
96+ util . handleError ( 'Error fetching copilot applications' , err , req , next ) ;
97+ } ) ;
9798 } ,
9899] ;
0 commit comments