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

Commit 09411af

Browse files
topcoder-archive/topcoder-platform-u-bahn-app#661 - Use email instead of handle for primary key
1 parent c39b0d3 commit 09411af

File tree

2 files changed

+49
-30
lines changed

2 files changed

+49
-30
lines changed

src/common/helper.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,12 @@ async function updateUBahnRecord (path, data) {
217217
}
218218

219219
/**
220-
* Returns the user in Topcoder identified by the handle
221-
* @param {String} handle The user handle
220+
* Returns the user in Topcoder identified by the email
221+
* @param {String} email The user email
222222
*/
223-
async function getUserInTopcoder (handle) {
223+
async function getUserInTopcoder (email) {
224224
const url = config.TOPCODER_USERS_API
225-
const params = { filter: `handle=${handle}` }
225+
const params = { filter: `email=${email}` }
226226
let token
227227

228228
try {
@@ -313,9 +313,9 @@ function parseExcel (file) {
313313
}
314314
}
315315

316-
if (!header.includes('handle')) {
317-
logger.error('"handle" column is missing. Cannot process the rows. Aborting.')
318-
throw Error('"handle" column is missing. Cannot process the rows. Aborting.')
316+
if (!header.includes('email')) {
317+
logger.error('"email" column is missing. Cannot process the rows. Aborting.')
318+
throw Error('"email" column is missing. Cannot process the rows. Aborting.')
319319
}
320320
for (let i = rowStart + 1; i <= rowEnd; i++) {
321321
const rowData = {}

src/services/ProcessorService.js

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ async function createUserInUbahn ({ handle, firstName, lastName }) {
3737
*/
3838
async function createUserInTopcoder (user) {
3939
const { handle, firstName, lastName, email, countryName, providerType, provider, userId } = user
40-
logger.debug(`Creating user with handle ${handle} in Topcoder`)
40+
logger.debug(`Creating user with handle ${handle} in Topcoder and email ${email}`)
4141

4242
const topcoderUser = {
4343
handle,
@@ -77,19 +77,10 @@ async function createUserInTopcoder (user) {
7777
*/
7878
async function createUser (user, organizationId) {
7979
let topcoderUserId
80-
// Check if the user exists in Topcoder
81-
const res = await helper.getUserInTopcoder(user.handle)
8280

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+
logger.debug(`User with email ${user.email} not found in Topcoder. Creating it...`)
82+
// Create the user in Topcoder
83+
topcoderUserId = await createUserInTopcoder(user)
9384

9485
// Create the user in UBahn api
9586
const ubahnUserId = await createUserInUbahn(user)
@@ -111,19 +102,47 @@ async function createUser (user, organizationId) {
111102
* @returns {Promise}
112103
*/
113104
async function getUserId (user, organizationId) {
114-
const record = await helper.getUbahnSingleRecord('/users', {
115-
handle: user.handle
116-
}, true)
117-
if (record) {
118-
return record.id
119-
}
105+
// Get the user's handle in topcoder
106+
const res = await helper.getUserInTopcoder(user.email)
107+
const topcoderUser = res.result.content.find(u => u.email === user.email)
108+
109+
if (topcoderUser) {
110+
logger.debug(`User with email ${user.email} found in Topcoder. Not creating the user in Topcoder again...`)
111+
// Use the handle from Topcoder (ignore the one in the excel, if provided)
112+
user.handle = topcoderUser.handle
113+
114+
// Get the user id in ubahn
115+
const record = await helper.getUbahnSingleRecord('/users', {
116+
handle: user.handle
117+
}, true)
118+
119+
if (record) {
120+
return record.id
121+
} else if (config.CREATE_MISSING_USER_FLAG) {
122+
// User exists in Topcoder, but not in ubahn
123+
// Create the user in UBahn
124+
125+
// Copy the details from the Topcoder user itself
126+
user.firstName = topcoderUser.firstName
127+
user.lastName = topcoderUser.lastName
128+
const ubahnUserId = await createUserInUbahn(user)
129+
130+
// Now, proceed to map the topcoder user id with the ubahn user id
131+
await helper.createUbahnRecord(`/users/${ubahnUserId}/externalProfiles`, {
132+
organizationId,
133+
externalId: topcoderUser.id
134+
})
135+
136+
// We will be only working with the user id in UBahn
137+
return ubahnUserId
138+
}
120139

121-
// No user found. Should we create the user or treat it as an error?
122-
if (config.CREATE_MISSING_USER_FLAG) {
140+
throw new Error(`Could not find user with handle ${user.handle} and email ${user.email} in Ubahn`)
141+
} else if (config.CREATE_MISSING_USER_FLAG) {
123142
return createUser(user, organizationId)
124-
} else {
125-
throw new Error(`Could not find user with handle ${user.handle}`)
126143
}
144+
145+
throw new Error(`Could not find user with email ${user.email} in Topcoder`)
127146
}
128147

129148
/**

0 commit comments

Comments
 (0)