diff --git a/suppliers/app/services.cds b/suppliers/app/services.cds index 595023e9..92fc127e 100644 --- a/suppliers/app/services.cds +++ b/suppliers/app/services.cds @@ -3,7 +3,7 @@ */ using from './admin/fiori-service'; -using from './browse/fiori-service'; +//using from './browse/fiori-service'; using from './common'; using from '@capire/common'; diff --git a/suppliers/srv/external/data/API_BUSINESS_PARTNER-Suppliers.csv b/suppliers/srv/external/data/API_BUSINESS_PARTNER-Suppliers.csv deleted file mode 100644 index cbb3ca2e..00000000 --- a/suppliers/srv/external/data/API_BUSINESS_PARTNER-Suppliers.csv +++ /dev/null @@ -1,5 +0,0 @@ -ID;name -ACME;A Company Making Everything -B4U;Books for You -S&C;Shakespeare & Co. -WSL;Waterstones diff --git a/suppliers/srv/external/data/sap.capire.bookshop-Suppliers.csv b/suppliers/srv/external/data/sap.capire.bookshop-Suppliers.csv index 1096eb4a..69c08d69 100644 --- a/suppliers/srv/external/data/sap.capire.bookshop-Suppliers.csv +++ b/suppliers/srv/external/data/sap.capire.bookshop-Suppliers.csv @@ -1,6 +1,3 @@ ID;name -ACME;A Company Making Everything (local) -B4U;Books for You -S&C;Shakespeare & Co. -WSL;Waterstones +ACME;A Company Making Everything (replicated) diff --git a/suppliers/srv/mashup.cds b/suppliers/srv/mashup.cds index a6e28b0e..5ca632ce 100644 --- a/suppliers/srv/mashup.cds +++ b/suppliers/srv/mashup.cds @@ -10,44 +10,18 @@ using { API_BUSINESS_PARTNER as S4 } from './external/API_BUSINESS_PARTNER.csn'; @cds.persistence: {table,skip:false} // add persistency @readonly entity sap.capire.bookshop.Suppliers as projection on S4.A_BusinessPartner { - // TODO: Aliases not supported in Java, yet? key BusinessPartner as ID, - BusinessPartnerFullName as name, - - // REVISIT: following is not supported so far in cds compiler... - // to_BusinessPartnerAddress as city { - // CityCode as code, - // CityName as name - // } - - // to_BusinessPartnerAddress - - // REVISIT: following is not supported so far in cqn2odata... - // to_BusinessPartnerAddress.CityCode as city, - // to_BusinessPartnerAddress.CityName as city_name, - - //// REVISIT: Should this be here or in the service, when it is only used for Fiori? - //// Compositions should work as well - //// The version with "virtual" is prefered, as this makes clear that the association is "added" here - // virtual books: Association to Books on book.supplier = $self, - // books2: Association to Books on book.supplier = $self, - //// Add virtual field, that does'nt exisits in the persistence or the underlying service - // virtual saveEnabled: Boolean -} excluding { - OrganizationBPName1, OrganizationBPName2,OrganizationBPName3, OrganizationBPName4, to_BuPaIdentification, to_BuPaIndustry, to_BusinessPartnerAddress, to_BusinessPartnerBank, to_BusinessPartnerContact, to_BusinessPartnerRole, to_BusinessPartnerTax, to_Customer, to_Supplier + BusinessPartnerFullName as name } - -// REVISIT: Alternative idea to use a specific replication view, but request data from -// a different view and manual map values. -// entity ReplicatedSuppliers as projection on Suppliers { -// ID, -// name, -// to_BusinessPartnerAddress.CityCode as city, -// to_BusinessPartnerAddress.CityName as city_name -// } - - +/* + You can also expose external entities through your own services + For this to work, you need to delegate the respective calls + addressed to your services into calls to the external service. + */ +extend service AdminService with { + entity Suppliers as projection on bookshop.Suppliers; +} /* @@ -59,16 +33,6 @@ extend Books with { supplier : Association to bookshop.Suppliers; } - -/* - You can also expose external entities through your own services - For this to work, you need to delegate the respective calls - addressed to your services into calls to the external service. - */ -extend service AdminService with { - entity Suppliers as projection on bookshop.Suppliers; -} - /** Having locally cached replicas also allows us to display supplier data in lists of books, which otherwise would generate unwanted diff --git a/suppliers/srv/mashup.js b/suppliers/srv/mashup.js index 981634ac..f7aae6dd 100644 --- a/suppliers/srv/mashup.js +++ b/suppliers/srv/mashup.js @@ -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); - } - }