4
4
5
5
const Joi = require ( '@hapi/joi' )
6
6
const config = require ( 'config' )
7
- const _ = require ( 'lodash' )
8
7
const logger = require ( '../common/logger' )
9
8
const helper = require ( '../common/helper' )
10
9
const constants = require ( '../common/constants' )
11
10
12
11
const esClient = helper . getESClient ( )
13
12
14
13
/**
15
- * Process create entity message
16
- * @param {Object } message the kafka message
17
- * @param {String } transactionId
18
- */
14
+ * Process create entity message
15
+ * @param {Object } message the kafka message
16
+ * @param {String } transactionId
17
+ */
19
18
async function processCreate ( message , transactionId ) {
20
- const data = message . payload
19
+ const workPeriodPayment = message . payload
21
20
// find related resourceBooking
22
- const result = await esClient . search ( {
21
+ const resourceBooking = await esClient . search ( {
23
22
index : config . get ( 'esConfig.ES_INDEX_RESOURCE_BOOKING' ) ,
24
23
body : {
25
24
query : {
26
25
nested : {
27
26
path : 'workPeriods' ,
28
27
query : {
29
- match : { 'workPeriods.id' : data . workPeriodId }
28
+ match : { 'workPeriods.id' : workPeriodPayment . workPeriodId }
30
29
}
31
30
}
32
31
}
33
32
}
34
33
} )
35
- if ( ! result . body . hits . total . value ) {
36
- throw new Error ( `id: ${ data . workPeriodId } "WorkPeriod" not found` )
34
+ if ( ! resourceBooking . body . hits . total . value ) {
35
+ throw new Error ( `id: ${ workPeriodPayment . workPeriodId } "WorkPeriod" not found` )
37
36
}
38
- const resourceBooking = result . body . hits . hits [ 0 ] . _source
39
- // find related workPeriod record
40
- const workPeriod = _ . find ( resourceBooking . workPeriods , [ 'id' , data . workPeriodId ] )
41
- // Get workPeriod's existing payments
42
- const payments = _ . isArray ( workPeriod . payments ) ? workPeriod . payments : [ ]
43
- // Append new payment
44
- payments . push ( data )
45
- // Assign new payments array to workPeriod
46
- workPeriod . payments = payments
47
- // Update ResourceBooking's workPeriods property
48
- await esClient . updateExtra ( {
37
+ await esClient . update ( {
49
38
index : config . get ( 'esConfig.ES_INDEX_RESOURCE_BOOKING' ) ,
50
- id : resourceBooking . id ,
39
+ id : resourceBooking . body . hits . hits [ 0 ] . _id ,
51
40
transactionId,
52
41
body : {
53
- doc : { workPeriods : resourceBooking . workPeriods }
42
+ script : {
43
+ lang : 'painless' ,
44
+ source : 'def wp = ctx._source.workPeriods.find(workPeriod -> workPeriod.id == params.workPeriodPayment.workPeriodId); if(!wp.containsKey("payments") || wp.payments == null){wp["payments"]=[]}wp.payments.add(params.workPeriodPayment)' ,
45
+ params : { workPeriodPayment }
46
+ }
54
47
} ,
55
48
refresh : constants . esRefreshOption
56
49
} )
@@ -62,13 +55,24 @@ processCreate.schema = {
62
55
originator : Joi . string ( ) . required ( ) ,
63
56
timestamp : Joi . date ( ) . required ( ) ,
64
57
'mime-type' : Joi . string ( ) . required ( ) ,
58
+ key : Joi . string ( ) . allow ( null ) ,
65
59
payload : Joi . object ( ) . keys ( {
66
60
id : Joi . string ( ) . uuid ( ) . required ( ) ,
67
61
workPeriodId : Joi . string ( ) . uuid ( ) . required ( ) ,
68
62
challengeId : Joi . string ( ) . uuid ( ) . allow ( null ) ,
63
+ memberRate : Joi . number ( ) . required ( ) ,
64
+ customerRate : Joi . number ( ) . allow ( null ) ,
65
+ days : Joi . number ( ) . integer ( ) . min ( 1 ) . max ( 5 ) . required ( ) ,
69
66
amount : Joi . number ( ) . greater ( 0 ) . allow ( null ) ,
70
67
status : Joi . workPeriodPaymentStatus ( ) . required ( ) ,
71
68
billingAccountId : Joi . number ( ) . allow ( null ) ,
69
+ statusDetails : Joi . object ( ) . keys ( {
70
+ errorMessage : Joi . string ( ) . required ( ) ,
71
+ errorCode : Joi . number ( ) . integer ( ) . allow ( null ) ,
72
+ retry : Joi . number ( ) . integer ( ) . allow ( null ) ,
73
+ step : Joi . string ( ) . allow ( null ) ,
74
+ challengeId : Joi . string ( ) . uuid ( ) . allow ( null )
75
+ } ) . unknown ( true ) . allow ( null ) ,
72
76
createdAt : Joi . date ( ) . required ( ) ,
73
77
createdBy : Joi . string ( ) . uuid ( ) . required ( ) ,
74
78
updatedAt : Joi . date ( ) . allow ( null ) ,
@@ -79,14 +83,14 @@ processCreate.schema = {
79
83
}
80
84
81
85
/**
82
- * Process update entity message
83
- * @param {Object } message the kafka message
84
- * @param {String } transactionId
85
- */
86
+ * Process update entity message
87
+ * @param {Object } message the kafka message
88
+ * @param {String } transactionId
89
+ */
86
90
async function processUpdate ( message , transactionId ) {
87
91
const data = message . payload
88
92
// find workPeriodPayment in it's parent ResourceBooking
89
- let result = await esClient . search ( {
93
+ const resourceBooking = await esClient . search ( {
90
94
index : config . get ( 'esConfig.ES_INDEX_RESOURCE_BOOKING' ) ,
91
95
body : {
92
96
query : {
@@ -99,89 +103,19 @@ async function processUpdate (message, transactionId) {
99
103
}
100
104
}
101
105
} )
102
- if ( ! result . body . hits . total . value ) {
106
+ if ( ! resourceBooking . body . hits . total . value ) {
103
107
throw new Error ( `id: ${ data . id } "WorkPeriodPayment" not found` )
104
108
}
105
- const resourceBooking = _ . cloneDeep ( result . body . hits . hits [ 0 ] . _source )
106
- let workPeriod = null
107
- let payment = null
108
- let paymentIndex = null
109
- // find workPeriod and workPeriodPayment records
110
- _ . forEach ( resourceBooking . workPeriods , wp => {
111
- _ . forEach ( wp . payments , ( p , pi ) => {
112
- if ( p . id === data . id ) {
113
- payment = p
114
- paymentIndex = pi
115
- return false
116
- }
117
- } )
118
- if ( payment ) {
119
- workPeriod = wp
120
- return false
121
- }
122
- } )
123
- let payments
124
- // if WorkPeriodPayment's workPeriodId changed then it must be deleted from the old WorkPeriod
125
- // and added to the new WorkPeriod
126
- if ( payment . workPeriodId !== data . workPeriodId ) {
127
- // remove payment from payments
128
- payments = _ . filter ( workPeriod . payments , p => p . id !== data . id )
129
- // assign payments to workPeriod record
130
- workPeriod . payments = payments
131
- // Update old ResourceBooking's workPeriods property
132
- await esClient . updateExtra ( {
133
- index : config . get ( 'esConfig.ES_INDEX_RESOURCE_BOOKING' ) ,
134
- id : resourceBooking . id ,
135
- transactionId,
136
- body : {
137
- doc : { workPeriods : resourceBooking . workPeriods }
138
- } ,
139
- refresh : constants . esRefreshOption
140
- } )
141
- // find workPeriodPayment's new parent WorkPeriod
142
- result = await esClient . search ( {
143
- index : config . get ( 'esConfig.ES_INDEX_RESOURCE_BOOKING' ) ,
144
- body : {
145
- query : {
146
- nested : {
147
- path : 'workPeriods' ,
148
- query : {
149
- match : { 'workPeriods.id' : data . workPeriodId }
150
- }
151
- }
152
- }
153
- }
154
- } )
155
- const newResourceBooking = result . body . hits . hits [ 0 ] . _source
156
- // find WorkPeriod record in ResourceBooking
157
- const newWorkPeriod = _ . find ( newResourceBooking . workPeriods , [ 'id' , data . workPeriodId ] )
158
- // Get WorkPeriod's existing payments
159
- const newPayments = _ . isArray ( newWorkPeriod . payments ) ? newWorkPeriod . payments : [ ]
160
- // Append new payment
161
- newPayments . push ( data )
162
- // Assign new payments array to workPeriod
163
- newWorkPeriod . payments = newPayments
164
- // Update new ResourceBooking's workPeriods property
165
- await esClient . updateExtra ( {
166
- index : config . get ( 'esConfig.ES_INDEX_RESOURCE_BOOKING' ) ,
167
- id : newResourceBooking . id ,
168
- transactionId,
169
- body : {
170
- doc : { workPeriods : newResourceBooking . workPeriods }
171
- } ,
172
- refresh : constants . esRefreshOption
173
- } )
174
- return
175
- }
176
- // update payment record
177
- workPeriod . payments [ paymentIndex ] = data
178
- // Update ResourceBooking's workPeriods property
179
- await esClient . updateExtra ( {
109
+ await esClient . update ( {
180
110
index : config . get ( 'esConfig.ES_INDEX_RESOURCE_BOOKING' ) ,
181
- id : resourceBooking . id ,
111
+ id : resourceBooking . body . hits . hits [ 0 ] . _id ,
182
112
transactionId,
183
113
body : {
184
- doc : { workPeriods : resourceBooking . workPeriods }
114
+ script : {
115
+ lang : 'painless' ,
116
+ source : 'def wp = ctx._source.workPeriods.find(workPeriod -> workPeriod.id == params.data.workPeriodId); wp.payments.removeIf(payment -> payment.id == params.data.id); wp.payments.add(params.data)' ,
117
+ params : { data }
118
+ }
185
119
} ,
186
120
refresh : constants . esRefreshOption
187
121
} )
0 commit comments