@@ -6,10 +6,11 @@ import config from 'config';
6
6
import models from '../../models' ;
7
7
import util from '../../util' ;
8
8
import { PERMISSION } from '../../permissions/constants' ;
9
- import { CONNECT_NOTIFICATION_EVENT , COPILOT_APPLICATION_STATUS , COPILOT_OPPORTUNITY_STATUS , COPILOT_REQUEST_STATUS , EVENT , INVITE_STATUS , PROJECT_MEMBER_ROLE , RESOURCES , TEMPLATE_IDS } from '../../constants' ;
9
+ import { CONNECT_NOTIFICATION_EVENT , COPILOT_APPLICATION_STATUS , COPILOT_OPPORTUNITY_STATUS , COPILOT_REQUEST_STATUS , EVENT , INVITE_STATUS , PROJECT_MEMBER_ROLE , RESOURCES , TEMPLATE_IDS , USER_ROLE } from '../../constants' ;
10
10
import { getCopilotTypeLabel } from '../../utils/copilot' ;
11
11
import { createEvent } from '../../services/busApi' ;
12
12
import moment from 'moment' ;
13
+ import { Op } from 'sequelize' ;
13
14
14
15
const assignCopilotOpportunityValidations = {
15
16
body : Joi . object ( ) . keys ( {
@@ -172,51 +173,55 @@ module.exports = [
172
173
return ;
173
174
}
174
175
175
- const existingInvite = await models . ProjectMemberInvite . findAll ( {
176
- where : {
177
- userId,
178
- projectId,
179
- role : PROJECT_MEMBER_ROLE . COPILOT ,
180
- status : INVITE_STATUS . PENDING ,
181
- } ,
182
- transaction : t ,
183
- } ) ;
184
-
185
- if ( existingInvite && existingInvite . length ) {
186
- const err = new Error ( `User already has an pending invite to the project` ) ;
187
- err . status = 400 ;
188
- throw err ;
189
- }
190
-
191
- const invite = await models . ProjectMemberInvite . create ( {
192
- status : INVITE_STATUS . PENDING ,
193
- role : PROJECT_MEMBER_ROLE . COPILOT ,
194
- userId,
176
+ const member = {
195
177
projectId,
196
- applicationId : application . id ,
178
+ role : USER_ROLE . TC_COPILOT ,
179
+ userId,
197
180
createdBy : req . authUser . userId ,
198
- createdAt : new Date ( ) ,
199
181
updatedBy : req . authUser . userId ,
200
- updatedAt : new Date ( ) ,
182
+ } ;
183
+ req . context = req . context || { } ;
184
+ req . context . currentProjectMembers = activeMembers ;
185
+ await util . addUserToProject ( req , member , t )
186
+
187
+ await application . update ( {
188
+ status : COPILOT_APPLICATION_STATUS . ACCEPTED ,
201
189
} , {
202
190
transaction : t ,
203
- } )
191
+ } ) ;
204
192
205
- util . sendResourceToKafkaBus (
206
- req ,
207
- EVENT . ROUTING_KEY . PROJECT_MEMBER_INVITE_CREATED ,
208
- RESOURCES . PROJECT_MEMBER_INVITE ,
209
- Object . assign ( { } , invite . toJSON ( ) , {
210
- source : 'copilot_portal' ,
211
- } ) ,
212
- ) ;
193
+ await opportunity . update ( {
194
+ status : COPILOT_OPPORTUNITY_STATUS . COMPLETED ,
195
+ } , {
196
+ transaction : t ,
197
+ } ) ;
213
198
214
- await application . update ( {
215
- status : COPILOT_APPLICATION_STATUS . INVITED ,
199
+
200
+ await copilotRequest . update ( {
201
+ status : COPILOT_REQUEST_STATUS . FULFILLED ,
216
202
} , {
217
203
transaction : t ,
218
204
} ) ;
219
205
206
+ // Cancel other applications
207
+ const otherApplications = await models . CopilotApplication . findAll ( {
208
+ where : {
209
+ opportunityId : copilotOpportunityId ,
210
+ id : {
211
+ [ Op . notIn ] : [ applicationId ] ,
212
+ } ,
213
+ } ,
214
+ transaction : t ,
215
+ } ) ;
216
+
217
+ for ( const otherApplication of otherApplications ) {
218
+ await otherApplication . update ( {
219
+ status : COPILOT_APPLICATION_STATUS . CANCELED ,
220
+ } , {
221
+ transaction : t ,
222
+ } ) ;
223
+ }
224
+
220
225
res . status ( 200 ) . send ( { id : applicationId } ) ;
221
226
} ) . catch ( err => next ( err ) ) ;
222
227
} ,
0 commit comments