-
Notifications
You must be signed in to change notification settings - Fork 55
fix(PM-1168): invite user when assigning as copilot #809
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 all commits
69eb97a
675858b
613defe
e3303fe
d83097b
68fdf7b
7383c70
aaea12c
35d27e0
1102fdc
fe085cf
42485c5
2d20d16
3ec8ebd
364e74b
d691100
3a4fc53
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module.exports = { | ||
up: async (queryInterface, Sequelize) => { | ||
await queryInterface.addColumn('project_member_invites', 'applicationId', { | ||
type: Sequelize.BIGINT, | ||
allowNull: true, | ||
references: { | ||
model: 'copilot_applications', | ||
key: 'id', | ||
}, | ||
onUpdate: 'CASCADE', | ||
onDelete: 'SET NULL', | ||
}); | ||
}, | ||
|
||
down: async (queryInterface) => { | ||
await queryInterface.removeColumn('project_member_invites', 'applicationId'); | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,11 +44,12 @@ module.exports = function defineProjectMember(sequelize, DataTypes) { | |
}) | ||
.then(res => _.without(_.map(res, 'projectId'), null)); | ||
|
||
ProjectMember.getActiveProjectMembers = projectId => ProjectMember.findAll({ | ||
ProjectMember.getActiveProjectMembers = (projectId, t) => ProjectMember.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 a default value for the transaction parameter |
||
where: { | ||
deletedAt: { $eq: null }, | ||
projectId, | ||
}, | ||
transaction: t, | ||
raw: true, | ||
}); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,16 @@ module.exports = function defineProjectMemberInvite(sequelize, DataTypes) { | |
isEmail: true, | ||
}, | ||
}, | ||
applicationId: { | ||
type: DataTypes.BIGINT, | ||
allowNull: true, | ||
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 a validation to ensure |
||
references: { | ||
model: 'copilot_applications', | ||
key: 'id', | ||
}, | ||
onUpdate: 'CASCADE', | ||
onDelete: 'CASCADE', | ||
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. Ensure that the |
||
}, | ||
role: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import _ from 'lodash'; | ||
import validate from 'express-validation'; | ||
import Joi from 'joi'; | ||
import config from 'config'; | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import models from '../../models'; | ||
import util from '../../util'; | ||
import { PERMISSION } from '../../permissions/constants'; | ||
import { COPILOT_APPLICATION_STATUS, COPILOT_OPPORTUNITY_STATUS, COPILOT_REQUEST_STATUS } from '../../constants'; | ||
import { createEvent } from '../../services/busApi'; | ||
import { CONNECT_NOTIFICATION_EVENT, COPILOT_APPLICATION_STATUS, COPILOT_OPPORTUNITY_STATUS, COPILOT_REQUEST_STATUS, EVENT, INVITE_STATUS, PROJECT_MEMBER_ROLE, RESOURCES } from '../../constants'; | ||
|
||
const assignCopilotOpportunityValidations = { | ||
body: Joi.object().keys({ | ||
|
@@ -64,7 +66,7 @@ module.exports = [ | |
|
||
const projectId = opportunity.projectId; | ||
const userId = application.userId; | ||
const activeMembers = await models.ProjectMember.getActiveProjectMembers(projectId); | ||
const activeMembers = await models.ProjectMember.getActiveProjectMembers(projectId, t); | ||
|
||
const existingUser = activeMembers.find(item => item.userId === userId); | ||
if (existingUser && existingUser.role === 'copilot') { | ||
|
@@ -73,20 +75,89 @@ module.exports = [ | |
throw err; | ||
} | ||
|
||
await models.CopilotRequest.update( | ||
{ status: COPILOT_REQUEST_STATUS.FULFILLED }, | ||
{ where: { id: opportunity.copilotRequestId }, transaction: t }, | ||
); | ||
const project = await models.Project.findOne({ | ||
where: { | ||
id: projectId, | ||
}, | ||
transaction: t, | ||
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. Ensure that the transaction |
||
}); | ||
|
||
const existingInvite = await models.ProjectMemberInvite.findAll({ | ||
where: { | ||
userId, | ||
projectId, | ||
role: PROJECT_MEMBER_ROLE.COPILOT, | ||
status: INVITE_STATUS.PENDING, | ||
}, | ||
transaction: t, | ||
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 addition of |
||
}); | ||
|
||
if (existingInvite && existingInvite.length) { | ||
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 condition |
||
const err = new Error(`User already has an pending invite to the project`); | ||
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 error message contains a grammatical error. It should be 'a pending invite' instead of 'an pending invite'. |
||
err.status = 400; | ||
throw err; | ||
} | ||
|
||
const applicationUser = await util.getMemberDetailsByUserIds([userId], req.log, req.id); | ||
|
||
const invite = await models.ProjectMemberInvite.create({ | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
status: INVITE_STATUS.PENDING, | ||
role: PROJECT_MEMBER_ROLE.COPILOT, | ||
userId, | ||
projectId, | ||
applicationId: application.id, | ||
email: applicationUser[0].email, | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
createdBy: req.authUser.userId, | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
createdAt: new Date(), | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
updatedBy: req.authUser.userId, | ||
updatedAt: new Date(), | ||
}, { | ||
transaction: t, | ||
}) | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
await opportunity.update( | ||
{ status: COPILOT_OPPORTUNITY_STATUS.COMPLETED }, | ||
{ transaction: t }, | ||
); | ||
util.sendResourceToKafkaBus( | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
req, | ||
EVENT.ROUTING_KEY.PROJECT_MEMBER_INVITE_CREATED, | ||
RESOURCES.PROJECT_MEMBER_INVITE, | ||
invite.toJSON()); | ||
|
||
await models.CopilotApplication.update( | ||
{ status: COPILOT_APPLICATION_STATUS.ACCEPTED }, | ||
{ where: { id: applicationId }, transaction: t }, | ||
); | ||
const authUserDetails = await util.getMemberDetailsByUserIds([req.authUser.userId], req.log, req.id); | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const emailEventType = CONNECT_NOTIFICATION_EVENT.PROJECT_MEMBER_EMAIL_INVITE_CREATED; | ||
await createEvent(emailEventType, { | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
data: { | ||
workManagerUrl: config.get('workManagerUrl'), | ||
accountsAppURL: config.get('accountsAppUrl'), | ||
subject: config.get('inviteEmailSubject'), | ||
projects: [{ | ||
name: project.name, | ||
projectId, | ||
sections: [ | ||
{ | ||
EMAIL_INVITES: true, | ||
title: config.get('inviteEmailSectionTitle'), | ||
projectName: project.name, | ||
projectId, | ||
initiator: authUserDetails[0], | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
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 removal of the code block that updates the status of |
||
isSSO: util.isSSO(project), | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
], | ||
}], | ||
}, | ||
recipients: [applicationUser[0].email], | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
version: 'v3', | ||
from: { | ||
name: config.get('EMAIL_INVITE_FROM_NAME'), | ||
email: config.get('EMAIL_INVITE_FROM_EMAIL'), | ||
}, | ||
categories: [`${process.env.NODE_ENV}:${emailEventType}`.toLowerCase()], | ||
}, req.log); | ||
|
||
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. Consider checking if the |
||
status: COPILOT_APPLICATION_STATUS.INVITED, | ||
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. Ensure that |
||
}, { | ||
transaction: t, | ||
}); | ||
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 handling the case where the transaction |
||
|
||
res.status(200).send({ id: applicationId }); | ||
}).catch(err => next(err)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ import Joi from 'joi'; | |
import { middleware as tcMiddleware } from 'tc-core-library-js'; | ||
import models from '../../models'; | ||
import util from '../../util'; | ||
import { INVITE_STATUS, EVENT, RESOURCES } from '../../constants'; | ||
import { INVITE_STATUS, EVENT, RESOURCES, COPILOT_APPLICATION_STATUS, COPILOT_OPPORTUNITY_STATUS, COPILOT_REQUEST_STATUS } from '../../constants'; | ||
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. It seems like new constants |
||
import { PERMISSION } from '../../permissions/constants'; | ||
|
||
/** | ||
|
@@ -94,7 +94,7 @@ module.exports = [ | |
if (updatedInvite.status === INVITE_STATUS.ACCEPTED || | ||
updatedInvite.status === INVITE_STATUS.REQUEST_APPROVED) { | ||
return models.ProjectMember.getActiveProjectMembers(projectId) | ||
.then((members) => { | ||
.then(async (members) => { | ||
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. Using |
||
req.context = req.context || {}; | ||
req.context.currentProjectMembers = members; | ||
let userId = updatedInvite.userId; | ||
|
@@ -117,10 +117,54 @@ module.exports = [ | |
createdBy: req.authUser.userId, | ||
updatedBy: req.authUser.userId, | ||
}; | ||
return util | ||
.addUserToProject(req, member) | ||
.then(() => res.json(util.postProcessInvites('$.email', updatedInvite, req))) | ||
.catch(err => next(err)); | ||
const t = await models.sequelize.transaction(); | ||
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 |
||
try { | ||
await util.addUserToProject(req, member, t); | ||
if (invite.applicationId) { | ||
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 |
||
const application = await models.CopilotApplication.findOne({ | ||
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 |
||
where: { | ||
id: invite.applicationId, | ||
}, | ||
transaction: t, | ||
}); | ||
|
||
await application.update({ status: COPILOT_APPLICATION_STATUS.ACCEPTED }, { | ||
transaction: t | ||
}); | ||
|
||
const opportunity = await models.CopilotOpportunity.findOne({ | ||
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. Similar to the previous comment, ensure that the |
||
where: { | ||
id: application.opportunityId, | ||
}, | ||
transaction: t, | ||
}); | ||
|
||
await opportunity.update({ | ||
status: COPILOT_OPPORTUNITY_STATUS.COMPLETED | ||
}, { | ||
transaction: t, | ||
}); | ||
|
||
const request = await models.CopilotRequest.findOne({ | ||
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. Ensure that the |
||
where: { | ||
id: opportunity.copilotRequestId, | ||
}, | ||
transaction: t, | ||
}); | ||
|
||
await request.update({ | ||
status: COPILOT_REQUEST_STATUS.FULFILLED | ||
}, { | ||
transaction: t, | ||
}); | ||
} | ||
|
||
await t.commit(); | ||
return res.json(util.postProcessInvites('$.email', updatedInvite, req)); | ||
} catch (e) { | ||
await t.rollback(); | ||
return next(e); | ||
} | ||
}); | ||
} | ||
return res.json(util.postProcessInvites('$.email', updatedInvite, req)); | ||
|
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.
Consider specifying an index on the
applicationId
column for improved query performance, especially if this column will be frequently used in WHERE clauses or joins.