diff --git a/.circleci/config.yml b/.circleci/config.yml index b3f2600c..74e60a37 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -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: diff --git a/src/routes/copilotOpportunity/assign.js b/src/routes/copilotOpportunity/assign.js index 6cab3b55..eabe9288 100644 --- a/src/routes/copilotOpportunity/assign.js +++ b/src/routes/copilotOpportunity/assign.js @@ -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({ @@ -172,51 +173,55 @@ module.exports = [ return; } - 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.TC_COPILOT, + userId, createdBy: req.authUser.userId, - createdAt: new Date(), updatedBy: req.authUser.userId, - updatedAt: new Date(), + }; + req.context = req.context || {}; + req.context.currentProjectMembers = activeMembers; + await util.addUserToProject(req, member, t) + + await application.update({ + status: COPILOT_APPLICATION_STATUS.ACCEPTED, }, { transaction: t, - }) + }); - util.sendResourceToKafkaBus( - req, - EVENT.ROUTING_KEY.PROJECT_MEMBER_INVITE_CREATED, - RESOURCES.PROJECT_MEMBER_INVITE, - Object.assign({}, invite.toJSON(), { - source: 'copilot_portal', - }), - ); + await opportunity.update({ + status: COPILOT_OPPORTUNITY_STATUS.COMPLETED, + }, { + transaction: t, + }); - await application.update({ - status: COPILOT_APPLICATION_STATUS.INVITED, + + await copilotRequest.update({ + status: COPILOT_REQUEST_STATUS.FULFILLED, }, { transaction: t, }); + // Cancel other applications + const otherApplications = await models.CopilotApplication.findAll({ + where: { + opportunityId: copilotOpportunityId, + id: { + [Op.notIn]: [applicationId], + }, + }, + transaction: t, + }); + + for (const otherApplication of otherApplications) { + await otherApplication.update({ + status: COPILOT_APPLICATION_STATUS.CANCELED, + }, { + transaction: t, + }); + } + res.status(200).send({ id: applicationId }); }).catch(err => next(err)); },