Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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-1398']
only: ['develop', 'migration-setup', 'pm-1610']
- deployProd:
context : org-global
filters:
Expand Down
63 changes: 27 additions & 36 deletions src/routes/copilotOpportunity/assign.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import config from 'config';
import models from '../../models';
import util from '../../util';
import { PERMISSION } from '../../permissions/constants';
import { CONNECT_NOTIFICATION_EVENT, COPILOT_APPLICATION_STATUS, COPILOT_OPPORTUNITY_STATUS, COPILOT_REQUEST_STATUS, EVENT, INVITE_STATUS, PROJECT_MEMBER_ROLE, RESOURCES, TEMPLATE_IDS } from '../../constants';
import { CONNECT_NOTIFICATION_EVENT, COPILOT_APPLICATION_STATUS, COPILOT_OPPORTUNITY_STATUS, COPILOT_REQUEST_STATUS, EVENT, INVITE_STATUS, PROJECT_MEMBER_ROLE, RESOURCES, TEMPLATE_IDS, USER_ROLE } from '../../constants';
import { getCopilotTypeLabel } from '../../utils/copilot';
import { createEvent } from '../../services/busApi';
import moment from 'moment';
import { Op } from 'sequelize';

const assignCopilotOpportunityValidations = {
body: Joi.object().keys({
Expand Down Expand Up @@ -172,51 +173,41 @@ module.exports = [
return;
}

Copy link

Choose a reason for hiding this comment

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

The removal of the check for existing pending invites may lead to duplicate invites being created. Consider implementing a mechanism to prevent adding a user as a copilot if they already have a pending invite.

const existingInvite = await models.ProjectMemberInvite.findAll({
where: {
userId,
projectId,
role: PROJECT_MEMBER_ROLE.COPILOT,
status: INVITE_STATUS.PENDING,
},
transaction: t,
});

if (existingInvite && existingInvite.length) {
const err = new Error(`User already has an pending invite to the project`);
err.status = 400;
throw err;
}

const invite = await models.ProjectMemberInvite.create({
status: INVITE_STATUS.PENDING,
role: PROJECT_MEMBER_ROLE.COPILOT,
userId,
const member = {
projectId,
applicationId: application.id,
role: USER_ROLE.COPILOT,
userId,
createdBy: req.authUser.userId,
createdAt: new Date(),
updatedBy: req.authUser.userId,
updatedAt: new Date(),
}, {
transaction: t,
})
};

util.sendResourceToKafkaBus(
req,
EVENT.ROUTING_KEY.PROJECT_MEMBER_INVITE_CREATED,
RESOURCES.PROJECT_MEMBER_INVITE,
Object.assign({}, invite.toJSON(), {
source: 'copilot_portal',
}),
);
await util.addUserToProject(req, member, t)

await application.update({
Copy link

Choose a reason for hiding this comment

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

The status update from COPILOT_APPLICATION_STATUS.INVITED to COPILOT_APPLICATION_STATUS.ACCEPTED should be verified to ensure it aligns with the intended business logic, as this change may have implications on the application workflow.

status: COPILOT_APPLICATION_STATUS.INVITED,
status: COPILOT_APPLICATION_STATUS.ACCEPTED,
}, {
transaction: t,
});

// Cancel other applications
const otherApplications = await models.CopilotApplication.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 query to manage potential database access issues or unexpected results when fetching other applications.

where: {
opportunityId: copilotOpportunityId,
id: {
[Op.notIn]: [applicationId],
},
},
transaction: t,
});

for (const otherApplication of otherApplications) {
await otherApplication.update({
Copy link

Choose a reason for hiding this comment

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

The loop updating the status of other applications to CANCELED should include error handling to manage potential failures during the update process, ensuring that the transaction can be rolled back if necessary.

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

res.status(200).send({ id: applicationId });
}).catch(err => next(err));
},
Expand Down