@@ -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,79 @@ 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+ const sendEmailToCopilot = async ( ) => {
207+ const memberDetails = await util . getMemberDetailsByUserIds ( [ application . userId ] , req . log , req . id ) ;
208+ const member = memberDetails [ 0 ] ;
209+ req . log . debug ( `Sending email notification to accepted copilot` ) ;
210+ const emailEventType = CONNECT_NOTIFICATION_EVENT . EXTERNAL_ACTION_EMAIL ;
211+ const copilotPortalUrl = config . get ( 'copilotPortalUrl' ) ;
212+ const requestData = copilotRequest . data ;
213+ createEvent ( emailEventType , {
214+ data : {
215+ opportunity_details_url : `${ copilotPortalUrl } /opportunity/${ opportunity . id } ` ,
216+ opportunity_title : requestData . opportunityTitle ,
217+ start_date : moment . utc ( requestData . startDate ) . format ( 'DD-MM-YYYY' ) ,
218+ user_name : member ? member . handle : "" ,
219+ } ,
220+ sendgrid_template_id : TEMPLATE_IDS . COPILOT_APPLICATION_ACCEPTED ,
221+ recipients : [ member . email ] ,
222+ version : 'v3' ,
223+ } , req . log ) ;
224+
225+ req . log . debug ( `Email sent to copilot` ) ;
226+ } ;
227+
228+ await sendEmailToCopilot ( ) ;
229+
230+ // Cancel other applications
231+ const otherApplications = await models . CopilotApplication . findAll ( {
232+ where : {
233+ opportunityId : copilotOpportunityId ,
234+ id : {
235+ [ Op . notIn ] : [ applicationId ] ,
236+ } ,
237+ } ,
238+ transaction : t ,
239+ } ) ;
240+
241+ for ( const otherApplication of otherApplications ) {
242+ await otherApplication . update ( {
243+ status : COPILOT_APPLICATION_STATUS . CANCELED ,
244+ } , {
245+ transaction : t ,
246+ } ) ;
247+ }
248+
220249 res . status ( 200 ) . send ( { id : applicationId } ) ;
221250 } ) . catch ( err => next ( err ) ) ;
222251 } ,
0 commit comments