cleaned up code
This commit is contained in:
@@ -12,7 +12,7 @@ module.exports = async()=>{ // called by server.js
|
|||||||
const db = await cds.connect.to('db') //> our primary database
|
const db = await cds.connect.to('db') //> our primary database
|
||||||
|
|
||||||
// Reflect CDS definition of the Suppliers entity
|
// Reflect CDS definition of the Suppliers entity
|
||||||
const Suppliers = db.entities.Suppliers;
|
const { Suppliers } = db.entities
|
||||||
|
|
||||||
admin.prepend (()=>{ //> to ensure our .on handlers below go before the default ones
|
admin.prepend (()=>{ //> to ensure our .on handlers below go before the default ones
|
||||||
|
|
||||||
@@ -23,19 +23,19 @@ module.exports = async()=>{ // called by server.js
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Replicate Supplier data when edited Books have suppliers
|
// Replicate Supplier data when edited Books have suppliers
|
||||||
admin.on (['CREATE','UPDATE'], 'Books', async ({data:{supplier_ID: supplierId}}, next) => {
|
admin.on (['CREATE','UPDATE'], 'Books', async ({data:{supplier_ID:ID}}, next) => {
|
||||||
// Using Promise.all(...) to parallelize local write, i.e. next(), and replication
|
if (ID) {
|
||||||
|
// Using Promise.all(...) to parallelize local write, i.e. next(), and replication
|
||||||
const replicateIfNotExists = async()=>{
|
const [result] = await Promise.all([ next(), (async()=>{
|
||||||
let replicated = await db.exists (Suppliers, supplierId);
|
let replicated = await db.exists (Suppliers,ID)
|
||||||
if (!replicated) await replicate (supplierId, 'initial');
|
if (!replicated) { // initially replicate Supplier info
|
||||||
};
|
let supplier = await S4bupa.read (Suppliers,ID)
|
||||||
|
await INSERT(supplier) .into (Suppliers)
|
||||||
if (supplierId) {
|
}
|
||||||
const [result, _] = await Promise.all([next(), replicateIfNotExists()]);
|
})()])
|
||||||
return result;
|
return result
|
||||||
} else
|
}
|
||||||
return next(); //> don't forget to pass down the interceptor stack
|
else return next() //> don't forget to pass down the interceptor stack
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
@@ -44,18 +44,11 @@ module.exports = async()=>{ // called by server.js
|
|||||||
S4bupa.on ('BusinessPartner.Changed', async msg => { //> would be great if we had batch events from S/4
|
S4bupa.on ('BusinessPartner.Changed', async msg => { //> would be great if we had batch events from S/4
|
||||||
console.log(">>", msg.event, msg.data)
|
console.log(">>", msg.event, msg.data)
|
||||||
const ID = msg.data.BusinessPartner;
|
const ID = msg.data.BusinessPartner;
|
||||||
let replica = await SELECT.one('ID').from(Suppliers).where({ID});
|
let replicated = await db.exists (Suppliers,ID)
|
||||||
if (replica) await replicate(ID);
|
if (replicated) { // update replicated Supplier info
|
||||||
|
let supplier = await S4bupa.read (Suppliers,ID)
|
||||||
|
await UPDATE(Suppliers,ID) .with (supplier)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper function to replicate Suppliers data.
|
|
||||||
* @param {string} ID a single ID
|
|
||||||
* @param {truthy|falsy} _initial indicates whether an insert or an update is required
|
|
||||||
*/
|
|
||||||
async function replicate (ID,_initial) {
|
|
||||||
let supplier = await S4bupa.run(SELECT.one(Suppliers).where({ID}));
|
|
||||||
if (_initial) return db.insert(supplier).into(Suppliers);
|
|
||||||
else return db.update(Suppliers).with(supplier).where({ID});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user