Patch of _reduceStock() under @sap/cds 4 (#136)
* Bump to cds 4 * Cosmetics in handler code * await and cds.run([...]) instead of Promise.all Co-authored-by: Christian Georgi <christian.georgi@sap.com>
This commit is contained in:
2
.npmrc
2
.npmrc
@@ -1 +1 @@
|
|||||||
@sap:registry=https://npm.sap.com
|
registry=https://registry.npmjs.org
|
||||||
850
package-lock.json
generated
850
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -11,7 +11,7 @@
|
|||||||
"bookshop": "cds watch packages/bookshop"
|
"bookshop": "cds watch packages/bookshop"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@sap/cds": "^3",
|
"@sap/cds": "^4",
|
||||||
"express": "^4"
|
"express": "^4"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"description": "",
|
"description": "",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@sap/approuter": "^6"
|
"@sap/approuter": "^8"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node node_modules/@sap/approuter/approuter.js"
|
"start": "node node_modules/@sap/approuter/approuter.js"
|
||||||
|
|||||||
@@ -4,10 +4,10 @@
|
|||||||
"description": "A simple bookshop application, build in a self-contained all-in-one fashion, i.e. w/o reusing other packages.",
|
"description": "A simple bookshop application, build in a self-contained all-in-one fashion, i.e. w/o reusing other packages.",
|
||||||
"license": "SAP SAMPLE CODE LICENSE",
|
"license": "SAP SAMPLE CODE LICENSE",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@sap/cds": "^3",
|
"@sap/cds": "^4",
|
||||||
"express": "^4",
|
"express": "^4",
|
||||||
"passport": "^0.4.1",
|
"hdb": "^0.18.1",
|
||||||
"hdb": "^0.17.1"
|
"passport": "^0.4.1"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "cds run --in-memory?",
|
"start": "cds run --in-memory?",
|
||||||
|
|||||||
@@ -14,16 +14,20 @@ function _addDiscount2 (each,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.transaction(req) .run (()=> OrderItems.map (order =>
|
if (!Array.isArray(orderItems)) return
|
||||||
UPDATE (Books) .set ('stock -=', order.amount)
|
|
||||||
.where ('ID =', order.book_ID) .and ('stock >=', order.amount)
|
const all = await cds.transaction(req).run(orderItems.map(item =>
|
||||||
)) .then (all => all.forEach ((affectedRows,i) => {
|
UPDATE(Books)
|
||||||
if (affectedRows === 0) req.error (409,
|
.set('stock -=', item.amount)
|
||||||
`${OrderItems[i].amount} exceeds stock for book #${OrderItems[i].book_ID}`
|
.where('ID =', item.book_ID).and('stock >=', item.amount)
|
||||||
)
|
))
|
||||||
}))
|
all.forEach((affectedRows, i) => {
|
||||||
|
if (affectedRows === 0) {
|
||||||
|
req.error(409, `${orderItems[i].amount} exceeds stock for book #${orderItems[i].book_ID}`)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return all
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user