@@ -7,16 +7,15 @@ const logger = require('../common/logger')
7
7
const helper = require ( '../common/helper' )
8
8
const constants = require ( '../common/constants' )
9
9
const config = require ( 'config' )
10
- const _ = require ( 'lodash' )
11
10
const esClient = helper . getESClient ( )
12
11
const ActionProcessorService = require ( '../services/ActionProcessorService' )
13
12
14
13
/**
15
- * Process create entity message
16
- * @param {Object } message the kafka message
17
- * @param {String } transactionId
18
- * @param {Object } options
19
- */
14
+ * Process create entity message
15
+ * @param {Object } message the kafka message
16
+ * @param {String } transactionId
17
+ * @param {Object } options
18
+ */
20
19
async function processCreate ( message , transactionId , options ) {
21
20
const workPeriod = message . payload
22
21
// Find related resourceBooking
@@ -44,20 +43,16 @@ async function processCreate (message, transactionId, options) {
44
43
throw err
45
44
}
46
45
}
47
-
48
- console . log ( `[RB value-999] before update: ${ JSON . stringify ( resourceBooking ) } ` )
49
- // Get ResourceBooking's existing workPeriods
50
- const workPeriods = _ . isArray ( resourceBooking . body . workPeriods ) ? resourceBooking . body . workPeriods : [ ]
51
- // Append new workPeriod
52
- workPeriods . push ( workPeriod )
53
- // Update ResourceBooking's workPeriods property
54
- console . log ( `[WP value-999]: ${ JSON . stringify ( workPeriod ) } ` )
55
- await esClient . updateExtra ( {
46
+ await esClient . update ( {
56
47
index : config . get ( 'esConfig.ES_INDEX_RESOURCE_BOOKING' ) ,
57
- id : workPeriod . resourceBookingId ,
48
+ id : resourceBooking . body . id ,
58
49
transactionId,
59
50
body : {
60
- doc : { workPeriods }
51
+ script : {
52
+ lang : 'painless' ,
53
+ source : 'if(!ctx._source.containsKey("workPeriods") || ctx._source.workPeriods == null){ctx._source["workPeriods"]=[]}ctx._source.workPeriods.add(params.workPeriod)' ,
54
+ params : { workPeriod }
55
+ }
61
56
} ,
62
57
refresh : constants . esRefreshOption
63
58
} )
@@ -96,14 +91,14 @@ processCreate.schema = {
96
91
}
97
92
98
93
/**
99
- * Process update entity message
100
- * @param {Object } message the kafka message
101
- * @param {String } transactionId
102
- */
94
+ * Process update entity message
95
+ * @param {Object } message the kafka message
96
+ * @param {String } transactionId
97
+ */
103
98
async function processUpdate ( message , transactionId ) {
104
99
const data = message . payload
105
100
// find workPeriod in it's parent ResourceBooking
106
- let resourceBooking = await esClient . search ( {
101
+ const resourceBooking = await esClient . search ( {
107
102
index : config . get ( 'esConfig.ES_INDEX_RESOURCE_BOOKING' ) ,
108
103
transactionId,
109
104
body : {
@@ -120,62 +115,16 @@ async function processUpdate (message, transactionId) {
120
115
if ( ! resourceBooking . body . hits . total . value ) {
121
116
throw new Error ( `id: ${ data . id } "WorkPeriod" not found` )
122
117
}
123
- let workPeriods
124
- // if WorkPeriod's resourceBookingId changed then it must be deleted from the old ResourceBooking
125
- // and added to the new ResourceBooking
126
- if ( resourceBooking . body . hits . hits [ 0 ] . _source . id !== data . resourceBookingId ) {
127
- // find old workPeriod record, so we can keep it's existing nested payments field
128
- let oldWorkPeriod = _ . find ( resourceBooking . body . hits . hits [ 0 ] . _source . workPeriods , [ 'id' , data . id ] )
129
- // remove workPeriod from it's old parent
130
- workPeriods = _ . filter ( resourceBooking . body . hits . hits [ 0 ] . _source . workPeriods , ( workPeriod ) => workPeriod . id !== data . id )
131
- // Update old ResourceBooking's workPeriods property
132
- await esClient . updateExtra ( {
133
- index : config . get ( 'esConfig.ES_INDEX_RESOURCE_BOOKING' ) ,
134
- id : resourceBooking . body . hits . hits [ 0 ] . _source . id ,
135
- transactionId,
136
- body : {
137
- doc : { workPeriods }
138
- } ,
139
- refresh : constants . esRefreshOption
140
- } )
141
- // find workPeriod's new parent ResourceBooking
142
- resourceBooking = await esClient . getExtra ( {
143
- index : config . get ( 'esConfig.ES_INDEX_RESOURCE_BOOKING' ) ,
144
- transactionId,
145
- id : data . resourceBookingId
146
- } )
147
- // Get ResourceBooking's existing workPeriods
148
- workPeriods = _ . isArray ( resourceBooking . body . workPeriods ) ? resourceBooking . body . workPeriods : [ ]
149
- // Update workPeriod record
150
- const newData = _ . assign ( oldWorkPeriod , data )
151
- // Append updated workPeriod to workPeriods
152
- workPeriods . push ( newData )
153
- // Update new ResourceBooking's workPeriods property
154
- await esClient . updateExtra ( {
155
- index : config . get ( 'esConfig.ES_INDEX_RESOURCE_BOOKING' ) ,
156
- id : data . resourceBookingId ,
157
- transactionId,
158
- body : {
159
- doc : { workPeriods }
160
- } ,
161
- refresh : constants . esRefreshOption
162
- } )
163
- return
164
- }
165
- // Update workPeriod record
166
- workPeriods = _ . map ( resourceBooking . body . hits . hits [ 0 ] . _source . workPeriods , ( workPeriod ) => {
167
- if ( workPeriod . id === data . id ) {
168
- return _ . assign ( workPeriod , data )
169
- }
170
- return workPeriod
171
- } )
172
- // Update ResourceBooking's workPeriods property
173
- await esClient . updateExtra ( {
118
+ await esClient . update ( {
174
119
index : config . get ( 'esConfig.ES_INDEX_RESOURCE_BOOKING' ) ,
175
- id : data . resourceBookingId ,
120
+ id : resourceBooking . body . hits . hits [ 0 ] . _id ,
176
121
transactionId,
177
122
body : {
178
- doc : { workPeriods }
123
+ script : {
124
+ lang : 'painless' ,
125
+ source : 'def wp = ctx._source.workPeriods.find(workPeriod -> workPeriod.id == params.data.id); ctx._source.workPeriods.removeIf(workPeriod -> workPeriod.id == params.data.id); params.data.payments = wp.payments; ctx._source.workPeriods.add(params.data)' ,
126
+ params : { data }
127
+ }
179
128
} ,
180
129
refresh : constants . esRefreshOption
181
130
} )
@@ -184,10 +133,10 @@ async function processUpdate (message, transactionId) {
184
133
processUpdate . schema = processCreate . schema
185
134
186
135
/**
187
- * Process delete entity message
188
- * @param {Object } message the kafka message
189
- * @param {String } transactionId
190
- */
136
+ * Process delete entity message
137
+ * @param {Object } message the kafka message
138
+ * @param {String } transactionId
139
+ */
191
140
async function processDelete ( message , transactionId ) {
192
141
const data = message . payload
193
142
// Find related ResourceBooking
@@ -208,15 +157,16 @@ async function processDelete (message, transactionId) {
208
157
if ( ! resourceBooking . body . hits . total . value ) {
209
158
throw new Error ( `id: ${ data . id } "WorkPeriod" not found` )
210
159
}
211
- // Remove workPeriod from workPeriods
212
- const workPeriods = _ . filter ( resourceBooking . body . hits . hits [ 0 ] . _source . workPeriods , ( workPeriod ) => workPeriod . id !== data . id )
213
- // Update ResourceBooking's workPeriods property
214
- await esClient . updateExtra ( {
160
+ await esClient . update ( {
215
161
index : config . get ( 'esConfig.ES_INDEX_RESOURCE_BOOKING' ) ,
216
- id : resourceBooking . body . hits . hits [ 0 ] . _source . id ,
162
+ id : resourceBooking . body . hits . hits [ 0 ] . _id ,
217
163
transactionId,
218
164
body : {
219
- doc : { workPeriods }
165
+ script : {
166
+ lang : 'painless' ,
167
+ source : 'ctx._source.workPeriods.removeIf(workPeriod -> workPeriod.id == params.data.id)' ,
168
+ params : { data }
169
+ }
220
170
} ,
221
171
refresh : constants . esRefreshOption
222
172
} )
0 commit comments