-
Notifications
You must be signed in to change notification settings - Fork 56
feat(PM-1379): cancel copilot opportunity #825
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
8d2840c
70422b7
c3390fc
66a6ffa
a09a791
852c89a
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import _ from 'lodash'; | ||
|
||
import models from '../../models'; | ||
import util from '../../util'; | ||
import { COPILOT_APPLICATION_STATUS, COPILOT_OPPORTUNITY_STATUS, COPILOT_REQUEST_STATUS } from '../../constants'; | ||
import { PERMISSION } from '../../permissions/constants'; | ||
|
||
module.exports = [ | ||
(req, res, next) => { | ||
if (!util.hasPermissionByReq(PERMISSION.CANCEL_COPILOT_OPPORTUNITY, req)) { | ||
const err = new Error('Unable to cancel copilot opportunity'); | ||
_.assign(err, { | ||
details: JSON.stringify({ message: 'You do not have permission to cancel copilot opportunity' }), | ||
status: 403, | ||
}); | ||
return Promise.reject(err); | ||
} | ||
// default values | ||
const opportunityId = _.parseInt(req.params.id); | ||
|
||
return models.sequelize.transaction(async (transaction) => { | ||
req.log.debug('Canceling Copilot opportunity transaction', opportunityId); | ||
const opportunity = await models.CopilotOpportunity.findOne({ | ||
where: { id: opportunityId }, | ||
transaction, | ||
}); | ||
|
||
if (!opportunity) { | ||
const err = new Error(`No opportunity available for id ${opportunityId}`); | ||
err.status = 404; | ||
throw err; | ||
} | ||
|
||
const copilotRequest = await models.CopilotRequest.findOne({ | ||
where: { | ||
id: opportunity.copilotRequestId, | ||
}, | ||
transaction, | ||
}); | ||
|
||
const applications = await models.CopilotApplication.findAll({ | ||
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 model name |
||
where: { | ||
opportunityId: opportunity.id, | ||
}, | ||
transaction, | ||
}); | ||
|
||
applications.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 |
||
status: COPILOT_APPLICATION_STATUS.CANCELED, | ||
}, { | ||
transaction, | ||
}); | ||
|
||
copilotRequest.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 |
||
status: COPILOT_REQUEST_STATUS.CANCELED, | ||
}, { | ||
transaction, | ||
}); | ||
|
||
opportunity.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 |
||
status: COPILOT_OPPORTUNITY_STATUS.CANCELED, | ||
}, { | ||
transaction, | ||
}); | ||
|
||
res.status(200).send({ id: opportunity.id }); | ||
}) | ||
|
||
.catch((err) => { | ||
if (err.message) { | ||
_.assign(err, { details: err.message }); | ||
} | ||
util.handleError('Error canceling copilot opportunity', err, req, next); | ||
}); | ||
}, | ||
]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -419,6 +419,10 @@ router.route('/v5/projects/copilots/opportunity/:id(\\d+)/applications') | |
router.route('/v5/projects/copilots/opportunity/:id(\\d+)/assign') | ||
.post(require('./copilotOpportunity/assign')); | ||
|
||
// Cancel Copilot opportunity | ||
router.route('/v5/projects/copilots/opportunity/:id(\\d+)/cancel') | ||
.delete(require('./copilotOpportunity/delete')); | ||
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 file being required here is named |
||
|
||
// Project Estimation Items | ||
router.route('/v5/projects/:projectId(\\d+)/estimations/:estimationId(\\d+)/items') | ||
.get(require('./projectEstimationItems/list')); | ||
|
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 log message has been updated to include
opportunityId
, which is a good improvement for debugging. However, ensure thatopportunityId
is always a valid integer before logging to avoid potential issues with malformed input.