-
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 15 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,33 @@ module.exports = [ | |
return next(err); | ||
} | ||
|
||
const sendEmailToAllApplicants = async (copilotRequest, allApplications) => { | ||
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 function signature of |
||
|
||
const userIds = allApplications.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 a |
||
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 }, | ||
|
@@ -238,6 +265,9 @@ module.exports = [ | |
transaction: t, | ||
}); | ||
|
||
// Send email to all applicants about opportunity completion | ||
await sendEmailToAllApplicants(copilotRequest, otherApplications); | ||
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 |
||
|
||
for (const otherApplication of otherApplications) { | ||
await otherApplication.update({ | ||
status: COPILOT_APPLICATION_STATUS.CANCELED, | ||
|
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 }); | ||
}) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ import util from '../../util'; | |
module.exports = [ | ||
(req, res, next) => { | ||
const { id } = req.params; | ||
|
||
req.log.info("Reached get endpoint"); | ||
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 more descriptive log message that includes the opportunity ID to provide better context for debugging, e.g., |
||
if (!id || isNaN(id)) { | ||
return util.handleError('Invalid opportunity ID', null, req, next, 400); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import _ from 'lodash'; | ||
import { Op, Sequelize } from 'sequelize'; | ||
|
||
import models from '../../models'; | ||
import util from '../../util'; | ||
import { PERMISSION } from '../../permissions/constants'; | ||
import { DEFAULT_PAGE_SIZE } from '../../constants'; | ||
|
||
module.exports = [ | ||
(req, res, next) => { | ||
|
@@ -15,6 +17,10 @@ module.exports = [ | |
return next(err); | ||
} | ||
|
||
const page = parseInt(req.query.page, 10) || 1; | ||
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 |
||
const pageSize = parseInt(req.query.pageSize, 10) || DEFAULT_PAGE_SIZE; | ||
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. Similar to the previous comment, ensure that |
||
const offset = (page - 1) * pageSize; | ||
|
||
const projectId = _.parseInt(req.params.projectId); | ||
|
||
let sort = req.query.sort ? decodeURIComponent(req.query.sort) : 'createdAt desc'; | ||
|
@@ -29,19 +35,22 @@ module.exports = [ | |
|
||
const whereCondition = projectId ? { projectId } : {}; | ||
|
||
return models.CopilotRequest.findAll({ | ||
return models.CopilotRequest.findAndCountAll({ | ||
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 checking if |
||
where: whereCondition, | ||
include: [ | ||
{ | ||
model: models.CopilotOpportunity, | ||
as: 'copilotOpportunity', | ||
}, | ||
{ model: models.CopilotOpportunity, as: 'copilotOpportunity', required: false }, | ||
{ model: models.Project, as: 'project', required: false }, | ||
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. Ensure that the inclusion of the |
||
], | ||
order: [[sortParams[0], sortParams[1]]], | ||
}) | ||
.then(copilotRequests => res.json(copilotRequests)) | ||
.catch((err) => { | ||
util.handleError('Error fetching copilot requests', err, req, next); | ||
}); | ||
limit: pageSize, | ||
offset, | ||
distinct: true, | ||
subQuery: false, | ||
}).then(({rows: copilotRequests, count}) => util.setPaginationHeaders(req, res, { | ||
kkartunov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
count: count, | ||
rows: copilotRequests, | ||
page, | ||
pageSize, | ||
})); | ||
}, | ||
]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -473,6 +473,7 @@ router.use((err, req, res, next) => { // eslint-disable-line no-unused-vars | |
|
||
// catch 404 and forward to error handler | ||
router.use((req, res, next) => { | ||
req.log.info("reached middleware") | ||
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 more descriptive log message to provide better context for debugging purposes. For example, specify which middleware was reached or the purpose of this log entry. |
||
const err = new Error('Not Found'); | ||
err.status = 404; | ||
next(err); | ||
|
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.
Typo in the model name:
CopliotRequest
should beCopilotRequest
. This typo is present in the existing code as well, but it would be good to correct it to maintain consistency and avoid potential issues.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.
@hentrymartin please remane and update for the typo. Also, check on all refrences using it.
Funny how we missed this one?