1
1
import _ from 'lodash' ;
2
2
import { Op } from 'sequelize' ;
3
+ import config from 'config' ;
3
4
4
5
import models from '../../models' ;
5
6
import 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' ;
7
9
import { PERMISSION } from '../../permissions/constants' ;
8
10
9
11
@@ -20,6 +22,31 @@ module.exports = [
20
22
// default values
21
23
const opportunityId = _ . parseInt ( req . params . id ) ;
22
24
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
+
23
50
return models . sequelize . transaction ( async ( transaction ) => {
24
51
req . log . debug ( 'Canceling Copilot opportunity transaction' , opportunityId ) ;
25
52
const opportunity = await models . CopilotOpportunity . findOne ( {
@@ -93,6 +120,8 @@ module.exports = [
93
120
invite . toJSON ( ) ) ;
94
121
}
95
122
123
+ await sendEmailToAllApplicants ( copilotRequest , applications )
124
+
96
125
res . status ( 200 ) . send ( { id : opportunity . id } ) ;
97
126
} )
98
127
0 commit comments