Adjustments

This commit is contained in:
Uwe Klinger
2021-06-11 10:40:17 +02:00
parent 870f8d063d
commit 097b74db03
5 changed files with 24 additions and 94 deletions

View File

@@ -31,10 +31,11 @@ module.exports = async()=>{ // called by server.js
if (!replicated) await replicate (supplierId, 'initial');
};
if (supplierId)
return (await Promise.all ([ next(), replicateIfNotExists() ]))[0]
else
return next() //> don't forget to pass down the interceptor stack
if (supplierId) {
const [result, _] = await Promise.all([next(), replicateIfNotExists()]);
return result;
} else
return next(); //> don't forget to pass down the interceptor stack
})
})
@@ -42,46 +43,19 @@ module.exports = async()=>{ // called by server.js
// Subscribe to changes in the S4 origin of Suppliers data
S4bupa.on ('BusinessPartner.Changed', async msg => { //> would be great if we had batch events from S/4
console.log(">>", msg.event)
const id = msg.data.BusinessPartner;
let replica = await SELECT.one('ID').from (Suppliers) .where ('ID =', id);
if (replica) await replicate(id);
const ID = msg.data.BusinessPartner;
let replica = await SELECT.one('ID').from(Suppliers).where({ID});
if (replica) await replicate(ID);
})
/**
* Helper function to replicate Suppliers data.
* @param {string} a single ID
* @param {string} ID a single ID
* @param {truthy|falsy} _initial indicates whether an insert or an update is required
*/
async function replicate (id,_initial) {
// TODO: Doesn't work when running in same process with mocked API_BUSINESS_PARTNER
// 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 db.update(Suppliers,id) .with (suppliers[0]);
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});
}
// TODO: remove test code
{
// one server: returns AdminService.Suppliers
// two servers: returns API_BUSINESS_PARTNER.A_BusinessPartner
const tx = S4bupa.tx({});
let result = await tx.run(SELECT('*').from ('AdminService.Suppliers') .where ('ID =', 'ACME'));
tx.commit();
console.log(result);
}
// TODO: remove test code
{
// one server: returns AdminService.Suppliers
// two servers: returns AdminService.Suppliers
const tx = db.tx({});
let result = await db.run(SELECT('*').from ('AdminService.Suppliers') .where ('ID =', 'ACME'));
tx.commit();
console.log(result);
}
}