Skip to content

Commit 1cb6e25

Browse files
committed
fix: find users by uuid, allow emply cc, corect cc format
1 parent 93e4665 commit 1cb6e25

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

config/default.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ module.exports = {
3030

3131
TC_API_V3_BASE_URL: process.env.TC_API_V3_BASE_URL || 'http://api.topcoder-dev.com/v3',
3232
TC_API_V4_BASE_URL: process.env.TC_API_V4_BASE_URL || '',
33-
TC_API_V5_BASE_URL: process.env.TC_API_V5_BASE_URL || '',
34-
TC_API_V5_USERS_URL: process.env.TC_API_V5_USERS_URL || 'https://api.topcoder-dev.com/v5/users',
33+
TC_API_V5_BASE_URL: process.env.TC_API_V5_BASE_URL || 'https://api.topcoder-dev.com/v5',
3534
API_CONTEXT_PATH: process.env.API_CONTEXT_PATH || '/v5/notifications',
3635
TC_API_BASE_URL: process.env.TC_API_BASE_URL || '',
3736

src/common/tcApiHelper.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ function* getUsersByEmails(emails) {
162162
* @param {Array<Object>} ids the objects that has user uuids.
163163
* @returns {Array<Object>} the matched users
164164
*/
165-
function* getUsersByUserUUIDs(ids) {
165+
function* getUsersByUserUUIDs(ids, enrich) {
166166
if (!ids || ids.length === 0) {
167167
return [];
168168
}
@@ -171,7 +171,7 @@ function* getUsersByUserUUIDs(ids) {
171171
try {
172172
for (const id of ids) {
173173
const res = yield request
174-
.get(`${config.TC_API_V5_USERS_URL}/${id}`)
174+
.get(`${config.TC_API_V5_BASE_URL}/users/${id.userUUID}${enrich ? '?enrich=true' : ''}`)
175175
.set('Authorization', `Bearer ${token}`);
176176
const user = res.body;
177177
logger.verbose(`Searched users: ${JSON.stringify(user, null, 4)}`);
@@ -288,6 +288,7 @@ function* notifyUserViaWeb(message) {
288288
function* notifyUserViaEmail(message) {
289289
const notificationType = message.type;
290290
const topic = constants.BUS_API_EVENT.EMAIL.UNIVERSAL;
291+
const cc = _.map(_.filter(message.details.cc, c => !_.isUndefined(c.email)), 'email');
291292
for (const recipient of message.details.recipients) {
292293
const userId = recipient.userId;
293294
let userEmail;
@@ -297,7 +298,7 @@ function* notifyUserViaEmail(message) {
297298
} else {
298299
userEmail = recipient.email;
299300
if (!userEmail) {
300-
logger.error(`Email not received for user: ${userId}`);
301+
logger.error(`Email not received for user: ${JSON.stringify(recipient, null, 4)}`);
301302
continue;
302303
}
303304
}
@@ -315,7 +316,7 @@ function* notifyUserViaEmail(message) {
315316
const payload = {
316317
from: message.details.from,
317318
recipients,
318-
cc: message.details.cc || [],
319+
cc,
319320
data: message.details.data || {},
320321
sendgrid_template_id: message.details.sendgridTemplateId,
321322
version: message.details.version,

src/services/UniversalNotificationService.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const emailSchema = joi.object().keys({
2828
userUUID: joi.string().uuid(),
2929
email: joi.string().email(),
3030
handle: joi.string(),
31-
}).min(1).required()
31+
}).min(1)
3232
),
3333
data: joi.object().keys({
3434
subject: joi.string(),
@@ -74,6 +74,15 @@ function validator(data, schema) {
7474
return true;
7575
}
7676

77+
78+
/**
79+
* Complete missing user fields of given payload details
80+
* This function mutates the given details object.
81+
* @param {Object} details the object which has recipients array
82+
* @param {Boolean} findEmail true if emails are needed
83+
* @param {Boolean} findUserId true if userIds are needed
84+
* @returns {undefined}
85+
*/
7786
function* completeMissingFields(details, findEmail, findUserId) {
7887
const getFieldsByUserId = [];
7988
const getFieldsByHandle = [];
@@ -127,20 +136,26 @@ function* completeMissingFields(details, findEmail, findUserId) {
127136
}
128137
}
129138
}
130-
const foundUsersByUUID = yield tcApiHelper.getUsersByUserUUIDs(getFieldsByUserUUID);
139+
const foundUsersByUUID = yield tcApiHelper.getUsersByUserUUIDs(getFieldsByUserUUID, true);
131140
if (!_.isEmpty(foundUsersByUUID)) {
132141
for (const user of getFieldsByUserUUID) {
133142
const found = _.find(foundUsersByUUID, ['id', user.userUUID]) || {};
134143
if (!_.isUndefined(found.externalProfiles) && !_.isEmpty(found.externalProfiles)) {
135144
_.assign(user, { userId: _.toInteger(_.get(found.externalProfiles[0], 'externalId')) });
136145
}
146+
if (!_.isUndefined(found.handle) && _.isUndefined(user.handle)) {
147+
_.assign(user, { handle: found.handle });
148+
}
137149
}
150+
138151
if (findEmail) {
139152
const usersHaveId = _.filter(getFieldsByUserUUID, u => !_.isUndefined(u.userId));
140-
const foundUsersById = yield tcApiHelper.getUsersByHandlesAndUserIds([], usersHaveId);
141-
if (!_.isEmpty(foundUsersById)) {
153+
const usersHaveHandle = _.filter(getFieldsByUserUUID, u => _.isUndefined(u.userId) && !_.isUndefined(u.handle));
154+
const foundUser = yield tcApiHelper.getUsersByHandlesAndUserIds(usersHaveHandle, usersHaveId);
155+
if (!_.isEmpty(foundUser)) {
142156
for (const user of getFieldsByUserUUID) {
143-
const found = _.find(foundUsersById, ['userId', user.userId]) || {};
157+
const found = _.find(foundUser, !_.isUndefined(user.handle)
158+
? ['handle', user.handle] : ['userId', user.userId]) || {};
144159
if (!_.isUndefined(found.email)) {
145160
_.assign(user, { email: found.email });
146161
}
@@ -162,6 +177,7 @@ function* handle(message) {
162177
switch (data.serviceId) {
163178
case constants.SETTINGS_EMAIL_SERVICE_ID:
164179
if (validator(data, emailSchema)) {
180+
// find missing emails and userIds
165181
yield completeMissingFields(data.details, true, true);
166182
yield tcApiHelper.notifyUserViaEmail(data);
167183
}
@@ -173,6 +189,7 @@ function* handle(message) {
173189
break;
174190
case constants.SETTINGS_WEB_SERVICE_ID:
175191
if (validator(data, webSchema)) {
192+
// find missing userIds
176193
yield completeMissingFields(data.details, false, true);
177194
const _notifications = yield tcApiHelper.notifyUserViaWeb(data);
178195
if (_notifications) {

0 commit comments

Comments
 (0)