This commit is contained in:
D065023
2019-12-23 10:10:03 +01:00
parent 1f01bdf202
commit e16a343ce3
3 changed files with 104 additions and 85 deletions

View File

@@ -1,21 +1,27 @@
const cds = require('@sap/cds') const cds = require('@sap/cds')
// We are mashing up three services... // We are mashing up three services...
const admin = cds.connect.to ('AdminService') const admin = cds.connect.to('AdminService')
const bupa = cds.connect.to ('API_BUSINESS_PARTNER') const bupa = cds.connect.to('API_BUSINESS_PARTNER')
const db = cds.connect.to ('db') const db = cds.connect.to('db')
// Using reflected definitions from connected services/database // Using reflected definitions from connected services/database
const { Addresses: externalAddresses } = bupa.entities // projection on external addresses const { Addresses: externalAddresses } = bupa.entities // projection on external addresses
const { Books, Addresses } = db.entities // entities in local database const { Books, Addresses } = db.entities // entities in local database
console.log (Addresses._service.name, Addresses._service) console.log(Addresses._service.name, Addresses._service)
module.exports = (admin => { module.exports = admin => {
// Handler to delegate ValueHelp requests to S/4 backend, fetching current user's addresses from there // Handler to delegate ValueHelp requests to S/4 backend, fetching current user's addresses from there
// admin.on ('READ', 'usersAddresses', (req) => { // REVISIT: all requests go to auto-exposed Addresses // admin.on ('READ', 'usersAddresses', (req) => { // REVISIT: all requests go to auto-exposed Addresses
admin.on ('READ', 'Addresses', (req) => { admin.on('READ', 'Addresses', req => {
return bupa.tx(req) .run (SELECT.from (externalAddresses) .where ({ contact: req.user.id || 'anonymous' })) return bupa
.tx(req)
.run(
SELECT.from(externalAddresses).where({
contact: req.user.id || 'anonymous'
})
)
// return bupa.tx(req) .read (externalAddresses) .where ({ contact: req.user.id || 'anonymous' }) //> FIXME: doesn't work !?!? // return bupa.tx(req) .read (externalAddresses) .where ({ contact: req.user.id || 'anonymous' }) //> FIXME: doesn't work !?!?
// const { SELECT } = cds.ql(req) //> convenient alternative to bupa.transaction(req).run(SELECT...) // const { SELECT } = cds.ql(req) //> convenient alternative to bupa.transaction(req).run(SELECT...)
// return SELECT.from (externalAddresses) .where ({ contact: req.user.id || 'anonymous' }) // return SELECT.from (externalAddresses) .where ({ contact: req.user.id || 'anonymous' })
@@ -31,64 +37,64 @@ module.exports = (admin => {
// a.HouseNumber.as('houseNumber') // a.HouseNumber.as('houseNumber')
// }) .where ({ BusinessPartner: req.user.id }) // }) .where ({ BusinessPartner: req.user.id })
}) })
}) }
// Replicate chosen addresses from S/4 when filing orders. // Replicate chosen addresses from S/4 when filing orders.
admin.before ('PATCH', 'Orders', async (req) => { admin.before('PATCH', 'Orders', async req => {
// REVISIT: Investigate strange behavior when first assigning A then B ... together with Fiori colleagues // REVISIT: Investigate strange behavior when first assigning A then B ... together with Fiori colleagues
// const assigned = { ID: req.data.shippingAddress_ID, contact: req.data.shippingAddress_contact } // const assigned = { ID: req.data.shippingAddress_ID, contact: req.data.shippingAddress_contact }
const assigned = { ID: req.data.shippingAddress_ID, contact: req.user.id } const assigned = { ID: req.data.shippingAddress_ID, contact: req.user.id }
if (!assigned.ID) return //> something else if (!assigned.ID) return //> something else
// const { SELECT, INSERT, UPSERT } = cds.ql(req) //> convenient alternative to <srv>.transaction(req).run(SELECT...) // const { SELECT, INSERT, UPSERT } = cds.ql(req) //> convenient alternative to <srv>.transaction(req).run(SELECT...)
const local = db.transaction (req) const local = db.transaction(req)
const [replica] = await local.read (Addresses) .where (assigned) const [replica] = await local.read(Addresses).where(assigned)
if (replica) return //> already replicated if (replica) return //> already replicated
// const [address] = await bupa.read (externalAddresses) .where ({ //> FIXME -> das erzeugt ein super-komisches Verhalten, mit zweimal PATCH, etc. !!! // const [address] = await bupa.read (externalAddresses) .where ({ //> FIXME -> das erzeugt ein super-komisches Verhalten, mit zweimal PATCH, etc. !!!
// const [address] = await bupa.tx(req) .read (externalAddresses) .where ({ //> FIXME !!! // const [address] = await bupa.tx(req) .read (externalAddresses) .where ({ //> FIXME !!!
const [address] = await bupa.tx(req) .run (SELECT.from (externalAddresses) .where (assigned)) const [address] = await bupa
if (address) return local.create (Addresses) .entries (address) .tx(req)
.run(SELECT.from(externalAddresses).where(assigned))
if (address) return local.create(Addresses).entries(address)
// const address = await SELECT.one.from (externalAddresses) .where ({ //> TODO // const address = await SELECT.one.from (externalAddresses) .where ({ //> TODO
// FIXME: Zweimal INSERT -> constraint violation !! // FIXME: Zweimal INSERT -> constraint violation !!
// if (address) return db.tx(req).run (INSERT.into (Addresses) .entries (address)) // if (address) return db.tx(req).run (INSERT.into (Addresses) .entries (address))
// if (address) return UPSERT (Addresses) .entries (address) //> TODO // if (address) return UPSERT (Addresses) .entries (address) //> TODO
}) })
// Update local replicas when sources change in S/4. // Update local replicas when sources change in S/4.
bupa.on ('BusinessPartner/Changed', async (msg) => { bupa.on('BusinessPartner/Changed', async msg => {
console.log('>> received:', msg.data) console.log('>> received:', msg.data)
const BPID = msg.data.KEY[0].BUSINESSPARTNER // TODO: .KEY[0] >> revisit w/ Oliver const BPID = msg.data.KEY[0].BUSINESSPARTNER // TODO: .KEY[0] >> revisit w/ Oliver
const { SELECT, UPDATE } = cds.ql(msg) //> convenient alternative to <srv>.transaction(req).run(SELECT...) const { SELECT, UPDATE } = cds.ql(msg) //> convenient alternative to <srv>.transaction(req).run(SELECT...)
// fetch affected entries from local replicas // fetch affected entries from local replicas
const replicas = await SELECT.from (Addresses) .where ({contact:BPID}) const replicas = await SELECT.from(Addresses).where({ contact: BPID })
if (replicas.length === 0) return //> not affected if (replicas.length === 0) return //> not affected
// fetch changed data from S/4 -> might be less than local due to deletes // fetch changed data from S/4 -> might be less than local due to deletes
const changed = await SELECT.from (externalAddresses) .where ({ const changed = await SELECT.from(externalAddresses).where({
contact:BPID, ID: replicas.map(a => a.ID) // where in contact: BPID,
ID: replicas.map(a => a.ID) // where in
}) })
// update local replicas with changes from S/4 // update local replicas with changes from S/4
const local = db.transaction (msg) //> using that variant to benefit from bulk runs const local = db.transaction(msg) //> using that variant to benefit from bulk runs
return local.run (changed.map (a => UPDATE (Addresses,a.ID) .with (a) )) return local.run(changed.map(a => UPDATE(Addresses, a.ID).with(a)))
}) })
// Validate incoming orders and reduce books' stocks. // Validate incoming orders and reduce books' stocks.
admin.before ('CREATE', 'Orders', async (req) => { admin.before('CREATE', 'Orders', async req => {
const { Items } = req.data const { Items } = req.data
// validate input... // validate input...
if (!Items || Items.length === 0) if (!Items || Items.length === 0)
return req.reject ('Please order at least one item.') return req.reject('Please order at least one item.')
if (!req.data.shippingAddress_ID) return req.reject ( if (!req.data.shippingAddress_ID)
'Please enter a valid shipping address.', return req.reject(
'shippingAddress_ID' 'Please enter a valid shipping address.',
) 'shippingAddress_ID'
)
// TODO: future way of doing that: // TODO: future way of doing that:
// const {assert} = req // const {assert} = req
// assert ('Items') .check (items => items && items.length, 'Please enter at least one order item') // assert ('Items') .check (items => items && items.length, 'Please enter at least one order item')
@@ -96,13 +102,20 @@ admin.before ('CREATE', 'Orders', async (req) => {
// if (req.hasErrors) return // if (req.hasErrors) return
// reduce stock on ordered books... // reduce stock on ordered books...
const all = await db.tx(req) .run (Items.map (each => const all = await db.tx(req).run(
UPDATE (Books) .where ('ID =', each.book_ID) Items.map(each =>
.and ('stock >=', each.amount) UPDATE(Books)
.set ('stock -=', each.amount) .where('ID =', each.book_ID)
)) .and('stock >=', each.amount)
all.forEach ((affectedRows,i) => affectedRows > 0 || req.error (409, .set('stock -=', each.amount)
`${Items[i].amount} exceeds stock for book #${Items[i].book_ID}` )
)) )
all.forEach(
(affectedRows, i) =>
affectedRows > 0 ||
req.error(
409,
`${Items[i].amount} exceeds stock for book #${Items[i].book_ID}`
)
)
}) })

View File

@@ -1,82 +1,88 @@
const cds = require('@sap/cds') const cds = require('@sap/cds')
module.exports = cds.service.impl (async()=>{ module.exports = cds.service.impl(async () => {
// We are mashing up three services... // We are mashing up three services...
const bupa = await cds.connect.to ('API_BUSINESS_PARTNER') const bupa = await cds.connect.to('API_BUSINESS_PARTNER')
const admin = await cds.connect.to ('AdminService') const admin = await cds.connect.to('AdminService')
const db = await cds.connect.to ('db') const db = await cds.connect.to('db')
// Using reflected definitions from connected services/database // Using reflected definitions from connected services/database
const { Addresses: externalAddresses } = bupa.entities // projection on external addresses const { Addresses: externalAddresses } = bupa.entities // projection on external addresses
const { Books, Addresses } = db.entities // entities in local database const { Books, Addresses } = db.entities // entities in local database
// Delegate ValueHelp requests to S/4 backend, fetching current user's addresses from there // Delegate ValueHelp requests to S/4 backend, fetching current user's addresses from there
admin.on ('READ', 'Addresses', (req) => { admin.on('READ', 'Addresses', req => {
console.log ('Delegating to S/4 bupa service...') console.log('Delegating to S/4 bupa service...')
const UsersAddresses = SELECT.from (externalAddresses) .where ({ contact: req.user.id }) const UsersAddresses = SELECT.from(externalAddresses).where({
return bupa.tx(req) .run (UsersAddresses.where (req.query.SELECT.where)) contact: req.user.id
})
return bupa.tx(req).run(UsersAddresses.where(req.query.SELECT.where))
}) })
// Replicate chosen addresses from S/4 when filing orders. // Replicate chosen addresses from S/4 when filing orders.
admin.before ('PATCH', 'Orders', async (req) => { admin.before('PATCH', 'Orders', async req => {
const assigned = { ID: req.data.shippingAddress_ID, contact: req.user.id } const assigned = { ID: req.data.shippingAddress_ID, contact: req.user.id }
if (!assigned.ID) return //> something else if (!assigned.ID) return //> something else
const local = db.transaction (req) const local = db.transaction(req)
const [replica] = await local.read (Addresses) .where (assigned) const [replica] = await local.read(Addresses).where(assigned)
if (replica) return //> already replicated if (replica) return //> already replicated
const [address] = await bupa.tx(req) .run (SELECT.from (externalAddresses) .where (assigned)) const [address] = await bupa
if (address) return local.create (Addresses) .entries (address) .tx(req)
.run(SELECT.from(externalAddresses).where(assigned))
if (address) return local.create(Addresses).entries(address)
}) })
// Subscribe to S/4 event to update local replicas when sources change in S/4. // Subscribe to S/4 event to update local replicas when sources change in S/4.
bupa.on ('BusinessPartner/Changed', async (msg) => { bupa.on('BusinessPartner/Changed', async msg => {
console.log('>> received:', msg.data) console.log('>> received:', msg.data)
const BPID = msg.data.KEY[0].BUSINESSPARTNER // TODO: .KEY[0] >> revisit w/ Oliver const BPID = msg.data.KEY[0].BUSINESSPARTNER // TODO: .KEY[0] >> revisit w/ Oliver
const { SELECT, UPDATE } = cds.ql(msg) //> convenient alternative to <srv>.transaction(req).run(SELECT...) const { SELECT, UPDATE } = cds.ql(msg) //> convenient alternative to <srv>.transaction(req).run(SELECT...)
// fetch affected entries from local replicas // fetch affected entries from local replicas
const replicas = await SELECT.from (Addresses) .where ({contact:BPID}) const replicas = await SELECT.from(Addresses).where({ contact: BPID })
if (replicas.length === 0) return //> not affected if (replicas.length === 0) return //> not affected
// fetch changed data from S/4 -> might be less than local due to deletes // fetch changed data from S/4 -> might be less than local due to deletes
const changed = await SELECT.from (externalAddresses) .where ({ const changed = await SELECT.from(externalAddresses).where({
contact:BPID, ID: replicas.map(a => a.ID) // where in contact: BPID,
ID: replicas.map(a => a.ID) // where in
}) })
// update local replicas with changes from S/4 // update local replicas with changes from S/4
const local = db.transaction (msg) //> using that variant to benefit from bulk runs const local = db.transaction(msg) //> using that variant to benefit from bulk runs
return local.run (changed.map (a => UPDATE (Addresses,a.ID) .with (a) )) return local.run(changed.map(a => UPDATE(Addresses, a.ID).with(a)))
}) })
// Validate incoming orders and reduce books' stocks. // Validate incoming orders and reduce books' stocks.
admin.before ('CREATE', 'Orders', async (req) => { admin.before('CREATE', 'Orders', async req => {
const { Items } = req.data const { Items } = req.data
// validate input... // validate input...
if (!Items || Items.length === 0) if (!Items || Items.length === 0)
return req.reject ('Please order at least one item.') return req.reject('Please order at least one item.')
if (!req.data.shippingAddress_ID) return req.reject ( if (!req.data.shippingAddress_ID)
'Please enter a valid shipping address.', return req.reject(
'shippingAddress_ID' 'Please enter a valid shipping address.',
) 'shippingAddress_ID'
)
// reduce stock on ordered books... // reduce stock on ordered books...
const all = await db.tx(req) .run (Items.map (each => const all = await db.tx(req).run(
UPDATE (Books) .where ('ID =', each.book_ID) Items.map(each =>
.and ('stock >=', each.amount) UPDATE(Books)
.set ('stock -=', each.amount) .where('ID =', each.book_ID)
)) .and('stock >=', each.amount)
all.forEach ((affectedRows,i) => affectedRows > 0 || req.error (409, .set('stock -=', each.amount)
`${Items[i].amount} exceeds stock for book #${Items[i].book_ID}` )
)) )
all.forEach(
(affectedRows, i) =>
affectedRows > 0 ||
req.error(
409,
`${Items[i].amount} exceeds stock for book #${Items[i].book_ID}`
)
)
}) })
}) })
require('./utils') // ugly workaround for AppStudio require('./utils') // ugly workaround for AppStudio

View File

@@ -1,8 +1,8 @@
// Hack for SAP Application Studio // Hack for SAP Application Studio
process.env["http_proxy"] = "" process.env['http_proxy'] = ''
process.env["https_proxy"] = "" process.env['https_proxy'] = ''
process.env["HTTP_PROXY"] = "" process.env['HTTP_PROXY'] = ''
process.env["HTTPS_PROXY"] = "" process.env['HTTPS_PROXY'] = ''
const diff = (obj1, obj2) => const diff = (obj1, obj2) =>
Object.keys(obj1).reduce( Object.keys(obj1).reduce(