Change event to reflect the real event definition
This commit is contained in:
22
README.md
22
README.md
@@ -95,7 +95,25 @@ Copyright (c) 2021 SAP SE or an SAP affiliate company. All rights reserved. This
|
|||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
1. Run: `PORT=5001 cds mock API_BUSINESS_PARTNER`
|
1. Run:
|
||||||
|
|
||||||
|
```
|
||||||
|
cds mock API_BUSINESS_PARTNER -p 5001
|
||||||
|
```
|
||||||
|
|
||||||
2. Wait until startup is completed
|
2. Wait until startup is completed
|
||||||
3. Run in a 2nd terminal: `cds serve all --with-mocks --in-memory`
|
3. Run in a 2nd terminal:
|
||||||
|
|
||||||
|
```
|
||||||
|
cds serve all --with-mocks --in-memory
|
||||||
|
```
|
||||||
|
|
||||||
4. Now, you can issues the requests listed in `suppliers/requests.http`
|
4. Now, you can issues the requests listed in `suppliers/requests.http`
|
||||||
|
|
||||||
|
## Request Sequence
|
||||||
|
|
||||||
|
* TODO
|
||||||
|
## URLs
|
||||||
|
|
||||||
|
* Get books with their replicated supplier: http://localhost:4004/browse/Books?$expand=supplier
|
||||||
|
* Get remote suppliers: http://localhost:4004/admin/Suppliers?$top=11
|
||||||
@@ -3,9 +3,9 @@ const cds = require('@sap/cds');
|
|||||||
module.exports = cds.service.impl(function () {
|
module.exports = cds.service.impl(function () {
|
||||||
const { A_BusinessPartner } = this.entities;
|
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 => {
|
this.after('UPDATE', A_BusinessPartner, async data => {
|
||||||
console.log(`>>> BusinessPartner updated ${data.BusinessPartner}`);
|
console.log(`>>> BusinessPartner updated ${data.BusinessPartner}`);
|
||||||
await this.emit("A_BusinessPartner.Changed", { businessPartners: [ data.BusinessPartner ] });
|
await this.emit("BusinessPartner.Changed", { BusinessPartner: data.BusinessPartner });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -69,8 +69,8 @@ extend projection CatalogService.ListOfBooks with {
|
|||||||
|
|
||||||
// Extend S4 service with not modelled event
|
// Extend S4 service with not modelled event
|
||||||
extend service S4 {
|
extend service S4 {
|
||||||
@topic: 'BusinessPartners/Changed'
|
@topic: 'BusinessPartner/Changed'
|
||||||
event A_BusinessPartner.Changed {
|
event BusinessPartner.Changed {
|
||||||
BusinessPartners: array of S4.A_BusinessPartner:BusinessPartner;
|
BusinessPartner: S4.A_BusinessPartner:BusinessPartner;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -38,31 +38,27 @@ module.exports = async()=>{ // called by server.js
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Subscribe to changes in the S4 origin of Suppliers data
|
// 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
|
S4bupa.on ('BusinessPartner.Changed', async msg => { //> would be great if we had batch events from S/4
|
||||||
// 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
|
|
||||||
await new Promise( resolve => setTimeout( resolve, 1000 ));
|
await new Promise( resolve => setTimeout( resolve, 1000 ));
|
||||||
let replicas = await SELECT('ID').from (Suppliers) .where ('ID in', msg.data.businessPartners);
|
const id = msg.data.BusinessPartner;
|
||||||
await replicate(replicas.map(each => each.ID));
|
let replica = await SELECT.one('ID').from (Suppliers) .where ('ID =', id);
|
||||||
|
if (replica) await replicate(id);
|
||||||
})
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper function to replicate Suppliers data.
|
* 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
|
* @param {truthy|falsy} _initial indicates whether an insert or an update is required
|
||||||
*/
|
*/
|
||||||
async function replicate (IDs,_initial) {
|
async function replicate (id,_initial) {
|
||||||
if (!Array.isArray(IDs)) IDs = [ IDs ]
|
|
||||||
// TODO: Doesn't work when running in same process with mocked API_BUSINESS_PARTNER
|
// TODO: Doesn't work when running in same process with mocked API_BUSINESS_PARTNER
|
||||||
|
|
||||||
// TODO: Issue
|
// TODO: Doesn't work because fields are not mapped back!
|
||||||
let suppliers = await S4bupa.read (Suppliers).where(...([[]].concat(IDs).reduce( (where, id, index ) => { where.push(`${index>1 ? "OR ":""}ID = `, id); return where })));
|
//let supplier = await S4bupa.run(SELECT.one('*').from(Suppliers).where('ID =',id));
|
||||||
//let suppliers = await S4bupa.read (Suppliers).where('ID in',IDs)
|
|
||||||
|
let suppliers = await S4bupa.read(Suppliers).where('ID =',id);
|
||||||
if (_initial) return db.insert (suppliers) .into (Suppliers) //> using bulk insert
|
if (_initial) return db.insert (suppliers) .into (Suppliers) //> using bulk insert
|
||||||
else return Promise.all(suppliers.map ( //> parallelizing updates
|
else return db.update(Suppliers,id) .with (suppliers[0]);
|
||||||
each => db.update (Suppliers,each.ID) .with (each)
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user