From 902afd8a760e4ea6b88fd57b40fafa706b2de705 Mon Sep 17 00:00:00 2001 From: Uwe Klinger Date: Wed, 5 May 2021 11:36:36 +0200 Subject: [PATCH] Change event to reflect the real event definition --- README.md | 24 ++++++++++++++--- .../srv/external/API_BUSINESS_PARTNER.js | 4 +-- suppliers/srv/mashup.cds | 6 ++--- suppliers/srv/mashup.js | 26 ++++++++----------- 4 files changed, 37 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index dce6bb91..42461ee1 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,25 @@ Copyright (c) 2021 SAP SE or an SAP affiliate company. All rights reserved. This ## 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 -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` \ No newline at end of file +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` + +## 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 \ No newline at end of file diff --git a/suppliers/srv/external/API_BUSINESS_PARTNER.js b/suppliers/srv/external/API_BUSINESS_PARTNER.js index b879e841..d770c612 100644 --- a/suppliers/srv/external/API_BUSINESS_PARTNER.js +++ b/suppliers/srv/external/API_BUSINESS_PARTNER.js @@ -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 }); }); }); diff --git a/suppliers/srv/mashup.cds b/suppliers/srv/mashup.cds index d1648bc7..c0bf86fe 100644 --- a/suppliers/srv/mashup.cds +++ b/suppliers/srv/mashup.cds @@ -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; } } \ No newline at end of file diff --git a/suppliers/srv/mashup.js b/suppliers/srv/mashup.js index 3b528ad3..3de8f285 100644 --- a/suppliers/srv/mashup.js +++ b/suppliers/srv/mashup.js @@ -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]); }