prettier
This commit is contained in:
@@ -35,27 +35,18 @@ module.exports = cds.service.impl(async () => {
|
|||||||
bupa.on('BusinessPartner/Changed', async msg => {
|
bupa.on('BusinessPartner/Changed', async msg => {
|
||||||
console.log('>> received:', msg.data)
|
console.log('>> received:', msg.data)
|
||||||
|
|
||||||
const contact = msg.data.KEY[0].BUSINESSPARTNER //> S/4HANA's weird payload format
|
const BuPaID = msg.data.KEY[0].BUSINESSPARTNER //> S/4HANA's weird payload format
|
||||||
const { SELECT, UPDATE } = cds.ql(msg) //> convenient alternative to <srv>.transaction(req).run(SELECT...)
|
const { SELECT, UPDATE } = cds.ql(msg) //> convenient alternative to <srv>.transaction(req).run(SELECT...)
|
||||||
|
|
||||||
// fetch affected entries from local replicas
|
// fetch affected entries from local replicas
|
||||||
const replicas = await SELECT.from(Addresses).where({ contact })
|
const replicas = await SELECT.from(Addresses).where({ contact: BuPaID })
|
||||||
if (replicas.length === 0) return //> not affected
|
if (replicas.length === 0) return //> not affected
|
||||||
|
|
||||||
// fetch changed data from S/4 -> might be less than local due to deletes
|
// fetch changed data from S/4 -> might be less than local due to deletes
|
||||||
const externals = await SELECT.from(externalAddresses).where({
|
const changed = await SELECT.from(externalAddresses).where({
|
||||||
contact
|
contact: BuPaID, ID: replicas.map(({ ID }) => ID)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Add tombstone if external address was deleted
|
|
||||||
const changed = replicas.map(
|
|
||||||
rep =>
|
|
||||||
externals.find(ext => ext.ID === rep.ID) || {
|
|
||||||
...rep,
|
|
||||||
...{ tombstone: true }
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
// update local replicas with changes from S/4
|
// update local replicas with changes from S/4
|
||||||
const local = db.transaction(msg) //> using that variant to benefit from bulk runs
|
const local = db.transaction(msg) //> using that variant to benefit from bulk runs
|
||||||
return local.run(changed.map(a => UPDATE(Addresses, a.ID).with(a)))
|
return local.run(changed.map(a => UPDATE(Addresses, a.ID).with(a)))
|
||||||
@@ -78,7 +69,10 @@ module.exports = cds.service.impl(async () => {
|
|||||||
.set('stock -=', each.amount)
|
.set('stock -=', each.amount)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
all.forEach((affectedRows, i) => affectedRows > 0 || req.error(409, `${Items[i].amount} exceeds stock for book #${Items[i].book_ID}`))
|
all.forEach(
|
||||||
|
(affectedRows, i) =>
|
||||||
|
affectedRows > 0 || req.error(409, `${Items[i].amount} exceeds stock for book #${Items[i].book_ID}`)
|
||||||
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
require('./utils')
|
require('./utils')
|
||||||
|
|||||||
@@ -4,12 +4,15 @@ process.env['https_proxy'] = ''
|
|||||||
process.env['HTTP_PROXY'] = ''
|
process.env['HTTP_PROXY'] = ''
|
||||||
process.env['HTTPS_PROXY'] = ''
|
process.env['HTTPS_PROXY'] = ''
|
||||||
|
|
||||||
const diff = (obj1, obj2) => Object.keys(obj1).reduce((res, curr) => (obj1[curr] === obj2[curr] ? res : (res[curr] = obj2[curr]) && res), {})
|
const diff = (obj1, obj2) =>
|
||||||
|
Object.keys(obj1).reduce((res, curr) => (obj1[curr] === obj2[curr] ? res : (res[curr] = obj2[curr]) && res), {})
|
||||||
|
|
||||||
const queriesToUpdateDifferences = (entity, ownEntries, otherEntries) =>
|
const queriesToUpdateDifferences = (entity, ownEntries, otherEntries) =>
|
||||||
ownEntries
|
ownEntries
|
||||||
.map(ownEntry => {
|
.map(ownEntry => {
|
||||||
const otherEntry = otherEntries.find(otherEntry => Object.keys(entity.keys).reduce((res, curr) => res && otherEntry[curr] === ownEntry[curr], true))
|
const otherEntry = otherEntries.find(otherEntry =>
|
||||||
|
Object.keys(entity.keys).reduce((res, curr) => res && otherEntry[curr] === ownEntry[curr], true)
|
||||||
|
)
|
||||||
if (otherEntry) {
|
if (otherEntry) {
|
||||||
const differences = diff(ownEntry, otherEntry)
|
const differences = diff(ownEntry, otherEntry)
|
||||||
if (Object.keys(differences).length) {
|
if (Object.keys(differences).length) {
|
||||||
|
|||||||
Reference in New Issue
Block a user