11import _ from 'lodash' ;
22import { Op } from 'sequelize' ;
3+ import config from 'config' ;
34
45import models from '../../models' ;
56import util from '../../util' ;
6- import { COPILOT_APPLICATION_STATUS , COPILOT_OPPORTUNITY_STATUS , COPILOT_REQUEST_STATUS , EVENT , INVITE_STATUS , RESOURCES } from '../../constants' ;
7+ import { CONNECT_NOTIFICATION_EVENT , COPILOT_APPLICATION_STATUS , COPILOT_OPPORTUNITY_STATUS , COPILOT_REQUEST_STATUS , EVENT , INVITE_STATUS , RESOURCES , TEMPLATE_IDS } from '../../constants' ;
8+ import { createEvent } from '../../services/busApi' ;
79import { PERMISSION } from '../../permissions/constants' ;
810
911
@@ -20,6 +22,31 @@ module.exports = [
2022 // default values
2123 const opportunityId = _ . parseInt ( req . params . id ) ;
2224
25+ const sendEmailToAllApplicants = async ( copilotRequest , applications ) => {
26+ const userIds = applications . map ( item => item . userId ) ;
27+ const users = await util . getMemberDetailsByUserIds ( userIds , req . log , req . id ) ;
28+
29+ users . forEach ( async ( user ) => {
30+ req . log . debug ( `Sending email notification to copilots who applied` ) ;
31+ const emailEventType = CONNECT_NOTIFICATION_EVENT . EXTERNAL_ACTION_EMAIL ;
32+ const copilotPortalUrl = config . get ( 'copilotPortalUrl' ) ;
33+ const requestData = copilotRequest . data ;
34+ createEvent ( emailEventType , {
35+ data : {
36+ opportunity_details_url : `${ copilotPortalUrl } /opportunity` ,
37+ opportunity_title : requestData . opportunityTitle ,
38+ user_name : user ? user . handle : "" ,
39+ } ,
40+ sendgrid_template_id : TEMPLATE_IDS . COPILOT_OPPORTUNITY_CANCELED ,
41+ recipients : [ user . email ] ,
42+ version : 'v3' ,
43+ } , req . log ) ;
44+
45+ req . log . debug ( `Email sent to copilots who applied` ) ;
46+ } ) ;
47+
48+ } ;
49+
2350 return models . sequelize . transaction ( async ( transaction ) => {
2451 req . log . debug ( 'Canceling Copilot opportunity transaction' , opportunityId ) ;
2552 const opportunity = await models . CopilotOpportunity . findOne ( {
@@ -93,6 +120,8 @@ module.exports = [
93120 invite . toJSON ( ) ) ;
94121 }
95122
123+ await sendEmailToAllApplicants ( copilotRequest , applications )
124+
96125 res . status ( 200 ) . send ( { id : opportunity . id } ) ;
97126 } )
98127
0 commit comments