Change event to reflect the real event definition

This commit is contained in:
Uwe Klinger
2021-05-05 11:36:36 +02:00
parent c4bee1f09a
commit 902afd8a76
4 changed files with 37 additions and 23 deletions

View File

@@ -3,9 +3,9 @@ const cds = require('@sap/cds');
module.exports = cds.service.impl(function () {
const { A_BusinessPartner } = this.entities;
// TODO: Take over the original S/4 event definition
// https://api.sap.com/event/SAPS4HANACloudBusinessEvents_BusinessPartner/resource
this.after('UPDATE', A_BusinessPartner, async data => {
console.log(`>>> BusinessPartner updated ${data.BusinessPartner}`);
await this.emit("A_BusinessPartner.Changed", { businessPartners: [ data.BusinessPartner ] });
await this.emit("BusinessPartner.Changed", { BusinessPartner: data.BusinessPartner });
});
});

View File

@@ -69,8 +69,8 @@ extend projection CatalogService.ListOfBooks with {
// Extend S4 service with not modelled event
extend service S4 {
@topic: 'BusinessPartners/Changed'
event A_BusinessPartner.Changed {
BusinessPartners: array of S4.A_BusinessPartner:BusinessPartner;
@topic: 'BusinessPartner/Changed'
event BusinessPartner.Changed {
BusinessPartner: S4.A_BusinessPartner:BusinessPartner;
}
}

View File

@@ -38,31 +38,27 @@ module.exports = async()=>{ // called by server.js
})
// Subscribe to changes in the S4 origin of Suppliers data
// REVISIT: cds context is still from the UPDATE method when running in same programm, but should
// be a separate
// 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 ('BusinessPartner.Changed', async msg => { //> would be great if we had batch events from S/4
await new Promise( resolve => setTimeout( resolve, 1000 ));
let replicas = await SELECT('ID').from (Suppliers) .where ('ID in', msg.data.businessPartners);
await replicate(replicas.map(each => each.ID));
const id = msg.data.BusinessPartner;
let replica = await SELECT.one('ID').from (Suppliers) .where ('ID =', id);
if (replica) await replicate(id);
})
/**
* Helper function to replicate Suppliers data.
* @param {string|string[]} IDs a single ID or an array of IDs
* @param {string} a single ID
* @param {truthy|falsy} _initial indicates whether an insert or an update is required
*/
async function replicate (IDs,_initial) {
if (!Array.isArray(IDs)) IDs = [ IDs ]
async function replicate (id,_initial) {
// TODO: Doesn't work when running in same process with mocked API_BUSINESS_PARTNER
// TODO: Issue
let suppliers = await S4bupa.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)
// TODO: Doesn't work because fields are not mapped back!
//let supplier = await S4bupa.run(SELECT.one('*').from(Suppliers).where('ID =',id));
let suppliers = await S4bupa.read(Suppliers).where('ID =',id);
if (_initial) return db.insert (suppliers) .into (Suppliers) //> using bulk insert
else return Promise.all(suppliers.map ( //> parallelizing updates
each => db.update (Suppliers,each.ID) .with (each)
))
else return db.update(Suppliers,id) .with (suppliers[0]);
}