some more cosmetics tweaks

This commit is contained in:
Daniel
2019-12-17 12:55:09 +01:00
parent 2c0f69a161
commit 8c8c5f3f9d

View File

@@ -35,7 +35,7 @@ module.exports = (admin => {
admin.before ('PATCH', 'Orders', async (req) => { admin.before ('PATCH', 'Orders', async (req) => {
const ID = req.data.shippingAddress_ID; if (!ID) return //> something else const ID = req.data.shippingAddress_ID; if (!ID) return //> something else
const { SELECT, UPSERT } = cds.ql(req) //> convenient alternative to <srv>.transaction(req).run(SELECT...) const { SELECT, UPSERT } = cds.ql(req) //> convenient alternative to <srv>.transaction(req).run(SELECT...)
const address = await SELECT.one.from(externalAddresses).where({ const address = await SELECT.one.from (externalAddresses) .where ({
ID, BusinessPartner: req.user.id ID, BusinessPartner: req.user.id
}) })
if (address) return UPSERT (Addresses) .entries (address) if (address) return UPSERT (Addresses) .entries (address)
@@ -50,22 +50,17 @@ bupa.on ('BusinessPartner/Changed', async (msg) => {
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 local = db.transaction (msg)
const replicas = await SELECT.from (Addresses) .where ({BusinessPartner}) const replicas = await SELECT.from (Addresses) .where ({BusinessPartner})
if (replicas.length === 0) return //> not affected
// skip if not affected
if (replicas.length === 0) return
// 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 changed = await SELECT.from (externalAddresses) .where ({ const changed = await SELECT.from (externalAddresses) .where ({
BusinessPartner, ID: replicas.map(a => a.ID) // where in BusinessPartner, ID: replicas.map(a => a.ID) // where in
}) })
// update local replicas with changes from remote // update local replicas with changes from S/4
return local.run (changed.map (a => const local = db.transaction (msg) //> using that variant to benefit from bulk runs
UPDATE (Addresses) .with(a) .where ({ ID: a.ID }) return local.run (changed.map (a => UPDATE (Addresses,a.ID) .with (a) ))
))
}) })