Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 001a187

Browse files
Check for user existence before creating the user in Topcoder
1 parent 5cfe9c8 commit 001a187

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

src/common/helper.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,34 @@ async function updateUBahnRecord (path, data) {
189189
}
190190
}
191191

192+
/**
193+
* Returns the user in Topcoder identified by the handle
194+
* @param {String} handle The user handle
195+
*/
196+
async function getUserInTopcoder (handle) {
197+
const url = config.TOPCODER_USERS_API
198+
const params = { filter: `handle=${handle}` }
199+
let token
200+
201+
try {
202+
token = await getTopcoderM2Mtoken()
203+
} catch (error) {
204+
logger.error('An error occurred fetching the m2m token for Topcoder APIs')
205+
logger.error(error)
206+
throw error
207+
}
208+
209+
logger.debug(`request GET ${url} with params: ${JSON.stringify(params)}`)
210+
211+
try {
212+
const res = await axios.get(url, { headers: { Authorization: `Bearer ${token}` }, params })
213+
return res.data
214+
} catch (err) {
215+
logger.error(err)
216+
throw Error(`get ${url} with params: ${JSON.stringify(params)} failed`)
217+
}
218+
}
219+
192220
/**
193221
* Creates user in Topcoder (sso user)
194222
* @param {Object} user The user to create
@@ -207,7 +235,7 @@ async function createUserInTopcoder (user) {
207235

208236
logger.debug(`request POST ${url} with data: ${JSON.stringify(user)}`)
209237
try {
210-
const res = await axios.post(`${url}`, requestBody, { headers: { Authorization: `Bearer ${token}` } })
238+
const res = await axios.post(url, requestBody, { headers: { Authorization: `Bearer ${token}` } })
211239
return res.data
212240
} catch (err) {
213241
logger.error(err)
@@ -276,6 +304,7 @@ module.exports = {
276304
getUbahnSingleRecord,
277305
createUbahnRecord,
278306
updateUBahnRecord,
307+
getUserInTopcoder,
279308
createUserInTopcoder,
280309
updateProcessStatus,
281310
uploadFailedRecord

src/services/ProcessorService.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,20 @@ async function createUserInTopcoder (user) {
7676
* @param {String} organizationId The org id to associate the new user with
7777
*/
7878
async function createUser (user, organizationId) {
79-
// Create the user in Topcoder
80-
const topcoderUserId = await createUserInTopcoder(user)
79+
let topcoderUserId
80+
// Check if the user exists in Topcoder
81+
const res = await helper.getUserInTopcoder(user.handle)
82+
83+
const topcoderUser = res.result.content.find(u => u.handle === user.handle)
84+
85+
if (!topcoderUser) {
86+
logger.debug(`User with handle ${user.handle} not found in Topcoder. Creating it...`)
87+
// Create the user in Topcoder
88+
topcoderUserId = await createUserInTopcoder(user)
89+
} else {
90+
logger.debug(`User with handle ${user.handle} found in Topcoder. Not creating it again...`)
91+
topcoderUserId = topcoderUser.id
92+
}
8193

8294
// Create the user in UBahn api
8395
const ubahnUserId = await createUserInUbahn(user)

0 commit comments

Comments
 (0)