Skip to content

Commit e7888e9

Browse files
committed
feat: send email when opportunity is completed or canceled
1 parent 82c1908 commit e7888e9

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

src/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ export const TEMPLATE_IDS = {
314314
INFORM_PM_COPILOT_APPLICATION_ACCEPTED: 'd-b35d073e302b4279a1bd208fcfe96f58',
315315
COPILOT_ALREADY_PART_OF_PROJECT: 'd-003d41cdc9de4bbc9e14538e8f2e0585',
316316
COPILOT_APPLICATION_ACCEPTED: 'd-eef5e7568c644940b250e76d026ced5b',
317+
COPILOT_OPPORTUNITY_COMPLETED: 'd-dc448919d11b4e7d8b4ba351c4b67b8b',
318+
COPILOT_OPPORTUNITY_CANCELED: 'd-2a67ba71e82f4d70891fe6989c3522a3'
317319
}
318320
export const REGEX = {
319321
URL: /^(http(s?):\/\/)?(www\.)?[a-zA-Z0-9\.\-\_]+(\.[a-zA-Z]{2,15})+(\:[0-9]{2,5})?(\/[a-zA-Z0-9\_\-\s\.\/\?\%\#\&\=;]*)?$/, // eslint-disable-line

src/routes/copilotOpportunity/assign.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,41 @@ const assignCopilotOpportunityValidations = {
1818
}),
1919
};
2020

21+
const sendEmailToAllApplicants = async (req, opportunity, copilotRequest, applicationId) => {
22+
const allApplications = await models.Sequelize.CopilotApplication.findAll({
23+
where: {
24+
opportunityId: opportunity.id,
25+
id: {
26+
[Op.notIn]: [applicationId],
27+
}
28+
}
29+
});
30+
31+
const userIds = allApplications.map(item => item.userId);
32+
33+
const users = await util.getMemberDetailsByUserIds(userIds, req.log, req.id);
34+
35+
users.forEach(async (user) => {
36+
req.log.debug(`Sending email notification to copilots who are not accepted`);
37+
const emailEventType = CONNECT_NOTIFICATION_EVENT.EXTERNAL_ACTION_EMAIL;
38+
const copilotPortalUrl = config.get('copilotPortalUrl');
39+
const requestData = copilotRequest.data;
40+
createEvent(emailEventType, {
41+
data: {
42+
opportunity_details_url: `${copilotPortalUrl}/opportunity`,
43+
opportunity_title: requestData.opportunityTitle,
44+
user_name: user ? user.handle : "",
45+
},
46+
sendgrid_template_id: TEMPLATE_IDS.COPILOT_OPPORTUNITY_COMPLETED,
47+
recipients: [user.email],
48+
version: 'v3',
49+
}, req.log);
50+
51+
req.log.debug(`Email sent to copilots who are not accepted`);
52+
});
53+
54+
};
55+
2156
module.exports = [
2257
validate(assignCopilotOpportunityValidations),
2358
async (req, res, next) => {
@@ -134,6 +169,9 @@ module.exports = [
134169
}, req.log);
135170

136171
req.log.debug(`Email sent`);
172+
173+
// Send email to all applicants about opportunity completion
174+
await sendEmailToAllApplicants(req, opportunity, copilotRequest, application.id);
137175
};
138176

139177
const existingMember = activeMembers.find(item => item.userId === userId);

src/routes/copilotOpportunity/delete.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,36 @@ import { Op } from 'sequelize';
33

44
import models from '../../models';
55
import util from '../../util';
6-
import { COPILOT_APPLICATION_STATUS, COPILOT_OPPORTUNITY_STATUS, COPILOT_REQUEST_STATUS, EVENT, INVITE_STATUS, RESOURCES } from '../../constants';
6+
import { CONNECT_NOTIFICATION_EVENT, COPILOT_APPLICATION_STATUS, COPILOT_OPPORTUNITY_STATUS, COPILOT_REQUEST_STATUS, EVENT, INVITE_STATUS, RESOURCES, TEMPLATE_IDS } from '../../constants';
7+
import { createEvent } from '../../services/busApi';
78
import { PERMISSION } from '../../permissions/constants';
89

910

11+
const sendEmailToAllApplicants = async (req, copilotRequest, applications) => {
12+
const userIds = applications.map(item => item.userId);
13+
const users = await util.getMemberDetailsByUserIds(userIds, req.log, req.id);
14+
15+
users.forEach(async (user) => {
16+
req.log.debug(`Sending email notification to copilots who applied`);
17+
const emailEventType = CONNECT_NOTIFICATION_EVENT.EXTERNAL_ACTION_EMAIL;
18+
const copilotPortalUrl = config.get('copilotPortalUrl');
19+
const requestData = copilotRequest.data;
20+
createEvent(emailEventType, {
21+
data: {
22+
opportunity_details_url: `${copilotPortalUrl}/opportunity`,
23+
opportunity_title: requestData.opportunityTitle,
24+
user_name: user ? user.handle : "",
25+
},
26+
sendgrid_template_id: TEMPLATE_IDS.COPILOT_OPPORTUNITY_CANCELED,
27+
recipients: [user.email],
28+
version: 'v3',
29+
}, req.log);
30+
31+
req.log.debug(`Email sent to copilots who applied`);
32+
});
33+
34+
};
35+
1036
module.exports = [
1137
(req, res, next) => {
1238
if (!util.hasPermissionByReq(PERMISSION.CANCEL_COPILOT_OPPORTUNITY, req)) {
@@ -93,6 +119,8 @@ module.exports = [
93119
invite.toJSON());
94120
}
95121

122+
await sendEmailToAllApplicants(req, copilotRequest, applications)
123+
96124
res.status(200).send({ id: opportunity.id });
97125
})
98126

0 commit comments

Comments
 (0)