Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/services/WorkPeriodPaymentProcessorService.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ const esClient = helper.getESClient()
*/
async function processCreate (message, transactionId) {
const data = message.payload
const workPeriod = await esClient.get({
const workPeriod = await esClient.getExtra({
index: config.get('esConfig.ES_INDEX_WORK_PERIOD'),
id: data.workPeriodId
})
const payments = _.isArray(workPeriod.body._source.payments) ? workPeriod.body._source.payments : []
const payments = _.isArray(workPeriod.body.payments) ? workPeriod.body.payments : []
payments.push(data)

return esClient.update({
return esClient.updateExtra({
index: config.get('esConfig.ES_INDEX_WORK_PERIOD'),
id: data.workPeriodId,
transactionId,
body: {
doc: _.assign(workPeriod.body._source, { payments })
doc: _.assign(workPeriod.body, { payments })
},
refresh: constants.esRefreshOption
})
Expand Down Expand Up @@ -77,31 +77,34 @@ async function processUpdate (message, transactionId) {
}
}
})
if (!workPeriod.body.hits.total.value) {
throw new Error(`id: ${data.id} "WorkPeriodPayments" not found`)
}
let payments
// if WorkPeriodPayment's workPeriodId changed then it must be deleted from the old WorkPeriod
// and added to the new WorkPeriod
if (workPeriod.body.hits.hits[0]._source.id !== data.workPeriodId) {
payments = _.filter(workPeriod.body.hits.hits[0]._source.payments, (payment) => payment.id !== data.id)
await esClient.update({
await esClient.updateExtra({
index: config.get('esConfig.ES_INDEX_WORK_PERIOD'),
id: workPeriod.body.hits.hits[0]._source.id,
transactionId,
body: {
doc: _.assign(workPeriod.body.hits.hits[0]._source, { payments })
}
})
workPeriod = await esClient.get({
workPeriod = await esClient.getExtra({
index: config.get('esConfig.ES_INDEX_WORK_PERIOD'),
id: data.workPeriodId
})
payments = _.isArray(workPeriod.body._source.payments) ? workPeriod.body._source.payments : []
payments = _.isArray(workPeriod.body.payments) ? workPeriod.body.payments : []
payments.push(data)
return esClient.update({
return esClient.updateExtra({
index: config.get('esConfig.ES_INDEX_WORK_PERIOD'),
id: data.workPeriodId,
transactionId,
body: {
doc: _.assign(workPeriod.body._source, { payments })
doc: _.assign(workPeriod.body, { payments })
}
})
}
Expand All @@ -113,7 +116,7 @@ async function processUpdate (message, transactionId) {
return payment
})

return esClient.update({
return esClient.updateExtra({
index: config.get('esConfig.ES_INDEX_WORK_PERIOD'),
id: data.workPeriodId,
transactionId,
Expand Down