@@ -6,10 +6,11 @@ import config from 'config';
66import models from '../../models' ;
77import util from '../../util' ;
88import { 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' ;
1010import { getCopilotTypeLabel } from '../../utils/copilot' ;
1111import { createEvent } from '../../services/busApi' ;
1212import moment from 'moment' ;
13+ import { Op } from 'sequelize' ;
1314
1415const assignCopilotOpportunityValidations = {
1516 body : Joi . object ( ) . keys ( {
@@ -172,51 +173,55 @@ module.exports = [
172173 return ;
173174 }
174175
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 = {
195177 projectId,
196- applicationId : application . id ,
178+ role : USER_ROLE . TC_COPILOT ,
179+ userId,
197180 createdBy : req . authUser . userId ,
198- createdAt : new Date ( ) ,
199181 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 ,
201189 } , {
202190 transaction : t ,
203- } )
191+ } ) ;
204192
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+ } ) ;
213198
214- await application . update ( {
215- status : COPILOT_APPLICATION_STATUS . INVITED ,
199+
200+ await copilotRequest . update ( {
201+ status : COPILOT_REQUEST_STATUS . FULFILLED ,
216202 } , {
217203 transaction : t ,
218204 } ) ;
219205
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+
220225 res . status ( 200 ) . send ( { id : applicationId } ) ;
221226 } ) . catch ( err => next ( err ) ) ;
222227 } ,
0 commit comments