@@ -15,7 +15,9 @@ const translationService = require('../../services/translationService')
15
15
// const csv = require('csv-parser')
16
16
const fs = require ( 'fs' )
17
17
const resourceService = require ( '../../services/resourceService' )
18
- const { v5 } = require ( 'uuid' )
18
+ // const { v5 } = require('uuid')
19
+
20
+ // const APPLICATIONS_USERID = '22770213'
19
21
20
22
const memberHandleCache = new HashMap ( )
21
23
@@ -27,31 +29,36 @@ const migrationFunction = {
27
29
let batch = 1
28
30
29
31
const challengeJson = { challenges : [ ] }
32
+ const challengeIdsToDelete = [ ]
33
+
34
+ // logger.warn(await getMemberIdFromCache('TICKET_60375'))
35
+ // return
30
36
31
37
while ( ! finish ) {
32
38
logger . info ( `Batch-${ batch } - Loading challenges` )
33
39
const challenges = await getMatchesFromES ( page , perPage )
34
40
logger . info ( `Found ${ challenges . length } challenges` )
35
41
if ( challenges . length > 0 ) {
36
- // // logger.info(`Updating ${challenges}`)
37
42
for ( const challenge of challenges ) {
38
43
logger . debug ( `Loading challenge ${ challenge . id } ` )
39
44
const c = await challengeService . getMMatchFromV4API ( challenge . id )
40
45
if ( ! c ) {
41
- logger . error ( `Challenge Not Found - ID: ${ challenge . id } , RoundID: ${ challenge . roundId } ` )
46
+ logger . warn ( `Challenge Not Found in v4 API - ID: ${ challenge . id } ` )
42
47
continue
43
48
}
44
49
45
50
const v5ChallengeLookup = await challengeService . getChallengeIDsFromV5 ( { legacyId : challenge . id } , 10 )
46
51
// logger.debug(JSON.stringify(v5ChallengeLookup))
47
52
if ( v5ChallengeLookup && v5ChallengeLookup . v5Ids && v5ChallengeLookup . v5Ids [ 0 ] ) {
48
- logger . debug ( 'Skipping!' )
49
- continue
50
-
51
- // for (const id of v5ChallengeLookup.v5Ids) {
52
- // logger.warn(`Deleting Entry for ${id}`)
53
- // await challengeService.deleteChallenge(id)
54
- // }
53
+ if ( _ . includes ( challengeIdsToDelete , challenge . id ) ) {
54
+ for ( const id of v5ChallengeLookup . v5Ids ) {
55
+ // logger.warn(`Deleting Entry for ${id}`)
56
+ await challengeService . deleteChallenge ( id )
57
+ }
58
+ } else {
59
+ logger . debug ( 'Already Exists! Skipping!' )
60
+ continue
61
+ }
55
62
}
56
63
57
64
const v5TrackProperties = translationService . convertV4TrackToV5 (
@@ -99,78 +106,65 @@ const migrationFunction = {
99
106
numOfRegistrants : _ . toNumber ( c . numberOfRegistrants )
100
107
}
101
108
102
- // "registrationEndDate": "2012-03-29T17:00:00.000Z",
103
- // "submissionEndDate": "2012-03-29T17:00:00.000Z",
104
-
105
109
const { phases, challengeStartDate, challengeEndDate } = convertPhases ( c . phases )
106
110
107
111
newChallenge . phases = phases
108
112
newChallenge . startDate = challengeStartDate
109
113
newChallenge . created = challengeStartDate
110
114
newChallenge . updated = challengeEndDate
111
115
112
- let handlesToLookup = [ ]
116
+ const winners = [ ]
117
+ const calculatedWinners = [ ]
113
118
if ( c . registrants && c . registrants . length > 0 ) {
114
- for ( const registrant of c . registrants ) {
115
- // build cache
116
- if ( ! getMemberIdFromCache ( registrant . handle ) ) {
117
- handlesToLookup . push ( registrant . handle )
118
- } else {
119
- // logger.debug(`Handle Found in Cache ${registrant.handle}`)
120
- }
121
- if ( handlesToLookup . length >= 15 ) {
122
- await cacheHandles ( handlesToLookup )
123
- handlesToLookup = [ ]
119
+ if ( c . winners && c . winners . length > 0 ) {
120
+ for ( const w of c . winners ) {
121
+ const userId = await getMemberIdFromCache ( w . submitter )
122
+ winners . push ( {
123
+ handle : w . submitter ,
124
+ userId,
125
+ points : w . points ,
126
+ submissionId : w . submissionId
127
+ } )
124
128
}
125
- }
126
- await cacheHandles ( handlesToLookup )
127
129
128
- // csv
129
- // challenge id, member id, member handle, submission id, score
130
- const winners = _ . map ( c . winners , w => {
131
- return {
132
- handle : w . submitter ,
133
- userId : getMemberIdFromCache ( w . submitter ) ,
134
- points : w . points ,
135
- submissionId : w . submissionId
136
- }
137
- } )
130
+ const sortedWinners = _ . orderBy ( winners , [ 'points' ] , [ 'desc' ] )
138
131
139
- const sortedWinners = _ . orderBy ( winners , [ 'points' ] , [ 'desc' ] )
132
+ let placement = 1
133
+ let counter = 1
134
+ let lastPointsValue = null
135
+ for ( const winner of sortedWinners ) {
136
+ // calculate rank
137
+ if ( lastPointsValue && lastPointsValue > winner . points ) {
138
+ placement = counter
139
+ }
140
+ const calculatedWinner = { }
141
+ calculatedWinner . handle = _ . toString ( winner . handle )
142
+ calculatedWinner . userId = _ . toString ( winner . userId )
143
+ calculatedWinner . placement = placement
144
+ lastPointsValue = winner . points
145
+ counter += 1
140
146
141
- // console.log('Sorted Winners', JSON.stringify(sortedWinners))
142
- let placement = 1
143
- let counter = 1
144
- let lastPointsValue = null
145
- const calculatedWinners = [ ]
146
- for ( const winner of sortedWinners ) {
147
- // calculate rank
148
- if ( lastPointsValue && lastPointsValue > winner . points ) {
149
- placement = counter
147
+ calculatedWinners . push ( calculatedWinner )
150
148
}
151
- const calculatedWinner = { }
152
- calculatedWinner . handle = _ . toString ( winner . handle )
153
- calculatedWinner . userId = _ . toString ( winner . userId )
154
- calculatedWinner . placement = placement
155
- lastPointsValue = winner . points
156
- counter += 1
157
-
158
- calculatedWinners . push ( calculatedWinner )
159
149
}
160
- // console.log('Calculated Winners', JSON.stringify(calculatedWinners))
161
150
162
151
newChallenge . winners = calculatedWinners
163
152
164
- const savedChallengeId = await challengeService . save ( newChallenge )
165
- // const savedChallengeId = uuid() // FOR TESTING
166
- // logger.debug(`Challenge: ${JSON.stringify(c.submissions)}`)
153
+ let savedChallengeId
154
+ try {
155
+ savedChallengeId = await challengeService . save ( newChallenge )
156
+ // savedChallengeId = uuid() // FOR TESTING
157
+ } catch ( e ) {
158
+ logger . error ( `Challenge ${ challenge . id } Could Not Be Saved ${ e } ` )
159
+ continue
160
+ }
167
161
168
162
const thisChallenge = {
169
163
challengeId : challenge . id ,
170
164
submissions : c . submissions
171
165
}
172
166
challengeJson . challenges . push ( thisChallenge )
173
- fs . writeFileSync ( 'src/scripts/files/challenges.json' , JSON . stringify ( challengeJson ) )
167
+ // fs.writeFileSync('src/scripts/files/challenges.json', JSON.stringify(challengeJson))
174
168
175
169
for ( const registrant of c . registrants ) {
176
170
const memberId = await getMemberIdFromCache ( registrant . handle )
@@ -185,13 +179,13 @@ const migrationFunction = {
185
179
challengeId : savedChallengeId ,
186
180
roleId : config . SUBMITTER_ROLE_ID
187
181
}
188
- await resourceService . saveResource ( newResource )
189
- // logger.debug(`Resource: ${JSON.stringify(newResource)}`)
182
+ // await resourceService.saveResource(newResource)
183
+ logger . debug ( `Resource: ${ JSON . stringify ( newResource ) } ` )
190
184
}
191
185
} else {
192
186
logger . warn ( `No Registrants for Challenge ${ challenge . id } ` )
193
187
}
194
- // return
188
+ return
195
189
}
196
190
} else {
197
191
logger . info ( 'Finished' )
@@ -204,32 +198,57 @@ const migrationFunction = {
204
198
}
205
199
}
206
200
207
- function getMemberIdFromCache ( handle ) {
208
- if ( memberHandleCache . get ( handle ) ) {
209
- return memberHandleCache . get ( handle )
210
- }
211
- return false
212
- }
201
+ async function getMemberIdFromCache ( handle ) {
202
+ if ( memberHandleCache . get ( handle ) ) return memberHandleCache . get ( handle )
203
+
204
+ const url = `https://api.topcoder.com/v5/members?handle= ${ encodeURIComponent ( handle ) } &fields=handleLower,userId`
205
+ const token = await getM2MToken ( )
206
+ const res = await request . get ( url ) . set ( { Authorization : `Bearer ${ token } ` } )
213
207
214
- function cacheMemberIdForHandle ( handle , memberId ) {
215
- memberHandleCache . set ( handle , memberId )
208
+ // const response = _.get(res.body, 'result.content')
209
+ if ( res && res . body [ 0 ] ) {
210
+ const memberId = res . body [ 0 ] . userId
211
+ memberHandleCache . set ( handle , memberId )
212
+ return memberId
213
+ } else {
214
+ logger . warn ( `Could not find member id in v5 for handle ${ handle } , encoded: ${ encodeURIComponent ( handle ) } ` )
215
+ return getMemberIdFromV3 ( handle )
216
+ }
216
217
}
217
218
218
- async function cacheHandles ( handles ) {
219
- logger . debug ( `Caching ${ handles . length } handles` )
220
- // curl --location --request GET 'https://api.topcoder-dev.com/v3/members/_search/?fields=userId%2Chandle%2CfirstName%2Cemail%2ClastName&query=handleLower:upbeat%20OR%20handleLower:tonyj'
221
- const ids = _ . map ( handles , h => `handleLower:${ h } ` )
222
- const query = encodeURIComponent ( escapeChars ( ids . join ( '%20OR%20' ) ) )
219
+ async function getMemberIdFromV3 ( handle ) {
220
+ const query = encodeURIComponent ( escapeChars ( `handleLower:${ handle } ` ) )
223
221
const token = await getM2MToken ( )
224
222
const url = `${ config . V3_MEMBER_API_URL } /_search?fields=userId%2Chandle&query=${ query } `
223
+ logger . debug ( `Getting Handle from v3 ${ url } ` )
225
224
const res = await request . get ( url ) . set ( { Authorization : `Bearer ${ token } ` } )
226
225
const handleArray = _ . get ( res . body , 'result.content' )
227
- for ( const h of handleArray ) {
228
- cacheMemberIdForHandle ( h . handle , h . userId )
226
+ if ( handleArray && handleArray [ 0 ] ) {
227
+ // cacheMemberIdForHandle(h.handle, h.userId)
228
+ memberHandleCache . set ( handleArray [ 0 ] . handle , handleArray [ 0 ] . userId )
229
+ return handleArray [ 0 ] . userId
230
+ }
231
+ logger . error ( `Handle ${ handle } not found in v3, going to user api` )
232
+ return getMemberIdFromUserAPI ( handle )
233
+ }
234
+
235
+ async function getMemberIdFromUserAPI ( handle ) {
236
+ const query = encodeURIComponent ( escapeChars ( `${ handle } ` ) )
237
+ const token = await getM2MToken ( )
238
+ const url = `https://api.topcoder.com/v3/users?filter=handle=${ query } `
239
+ logger . debug ( `Getting Handle from v3 Users API ${ url } ` )
240
+ const res = await request . get ( url ) . set ( { Authorization : `Bearer ${ token } ` } )
241
+ const [ userObj ] = _ . get ( res . body , 'result.content' )
242
+ if ( userObj ) {
243
+ // cacheMemberIdForHandle(h.handle, h.userId)
244
+ memberHandleCache . set ( handle , userObj . id )
245
+ return userObj . id
229
246
}
247
+ logger . error ( `Handle ${ handle } not found in v3 User API, Giving Up` )
248
+ return 0
230
249
}
231
250
232
- // + - && || ! ( ) { } [ ] ^ " ~ * ? : \
251
+ // // + - && || ! ( ) { } [ ] ^ " ~ * ? : \
233
252
function escapeChars ( str ) {
234
253
str = str . replace ( / ] / g, '\\]' )
235
254
str = str . replace ( / \[ / g, '\\[' )
@@ -239,6 +258,7 @@ function escapeChars (str) {
239
258
str = str . replace ( / \) / g, '\\)' )
240
259
str = str . replace ( / \( / g, '\\(' )
241
260
str = str . replace ( / \/ / g, '\\/' )
261
+ str = str . replace ( / \. / g, '\\.' )
242
262
return str
243
263
}
244
264
0 commit comments