From 920600a5ff291056285a1395a0e9e1e34f7975a2 Mon Sep 17 00:00:00 2001 From: D065023 Date: Wed, 8 Jan 2020 13:49:59 +0100 Subject: [PATCH] prettier --- packages/bookshop/srv/admin-service.js | 22 ++++++++-------------- packages/bookshop/srv/utils.js | 7 +++++-- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/packages/bookshop/srv/admin-service.js b/packages/bookshop/srv/admin-service.js index 06b32991..af181413 100644 --- a/packages/bookshop/srv/admin-service.js +++ b/packages/bookshop/srv/admin-service.js @@ -35,27 +35,18 @@ module.exports = cds.service.impl(async () => { bupa.on('BusinessPartner/Changed', async msg => { 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 .transaction(req).run(SELECT...) // 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 // fetch changed data from S/4 -> might be less than local due to deletes - const externals = await SELECT.from(externalAddresses).where({ - contact + const changed = await SELECT.from(externalAddresses).where({ + 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 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))) @@ -78,7 +69,10 @@ module.exports = cds.service.impl(async () => { .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') diff --git a/packages/bookshop/srv/utils.js b/packages/bookshop/srv/utils.js index b002ca19..69b7d6f3 100644 --- a/packages/bookshop/srv/utils.js +++ b/packages/bookshop/srv/utils.js @@ -4,12 +4,15 @@ process.env['https_proxy'] = '' process.env['HTTP_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) => ownEntries .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) { const differences = diff(ownEntry, otherEntry) if (Object.keys(differences).length) {