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