-
Notifications
You must be signed in to change notification settings - Fork 55
feat(PM-1610): Directly add copilot to project #845
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
Changes from 2 commits
e54354f
fc14edf
aa81874
b120f6c
6b9eed9
e208bc1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,41 @@ 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.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({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The status update from |
||
status: COPILOT_APPLICATION_STATUS.INVITED, | ||
status: COPILOT_APPLICATION_STATUS.ACCEPTED, | ||
}, { | ||
transaction: t, | ||
}); | ||
|
||
// Cancel other applications | ||
const otherApplications = await models.CopilotApplication.findAll({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding error handling for the |
||
where: { | ||
opportunityId: copilotOpportunityId, | ||
id: { | ||
[Op.notIn]: [applicationId], | ||
}, | ||
}, | ||
transaction: t, | ||
}); | ||
|
||
for (const otherApplication of otherApplications) { | ||
await otherApplication.update({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The loop updating the status of other applications to |
||
status: COPILOT_APPLICATION_STATUS.CANCELED, | ||
}, { | ||
transaction: t, | ||
}); | ||
} | ||
|
||
res.status(200).send({ id: applicationId }); | ||
}).catch(err => next(err)); | ||
}, | ||
|
There was a problem hiding this comment.
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.