Fixes and improvements

This commit is contained in:
Uwe Klinger
2021-05-05 11:14:02 +02:00
parent 278258c436
commit c4bee1f09a
2 changed files with 6 additions and 20 deletions

View File

@@ -1,10 +1,11 @@
const cds = require('@sap/cds'); const cds = require('@sap/cds');
module.exports = cds.service.impl(async function (srv) { module.exports = cds.service.impl(function () {
const { A_BusinessPartner } = this.entities; 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}`); console.log(`>>> BusinessPartner updated ${data.BusinessPartner}`);
srv.emit("A_BusinessPartner.Changed", { businessPartners: [ data.BusinessPartner ] }); await this.emit("A_BusinessPartner.Changed", { businessPartners: [ data.BusinessPartner ] });
}); });
}); });

View File

@@ -43,10 +43,8 @@ module.exports = async()=>{ // called by server.js
// https://github.wdf.sap.corp/cap/matters/projects/44#card-196556 // 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 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 )); await new Promise( resolve => setTimeout( resolve, 1000 ));
const tx = cds.db.tx(msg); let replicas = await SELECT('ID').from (Suppliers) .where ('ID in', msg.data.businessPartners);
let replicas = await tx.run(SELECT('ID').from (Suppliers) .where ('ID in', msg.data.businessPartners)); await replicate(replicas.map(each => each.ID));
await replicateTx(replicas.map(each => each.ID), undefined, tx, msg);
await tx.commit();
}) })
/** /**
@@ -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 // TODO: remove test code
{ {