async style
This commit is contained in:
@@ -12,14 +12,14 @@ module.exports = cds.service.impl(function () {
|
|||||||
})
|
})
|
||||||
|
|
||||||
/** Add some discount for overstocked books */
|
/** Add some discount for overstocked books */
|
||||||
function _addDiscount2 (each, discount) {
|
function _addDiscount2(each, discount) {
|
||||||
each.title += ` -- ${discount}% discount!`
|
each.title += ` -- ${discount}% discount!`
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Reduce stock of ordered books if available stock suffices */
|
/** Reduce stock of ordered books if available stock suffices */
|
||||||
async function _reduceStock (req) {
|
async function _reduceStock(req) {
|
||||||
const { Items: OrderItems } = req.data
|
const { Items: OrderItems } = req.data
|
||||||
return cds
|
const all = await cds
|
||||||
.transaction(req)
|
.transaction(req)
|
||||||
.run(() =>
|
.run(() =>
|
||||||
OrderItems.map(order =>
|
OrderItems.map(order =>
|
||||||
@@ -29,15 +29,13 @@ async function _reduceStock (req) {
|
|||||||
.and('stock >=', order.amount)
|
.and('stock >=', order.amount)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.then(all =>
|
all.forEach((affectedRows, i) => {
|
||||||
all.forEach((affectedRows, i) => {
|
if (affectedRows === 0)
|
||||||
if (affectedRows === 0)
|
req.error(
|
||||||
req.error(
|
409,
|
||||||
409,
|
`${OrderItems[i].amount} exceeds stock for book #${
|
||||||
`${OrderItems[i].amount} exceeds stock for book #${
|
OrderItems[i].book_ID
|
||||||
OrderItems[i].book_ID
|
}`
|
||||||
}`
|
)
|
||||||
)
|
})
|
||||||
})
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user