From 6d3e9a211ab453f040ec1506972423ee7fe22de4 Mon Sep 17 00:00:00 2001 From: D065023 Date: Tue, 28 Jan 2020 16:04:32 +0100 Subject: [PATCH] rm bug --- packages/bookshop/srv/cat-service.js | 36 ++++++++++++++-------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/packages/bookshop/srv/cat-service.js b/packages/bookshop/srv/cat-service.js index f01501f6..173b6a0b 100644 --- a/packages/bookshop/srv/cat-service.js +++ b/packages/bookshop/srv/cat-service.js @@ -13,22 +13,22 @@ module.exports = cds.service.impl(async function () { const orders = await cds.tx(msg).run(SELECT.from(Orders).where({ createdBy: BUSINESSPARTNER })) console.log(orders) }) + + /** Add some discount for overstocked books */ + function _addDiscount2(each, discount) { + each.title += ` -- ${discount}% discount!` + } + + /** Reduce stock of ordered books if available stock suffices */ + async function _reduceStock(req) { + const { Items: OrderItems } = req.data + return cds.transaction(req).run(() => OrderItems.map(order => + UPDATE(Books).set('stock -=', order.amount) + .where('ID =', order.book_ID).and('stock >=', order.amount) + )).then(all => all.forEach((affectedRows, i) => { + if (affectedRows === 0) req.error(409, + `${OrderItems[i].amount} exceeds stock for book #${OrderItems[i].book_ID}` + ) + })) + } }) - -/** Add some discount for overstocked books */ -function _addDiscount2(each, discount) { - each.title += ` -- ${discount}% discount!` -} - -/** Reduce stock of ordered books if available stock suffices */ -async function _reduceStock(req) { - const { Items: OrderItems } = req.data - return cds.transaction(req).run(() => OrderItems.map(order => - UPDATE(Books).set('stock -=', order.amount) - .where('ID =', order.book_ID).and('stock >=', order.amount) - )).then(all => all.forEach((affectedRows, i) => { - if (affectedRows === 0) req.error(409, - `${OrderItems[i].amount} exceeds stock for book #${OrderItems[i].book_ID}` - ) - })) -}