-
Notifications
You must be signed in to change notification settings - Fork 55
fix(PM-1510): added existing membership to the copilot application #840
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 10 commits
683b3d6
f0a889f
4823330
26dc288
59c6077
2ba33ac
04b4e37
f2a8c89
76a22c1
cf120a8
471d5e1
1559e31
44ac842
de8c14f
40076d3
d40373d
8b2fa3e
e7a328b
ab268ca
e82e6b2
dad92bc
ada10e8
c2df9ee
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 |
|---|---|---|
|
|
@@ -45,11 +45,17 @@ module.exports = [ | |
| throw err; | ||
| } | ||
|
|
||
| const copilotRequest = await models.CopilotRequest.findOne({ | ||
| where: { id: opportunity.copilotRequestId }, | ||
| transaction: t, | ||
| }); | ||
|
|
||
| const application = await models.CopilotApplication.findOne({ | ||
| where: { id: applicationId, opportunityId: copilotOpportunityId }, | ||
| transaction: t, | ||
| }); | ||
|
|
||
|
|
||
|
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. Remove the extra blank line here to maintain consistent formatting and readability. |
||
| if (!application) { | ||
| const err = new Error('No such application available'); | ||
| err.status = 400; | ||
|
|
@@ -65,12 +71,72 @@ module.exports = [ | |
| const projectId = opportunity.projectId; | ||
| const userId = application.userId; | ||
| const activeMembers = await models.ProjectMember.getActiveProjectMembers(projectId, t); | ||
|
|
||
| const existingUser = activeMembers.find(item => item.userId === userId); | ||
| if (existingUser && existingUser.role === 'copilot') { | ||
| const err = new Error(`User is already a copilot of this project`); | ||
| err.status = 400; | ||
| throw err; | ||
| const updateCopilotOpportunity = async () => { | ||
| await opportunity.update({ | ||
| status: COPILOT_OPPORTUNITY_STATUS.COMPLETED, | ||
| }, { | ||
| transaction: t, | ||
| }); | ||
| await application.update({ | ||
| status: COPILOT_APPLICATION_STATUS.ACCEPTED, | ||
| }, { | ||
| transaction: t, | ||
| }); | ||
|
|
||
| await copilotRequest.update({ | ||
| status: COPILOT_REQUEST_STATUS.FULFILLED, | ||
| }, { | ||
| transaction: t, | ||
| }); | ||
|
|
||
| await models.CopilotApplication.update({ | ||
| status: COPILOT_APPLICATION_STATUS.CANCELED, | ||
| }, { | ||
| where: { | ||
| projectId, | ||
| opportunityId: 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. The removal of |
||
| id: { | ||
| $ne: application.id, | ||
| }, | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| const existingMember = activeMembers.find(item => item.userId === userId); | ||
| if (existingMember) { | ||
|
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 logic for checking |
||
| req.log.debug(`User already part of project: ${JSON.stringify(existingMember)}`); | ||
|
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 |
||
| if (['copilot', 'manager'].includes(existingMember.role)) { | ||
| req.log.debug(`User is a copilot or manager`); | ||
|
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 debug log message |
||
| updateCopilotOpportunity(); | ||
|
||
| } else { | ||
| req.log.debug(`User has read/write role`); | ||
|
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 debug log message |
||
| await models.ProjectMember.update({ | ||
|
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 |
||
| role: 'copilot', | ||
| }, { | ||
| where: { | ||
| id: existingMember.id, | ||
| }, | ||
| }); | ||
|
|
||
| const projectMember = await models.ProjectMember.findOne({ | ||
| where: { | ||
| id: existingMember.id, | ||
| }, | ||
| }); | ||
|
|
||
| req.log.debug(`Updated project member: ${JSON.stringify(projectMember.get({plain: true}))}`); | ||
|
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 that might occur during the |
||
|
|
||
| util.sendResourceToKafkaBus( | ||
| req, | ||
| EVENT.ROUTING_KEY.PROJECT_MEMBER_UPDATED, | ||
| RESOURCES.PROJECT_MEMBER, | ||
| projectMember.get({ plain: true }), | ||
| existingMember); | ||
| req.log.debug(`Member updated in kafka`); | ||
|
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 |
||
| updateCopilotOpportunity(); | ||
|
||
| } | ||
| res.status(200).send({ id: applicationId }); | ||
| return; | ||
| } | ||
|
|
||
| const existingInvite = await models.ProjectMemberInvite.findAll({ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,19 +31,48 @@ module.exports = [ | |
| canAccessAllApplications ? {} : { createdBy: userId }, | ||
| ); | ||
|
|
||
|
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 |
||
| return models.CopilotApplication.findAll({ | ||
| where: whereCondition, | ||
| include: [ | ||
| { | ||
| model: models.CopilotOpportunity, | ||
| as: 'copilotOpportunity', | ||
| }, | ||
| ], | ||
| order: [[sortParams[0], sortParams[1]]], | ||
| return models.CopilotOpportunity.findOne({ | ||
| where: { | ||
| id: opportunityId, | ||
| } | ||
| }).then((opportunity) => { | ||
| if (!opportunity) { | ||
| const err = new Error('No opportunity found'); | ||
| err.status = 404; | ||
| throw err; | ||
| } | ||
| return models.CopilotApplication.findAll({ | ||
| where: whereCondition, | ||
| include: [ | ||
| { | ||
| model: models.CopilotOpportunity, | ||
| as: 'copilotOpportunity', | ||
| }, | ||
| ], | ||
| order: [[sortParams[0], sortParams[1]]], | ||
| }) | ||
| .then(copilotApplications => { | ||
| req.log.debug(`CopilotApplications ${JSON.stringify(copilotApplications)}`); | ||
|
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 clarify what |
||
| return models.ProjectMember.getActiveProjectMembers(opportunity.projectId).then((members) => { | ||
| req.log.debug(`Fetched existing active members ${JSON.stringify(members)}`); | ||
| req.log.debug(`Applications ${JSON.stringify(copilotApplications)}`); | ||
|
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 |
||
| const enrichedApplications = copilotApplications.map(application => { | ||
|
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 |
||
| const m = members.find(m => m.userId === application.userId); | ||
|
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 debug log statement was removed but the comment remains. Consider removing the comment as it is not necessary to keep commented-out code. |
||
| req.log.debug(`Existing member to application ${JSON.stringify(Object.assign({}, application, { | ||
|
||
| existingMembership: m, | ||
| }))}`); | ||
| return Object.assign({}, application, { | ||
| existingMembership: m, | ||
| }); | ||
| }); | ||
|
|
||
| req.log.debug(`Enriched Applications ${JSON.stringify(enrichedApplications)}`); | ||
|
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 debug log message could be more informative by including the number of enriched applications. Consider adding |
||
| res.status(200).send(enrichedApplications); | ||
|
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 response method has been changed from |
||
| }); | ||
| }) | ||
| }) | ||
| .then(copilotApplications => res.json(copilotApplications)) | ||
| .catch((err) => { | ||
| util.handleError('Error fetching copilot applications', err, req, next); | ||
| }); | ||
| .catch((err) => { | ||
| util.handleError('Error fetching copilot applications', err, req, next); | ||
| }); | ||
| }, | ||
| ]; | ||
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 checking if
copilotRequestis null or undefined after fetching it from the database. This will help prevent potential runtime errors if the request is not found.