-
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 3 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 |
---|---|---|
|
@@ -18,6 +18,41 @@ const assignCopilotOpportunityValidations = { | |
}), | ||
}; | ||
|
||
const sendEmailToAllApplicants = async (req, 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`, | ||
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 URL construction for |
||
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 handling the case where |
||
}, | ||
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`); | ||
}); | ||
|
||
}; | ||
|
||
module.exports = [ | ||
validate(assignCopilotOpportunityValidations), | ||
async (req, res, next) => { | ||
|
@@ -134,6 +169,9 @@ module.exports = [ | |
}, req.log); | ||
|
||
req.log.debug(`Email sent`); | ||
|
||
// Send email to all applicants about opportunity completion | ||
await sendEmailToAllApplicants(req, opportunity, copilotRequest, application.id); | ||
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 handling potential errors from the |
||
}; | ||
|
||
const existingMember = activeMembers.find(item => item.userId === userId); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,39 @@ | ||
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'; | ||
|
||
|
||
const sendEmailToAllApplicants = async (req, 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`, | ||
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 URL concatenation for |
||
opportunity_title: requestData.opportunityTitle, | ||
user_name: user ? user.handle : "", | ||
}, | ||
sendgrid_template_id: TEMPLATE_IDS.COPILOT_OPPORTUNITY_CANCELED, | ||
recipients: [user.email], | ||
version: 'v3', | ||
}, req.log); | ||
|
||
req.log.debug(`Email sent to copilots who applied`); | ||
}); | ||
|
||
}; | ||
|
||
module.exports = [ | ||
(req, res, next) => { | ||
if (!util.hasPermissionByReq(PERMISSION.CANCEL_COPILOT_OPPORTUNITY, req)) { | ||
|
@@ -93,6 +120,8 @@ module.exports = [ | |
invite.toJSON()); | ||
} | ||
|
||
await sendEmailToAllApplicants(req, 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. It seems like the function |
||
|
||
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.
Using
forEach
with anasync
function can lead to unexpected behavior because the asynchronous operations inside the loop won't be awaited. Consider using afor...of
loop orPromise.all
to handle asynchronous operations properly.