Change event to reflect the real event definition
This commit is contained in:
@@ -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]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user