Skip to content

fix(PM-1398): cancel invites on canceling the copilot opportunity #844

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 1 commit into from
Aug 1, 2025
Merged
Changes from all 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
27 changes: 26 additions & 1 deletion src/routes/copilotOpportunity/delete.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import _ from 'lodash';
import { Op } from 'sequelize';

import models from '../../models';
import util from '../../util';
import { COPILOT_APPLICATION_STATUS, COPILOT_OPPORTUNITY_STATUS, COPILOT_REQUEST_STATUS } from '../../constants';
import { COPILOT_APPLICATION_STATUS, COPILOT_OPPORTUNITY_STATUS, COPILOT_REQUEST_STATUS, EVENT, INVITE_STATUS, RESOURCES } from '../../constants';
import { PERMISSION } from '../../permissions/constants';


module.exports = [
(req, res, next) => {
if (!util.hasPermissionByReq(PERMISSION.CANCEL_COPILOT_OPPORTUNITY, req)) {
Expand Down Expand Up @@ -54,6 +56,14 @@ module.exports = [
}));
});

const allInvites = await models.ProjectMemberInvite.findAll({
Copy link

Choose a reason for hiding this comment

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

Consider adding error handling for the findAll operation to ensure that any issues with retrieving invites do not cause the entire operation to fail.

where: {
applicationId: {
[Op.in]: applications.map(item => item.id),
Copy link

Choose a reason for hiding this comment

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

Ensure that applications.map(item => item.id) returns a valid array of IDs. If applications is empty or undefined, this could lead to unexpected behavior. Consider adding a check or default value.

},
},
});

await Promise.all(promises);

await copilotRequest.update({
Expand All @@ -68,6 +78,21 @@ module.exports = [
transaction,
});

// update all the existing invites which are
// associated to the copilot opportunity
// with cancel status
for (const invite of allInvites) {
await invite.update({
Copy link

Choose a reason for hiding this comment

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

Consider adding error handling for the invite.update operation to ensure that any issues during the update process are caught and managed appropriately.

status: INVITE_STATUS.CANCELED,
});
await invite.reload();
Copy link

Choose a reason for hiding this comment

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

The invite.reload() operation might not be necessary if the invite.update operation already returns the updated instance. Consider checking if this step can be optimized or removed.

util.sendResourceToKafkaBus(
Copy link

Choose a reason for hiding this comment

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

Consider adding error handling for the util.sendResourceToKafkaBus function to ensure that any issues with sending the message to the Kafka bus are caught and managed appropriately.

req,
EVENT.ROUTING_KEY.PROJECT_MEMBER_INVITE_UPDATED,
RESOURCES.PROJECT_MEMBER_INVITE,
invite.toJSON());
}

res.status(200).send({ id: opportunity.id });
})

Expand Down