Using fake Products entity in @capire/orders

This commit is contained in:
Daniel
2020-11-20 12:33:18 +01:00
committed by Daniel Hutzel
parent 932f56812c
commit 8f01bf911e
6 changed files with 25 additions and 18 deletions

View File

@@ -7,30 +7,30 @@ class OrdersService extends cds.ApplicationService {
this.before ('UPDATE', 'Orders', async function(req) {
const { ID, Items } = req.data
if (Items) for (let { article, amount } of Items) {
if (Items) for (let { product_ID, amount } of Items) {
const { amount:before } = await cds.tx(req).run (
SELECT.one.from (OrderItems, oi => oi.amount) .where ({order_ID:ID, article})
SELECT.one.from (OrderItems, oi => oi.amount) .where ({order_ID:ID, product_ID})
)
if (amount != before) this.orderChanged (article, amount-before)
if (amount != before) this.orderChanged (product_ID, amount-before)
}
})
this.before ('DELETE', 'Orders', async function(req) {
const { ID } = req.data
const Items = await cds.tx(req).run (
SELECT.from (OrderItems, oi => { oi.article, oi.amount }) .where ({order_ID:ID})
SELECT.from (OrderItems, oi => { oi.product_ID, oi.amount }) .where ({order_ID:ID})
)
if (Items) for (let it of Items) this.orderChanged (it.article, -it.amount)
if (Items) for (let it of Items) this.orderChanged (it.product_ID, -it.amount)
})
return super.init()
}
/** order changed -> broadcast event */
orderChanged (article, deltaAmount) {
orderChanged (product, deltaAmount) {
// Emit events to inform subscribers about changes in orders
console.log ('> emitting:', 'OrderChanged', { article, deltaAmount })
return this.emit ('OrderChanged', { article, deltaAmount })
console.log ('> emitting:', 'OrderChanged', { product, deltaAmount })
return this.emit ('OrderChanged', { product, deltaAmount })
}
}