1+ import { USER_ROLE } from '../../constants' ;
12import models from '../../models' ;
23import util from '../../util' ;
34
@@ -8,9 +9,11 @@ module.exports = [
89 return util . handleError ( 'Invalid opportunity ID' , null , req , next , 400 ) ;
910 }
1011
12+ const isAdminOrManager = util . hasRoles ( req , [ USER_ROLE . CONNECT_ADMIN , USER_ROLE . TOPCODER_ADMIN , USER_ROLE . PROJECT_MANAGER ] ) ;
13+
1114 return models . CopilotOpportunity . findOne ( {
1215 where : { id } ,
13- include : [
16+ include : isAdminOrManager ? [
1417 {
1518 model : models . CopilotRequest ,
1619 as : 'copilotRequest' ,
@@ -27,24 +30,36 @@ module.exports = [
2730 } ,
2831 ]
2932 } ,
33+ ] : [
34+ {
35+ model : models . CopilotRequest ,
36+ as : 'copilotRequest' ,
37+ } ,
3038 ] ,
3139 } )
3240 . then ( ( copilotOpportunity ) => {
3341 const plainOpportunity = copilotOpportunity . get ( { plain : true } ) ;
34- const memberIds = plainOpportunity . project . members && plainOpportunity . project . members . map ( ( member ) => member . userId ) ;
42+ const memberIds = ( plainOpportunity . project && plainOpportunity . project . members && plainOpportunity . project . members . map ( ( member ) => member . userId ) ) || [ ] ;
3543 let canApplyAsCopilot = false ;
3644 if ( req . authUser ) {
3745 canApplyAsCopilot = ! memberIds . includes ( req . authUser . userId )
3846 }
39- // This shouldn't be exposed to the clientside
40- delete plainOpportunity . project . members ;
47+
48+ if ( plainOpportunity . project ) {
49+ // This shouldn't be exposed to the clientside
50+ delete plainOpportunity . project . members ;
51+ }
4152 const formattedOpportunity = Object . assign ( {
4253 members : memberIds ,
4354 canApplyAsCopilot,
4455 } , plainOpportunity ,
4556 plainOpportunity . copilotRequest ? plainOpportunity . copilotRequest . data : { } ,
4657 { copilotRequest : undefined } ,
4758 ) ;
59+
60+ if ( ! isAdminOrManager ) {
61+ delete formattedOpportunity . projectId ;
62+ }
4863 res . json ( formattedOpportunity ) ;
4964 } )
5065 . catch ( ( err ) => {
0 commit comments