This repository was archived by the owner on Mar 13, 2025. It is now read-only.
File tree 2 files changed +44
-3
lines changed
2 files changed +44
-3
lines changed Original file line number Diff line number Diff line change @@ -189,6 +189,34 @@ async function updateUBahnRecord (path, data) {
189
189
}
190
190
}
191
191
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
+
192
220
/**
193
221
* Creates user in Topcoder (sso user)
194
222
* @param {Object } user The user to create
@@ -207,7 +235,7 @@ async function createUserInTopcoder (user) {
207
235
208
236
logger . debug ( `request POST ${ url } with data: ${ JSON . stringify ( user ) } ` )
209
237
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 } ` } } )
211
239
return res . data
212
240
} catch ( err ) {
213
241
logger . error ( err )
@@ -276,6 +304,7 @@ module.exports = {
276
304
getUbahnSingleRecord,
277
305
createUbahnRecord,
278
306
updateUBahnRecord,
307
+ getUserInTopcoder,
279
308
createUserInTopcoder,
280
309
updateProcessStatus,
281
310
uploadFailedRecord
Original file line number Diff line number Diff line change @@ -76,8 +76,20 @@ async function createUserInTopcoder (user) {
76
76
* @param {String } organizationId The org id to associate the new user with
77
77
*/
78
78
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
+ }
81
93
82
94
// Create the user in UBahn api
83
95
const ubahnUserId = await createUserInUbahn ( user )
You can’t perform that action at this time.
0 commit comments