Compare commits
7 Commits
openSAP-we
...
openSAP-we
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7b9ee0f827 | ||
|
|
b75a47c6ef | ||
|
|
8055848430 | ||
|
|
a6e9fac097 | ||
|
|
e33fb7df0b | ||
|
|
e2aac07054 | ||
|
|
e6ab102512 |
@@ -11,7 +11,7 @@ In SAP Business Application Studio, open a terminal.
|
|||||||
Then clone the repo with this specific branch:
|
Then clone the repo with this specific branch:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
git clone https://github.com/sap-samples/cloud-cap-samples projects/cloud-cap-samples -b openSAP-week4-unit1-final
|
git clone https://github.com/sap-samples/cloud-cap-samples projects/cloud-cap-samples -b openSAP-week4-unit3-final
|
||||||
cd projects/cloud-cap-samples
|
cd projects/cloud-cap-samples
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -35,8 +35,8 @@ entity Authors : managed {
|
|||||||
|
|
||||||
entity Orders : cuid, managed {
|
entity Orders : cuid, managed {
|
||||||
OrderNo : String @title : 'Order Number'; //> readable key
|
OrderNo : String @title : 'Order Number'; //> readable key
|
||||||
status : Status default 'processing';
|
status : Status default 'processing';
|
||||||
Items : Composition of many OrderItems
|
Items : Composition of many OrderItems
|
||||||
on Items.parent = $self;
|
on Items.parent = $self;
|
||||||
total : Decimal(9, 2)@readonly;
|
total : Decimal(9, 2)@readonly;
|
||||||
currency : Currency;
|
currency : Currency;
|
||||||
|
|||||||
@@ -18,7 +18,14 @@
|
|||||||
"requires": {
|
"requires": {
|
||||||
"API_BUSINESS_PARTNER": {
|
"API_BUSINESS_PARTNER": {
|
||||||
"kind": "odata",
|
"kind": "odata",
|
||||||
"model": "srv/external/API_BUSINESS_PARTNER"
|
"model": "srv/external/API_BUSINESS_PARTNER",
|
||||||
|
"--credentials": {
|
||||||
|
"prefix": "sap/S4HANAOD/c098/BO",
|
||||||
|
"destination": "cap-api098"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"--messaging": {
|
||||||
|
"kind": "enterprise-messaging"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
6
packages/bookshop/req.http
Normal file
6
packages/bookshop/req.http
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
PATCH http://localhost:4004/api-business-partner/A_BusinessPartner('ALICE')
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"BusinessPartnerIsBlocked": true
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using { sap.capire.bookshop as my } from '../db/schema';
|
using { sap.capire.bookshop as my } from '../db/schema';
|
||||||
|
using { API_BUSINESS_PARTNER as external } from './external/API_BUSINESS_PARTNER.csn';
|
||||||
|
|
||||||
@path:'/browse'
|
@path:'/browse'
|
||||||
service CatalogService {
|
service CatalogService {
|
||||||
@@ -7,6 +8,18 @@ service CatalogService {
|
|||||||
author.name as author
|
author.name as author
|
||||||
} excluding { createdBy, modifiedBy };
|
} excluding { createdBy, modifiedBy };
|
||||||
|
|
||||||
|
@readonly entity BusinessPartners as projection on external.A_BusinessPartner {
|
||||||
|
key BusinessPartner as ID,
|
||||||
|
FirstName,
|
||||||
|
MiddleName,
|
||||||
|
LastName,
|
||||||
|
BusinessPartnerIsBlocked
|
||||||
|
};
|
||||||
|
|
||||||
|
event OrderBlocked {
|
||||||
|
ID: UUID;
|
||||||
|
};
|
||||||
|
|
||||||
@requires_: 'authenticated-user'
|
@requires_: 'authenticated-user'
|
||||||
@insertonly entity Orders as projection on my.Orders;
|
@insertonly entity Orders as projection on my.Orders;
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,25 @@
|
|||||||
const cds = require('@sap/cds')
|
const cds = require('@sap/cds')
|
||||||
|
|
||||||
/** Service implementation for CatalogService */
|
/** Service implementation for CatalogService */
|
||||||
module.exports = cds.service.impl(function () {
|
module.exports = cds.service.impl(async function () {
|
||||||
const { Books, Orders } = this.entities
|
const { Books, Orders, BusinessPartners } = this.entities
|
||||||
|
const bupaSrv = await cds.connect.to('API_BUSINESS_PARTNER')
|
||||||
this.after('READ', Books, each => each.stock > 111 && _addDiscount2(each, 11))
|
this.after('READ', Books, each => each.stock > 111 && _addDiscount2(each, 11))
|
||||||
this.before('CREATE', Orders, _reduceStock)
|
this.before('CREATE', Orders, _reduceStock)
|
||||||
|
this.on('READ', BusinessPartners, req => bupaSrv.tx(req).run(req.query))
|
||||||
|
|
||||||
|
/** Block orders if business partner is blocked */
|
||||||
|
bupaSrv.on('BusinessPartner/Changed', async msg => {
|
||||||
|
console.log('>> Received BusinessPartner/Changed', msg.data)
|
||||||
|
const BUSINESSPARTNER = msg.data.KEY[0].BUSINESSPARTNER
|
||||||
|
const tx = cds.tx(msg)
|
||||||
|
const orders = await tx.run(SELECT('ID').from(Orders).where({ createdBy: BUSINESSPARTNER, status: 'processing' }))
|
||||||
|
if (!orders.length) return
|
||||||
|
const businessPartner = await bupaSrv.tx(msg).run(SELECT.one('BusinessPartnerIsBlocked').from(BusinessPartners).where({ ID: BUSINESSPARTNER }))
|
||||||
|
if (!businessPartner || !businessPartner.BusinessPartnerIsBlocked) return
|
||||||
|
await Promise.all(orders.map(order => tx.run(UPDATE(Orders).where(order).set({ status: 'blocked' }))))
|
||||||
|
orders.forEach(order => this.emit('OrderBlocked', order) && console.log('>> Emitted OrderBlocked', order))
|
||||||
|
})
|
||||||
|
|
||||||
/** Add some discount for overstocked books */
|
/** Add some discount for overstocked books */
|
||||||
function _addDiscount2(each, discount) {
|
function _addDiscount2(each, discount) {
|
||||||
|
|||||||
9
packages/bookshop/srv/external/API_BUSINESS_PARTNER.js
vendored
Normal file
9
packages/bookshop/srv/external/API_BUSINESS_PARTNER.js
vendored
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
module.exports = srv => {
|
||||||
|
srv.on(['CREATE', 'UPDATE', 'DELETE'], req => {
|
||||||
|
const payload = {
|
||||||
|
KEY: [{ BUSINESSPARTNER: req.data.BusinessPartner }]
|
||||||
|
}
|
||||||
|
srv.emit('BusinessPartner/Changed', payload)
|
||||||
|
console.log('<< Emitted BusinessPartner/Changed', payload)
|
||||||
|
})
|
||||||
|
}
|
||||||
4
packages/bookshop/srv/external/data/API_BUSINESS_PARTNER-A_BusinessPartner.csv
vendored
Normal file
4
packages/bookshop/srv/external/data/API_BUSINESS_PARTNER-A_BusinessPartner.csv
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
BusinessPartner;FirstName;MiddleName;LastName;BusinessPartnerIsBlocked
|
||||||
|
ALICE;Alice;In;Wonderland;false
|
||||||
|
BOB;Bob;The;Builder;false
|
||||||
|
JABBA;Jabba;The;Hutt;true
|
||||||
|
Reference in New Issue
Block a user