@@ -14,9 +14,10 @@ const esClient = helper.getEsClient()
14
14
/*
15
15
* Migrate records from DB to ES
16
16
* @param tableName {String} DynamoDB table name
17
+ * @param customFunction {Function} custom function to handle record
17
18
* @returns {Promise }
18
19
*/
19
- function * migrateRecords ( tableName ) {
20
+ function * migrateRecords ( tableName , customFunction ) {
20
21
let body = [ ]
21
22
let batchCounter = 1
22
23
const params = {
@@ -27,7 +28,8 @@ function * migrateRecords (tableName) {
27
28
const records = yield dbhelper . scanRecords ( params )
28
29
logger . debug ( `Number of ${ tableName } s currently fetched from DB - ` + records . Items . length )
29
30
let i = 0
30
- for ( const item of records . Items ) {
31
+ for ( const recordItem of records . Items ) {
32
+ const item = customFunction ( recordItem )
31
33
// action
32
34
body . push ( {
33
35
index : {
@@ -69,12 +71,30 @@ function * migrateRecords (tableName) {
69
71
70
72
co ( function * ( ) {
71
73
const promises = [ ]
72
- promises . push ( migrateRecords ( 'ReviewType' ) )
73
- promises . push ( migrateRecords ( 'Submission' ) )
74
- promises . push ( migrateRecords ( 'Review' ) )
75
- promises . push ( migrateRecords ( 'ReviewSummation' ) )
74
+ const reviews = [ ]
75
+ const reviewSummations = [ ]
76
+ promises . push ( migrateRecords ( 'ReviewType' , t => t ) )
77
+ promises . push ( migrateRecords ( 'Review' , t => {
78
+ reviews . push ( t )
79
+ return t
80
+ } ) )
81
+ promises . push ( migrateRecords ( 'ReviewSummation' , t => {
82
+ reviewSummations . push ( t )
83
+ return t
84
+ } ) )
76
85
// Process migration in parallel
77
86
yield promises
87
+ yield migrateRecords ( 'Submission' , t => {
88
+ t . review = _ . map ( _ . filter ( reviews , [ 'submissionId' , t . id ] ) , r => _ . omit ( r , [ 'resource' ] ) )
89
+ t . reviewSummation = _ . map ( _ . filter ( reviewSummations , [ 'submissionId' , t . id ] ) , r => _ . omit ( r , [ 'resource' ] ) )
90
+ if ( _ . isEmpty ( t . review ) ) {
91
+ t = _ . omit ( t , [ 'review' ] )
92
+ }
93
+ if ( _ . isEmpty ( t . reviewSummation ) ) {
94
+ t = _ . omit ( t , [ 'reviewSummation' ] )
95
+ }
96
+ return t
97
+ } )
78
98
} ) . catch ( ( err ) => {
79
99
logger . logFullError ( err )
80
100
} )
0 commit comments