Skip to content

Commit d38c9d1

Browse files
committed
ignore duplicate member invites; fix invite remove error
1 parent 95595e1 commit d38c9d1

File tree

5 files changed

+14
-47
lines changed

5 files changed

+14
-47
lines changed

src/events/projectMembers/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ const projectMemberAddedHandler = Promise.coroutine(function* a(logger, msg, cha
8888
members.push(payload);
8989
// now merge the updated changes and reindex the document for invites
9090
const invites = _.isArray(doc._source.invites) ? doc._source.invites : []; // eslint-disable-line no-underscore-dangle
91-
_.remove(invites, invite => invite === payload.email || invite === payload.userId);
91+
// removing any invites for the member just added to the team
92+
_.remove(invites, invite => invite.email === payload.email || invite.userId === payload.userId);
9293
return _.merge(doc._source, { members, invites }); // eslint-disable-line no-underscore-dangle
9394
});
9495
yield Promise.all([directUpdatePromise(), updateESPromise(logger, origRequestId, projectId, updateDocPromise)]);

src/routes/projectMemberInvites/create.js

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ module.exports = [
4949

5050
const promises = [];
5151
if (invite.userIds) {
52+
// remove members already in the team
53+
_.remove(invite.userIds, u => _.some(members, m => m.userId === u));
5254
// permission:
5355
// user has to have constants.MANAGER_ROLES role
5456
// to be invited as PROJECT_MEMBER_ROLE.MANAGER
@@ -58,19 +60,6 @@ module.exports = [
5860
promises.push(util.getUserRoles(userId, req.log, req.id));
5961
});
6062
}
61-
62-
// validate each userId is not already a member
63-
const alreadyMembers = [];
64-
_.forEach(members, (member) => {
65-
if (invite.userIds.includes(member.userId)) {
66-
alreadyMembers.push(member.userId);
67-
}
68-
});
69-
if (alreadyMembers.length > 0) {
70-
const err = new Error(`${alreadyMembers.join()} are already members of project ${projectId}`);
71-
err.status = 400;
72-
return next(err);
73-
}
7463
}
7564

7665
if (invite.emails) {
@@ -104,24 +93,6 @@ module.exports = [
10493
}
10594
return models.ProjectMemberInvite.getPendingInvitesForProject(projectId)
10695
.then((invites) => {
107-
req.log.debug('Chekcing if user has been invited');
108-
// validate for each userId/email there is no existing invitation
109-
const alreadyInvites = [];
110-
_.forEach(invites, (i) => {
111-
if (invite.userIds) {
112-
if (invite.userIds.includes(i.userId)) {
113-
alreadyInvites.push(i.userId);
114-
}
115-
} else if (invite.emails.includes(i.email)) {
116-
alreadyInvites.push(i.email);
117-
}
118-
});
119-
if (alreadyInvites.length > 0) {
120-
const err = new Error(`${alreadyInvites.join()} are already invited`);
121-
err.status = 400;
122-
return next(err);
123-
}
124-
12596
const data = {
12697
projectId,
12798
role: invite.role,
@@ -131,6 +102,8 @@ module.exports = [
131102
};
132103
const invitePromises = [];
133104
if (invite.userIds) {
105+
// remove invites for users that are invited already
106+
_.remove(invite.userIds, u => _.some(invites, i => i.userId === u));
134107
invite.userIds.forEach((userId) => {
135108
const dataNew = _.clone(data);
136109
_.assign(dataNew, {
@@ -142,6 +115,8 @@ module.exports = [
142115
data.userId = null;
143116

144117
if (invite.emails) {
118+
// remove invites for users that are invited already
119+
_.remove(invite.emails, u => _.some(invites, i => i.email === u));
145120
invite.emails.forEach((email) => {
146121
const dataNew = _.clone(data);
147122
_.assign(dataNew, {

src/routes/projectMemberInvites/get.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ module.exports = [
1818
const projectId = _.parseInt(req.params.projectId);
1919
const currentUserId = req.authUser.userId;
2020
let invite;
21-
return models.ProjectMemberInvite.getPendingInviteByEmailOrUserId(projectId, null, currentUserId)
21+
return models.ProjectMemberInvite.getPendingInviteByEmailOrUserId(projectId, req.authUser.email, currentUserId)
2222
.then((_invite) => {
2323
invite = _invite;
2424
if (!invite) {
2525
// check there is an existing invite for the user with status PENDING
2626
// handle 404
2727
const err = new Error('invite not found for project id ' +
28-
`${projectId}, userId ${currentUserId}`);
28+
`${projectId}, userId ${currentUserId}, email ${req.authUser.email}`);
2929
err.status = 404;
3030
return next(err);
3131
}

src/routes/projectMemberInvites/update.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Joi from 'joi';
44
import { middleware as tcMiddleware } from 'tc-core-library-js';
55
import models from '../../models';
66
import util from '../../util';
7-
import { PROJECT_MEMBER_ROLE, MANAGER_ROLES, USER_ROLE, INVITE_STATUS, EVENT } from '../../constants';
7+
import { PROJECT_MEMBER_ROLE, MANAGER_ROLES, INVITE_STATUS, EVENT } from '../../constants';
88

99
/**
1010
* API to update invite member to project.
@@ -36,13 +36,6 @@ module.exports = [
3636
const putInvite = req.body.param;
3737
const projectId = _.parseInt(req.params.projectId);
3838

39-
// not userId and email at the same time
40-
if (!!putInvite.userId && !!putInvite.email) {
41-
const err = new Error('userId and email cannot be presented in the same request');
42-
err.status = 400;
43-
return next(err);
44-
}
45-
4639
// userId or email should be provided
4740
if (!putInvite.userId && !putInvite.email) {
4841
const err = new Error('userId or email should be provided');
@@ -70,9 +63,7 @@ module.exports = [
7063
req.log.debug('Chekcing user permission for updating invite');
7164
let error = null;
7265
if (putInvite.status === INVITE_STATUS.CANCELED) {
73-
if (util.hasRole(req, USER_ROLE.COPILOT) && invite.role !== PROJECT_MEMBER_ROLE.CUSTOMER) {
74-
error = `Copilot can cancel invites only for ${PROJECT_MEMBER_ROLE.CUSTOMER}`;
75-
} else if (!util.hasRoles(req, MANAGER_ROLES)) {
66+
if (!util.hasRoles(req, MANAGER_ROLES) && invite.role !== PROJECT_MEMBER_ROLE.CUSTOMER) {
7667
error = `Project members can cancel invites only for ${PROJECT_MEMBER_ROLE.CUSTOMER}`;
7768
}
7869
} else if (!!putInvite.userId && putInvite.userId !== req.authUser.userId) {
@@ -111,7 +102,7 @@ module.exports = [
111102
const member = {
112103
projectId,
113104
role: updatedInvite.role,
114-
userId: updatedInvite.userId,
105+
userId: req.authUser.userId,
115106
createdBy: req.authUser.userId,
116107
updatedBy: req.authUser.userId,
117108
};

src/routes/projects/list.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const PROJECT_MEMBER_ATTRIBUTES = _.without(
3232
);
3333
const PROJECT_MEMBER_INVITE_ATTRIBUTES = _.without(
3434
_.keys(models.ProjectMemberInvite.rawAttributes),
35-
'deletedAt'
35+
'deletedAt',
3636
);
3737
const PROJECT_ATTACHMENT_ATTRIBUTES = _.without(
3838
_.keys(models.ProjectAttachment.rawAttributes),

0 commit comments

Comments
 (0)