@@ -5,8 +5,9 @@ import Joi from 'joi';
5
5
import { middleware as tcMiddleware } from 'tc-core-library-js' ;
6
6
import models from '../../models' ;
7
7
import util from '../../util' ;
8
- import { EVENT , RESOURCES , PROJECT_MEMBER_ROLE } from '../../constants' ;
8
+ import { EVENT , RESOURCES , PROJECT_MEMBER_ROLE , COPILOT_REQUEST_STATUS , COPILOT_OPPORTUNITY_STATUS , COPILOT_APPLICATION_STATUS } from '../../constants' ;
9
9
import { PERMISSION , PROJECT_TO_TOPCODER_ROLES_MATRIX } from '../../permissions/constants' ;
10
+ import { Op } from 'sequelize' ;
10
11
11
12
/**
12
13
* API to update a project member.
@@ -27,12 +28,85 @@ const updateProjectMemberValdiations = {
27
28
PROJECT_MEMBER_ROLE . SOLUTION_ARCHITECT ,
28
29
PROJECT_MEMBER_ROLE . PROJECT_MANAGER ,
29
30
) . required ( ) ,
31
+ action : Joi . string ( ) . optional ( ) ,
30
32
} ) ,
31
33
query : {
32
34
fields : Joi . string ( ) . optional ( ) ,
33
35
} ,
34
36
} ;
35
37
38
+ const completeAllCopilotRequests = async ( req , projectId , _transaction ) => {
39
+ const allCopilotRequests = await models . CopilotRequest . findAll ( {
40
+ where : {
41
+ projectId,
42
+ } ,
43
+ transaction : _transaction ,
44
+ } ) ;
45
+
46
+ req . log . debug ( `all copilot requests ${ JSON . stringify ( allCopilotRequests ) } ` ) ;
47
+
48
+ await models . CopilotRequest . update ( {
49
+ status : COPILOT_REQUEST_STATUS . FULFILLED ,
50
+ } , {
51
+ where : {
52
+ id : {
53
+ [ Op . in ] : allCopilotRequests . map ( item => item . id ) ,
54
+ }
55
+ } ,
56
+ transaction : _transaction ,
57
+ } ) ;
58
+
59
+ req . log . debug ( `updated all copilot requests` ) ;
60
+
61
+ const copilotOpportunites = await models . CopilotOpportunity . findAll ( {
62
+ where : {
63
+ copilotRequestId : {
64
+ [ Op . in ] : allCopilotRequests . map ( item => item . id ) ,
65
+ } ,
66
+ } ,
67
+ transaction : _transaction ,
68
+ } ) ;
69
+
70
+ req . log . debug ( `all copilot opportunities ${ JSON . stringify ( copilotOpportunites ) } ` ) ;
71
+
72
+ await models . CopilotOpportunity . update ( {
73
+ status : COPILOT_OPPORTUNITY_STATUS . COMPLETED ,
74
+ } , {
75
+ where : {
76
+ id : {
77
+ [ Op . in ] : copilotOpportunites . map ( item => item . id ) ,
78
+ }
79
+ } ,
80
+ transaction : _transaction ,
81
+ } ) ;
82
+
83
+ req . log . debug ( `updated all copilot opportunities` ) ;
84
+
85
+ const allCopilotApplications = await models . CopilotApplication . findAll ( {
86
+ where : {
87
+ opportunityId : {
88
+ [ Op . in ] : copilotOpportunites . map ( item => item . id ) ,
89
+ } ,
90
+ } ,
91
+ transaction : _transaction ,
92
+ } ) ;
93
+
94
+ req . log . debug ( `all copilot applications ${ JSON . stringify ( allCopilotApplications ) } ` ) ;
95
+
96
+ await models . CopilotApplication . update ( {
97
+ status : COPILOT_APPLICATION_STATUS . CANCELED ,
98
+ } , {
99
+ where : {
100
+ id : {
101
+ [ Op . in ] : allCopilotApplications . map ( item => item . id ) ,
102
+ } ,
103
+ } ,
104
+ transaction : _transaction ,
105
+ } ) ;
106
+
107
+ req . log . debug ( `updated all copilot applications` ) ;
108
+ } ;
109
+
36
110
module . exports = [
37
111
// handles request validations
38
112
validate ( updateProjectMemberValdiations ) ,
@@ -45,15 +119,16 @@ module.exports = [
45
119
let updatedProps = req . body ;
46
120
const projectId = _ . parseInt ( req . params . projectId ) ;
47
121
const memberRecordId = _ . parseInt ( req . params . id ) ;
122
+ const action = updatedProps . action ;
48
123
updatedProps = _ . pick ( updatedProps , [ 'isPrimary' , 'role' ] ) ;
49
124
const fields = req . query . fields ? req . query . fields . split ( ',' ) : null ;
50
125
51
126
let previousValue ;
52
127
// let newValue;
53
- models . sequelize . transaction ( ( ) => models . ProjectMember . findOne ( {
128
+ models . sequelize . transaction ( ( _transaction ) => models . ProjectMember . findOne ( {
54
129
where : { id : memberRecordId , projectId } ,
55
130
} )
56
- . then ( ( _member ) => {
131
+ . then ( async ( _member ) => {
57
132
if ( ! _member ) {
58
133
// handle 404
59
134
const err = new Error ( `project member not found for project id ${ projectId } ` +
@@ -76,10 +151,13 @@ module.exports = [
76
151
return Promise . reject ( err ) ;
77
152
}
78
153
154
+ req . log . debug ( `updated props ${ JSON . stringify ( updatedProps ) } ` ) ;
155
+ req . log . debug ( `previous values ${ JSON . stringify ( previousValue ) } ` ) ;
79
156
// no updates if no change
80
- if ( updatedProps . role === previousValue . role &&
157
+ if ( ( updatedProps . role === previousValue . role || action === 'complete-copilot-requests' ) &&
81
158
( _ . isUndefined ( updatedProps . isPrimary ) ||
82
159
updatedProps . isPrimary === previousValue . isPrimary ) ) {
160
+ await completeAllCopilotRequests ( req , projectId , _transaction ) ;
83
161
return Promise . resolve ( ) ;
84
162
}
85
163
@@ -121,9 +199,13 @@ module.exports = [
121
199
} ) ;
122
200
} )
123
201
. then ( ( ) => projectMember . reload ( projectMember . id ) )
124
- . then ( ( ) => {
202
+ . then ( async ( ) => {
125
203
projectMember = projectMember . get ( { plain : true } ) ;
126
204
projectMember = _ . omit ( projectMember , [ 'deletedAt' ] ) ;
205
+
206
+ if ( [ 'observer' , 'customer' ] . includes ( updatedProps . role ) ) {
207
+ await completeAllCopilotRequests ( req , projectId , _transaction ) ;
208
+ }
127
209
} )
128
210
. then ( ( ) => (
129
211
util . getObjectsWithMemberDetails ( [ projectMember ] , fields , req )
0 commit comments