@@ -13,17 +13,10 @@ const helper = require('../src/common/helper')
1313 * Update Submission's challenge id to v5
1414 * @param {Object } submission The submission record
1515 * @param {Array } failedContainer The failed records container
16+ * @param {String } v5challengeId The v5 challenge id
1617 * @returns {Promise }
1718 */
18- function * updateRecord ( submission , failedContainer ) {
19- let v5challengeId
20- try {
21- v5challengeId = yield helper . getV5ChallengeId ( submission . challengeId )
22- } catch ( err ) {
23- logger . error ( `fetching the details of the challenge(${ submission . challengeId } ) failed, ${ err . message } ` )
24- failedContainer . push ( submission )
25- return
26- }
19+ function * updateRecord ( submission , failedContainer , v5challengeId ) {
2720 const record = {
2821 TableName : 'Submission' ,
2922 Key : {
@@ -35,13 +28,11 @@ function * updateRecord (submission, failedContainer) {
3528 ':l' : submission . challengeId
3629 }
3730 }
38- if ( ! v5challengeId ) {
39- logger . warn ( `the challengeId: ${ submission . challengeId } is not having a v5 challengeId` )
40- failedContainer . push ( submission )
41- } else if ( v5challengeId === submission . challengeId ) {
42- logger . info ( `the challengeId: ${ submission . challengeId } is already a v5 challengeId` )
43- } else {
31+ try {
4432 yield dbhelper . updateRecord ( record )
33+ } catch ( err ) {
34+ logger . error ( `update submission record error: ${ err . message } ` )
35+ failedContainer . push ( submission )
4536 }
4637}
4738
@@ -53,23 +44,34 @@ function * updateRecords () {
5344 const tableName = config . SUBMISSION_TABLE_NAME
5445 const promises = [ ]
5546 const failedRecords = [ ]
56- const params = {
57- TableName : tableName
58- }
5947 // Process until all the records from DB is fetched
60- while ( true ) {
61- const records = yield dbhelper . scanRecords ( params )
62- const totalRecords = records . Items . length
63- logger . debug ( `Number of ${ tableName } s fetched from DB - ${ totalRecords } . More fetch iterations may follow (pagination in progress)` )
64- for ( let i = 0 ; i < totalRecords ; i ++ ) {
65- const record = records . Items [ i ]
66- promises . push ( updateRecord ( record , failedRecords ) )
48+ const challengeIds = yield helper . getLatestChallenges ( )
49+ logger . debug ( `Total number of challenges fetched from api - ${ challengeIds . length } .` )
50+ const batchIds = _ . chunk ( challengeIds , config . UPDATE_V5_CHALLENGE_BATCH_SIZE )
51+ for ( const cId of batchIds ) {
52+ const queryParams = _ . fromPairs ( _ . map ( cId , ( c , i ) => [ `:challengeId${ i } ` , c . legacyId ] ) )
53+ const params = {
54+ TableName : tableName ,
55+ FilterExpression : `#challengeId IN (${ _ . join ( _ . keys ( queryParams ) , ',' ) } )` ,
56+ ExpressionAttributeNames : {
57+ '#challengeId' : 'challengeId'
58+ } ,
59+ ExpressionAttributeValues : queryParams
6760 }
68- // Continue fetching the remaining records from Database
69- if ( typeof records . LastEvaluatedKey !== 'undefined' ) {
70- params . ExclusiveStartKey = records . LastEvaluatedKey
71- } else {
72- break // If there are no more records to process, exit the loop
61+ while ( true ) {
62+ const records = yield dbhelper . scanRecords ( params )
63+ const totalRecords = records . Items . length
64+ logger . debug ( `Number of ${ tableName } s fetched from DB - ${ totalRecords } . More fetch iterations may follow (pagination in progress)` )
65+ for ( let i = 0 ; i < totalRecords ; i ++ ) {
66+ const record = records . Items [ i ]
67+ promises . push ( updateRecord ( record , failedRecords , _ . find ( cId , [ 'legacyId' , record . challengeId ] ) . id ) )
68+ }
69+ // Continue fetching the remaining records from Database
70+ if ( typeof records . LastEvaluatedKey !== 'undefined' ) {
71+ params . ExclusiveStartKey = records . LastEvaluatedKey
72+ } else {
73+ break // If there are no more records to process, exit the loop
74+ }
7375 }
7476 }
7577 logger . debug ( `All records fetched. Proceeding to update them in batches of ${ config . UPDATE_V5_CHALLENGE_BATCH_SIZE } ` )
0 commit comments