-
Notifications
You must be signed in to change notification settings - Fork 56
feat(PM-1173): Notify all copilots on copilot opportunity #815
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
79fdf6f
4b8c9f7
0c54ecf
223b7bd
82dd797
9793dcc
5b29f65
a29f1c9
deaed3a
d4ed7a9
91f23ab
fb9b8a9
31a2b66
ae5952d
c16d06e
190c04c
4c98fe7
0ad22c3
cc5a28d
b52b16e
5fbd197
49356d6
e34f785
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 |
---|---|---|
|
@@ -302,8 +302,15 @@ export const CONNECT_NOTIFICATION_EVENT = { | |
TOPIC_UPDATED: 'connect.notification.project.topic.updated', | ||
POST_CREATED: 'connect.notification.project.post.created', | ||
POST_UPDATED: 'connect.notification.project.post.edited', | ||
|
||
// External action email | ||
EXTERNAL_ACTION_EMAIL: 'external.action.email', | ||
}; | ||
|
||
export const TEMPLATE_IDS = { | ||
APPLY_COPILOT: 'd-d7c1f48628654798a05c8e09e52db14f', | ||
CREATE_REQUEST: 'd-3efdc91da580479d810c7acd50a4c17f', | ||
} | ||
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. Add a semicolon at the end of the object declaration for consistency with the rest of the codebase. |
||
export const REGEX = { | ||
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 | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import _ from 'lodash'; | ||
import validate from 'express-validation'; | ||
import Joi from 'joi'; | ||
import config from 'config'; | ||
|
||
import models from '../../models'; | ||
import util from '../../util'; | ||
import { PERMISSION } from '../../permissions/constants'; | ||
import { COPILOT_OPPORTUNITY_STATUS } from '../../constants'; | ||
import { CONNECT_NOTIFICATION_EVENT, COPILOT_OPPORTUNITY_STATUS, TEMPLATE_IDS, USER_ROLE } from '../../constants'; | ||
import { createEvent } from '../../services/busApi'; | ||
|
||
const applyCopilotRequestValidations = { | ||
body: Joi.object().keys({ | ||
|
@@ -65,7 +67,36 @@ module.exports = [ | |
} | ||
|
||
return models.CopilotApplication.create(data) | ||
.then((result) => { | ||
.then(async (result) => { | ||
const pmRole = await util.getRolesByRoleName(USER_ROLE.PROJECT_MANAGER, req.log, req.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. The change from a string to a constant |
||
const { subjects = [] } = await util.getRoleInfo(pmRole[0], req.log, req.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 adding error handling for the |
||
|
||
const creator = await util.getMemberDetailsByUserIds([opportunity.userId], req.log, req.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. The change from |
||
const listOfSubjects = subjects; | ||
if (creator) { | ||
const isCreatorPartofSubjects = subjects.find(item => item.email.toLowerCase() === creator[0].email.toLowerCase()); | ||
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. Converting emails to lowercase for comparison is a good practice to ensure case-insensitive matching. However, ensure that both |
||
if (!isCreatorPartofSubjects) { | ||
listOfSubjects.push({ | ||
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 |
||
email: creator[0].email, | ||
handle: creator[0].handle, | ||
}); | ||
} | ||
} | ||
|
||
const emailEventType = CONNECT_NOTIFICATION_EVENT.EXTERNAL_ACTION_EMAIL; | ||
const copilotPortalUrl = config.get('copilotPortalUrl'); | ||
listOfSubjects.forEach((subject) => { | ||
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 adding error handling for the |
||
createEvent(emailEventType, { | ||
data: { | ||
user_name: subject.handle, | ||
opportunity_details_url: `${copilotPortalUrl}/opportunity/${opportunity.id}#applications`, | ||
}, | ||
sendgrid_template_id: TEMPLATE_IDS.APPLY_COPILOT, | ||
recipients: [subject.email], | ||
version: 'v3', | ||
}, req.log); | ||
}); | ||
|
||
res.status(201).json(result); | ||
return Promise.resolve(); | ||
}) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,7 @@ module.exports = [ | |
updatedBy: req.authUser.userId, | ||
}); | ||
|
||
return approveRequest(data) | ||
return approveRequest(req, data) | ||
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 |
||
.then(_newCopilotOpportunity => res.status(201).json(_newCopilotOpportunity)) | ||
.catch((err) => { | ||
if (err.message) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
import _ from 'lodash'; | ||
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 adding a check to ensure that the 'config' module is correctly configured and contains the necessary properties before using it. This can help prevent runtime errors if the configuration is missing or incorrect. |
||
|
||
import models from '../../models'; | ||
import { COPILOT_REQUEST_STATUS } from '../../constants'; | ||
import { CONNECT_NOTIFICATION_EVENT, COPILOT_REQUEST_STATUS, TEMPLATE_IDS, USER_ROLE } from '../../constants'; | ||
import util from '../../util'; | ||
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 'createEvent' function from 'busApi' is used correctly later in the code. If not already done, verify that it handles errors and edge cases appropriately. |
||
|
||
const resolveTransaction = (transaction, callback) => { | ||
if (transaction) { | ||
|
@@ -11,7 +14,7 @@ const resolveTransaction = (transaction, callback) => { | |
return models.sequelize.transaction(callback); | ||
}; | ||
|
||
module.exports = (data, existingTransaction) => { | ||
module.exports = (req, data, existingTransaction) => { | ||
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 has been changed to include |
||
const { projectId, copilotRequestId } = data; | ||
|
||
return resolveTransaction(existingTransaction, transaction => | ||
|
@@ -52,6 +55,28 @@ module.exports = (data, existingTransaction) => { | |
return models.CopilotOpportunity | ||
.create(data, { transaction }); | ||
})) | ||
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 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. Is there any reason to use console here instead of the req.log? |
||
.then(async (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. Consider removing the |
||
const roles = await util.getRolesByRoleName(USER_ROLE.TC_COPILOT, req.log, req.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. The change from a string 'copilot' to |
||
const { subjects = [] } = await util.getRoleInfo(roles[0], req.log, req.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. The 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 log message for getting subjects for roles has been removed. If this was intentional, ensure that the removal does not affect the debugging process. If not, consider reinstating it for better traceability. |
||
const emailEventType = CONNECT_NOTIFICATION_EVENT.EXTERNAL_ACTION_EMAIL; | ||
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 change of |
||
const copilotPortalUrl = config.get('copilotPortalUrl'); | ||
req.log.info("Sending emails to all copilots about new opportunity"); | ||
subjects.forEach(subject => { | ||
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 |
||
createEvent(emailEventType, { | ||
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 log statement 'Each copilot members' was removed. If this was intentional, ensure that the logging is still sufficient for debugging purposes. If not, consider adding a relevant log statement to track each copilot being notified. |
||
data: { | ||
user_name: subject.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 key 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 camelCase for consistency with JavaScript naming conventions. Change |
||
opportunity_details_url: `${copilotPortalUrl}/opportunity/${opportunity.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 using camelCase for consistency with JavaScript naming conventions. Change |
||
}, | ||
sendgrid_template_id: TEMPLATE_IDS.CREATE_REQUEST, | ||
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 change to use |
||
recipients: [subject.email], | ||
version: 'v3', | ||
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 version identifier has been changed from a specific UUID to 'v3'. Ensure that 'v3' is the correct and intended version for the sendgrid template. If 'v3' is a placeholder or not a valid version, it may cause issues in the notification process. |
||
}, req.log); | ||
}); | ||
|
||
req.log.info("Finished sending emails to copilots"); | ||
|
||
return opportunity; | ||
}) | ||
.catch((err) => { | ||
transaction.rollback(); | ||
return Promise.reject(err); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,7 +98,7 @@ module.exports = [ | |
updatedBy: req.authUser.userId, | ||
type: copilotRequest.data.projectType, | ||
}); | ||
return approveRequest(approveData, transaction).then(() => copilotRequest); | ||
return approveRequest(req, approveData, transaction).then(() => copilotRequest); | ||
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 |
||
}).then(copilotRequest => res.status(201).json(copilotRequest)) | ||
.catch((err) => { | ||
try { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -815,6 +815,54 @@ const projectServiceUtils = { | |
} | ||
}, | ||
|
||
getRoleInfo: Promise.coroutine(function* (roleId, logger, requestId) { // eslint-disable-line func-names | ||
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 async/await syntax instead of Promise.coroutine for better readability and modern JavaScript practices. |
||
try { | ||
const token = yield this.getM2MToken(); | ||
const httpClient = this.getHttpClient({ id: requestId, log: logger }); | ||
httpClient.defaults.timeout = 6000; | ||
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. Setting a timeout for the HTTP client is a good practice to prevent hanging requests. However, ensure that 6000 milliseconds is an appropriate timeout for all expected network conditions and use cases. Consider making this configurable if different timeouts might be needed in different environments. |
||
logger.debug(`${config.identityServiceEndpoint}roles/${roleId}`, "fetching role info"); | ||
return httpClient.get(`${config.identityServiceEndpoint}roles/${roleId}`, { | ||
params: { | ||
fields: `subjects`, | ||
}, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}).then((res) => { | ||
logger.debug(`Role info by ${roleId}: ${JSON.stringify(res.data.result.content)}`); | ||
return _.get(res, 'data.result.content', []); | ||
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 use of |
||
}); | ||
} catch (err) { | ||
logger.debug(err, "error on getting role info"); | ||
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 error message to provide better context for debugging. For example, include information about the operation that failed or the parameters involved. |
||
return Promise.reject(err); | ||
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. Instead of returning |
||
} | ||
}), | ||
|
||
getRolesByRoleName: Promise.coroutine(function* (roleName, logger, requestId) { // eslint-disable-line func-names | ||
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 async/await instead of Promise.coroutine for better readability and modern syntax. |
||
try { | ||
const token = yield this.getM2MToken(); | ||
const httpClient = this.getHttpClient({ id: requestId, log: logger }); | ||
httpClient.defaults.timeout = 6000; | ||
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. Setting a timeout for the HTTP client is a good practice to prevent hanging requests. However, consider making the timeout value configurable through an environment variable or configuration file instead of hardcoding it. This would allow for easier adjustments in different environments or scenarios. |
||
return httpClient.get(`${config.identityServiceEndpoint}roles`, { | ||
params: { | ||
filter: `roleName=${roleName}`, | ||
}, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}).then((res) => { | ||
logger.debug(`Roles by ${roleName}: ${JSON.stringify(res.data.result.content)}`); | ||
return _.get(res, 'data.result.content', []) | ||
.filter(item => item.roleName === roleName) | ||
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 variable name than |
||
.map(r => r.id); | ||
}); | ||
} catch (err) { | ||
return Promise.reject(err); | ||
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. Instead of catching the error and returning a rejected promise, consider allowing the error to propagate naturally. This will make the function easier to handle with async/await syntax. |
||
} | ||
}), | ||
|
||
/** | ||
* Retrieve member details from userIds | ||
*/ | ||
|
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.
The change from
COPILOT_OPPORTUNITY_CREATED
toEXTERNAL_ACTION_EMAIL
seems to alter the meaning of the event. Ensure that this change aligns with the intended functionality described in the pull request. If the intention is to notify all copilots, verify that the event name accurately reflects this purpose.