Skip to content

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

Merged
merged 6 commits into from
Jun 19, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ workflows:
context : org-global
filters:
branches:
only: ['develop', 'migration-setup', 'pm-1273']
only: ['develop', 'migration-setup', 'pm-1378']
- deployProd:
context : org-global
filters:
Expand Down
13 changes: 13 additions & 0 deletions src/permissions/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,19 @@ export const PERMISSION = { // eslint-disable-line import/prefer-default-export
],
scopes: SCOPES_PROJECTS_WRITE,
},

CANCEL_COPILOT_OPPORTUNITY: {
meta: {
title: 'Cancel copilot opportunity',
group: 'Cancel copilot opportunity',
description: 'Who can cancel copilot opportunity.',
},
topcoderRoles: [
USER_ROLE.PROJECT_MANAGER,
USER_ROLE.TOPCODER_ADMIN,
],
scopes: SCOPES_PROJECTS_WRITE,
},

LIST_COPILOT_OPPORTUNITY: {
meta: {
Expand Down
76 changes: 76 additions & 0 deletions src/routes/copilotOpportunity/delete.js
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);

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 that opportunityId is always a valid integer before logging to avoid potential issues with malformed input.

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({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The model name CopilotApplications was changed to CopilotApplication. Ensure that this change is consistent across the entire codebase to prevent any potential issues with model references.

where: {
opportunityId: opportunity.id,
},
transaction,
});

applications.update({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The applications.update method is being called on an array of applications, which will not work as expected. You should iterate over each application and call the update method individually.

status: COPILOT_APPLICATION_STATUS.CANCELED,
}, {
transaction,
});

copilotRequest.update({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The copilotRequest.update method should be awaited to ensure the update operation completes before proceeding.

status: COPILOT_REQUEST_STATUS.CANCELED,
}, {
transaction,
});

opportunity.update({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The opportunity.update method should be awaited to ensure the update operation completes before proceeding.

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);
});
},
];
1 change: 0 additions & 1 deletion src/routes/copilotOpportunity/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ module.exports = [
})
.then((copilotOpportunity) => {
const plainOpportunity = copilotOpportunity.get({ plain: true });
req.log.info("authUser", req.authUser);
const memberIds = plainOpportunity.project.members && plainOpportunity.project.members.map((member) => member.userId);
let canApplyAsCopilot = false;
if (req.authUser) {
Expand Down
4 changes: 4 additions & 0 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file being required here is named delete, which might be misleading since the route is for canceling an opportunity. Consider renaming the file to something more descriptive, like cancel, to better reflect its purpose.


// Project Estimation Items
router.route('/v5/projects/:projectId(\\d+)/estimations/:estimationId(\\d+)/items')
.get(require('./projectEstimationItems/list'));
Expand Down