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

@@ -16,10 +16,10 @@ extend Books with {
} }
// //
// Extend Orders with Books as articles // Extend Orders with Books as Products
// //
using { sap.capire.orders.OrderItems } from '@capire/orders'; using { sap.capire.orders.OrderItems } from '@capire/orders';
extend OrderItems with { extend OrderItems with {
book : Association to Books on article = book.ID book : Association to Books on product.ID = book.ID
} }

View File

@@ -31,7 +31,7 @@ module.exports = async()=>{ // called by server.js
const { title, price } = await db.tx(msg).read (Books, book, b => { b.title, b.price }) const { title, price } = await db.tx(msg).read (Books, book, b => { b.title, b.price })
return OrdersService.tx(msg).create ('Orders').entries({ return OrdersService.tx(msg).create ('Orders').entries({
OrderNo: 'Order at '+ (new Date).toLocaleString(), OrderNo: 'Order at '+ (new Date).toLocaleString(),
Items: [{ article:`${book}`, title, price, amount }], Items: [{ product:{ID:`${book}`}, title, price, amount }],
buyer, createdBy: buyer buyer, createdBy: buyer
}) })
}) })
@@ -51,8 +51,8 @@ module.exports = async()=>{ // called by server.js
// //
OrdersService.on ('OrderChanged', async (msg) => { OrdersService.on ('OrderChanged', async (msg) => {
console.debug ('> received:', msg.event, msg.data) console.debug ('> received:', msg.event, msg.data)
const { article, deltaAmount } = msg.data const { product, deltaAmount } = msg.data
return UPDATE (Books) .where ('ID =', article) return UPDATE (Books) .where ('ID =', product)
.and ('stock >=', deltaAmount) .and ('stock >=', deltaAmount)
.set ('stock -=', deltaAmount) .set ('stock -=', deltaAmount)
}) })

View File

@@ -71,14 +71,14 @@ annotate OrdersService.Orders with @(
annotate OrderItems with @( annotate OrderItems with @(
UI: { UI: {
LineItem: [ LineItem: [
{Value: article, Label:'Article ID'}, {Value: product_ID, Label:'Product ID'},
{Value: title, Label:'Article Title'}, {Value: title, Label:'Product Title'},
{Value: price, Label:'Unit Price'}, {Value: price, Label:'Unit Price'},
{Value: amount, Label:'Quantity'}, {Value: amount, Label:'Quantity'},
], ],
Identification: [ //Is the main field group Identification: [ //Is the main field group
{Value: amount, Label:'Amount'}, {Value: amount, Label:'Amount'},
{Value: title, Label:'Article'}, {Value: title, Label:'Product'},
{Value: price, Label:'Unit Price'}, {Value: price, Label:'Unit Price'},
], ],
Facets: [ Facets: [

View File

@@ -1,4 +1,4 @@
ID;order_ID;amount;article;title;price ID;order_ID;amount;product_ID;title;price
58040e66-1dcd-4ffb-ab10-fdce32028b79;7e2f2640-6866-4dcf-8f4d-3027aa831cad;1;201;Wuthering Heights;11.11 58040e66-1dcd-4ffb-ab10-fdce32028b79;7e2f2640-6866-4dcf-8f4d-3027aa831cad;1;201;Wuthering Heights;11.11
64e718c9-ff99-47f1-8ca3-950c850777d4;7e2f2640-6866-4dcf-8f4d-3027aa831cad;1;271;Catweazle;15 64e718c9-ff99-47f1-8ca3-950c850777d4;7e2f2640-6866-4dcf-8f4d-3027aa831cad;1;271;Catweazle;15
e9641166-e050-4261-bfee-d1e797e6cb7f;64e718c9-ff99-47f1-8ca3-950c850777d4;2;252;Eleonora;28 e9641166-e050-4261-bfee-d1e797e6cb7f;64e718c9-ff99-47f1-8ca3-950c850777d4;2;252;Eleonora;28
1 ID order_ID amount article product_ID title price
2 58040e66-1dcd-4ffb-ab10-fdce32028b79 7e2f2640-6866-4dcf-8f4d-3027aa831cad 1 201 201 Wuthering Heights 11.11
3 64e718c9-ff99-47f1-8ca3-950c850777d4 7e2f2640-6866-4dcf-8f4d-3027aa831cad 1 271 271 Catweazle 15
4 e9641166-e050-4261-bfee-d1e797e6cb7f 64e718c9-ff99-47f1-8ca3-950c850777d4 2 252 252 Eleonora 28

View File

@@ -12,8 +12,15 @@ entity Orders : cuid, managed {
entity OrderItems { entity OrderItems {
key ID : UUID; key ID : UUID;
order : Association to Orders; order : Association to Orders;
@assert.integrity:false // REVISIT: this is a temporary workaround for a glitch in cds-runtime
product : Association to Products;
amount : Integer; amount : Integer;
article : String; //> to allow for arbitrary keys
title : String; title : String;
price : Double; price : Double;
} }
/** This is a stand-in for arbitrary ordered Products */
@cds.persistence.skip:'always'
entity Products {
key ID : String;
}

View File

@@ -7,30 +7,30 @@ class OrdersService extends cds.ApplicationService {
this.before ('UPDATE', 'Orders', async function(req) { this.before ('UPDATE', 'Orders', async function(req) {
const { ID, Items } = req.data 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 ( 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) { this.before ('DELETE', 'Orders', async function(req) {
const { ID } = req.data const { ID } = req.data
const Items = await cds.tx(req).run ( 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() return super.init()
} }
/** order changed -> broadcast event */ /** order changed -> broadcast event */
orderChanged (article, deltaAmount) { orderChanged (product, deltaAmount) {
// Emit events to inform subscribers about changes in orders // Emit events to inform subscribers about changes in orders
console.log ('> emitting:', 'OrderChanged', { article, deltaAmount }) console.log ('> emitting:', 'OrderChanged', { product, deltaAmount })
return this.emit ('OrderChanged', { article, deltaAmount }) return this.emit ('OrderChanged', { product, deltaAmount })
} }
} }