Adding suppliers showing integration with S/4

This commit is contained in:
Uwe Klinger
2021-04-16 11:28:55 +02:00
parent c1141e6c87
commit 5ae31f7c67
18 changed files with 5940 additions and 6 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,7 @@
BusinessPartner;BusinessPartnerFullName
ACME;A Company Making Everything
B4U;Books for You
S&C;Shakespeare & Co.
WSL;Waterstones
TLD;Thalia
PNG;Penguin Books
1 BusinessPartner BusinessPartnerFullName
2 ACME A Company Making Everything
3 B4U Books for You
4 S&C Shakespeare & Co.
5 WSL Waterstones
6 TLD Thalia
7 PNG Penguin Books

View File

@@ -0,0 +1,5 @@
ID;name
ACME;A Company Making Everything
B4U;Books for You
S&C;Shakespeare & Co.
WSL;Waterstones
1 ID name
2 ACME A Company Making Everything
3 B4U Books for You
4 S&C Shakespeare & Co.
5 WSL Waterstones

47
suppliers/srv/mashup.cds Normal file
View File

@@ -0,0 +1,47 @@
/*
Optionally add projections to external entities, to capture what
you actually want to use from there.
*/
using { API_BUSINESS_PARTNER as S4 } from './external/API_BUSINESS_PARTNER.csn';
extend service S4 with {
entity Suppliers as projection on S4.A_BusinessPartner {
key BusinessPartner as ID,
BusinessPartnerFullName as name,
}
}
/*
You can mashup entities from external services, or projections
thereof, with your project's own entities
*/
using { sap.capire.bookshop.Books } from '@capire/bookshop';
extend Books with {
supplier : Association to S4.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 S4.Suppliers;
}
/*
Optionally add a local persistence to keep replicas of external
entities to have data in fast access locally; much like a cache.
*/
annotate S4.Suppliers with @cds.persistence:{table,skip:false};
/**
Having locally cached replicas also allows us to display supplier
data in lists of books, which otherwise would generate unwanted
traffic on S4 backends.
*/
extend projection CatalogService.ListOfBooks with {
supplier
}

52
suppliers/srv/mashup.js Normal file
View File

@@ -0,0 +1,52 @@
////////////////////////////////////////////////////////////////////////////
//
// Mashing up provided and required services...
//
module.exports = async()=>{ // called by server.js
if (!cds.services.AdminService) return //> mocking S4 service only
// Connect to services we want to mashup below...
const S4bupa = await cds.connect.to('API_BUSINESS_PARTNER') //> external S4 service
const admin = await cds.connect.to('AdminService') //> local domain service
const db = await cds.connect.to('db') //> our primary database
// Reflect CDS definition of the Suppliers entity
const { Suppliers } = S4bupa.entities
admin.prepend (()=>{
// Delegate Value Help reads for Suppliers to S4 backend
admin.on ('READ', 'Suppliers', async req => {
console.log ('>> delegating to S4 service...')
return await S4bupa.run(req.query)
})
// Replicate Supplier data when Books are edited
admin.on (['CREATE','UPDATE'], 'Books', async (req,next) => {
let { supplier } = req.data
if (supplier) {
let cached = await db.exists (Suppliers, supplier)
if (!cached) await replicate (supplier,'initial')
}
return next()
})
})
// Subscribe to changes in the S4 origin of Suppliers data
S4bupa.on ('BusinessPartner/Changed', async msg => {
let cached = await SELECT('ID').from (Suppliers)
.where ('ID in', msg.businessPartner.KEYS)
for (let each of cached) replicate (each)
})
// Helper function to replicate Suppliers data
async function replicate (ID,_initial) {
let data = await S4bupa.read (Suppliers, ID)
if (_initial) return db.insert (data) .into (Suppliers)
else return db.update (Suppliers,ID) .with (data)
}
}