Skip to content

Commit c966138

Browse files
committed
Final fixes for adding work period payments.
1 parent e965836 commit c966138

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/services/WorkPeriodPaymentProcessorService.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ const esClient = helper.getESClient()
1818
*/
1919
async function processCreate (message, transactionId) {
2020
const data = message.payload
21-
const workPeriod = await esClient.get({
21+
const workPeriod = await esClient.getExtra({
2222
index: config.get('esConfig.ES_INDEX_WORK_PERIOD'),
2323
id: data.workPeriodId
2424
})
25-
const payments = _.isArray(workPeriod.body._source.payments) ? workPeriod.body._source.payments : []
25+
const payments = _.isArray(workPeriod.body.payments) ? workPeriod.body.payments : []
2626
payments.push(data)
2727

28-
return esClient.update({
28+
return esClient.updateExtra({
2929
index: config.get('esConfig.ES_INDEX_WORK_PERIOD'),
3030
id: data.workPeriodId,
3131
transactionId,
3232
body: {
33-
doc: _.assign(workPeriod.body._source, { payments })
33+
doc: _.assign(workPeriod.body, { payments })
3434
},
3535
refresh: constants.esRefreshOption
3636
})
@@ -77,31 +77,34 @@ async function processUpdate (message, transactionId) {
7777
}
7878
}
7979
})
80+
if (!workPeriod.body.hits.total.value) {
81+
throw new Error(`id: ${data.id} "WorkPeriodPayments" not found`)
82+
}
8083
let payments
8184
// if WorkPeriodPayment's workPeriodId changed then it must be deleted from the old WorkPeriod
8285
// and added to the new WorkPeriod
8386
if (workPeriod.body.hits.hits[0]._source.id !== data.workPeriodId) {
8487
payments = _.filter(workPeriod.body.hits.hits[0]._source.payments, (payment) => payment.id !== data.id)
85-
await esClient.update({
88+
await esClient.updateExtra({
8689
index: config.get('esConfig.ES_INDEX_WORK_PERIOD'),
8790
id: workPeriod.body.hits.hits[0]._source.id,
8891
transactionId,
8992
body: {
9093
doc: _.assign(workPeriod.body.hits.hits[0]._source, { payments })
9194
}
9295
})
93-
workPeriod = await esClient.get({
96+
workPeriod = await esClient.getExtra({
9497
index: config.get('esConfig.ES_INDEX_WORK_PERIOD'),
9598
id: data.workPeriodId
9699
})
97-
payments = _.isArray(workPeriod.body._source.payments) ? workPeriod.body._source.payments : []
100+
payments = _.isArray(workPeriod.body.payments) ? workPeriod.body.payments : []
98101
payments.push(data)
99-
return esClient.update({
102+
return esClient.updateExtra({
100103
index: config.get('esConfig.ES_INDEX_WORK_PERIOD'),
101104
id: data.workPeriodId,
102105
transactionId,
103106
body: {
104-
doc: _.assign(workPeriod.body._source, { payments })
107+
doc: _.assign(workPeriod.body, { payments })
105108
}
106109
})
107110
}
@@ -113,7 +116,7 @@ async function processUpdate (message, transactionId) {
113116
return payment
114117
})
115118

116-
return esClient.update({
119+
return esClient.updateExtra({
117120
index: config.get('esConfig.ES_INDEX_WORK_PERIOD'),
118121
id: data.workPeriodId,
119122
transactionId,

0 commit comments

Comments
 (0)