@@ -7,16 +7,15 @@ const logger = require('../common/logger')
77const helper = require ( '../common/helper' )
88const constants = require ( '../common/constants' )
99const config = require ( 'config' )
10- const _ = require ( 'lodash' )
1110const esClient = helper . getESClient ( )
1211const ActionProcessorService = require ( '../services/ActionProcessorService' )
1312
1413/**
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+ */
2019async function processCreate ( message , transactionId , options ) {
2120 const workPeriod = message . payload
2221 // Find related resourceBooking
@@ -44,20 +43,16 @@ async function processCreate (message, transactionId, options) {
4443 throw err
4544 }
4645 }
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 ( {
5647 index : config . get ( 'esConfig.ES_INDEX_RESOURCE_BOOKING' ) ,
57- id : workPeriod . resourceBookingId ,
48+ id : resourceBooking . body . id ,
5849 transactionId,
5950 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+ }
6156 } ,
6257 refresh : constants . esRefreshOption
6358 } )
@@ -96,14 +91,14 @@ processCreate.schema = {
9691}
9792
9893/**
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+ */
10398async function processUpdate ( message , transactionId ) {
10499 const data = message . payload
105100 // find workPeriod in it's parent ResourceBooking
106- let resourceBooking = await esClient . search ( {
101+ const resourceBooking = await esClient . search ( {
107102 index : config . get ( 'esConfig.ES_INDEX_RESOURCE_BOOKING' ) ,
108103 transactionId,
109104 body : {
@@ -120,62 +115,16 @@ async function processUpdate (message, transactionId) {
120115 if ( ! resourceBooking . body . hits . total . value ) {
121116 throw new Error ( `id: ${ data . id } "WorkPeriod" not found` )
122117 }
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 ( {
174119 index : config . get ( 'esConfig.ES_INDEX_RESOURCE_BOOKING' ) ,
175- id : data . resourceBookingId ,
120+ id : resourceBooking . body . hits . hits [ 0 ] . _id ,
176121 transactionId,
177122 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+ }
179128 } ,
180129 refresh : constants . esRefreshOption
181130 } )
@@ -184,10 +133,10 @@ async function processUpdate (message, transactionId) {
184133processUpdate . schema = processCreate . schema
185134
186135/**
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+ */
191140async function processDelete ( message , transactionId ) {
192141 const data = message . payload
193142 // Find related ResourceBooking
@@ -208,15 +157,16 @@ async function processDelete (message, transactionId) {
208157 if ( ! resourceBooking . body . hits . total . value ) {
209158 throw new Error ( `id: ${ data . id } "WorkPeriod" not found` )
210159 }
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 ( {
215161 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 ,
217163 transactionId,
218164 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+ }
220170 } ,
221171 refresh : constants . esRefreshOption
222172 } )
0 commit comments