Fixes and improvements
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
const cds = require('@sap/cds');
|
||||
|
||||
module.exports = cds.service.impl(async function (srv) {
|
||||
module.exports = cds.service.impl(function () {
|
||||
const { A_BusinessPartner } = this.entities;
|
||||
|
||||
srv.after('UPDATE', A_BusinessPartner, data => {
|
||||
// TODO: Take over the original S/4 event definition
|
||||
this.after('UPDATE', A_BusinessPartner, async data => {
|
||||
console.log(`>>> BusinessPartner updated ${data.BusinessPartner}`);
|
||||
srv.emit("A_BusinessPartner.Changed", { businessPartners: [ data.BusinessPartner ] });
|
||||
await this.emit("A_BusinessPartner.Changed", { businessPartners: [ data.BusinessPartner ] });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -43,10 +43,8 @@ module.exports = async()=>{ // called by server.js
|
||||
// https://github.wdf.sap.corp/cap/matters/projects/44#card-196556
|
||||
S4bupa.on ('A_BusinessPartner.Changed', async msg => { //> would be great if we had batch events from S/4
|
||||
await new Promise( resolve => setTimeout( resolve, 1000 ));
|
||||
const tx = cds.db.tx(msg);
|
||||
let replicas = await tx.run(SELECT('ID').from (Suppliers) .where ('ID in', msg.data.businessPartners));
|
||||
await replicateTx(replicas.map(each => each.ID), undefined, tx, msg);
|
||||
await tx.commit();
|
||||
let replicas = await SELECT('ID').from (Suppliers) .where ('ID in', msg.data.businessPartners);
|
||||
await replicate(replicas.map(each => each.ID));
|
||||
})
|
||||
|
||||
/**
|
||||
@@ -67,19 +65,6 @@ module.exports = async()=>{ // called by server.js
|
||||
))
|
||||
}
|
||||
|
||||
async function replicateTx (IDs,_initial, tx, msg) {
|
||||
if (!Array.isArray(IDs)) IDs = [ IDs ]
|
||||
// TODO: Doesn't work when running in same process with mocked API_BUSINESS_PARTNER
|
||||
|
||||
// TODO: Issue
|
||||
let suppliers = await S4bupa.tx(msg).read (Suppliers).where(...([[]].concat(IDs).reduce( (where, id, index ) => { where.push(`${index>1 ? "OR ":""}ID = `, id); return where })));
|
||||
//let suppliers = await S4bupa.read (Suppliers).where('ID in',IDs)
|
||||
if (_initial) return tx.insert (suppliers) .into (Suppliers) //> using bulk insert
|
||||
else return Promise.all(suppliers.map ( //> parallelizing updates
|
||||
each => tx.update (Suppliers,each.ID) .with (each)
|
||||
))
|
||||
}
|
||||
|
||||
|
||||
// TODO: remove test code
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user