User mapping and refactoring
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
userID;businessPartnerID
|
||||
anonymous;1234567
|
||||
|
@@ -22,6 +22,11 @@ entity Authors : managed {
|
||||
books : Association to many Books on books.author = $self;
|
||||
}
|
||||
|
||||
entity UserMappings {
|
||||
key userID: String;
|
||||
businessPartnerID: String;
|
||||
}
|
||||
|
||||
entity Orders : cuid, managed {
|
||||
OrderNo : String @title:'Order Number'; //> readable key
|
||||
Items : Composition of many OrderItems on Items.parent = $self;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const cds = require('@sap/cds')
|
||||
const { Books, ShippingAddresses } = cds.entities
|
||||
const { Books, ShippingAddresses, UserMappings } = cds.entities
|
||||
|
||||
const bupaSrv = cds.connect.to('API_BUSINESS_PARTNER')
|
||||
|
||||
@@ -13,8 +13,11 @@ module.exports = cds.service.impl(function () {
|
||||
})
|
||||
|
||||
async function _readAddresses (req) {
|
||||
const businessPartnerID = await _getBusinessPartnerID(req)
|
||||
const tx = bupaSrv.transaction(req)
|
||||
const ql = SELECT.from('API_BUSINESS_PARTNER.A_BusinessPartnerAddress')
|
||||
const ql = SELECT.from('API_BUSINESS_PARTNER.A_BusinessPartnerAddress').where(
|
||||
{ BusinessPartner: businessPartnerID }
|
||||
)
|
||||
if (req.query.SELECT.columns) {
|
||||
ql.columns(req.query.SELECT.columns)
|
||||
} else {
|
||||
@@ -29,23 +32,35 @@ async function _readAddresses (req) {
|
||||
return result
|
||||
}
|
||||
|
||||
async function _getBusinessPartnerID (req) {
|
||||
const ownTx = cds.transaction(req)
|
||||
const { businessPartnerID } = await ownTx.run(
|
||||
SELECT.one(['businessPartnerID'])
|
||||
.from(UserMappings)
|
||||
.where({ userID: req.user.id })
|
||||
)
|
||||
return businessPartnerID
|
||||
}
|
||||
|
||||
/** Fill Address data from external service */
|
||||
async function _fillAddress (req) {
|
||||
console.log('retrieving addresses')
|
||||
if (req.data.shippingAddress_AddressID) {
|
||||
const businessPartnerID = await _getBusinessPartnerID(req)
|
||||
const tx = bupaSrv.transaction(req)
|
||||
const response = await tx.run(
|
||||
SELECT.from('API_BUSINESS_PARTNER.A_BusinessPartnerAddress')
|
||||
.columns('AddressID', 'CityName', 'StreetName', 'HouseNumber')
|
||||
.where({ AddressID: req.data.shippingAddress_AddressID })
|
||||
.where({
|
||||
AddressID: req.data.shippingAddress_AddressID,
|
||||
BusinessPartner: businessPartnerID
|
||||
})
|
||||
)
|
||||
if (response && response.length > 0) {
|
||||
console.log('filling addresses')
|
||||
const tx2 = cds.transaction(req)
|
||||
try {
|
||||
await tx2.run(
|
||||
INSERT.into('sap.capire.bookshop.ShippingAddresses').entries(response)
|
||||
)
|
||||
await tx2.run(INSERT.into(ShippingAddresses).entries(response))
|
||||
} catch (e) {
|
||||
// already in there
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
const cds = require('@sap/cds')
|
||||
const { Books, ShippingAddresses } = cds.entities
|
||||
|
||||
const bupaSrv = cds.connect.to('API_BUSINESS_PARTNER')
|
||||
const { Books } = cds.entities
|
||||
|
||||
/** Service implementation for CatalogService */
|
||||
module.exports = cds.service.impl(function () {
|
||||
@@ -11,47 +9,13 @@ module.exports = cds.service.impl(function () {
|
||||
each => each.stock > 111 && _addDiscount2(each, 11)
|
||||
)
|
||||
this.before('CREATE', 'Orders', _reduceStock)
|
||||
this.before('CREATE', 'Orders', _fillAddress)
|
||||
|
||||
this.on('READ', 'Addresses', _readAddresses)
|
||||
|
||||
// this.after('READ', 'Orders', (data, req) => {
|
||||
// if (req.query.SELECT.columns.includes('shippingAddress')) {
|
||||
// data.shippingAddress = _readAddresses()
|
||||
// }
|
||||
// })
|
||||
})
|
||||
|
||||
function _readAddresses (req) {
|
||||
// TODO: Delegate to external service
|
||||
return [
|
||||
{ AddressID: 'add1', CityName: 'Walldorf', StreetName: 'ExampleStreet' },
|
||||
{ AddressID: 'add2', CityName: 'Schwetzingen', StreetName: 'BestStreet' }
|
||||
]
|
||||
}
|
||||
|
||||
/** Add some discount for overstocked books */
|
||||
function _addDiscount2 (each, discount) {
|
||||
each.title += ` -- ${discount}% discount!`
|
||||
}
|
||||
|
||||
async function _fillAddress (req) {
|
||||
console.log('filling addresses')
|
||||
if (req.data.shippingAdress_AddressID) {
|
||||
const tx = bupaSrv.transaction(req)
|
||||
const result = tx.run(
|
||||
SELECT.one(['AdressID, CityName, StreetName, HouseNumber'])
|
||||
.from('A_BusinessPartnerAddress')
|
||||
.where({ AddressID: req.data.shippingAdress_AddressID })
|
||||
)
|
||||
if (result) {
|
||||
const tx2 = cds.transaction(req)
|
||||
console.log('filling addresses for real')
|
||||
tx2.run(INSERT.into(ShippingAddresses).entries([result]))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Reduce stock of ordered books if available stock suffices */
|
||||
async function _reduceStock (req) {
|
||||
const { Items: OrderItems } = req.data
|
||||
|
||||
@@ -6,8 +6,8 @@ module.exports = db => {
|
||||
DELETE.from(Addresses)
|
||||
INSERT.into(Addresses).entries(
|
||||
{
|
||||
BusinessPartner: 'anonymous',
|
||||
AddressID: '28238',
|
||||
BusinessPartner: '1234567',
|
||||
AddressID: '12345',
|
||||
CityName: 'Walldorf',
|
||||
StreetName: 'Dietmar-Hopp-Allee',
|
||||
HouseNumber: '123'
|
||||
|
||||
Reference in New Issue
Block a user