1
1
import _ from 'lodash' ;
2
- import { middleware as tcMiddleware } from 'tc-core-library-js' ;
3
2
4
3
import models from '../../models' ;
5
4
import { ADMIN_ROLES } from '../../constants' ;
6
5
import util from '../../util' ;
7
6
8
- const permissions = tcMiddleware . permissions ;
9
-
10
7
module . exports = [
11
- permissions ( 'copilotApplications.view' ) ,
12
8
( 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 ) ;
15
10
const opportunityId = _ . parseInt ( req . params . id ) ;
16
11
17
12
let sort = req . query . sort ? decodeURIComponent ( req . query . sort ) : 'createdAt desc' ;
@@ -24,17 +19,15 @@ module.exports = [
24
19
}
25
20
const sortParams = sort . split ( ' ' ) ;
26
21
27
- // Admin can see all requests and the PM can only see requests created by them
28
22
const whereCondition = _ . assign ( {
29
23
opportunityId,
30
24
} ,
31
- canAccessAllApplications ? { } : { createdBy : userId } ,
32
25
) ;
33
26
34
27
return models . CopilotOpportunity . findOne ( {
35
28
where : {
36
29
id : opportunityId ,
37
- }
30
+ } ,
38
31
} ) . then ( ( opportunity ) => {
39
32
if ( ! opportunity ) {
40
33
const err = new Error ( 'No opportunity found' ) ;
@@ -51,13 +44,13 @@ module.exports = [
51
44
] ,
52
45
order : [ [ sortParams [ 0 ] , sortParams [ 1 ] ] ] ,
53
46
} )
54
- . then ( copilotApplications => {
47
+ . then ( ( copilotApplications ) => {
55
48
req . log . debug ( `CopilotApplications ${ JSON . stringify ( copilotApplications ) } ` ) ;
56
49
return models . ProjectMember . getActiveProjectMembers ( opportunity . projectId ) . then ( ( members ) => {
57
50
req . log . debug ( `Fetched existing active members ${ JSON . stringify ( members ) } ` ) ;
58
51
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 ) ;
61
54
62
55
// Using spread operator fails in lint check
63
56
// While Object.assign fails silently during run time
@@ -77,22 +70,30 @@ module.exports = [
77
70
copilotOpportunity : application . copilotOpportunity ,
78
71
} ;
79
72
80
- if ( m ) {
81
- enriched . existingMembership = m ;
73
+ if ( member ) {
74
+ enriched . existingMembership = member ;
82
75
}
83
76
84
77
req . log . debug ( `Existing member to application ${ JSON . stringify ( enriched ) } ` ) ;
85
78
86
79
return enriched ;
87
80
} ) ;
88
81
82
+ const response = isAdminOrPM
83
+ ? enrichedApplications
84
+ : enrichedApplications . map ( app => ( {
85
+ userId : app . userId ,
86
+ status : app . status ,
87
+ createdAt : app . createdAt ,
88
+ } ) ) ;
89
+
89
90
req . log . debug ( `Enriched Applications ${ JSON . stringify ( enrichedApplications ) } ` ) ;
90
- res . status ( 200 ) . send ( enrichedApplications ) ;
91
+ res . status ( 200 ) . send ( response ) ;
91
92
} ) ;
92
- } )
93
+ } ) ;
93
94
} )
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
+ } ) ;
97
98
} ,
98
99
] ;
0 commit comments