-
Notifications
You must be signed in to change notification settings - Fork 55
PROD RELEASE - Copilot Portal updates #852
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 all commits
2474181
e65a766
e439cc8
e30591f
d4a88f4
902e2b3
44047b6
e7888e9
4aa668c
83c6db9
5071cb8
c55231a
ea6cd97
4e0bf89
39acdfa
76ec16c
d2425b4
c958c3c
b8343b9
dea96ef
8989bbe
07e16d1
b793329
317c097
fee1141
f7d96b3
39962a7
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 |
---|---|---|
|
@@ -2,7 +2,7 @@ import _ from 'lodash'; | |
import { COPILOT_REQUEST_STATUS } from '../constants'; | ||
|
||
module.exports = function defineCopilotRequest(sequelize, DataTypes) { | ||
const CopliotRequest = sequelize.define('CopilotRequest', { | ||
const CopilotRequest = sequelize.define('CopilotRequest', { | ||
id: { type: DataTypes.BIGINT, primaryKey: true, autoIncrement: true }, | ||
status: { | ||
type: DataTypes.STRING(16), | ||
|
@@ -30,9 +30,10 @@ module.exports = function defineCopilotRequest(sequelize, DataTypes) { | |
indexes: [], | ||
}); | ||
|
||
CopliotRequest.associate = (models) => { | ||
CopliotRequest.hasMany(models.CopilotOpportunity, { as: 'copilotOpportunity', foreignKey: 'copilotRequestId' }); | ||
CopilotRequest.associate = (models) => { | ||
CopilotRequest.hasMany(models.CopilotOpportunity, { as: 'copilotOpportunity', foreignKey: 'copilotRequestId' }); | ||
CopilotRequest.belongsTo(models.Project, { as: 'project', foreignKey: 'projectId' }); | ||
}; | ||
|
||
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. Typo in the return statement: |
||
return CopliotRequest; | ||
return CopilotRequest; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,34 @@ module.exports = [ | |
return next(err); | ||
} | ||
|
||
const sendEmailToAllApplicants = async (copilotRequest, allApplications) => { | ||
|
||
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. Using |
||
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, | ||
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 |
||
work_manager_url: config.get('workManagerUrl'), | ||
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`); | ||
}); | ||
|
||
}; | ||
|
||
return models.sequelize.transaction(async (t) => { | ||
const opportunity = await models.CopilotOpportunity.findOne({ | ||
where: { id: copilotOpportunityId }, | ||
|
@@ -238,6 +266,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 |
||
|
||
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'; | ||
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 the |
||
|
||
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'; | ||
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 |
||
import { PERMISSION } from '../../permissions/constants'; | ||
|
||
|
||
|
@@ -20,6 +22,32 @@ 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, | ||
work_manager_url: config.get('workManagerUrl'), | ||
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 ternary operation |
||
}, | ||
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 +121,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. It seems like the function |
||
|
||
res.status(200).send({ id: opportunity.id }); | ||
}) | ||
|
||
|
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,33 +17,67 @@ module.exports = [ | |
return next(err); | ||
} | ||
|
||
const page = parseInt(req.query.page, 10) || 1; | ||
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. Consider defining |
||
const offset = (page - 1) * pageSize; | ||
|
||
const projectId = _.parseInt(req.params.projectId); | ||
|
||
let sort = req.query.sort ? decodeURIComponent(req.query.sort) : 'createdAt desc'; | ||
if (sort.indexOf(' ') === -1) { | ||
sort += ' asc'; | ||
} | ||
const sortableProps = ['createdAt asc', 'createdAt desc']; | ||
const sortableProps = [ | ||
'createdAt asc', | ||
'createdAt desc', | ||
'projectName asc', | ||
'projectName desc', | ||
'opportunityTitle asc', | ||
'opportunityTitle desc', | ||
'projectType asc', | ||
'projectType desc', | ||
'status asc', | ||
'status desc', | ||
]; | ||
if (_.indexOf(sortableProps, sort) < 0) { | ||
return util.handleError('Invalid sort criteria', null, req, next); | ||
} | ||
const sortParams = sort.split(' '); | ||
let sortParams = sort.split(' '); | ||
let order = [[sortParams[0], sortParams[1]]]; | ||
const relationBasedSortParams = ['projectName']; | ||
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 variable |
||
const jsonBasedSortParams = ['opportunityTitle', 'projectType']; | ||
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 variable |
||
if (relationBasedSortParams.includes(sortParams[0])) { | ||
order = [ | ||
[{model: models.Project, as: 'project'}, 'name', sortParams[1]], | ||
['id', 'DESC'] | ||
] | ||
} | ||
|
||
if (jsonBasedSortParams.includes(sortParams[0])) { | ||
order = [ | ||
[models.sequelize.literal(`("CopilotRequest"."data"->>'${sortParams[0]}')`), sortParams[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. Using |
||
['id', 'DESC'], | ||
] | ||
} | ||
|
||
const whereCondition = projectId ? { projectId } : {}; | ||
|
||
return models.CopilotRequest.findAll({ | ||
return models.CopilotRequest.findAndCountAll({ | ||
where: whereCondition, | ||
include: [ | ||
{ | ||
model: models.CopilotOpportunity, | ||
as: 'copilotOpportunity', | ||
}, | ||
{ model: models.CopilotOpportunity, as: 'copilotOpportunity', required: false }, | ||
{ model: models.Project, as: 'project', required: false }, | ||
], | ||
order: [[sortParams[0], sortParams[1]]], | ||
}) | ||
.then(copilotRequests => res.json(copilotRequests)) | ||
.catch((err) => { | ||
util.handleError('Error fetching copilot requests', err, req, next); | ||
}); | ||
order, | ||
limit: pageSize, | ||
offset, | ||
distinct: true, | ||
subQuery: false, | ||
}).then(({rows: copilotRequests, count}) => util.setPaginationHeaders(req, res, { | ||
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 |
||
count: count, | ||
rows: copilotRequests, | ||
page, | ||
pageSize, | ||
})); | ||
}, | ||
]; |
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 be corrected toCopilotRequest
.