Skip to content

Challenge/30095006 #340

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

Merged
merged 5 commits into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,5 @@ export const INVITE_STATUS = {
REQUEST_APPROVED: 'request_approved',
CANCELED: 'canceled',
};

export const MAX_PARALLEL_REQUEST_QTY = 5;
4 changes: 2 additions & 2 deletions src/routes/projectMemberInvites/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { middleware as tcMiddleware } from 'tc-core-library-js';
import models from '../../models';
import util from '../../util';
import { PROJECT_MEMBER_ROLE, PROJECT_MEMBER_MANAGER_ROLES,
MANAGER_ROLES, INVITE_STATUS, EVENT, BUS_API_EVENT, USER_ROLE } from '../../constants';
MANAGER_ROLES, INVITE_STATUS, EVENT, BUS_API_EVENT, USER_ROLE, MAX_PARALLEL_REQUEST_QTY } from '../../constants';
import { createEvent } from '../../services/busApi';


Expand Down Expand Up @@ -80,7 +80,7 @@ const buildCreateInvitePromises = (req, invite, invites, data, failed) => {
if (invite.emails) {
// if for some emails there are already existent users, we will invite them by userId,
// to avoid sending them registration email
return util.lookupUserEmails(req, invite.emails)
return util.lookupMultipleUserEmails(req, invite.emails, MAX_PARALLEL_REQUEST_QTY)
.then((existentUsers) => {
// existent user we will invite by userId and email
const existentUsersWithNumberId = existentUsers.map((user) => {
Expand Down
8 changes: 4 additions & 4 deletions src/routes/projectMemberInvites/create.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ describe('Project Member Invite create', () => {
server.services.pubsub.publish.restore();
sinon.stub(server.services.pubsub, 'init', () => {});
sinon.stub(server.services.pubsub, 'publish', () => {});
// by default mock lookupUserEmails return nothing so all the cases are not broken
// by default mock lookupMultipleUserEmails return nothing so all the cases are not broken
sandbox.stub(util, 'getUserRoles', () => Promise.resolve([]));
sandbox.stub(util, 'lookupUserEmails', () => Promise.resolve([]));
sandbox.stub(util, 'lookupMultipleUserEmails', () => Promise.resolve([]));
sandbox.stub(util, 'getMemberDetailsByUserIds', () => Promise.resolve([{
userId: 40051333,
firstName: 'Admin',
Expand Down Expand Up @@ -366,8 +366,8 @@ describe('Project Member Invite create', () => {
}),
});
sandbox.stub(util, 'getHttpClient', () => mockHttpClient);
util.lookupUserEmails.restore();
sandbox.stub(util, 'lookupUserEmails', () => Promise.resolve([{
util.lookupMultipleUserEmails.restore();
sandbox.stub(util, 'lookupMultipleUserEmails', () => Promise.resolve([{
id: '12345',
email: 'hello@world.com',
}]));
Expand Down
66 changes: 66 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,72 @@ _.assignIn(util, {
});
},

/**
* Lookup user handles from multiple emails
* @param {Object} req request
* @param {Array} userEmails user emails
* @param {Number} maximumRequests limit number of request on one batch
* @param {Boolean} isPattern flag to indicate that pattern matching is required or not
* @return {Promise} promise
*/
lookupMultipleUserEmails(req, userEmails, maximumRequests, isPattern = false) {
req.log.debug(`identityServiceEndpoint: ${config.get('identityServiceEndpoint')}`);

const httpClient = util.getHttpClient({ id: req.id, log: req.log });
// request generator function
const generateRequest = ({ token, email }) => {
let filter = `email=${email}`;
if (isPattern) {
filter += '&like=true';
}
return httpClient.get(`${config.get('identityServiceEndpoint')}users`, {
headers: {
Authorization: `Bearer ${token}`,
Accept: 'application/json',
'Content-Type': 'application/json',
},
params: {
fields: 'handle,id,email',
filter,
},
// set longer timeout as default 3000 could be not enough for identity service response
timeout: 15000,
}).catch(() => {
// in case of any error happens during getting user by email
// we treat such users as not found and don't return error
// as per discussion in issue #334
});
};
// send batch of requests, one batch at one time
const sendBatch = (options) => {
const token = options.token;
const emails = options.emails;
const users = options.users || [];
const batch = options.batch || 0;
const start = batch * maximumRequests;
const end = (batch + 1) * maximumRequests;
const requests = emails.slice(start, end).map(userEmail =>
generateRequest({ token, email: userEmail }));
return Promise.all(requests)
.then((responses) => {
const data = responses.reduce((contents, response) => {
const content = _.get(response, 'data.result.content', []);
return _.concat(contents, content);
}, users);
req.log.debug(`UserHandle response batch-${batch}`, data);
if (end < emails.length) {
return sendBatch({ token, users: data, emails, batch: batch + 1 });
}
return data;
});
};
return util.getM2MToken()
.then((m2mToken) => {
req.log.debug(`Bearer ${m2mToken}`);
return sendBatch({ token: m2mToken, emails: userEmails });
});
},

/**
* Filter only members of topcoder team
* @param {Array} members project members
Expand Down