@@ -37,7 +37,7 @@ async function createUserInUbahn ({ handle, firstName, lastName }) {
37
37
*/
38
38
async function createUserInTopcoder ( user ) {
39
39
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 } ` )
41
41
42
42
const topcoderUser = {
43
43
handle,
@@ -77,19 +77,10 @@ async function createUserInTopcoder (user) {
77
77
*/
78
78
async function createUser ( user , organizationId ) {
79
79
let topcoderUserId
80
- // Check if the user exists in Topcoder
81
- const res = await helper . getUserInTopcoder ( user . handle )
82
80
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 )
93
84
94
85
// Create the user in UBahn api
95
86
const ubahnUserId = await createUserInUbahn ( user )
@@ -111,19 +102,47 @@ async function createUser (user, organizationId) {
111
102
* @returns {Promise }
112
103
*/
113
104
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
+ }
120
139
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 ) {
123
142
return createUser ( user , organizationId )
124
- } else {
125
- throw new Error ( `Could not find user with handle ${ user . handle } ` )
126
143
}
144
+
145
+ throw new Error ( `Could not find user with email ${ user . email } in Topcoder` )
127
146
}
128
147
129
148
/**
0 commit comments