Adding suppliers showing integration with S/4
This commit is contained in:
2425
suppliers/srv/external/API_BUSINESS_PARTNER.csn
vendored
Normal file
2425
suppliers/srv/external/API_BUSINESS_PARTNER.csn
vendored
Normal file
File diff suppressed because it is too large
Load Diff
3261
suppliers/srv/external/API_BUSINESS_PARTNER.edmx
vendored
Normal file
3261
suppliers/srv/external/API_BUSINESS_PARTNER.edmx
vendored
Normal file
File diff suppressed because it is too large
Load Diff
7
suppliers/srv/external/data/API_BUSINESS_PARTNER-A_BusinessPartner.csv
vendored
Normal file
7
suppliers/srv/external/data/API_BUSINESS_PARTNER-A_BusinessPartner.csv
vendored
Normal 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
|
||||
|
5
suppliers/srv/external/data/API_BUSINESS_PARTNER-Suppliers.csv
vendored
Normal file
5
suppliers/srv/external/data/API_BUSINESS_PARTNER-Suppliers.csv
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
ID;name
|
||||
ACME;A Company Making Everything
|
||||
B4U;Books for You
|
||||
S&C;Shakespeare & Co.
|
||||
WSL;Waterstones
|
||||
|
47
suppliers/srv/mashup.cds
Normal file
47
suppliers/srv/mashup.cds
Normal 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
52
suppliers/srv/mashup.js
Normal 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)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user