-
Notifications
You must be signed in to change notification settings - Fork 55
feat(PM-1611): send email when opportunity is completed or canceled #849
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
2474181
e65a766
e439cc8
e30591f
d4a88f4
902e2b3
44047b6
e7888e9
4aa668c
83c6db9
5071cb8
c55231a
ea6cd97
4e0bf89
39acdfa
76ec16c
d2425b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,41 @@ module.exports = [ | |
| return next(err); | ||
| } | ||
|
|
||
| const sendEmailToAllApplicants = async (opportunity, copilotRequest, applicationId) => { | ||
| const allApplications = await models.Sequelize.CopilotApplication.findAll({ | ||
| where: { | ||
| opportunityId: opportunity.id, | ||
| id: { | ||
| [Op.notIn]: [applicationId], | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| const userIds = allApplications.map(item => item.userId); | ||
|
|
||
| const users = await util.getMemberDetailsByUserIds(userIds, req.log, req.id); | ||
|
|
||
| users.forEach(async (user) => { | ||
| req.log.debug(`Sending email notification to copilots who are not accepted`); | ||
| const emailEventType = CONNECT_NOTIFICATION_EVENT.EXTERNAL_ACTION_EMAIL; | ||
| const copilotPortalUrl = config.get('copilotPortalUrl'); | ||
| const requestData = copilotRequest.data; | ||
| createEvent(emailEventType, { | ||
| data: { | ||
| opportunity_details_url: `${copilotPortalUrl}/opportunity`, | ||
| opportunity_title: requestData.opportunityTitle, | ||
| user_name: user ? user.handle : "", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| }, | ||
| sendgrid_template_id: TEMPLATE_IDS.COPILOT_OPPORTUNITY_COMPLETED, | ||
| recipients: [user.email], | ||
| version: 'v3', | ||
| }, req.log); | ||
|
|
||
| req.log.debug(`Email sent to copilots who are not accepted`); | ||
| }); | ||
|
|
||
| }; | ||
|
|
||
| return models.sequelize.transaction(async (t) => { | ||
| const opportunity = await models.CopilotOpportunity.findOne({ | ||
| where: { id: copilotOpportunityId }, | ||
|
|
@@ -134,6 +169,9 @@ module.exports = [ | |
| }, req.log); | ||
|
|
||
| req.log.debug(`Email sent`); | ||
|
|
||
| // Send email to all applicants about opportunity completion | ||
| await sendEmailToAllApplicants(opportunity, copilotRequest, application.id); | ||
|
||
| }; | ||
|
|
||
| const existingMember = activeMembers.find(item => item.userId === userId); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| import _ from 'lodash'; | ||
| import { Op } from 'sequelize'; | ||
| import config from 'config'; | ||
|
|
||
| import models from '../../models'; | ||
| import util from '../../util'; | ||
| import { COPILOT_APPLICATION_STATUS, COPILOT_OPPORTUNITY_STATUS, COPILOT_REQUEST_STATUS, EVENT, INVITE_STATUS, RESOURCES } from '../../constants'; | ||
| import { CONNECT_NOTIFICATION_EVENT, COPILOT_APPLICATION_STATUS, COPILOT_OPPORTUNITY_STATUS, COPILOT_REQUEST_STATUS, EVENT, INVITE_STATUS, RESOURCES, TEMPLATE_IDS } from '../../constants'; | ||
| import { createEvent } from '../../services/busApi'; | ||
| import { PERMISSION } from '../../permissions/constants'; | ||
|
|
||
|
|
||
|
|
@@ -20,6 +22,31 @@ module.exports = [ | |
| // default values | ||
| const opportunityId = _.parseInt(req.params.id); | ||
|
|
||
| const sendEmailToAllApplicants = async (copilotRequest, applications) => { | ||
| const userIds = applications.map(item => item.userId); | ||
| const users = await util.getMemberDetailsByUserIds(userIds, req.log, req.id); | ||
|
|
||
| users.forEach(async (user) => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using |
||
| req.log.debug(`Sending email notification to copilots who applied`); | ||
| const emailEventType = CONNECT_NOTIFICATION_EVENT.EXTERNAL_ACTION_EMAIL; | ||
| const copilotPortalUrl = config.get('copilotPortalUrl'); | ||
| const requestData = copilotRequest.data; | ||
| createEvent(emailEventType, { | ||
| data: { | ||
| opportunity_details_url: `${copilotPortalUrl}/opportunity`, | ||
| opportunity_title: requestData.opportunityTitle, | ||
| user_name: user ? user.handle : "", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using a default value or handling the case where |
||
| }, | ||
| sendgrid_template_id: TEMPLATE_IDS.COPILOT_OPPORTUNITY_CANCELED, | ||
| recipients: [user.email], | ||
| version: 'v3', | ||
| }, req.log); | ||
|
|
||
| req.log.debug(`Email sent to copilots who applied`); | ||
| }); | ||
|
|
||
| }; | ||
|
|
||
| return models.sequelize.transaction(async (transaction) => { | ||
| req.log.debug('Canceling Copilot opportunity transaction', opportunityId); | ||
| const opportunity = await models.CopilotOpportunity.findOne({ | ||
|
|
@@ -93,6 +120,8 @@ module.exports = [ | |
| invite.toJSON()); | ||
| } | ||
|
|
||
| await sendEmailToAllApplicants(copilotRequest, applications) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
|
||
| res.status(200).send({ id: opportunity.id }); | ||
| }) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using a
Promise.allto handle asynchronous operations in theforEachloop. This will ensure that all emails are sent before proceeding, and can help catch any errors in the email sending process.