From b1d7b407d7b47271e7f85c2323137411b27cfb2c Mon Sep 17 00:00:00 2001 From: Hentry Martin Date: Fri, 1 Aug 2025 17:20:08 +0200 Subject: [PATCH] cancel invites on canceling the copilot opportunity --- src/routes/copilotOpportunity/delete.js | 27 ++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/routes/copilotOpportunity/delete.js b/src/routes/copilotOpportunity/delete.js index 3c6d9bfa..5336807f 100644 --- a/src/routes/copilotOpportunity/delete.js +++ b/src/routes/copilotOpportunity/delete.js @@ -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)) { @@ -54,6 +56,14 @@ module.exports = [ })); }); + const allInvites = await models.ProjectMemberInvite.findAll({ + where: { + applicationId: { + [Op.in]: applications.map(item => item.id), + }, + }, + }); + await Promise.all(promises); await copilotRequest.update({ @@ -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({ + status: INVITE_STATUS.CANCELED, + }); + await invite.reload(); + util.sendResourceToKafkaBus( + req, + EVENT.ROUTING_KEY.PROJECT_MEMBER_INVITE_UPDATED, + RESOURCES.PROJECT_MEMBER_INVITE, + invite.toJSON()); + } + res.status(200).send({ id: opportunity.id }); })