-
Notifications
You must be signed in to change notification settings - Fork 56
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 10 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 |
---|---|---|
@@ -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({ | ||
|
@@ -73,20 +75,80 @@ 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, | ||
}, | ||
}); | ||
|
||
const existingInvite = await models.ProjectMemberInvite.findAll({ | ||
where: { | ||
userId, | ||
projectId, | ||
role: PROJECT_MEMBER_ROLE.COPILOT, | ||
status: INVITE_STATUS.PENDING, | ||
}, | ||
}); | ||
|
||
if (existingInvite) { | ||
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 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, | ||
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
|
||
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 opportunity.update( | ||
{ status: COPILOT_OPPORTUNITY_STATUS.COMPLETED }, | ||
{ transaction: t }, | ||
); | ||
const authUserDetails = await util.getMemberDetailsByUserIds([req.authUser.userId], req.log, req.id); | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
await models.CopilotApplication.update( | ||
{ status: COPILOT_APPLICATION_STATUS.ACCEPTED }, | ||
{ where: { id: applicationId }, transaction: t }, | ||
); | ||
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); | ||
|
||
res.status(200).send({ id: applicationId }); | ||
}).catch(err => next(err)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,7 +119,9 @@ module.exports = [ | |
}; | ||
return util | ||
.addUserToProject(req, member) | ||
.then(() => res.json(util.postProcessInvites('$.email', updatedInvite, req))) | ||
.then(async () => { | ||
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 use of |
||
return res.json(util.postProcessInvites('$.email', updatedInvite, req)) | ||
}) | ||
.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.
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.