More handlers for demo

This commit is contained in:
Christian Georgi
2020-02-07 13:13:31 +00:00
parent bf21d0220c
commit 452b442246

View File

@@ -2,9 +2,10 @@ const cds = require('@sap/cds')
const { Books } = cds.entities
/** Service implementation for CatalogService */
module.exports = cds.service.impl(function() {
this.after ('READ', 'Books', each => each.stock > 111 && _addDiscount2(each,11))
this.before ('CREATE', 'Orders', _reduceStock)
module.exports = cds.service.impl(srv => {
srv.after ('READ', 'Books', each => each.stock > 111 && _addDiscount2(each,11))
srv.before ('CREATE', 'Orders', _reduceStock)
// srv.before ('*', (req) => { console.debug ('>>>', req.method, req.target && req.target.name) })
})
/** Add some discount for overstocked books */
@@ -15,6 +16,8 @@ function _addDiscount2 (each,discount) {
/** Reduce stock of ordered books if available stock suffices */
async function _reduceStock (req) {
const { Items: OrderItems } = req.data
// req.on('failed', () => {console.debug ('>>> failed for order', req.data.ID)})
return cds.transaction(req) .run (()=> OrderItems.map (order =>
UPDATE (Books) .set ('stock -=', order.amount)
.where ('ID =', order.book_ID) .and ('stock >=', order.amount)