fixed keys
This commit is contained in:
@@ -75,6 +75,7 @@ annotate my.Authors with {
|
|||||||
|
|
||||||
annotate my.ShippingAddresses with {
|
annotate my.ShippingAddresses with {
|
||||||
AddressID @title:'{i18n>AddressID}';
|
AddressID @title:'{i18n>AddressID}';
|
||||||
|
BusinessPartner @UI.hidden;
|
||||||
CityName @title:'{i18n>CityName}';
|
CityName @title:'{i18n>CityName}';
|
||||||
StreetName @title:'{i18n>StreetName}';
|
StreetName @title:'{i18n>StreetName}';
|
||||||
PostalCode @title:'{i18n>PostalCode}';
|
PostalCode @title:'{i18n>PostalCode}';
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ annotate AdminService.Orders with {
|
|||||||
SearchSupported: 'true',
|
SearchSupported: 'true',
|
||||||
Parameters: [
|
Parameters: [
|
||||||
{ $Type: 'Common.ValueListParameterOut', LocalDataProperty: 'shippingAddress_AddressID', ValueListProperty: 'AddressID'},
|
{ $Type: 'Common.ValueListParameterOut', LocalDataProperty: 'shippingAddress_AddressID', ValueListProperty: 'AddressID'},
|
||||||
|
{ $Type: 'Common.ValueListParameterOut', LocalDataProperty: 'shippingAddress_BusinessPartner', ValueListProperty: 'BusinessPartner'},
|
||||||
{ $Type: 'Common.ValueListParameterDisplayOnly', ValueListProperty: 'PostalCode'},
|
{ $Type: 'Common.ValueListParameterDisplayOnly', ValueListProperty: 'PostalCode'},
|
||||||
{ $Type: 'Common.ValueListParameterDisplayOnly', ValueListProperty: 'CityName'},
|
{ $Type: 'Common.ValueListParameterDisplayOnly', ValueListProperty: 'CityName'},
|
||||||
{ $Type: 'Common.ValueListParameterDisplayOnly', ValueListProperty: 'StreetName'},
|
{ $Type: 'Common.ValueListParameterDisplayOnly', ValueListProperty: 'StreetName'},
|
||||||
@@ -40,10 +41,11 @@ annotate AdminService.Orders with {
|
|||||||
EffectTypes : #ValueChange,
|
EffectTypes : #ValueChange,
|
||||||
SourceProperties : [shippingAddress_AddressID],
|
SourceProperties : [shippingAddress_AddressID],
|
||||||
TargetProperties : [
|
TargetProperties : [
|
||||||
shippingAddress.HouseNumber,
|
shippingAddress.Country,
|
||||||
shippingAddress.StreetName,
|
shippingAddress.HouseNumber,
|
||||||
shippingAddress.CityName,
|
shippingAddress.StreetName,
|
||||||
shippingAddress.PostalCode
|
shippingAddress.CityName,
|
||||||
|
shippingAddress.PostalCode
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -82,7 +84,7 @@ annotate AdminService.Orders with @(
|
|||||||
{Value: createdBy, Label:'Customer'},
|
{Value: createdBy, Label:'Customer'},
|
||||||
{Value: createdAt, Label:'Date'},
|
{Value: createdAt, Label:'Date'},
|
||||||
{Value: OrderNo },
|
{Value: OrderNo },
|
||||||
{Value: 'shippingAddress_AddressID', Label: 'Address ID'}
|
{Value: 'shippingAddress', Label: 'Address ID'}
|
||||||
],
|
],
|
||||||
HeaderFacets: [
|
HeaderFacets: [
|
||||||
{$Type: 'UI.ReferenceFacet', Label: '{i18n>Created}', Target: '@UI.FieldGroup#Created'},
|
{$Type: 'UI.ReferenceFacet', Label: '{i18n>Created}', Target: '@UI.FieldGroup#Created'},
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
userID;businessPartnerID
|
userID;businessPartnerID
|
||||||
anonymous;1234567
|
anonymous;JOHNDOE
|
||||||
|
|||||||
|
@@ -22,11 +22,6 @@ 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;
|
||||||
@@ -42,8 +37,26 @@ entity OrderItems : cuid {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Use external information
|
// TODO: Use external information
|
||||||
entity ShippingAddresses {
|
// @cds.persistence.skip: false
|
||||||
|
// @cds.persistence.table
|
||||||
|
// entity ShippingAddresses as projection on extAddresses {
|
||||||
|
// key AddressID,
|
||||||
|
// key BusinessPartner,
|
||||||
|
// Country,
|
||||||
|
// CityName,
|
||||||
|
// PostalCode,
|
||||||
|
// StreetName,
|
||||||
|
// HouseNumber
|
||||||
|
// // key AddressID: String;
|
||||||
|
// // Country: String @readonly;
|
||||||
|
// // CityName: String @readonly;
|
||||||
|
// // PostalCode: String @readonly;
|
||||||
|
// // StreetName: String @readonly;
|
||||||
|
// // HouseNumber: String @readonly;
|
||||||
|
// }
|
||||||
|
entity ShippingAddresses {
|
||||||
key AddressID: String;
|
key AddressID: String;
|
||||||
|
key BusinessPartner: String;
|
||||||
Country: String @readonly;
|
Country: String @readonly;
|
||||||
CityName: String @readonly;
|
CityName: String @readonly;
|
||||||
PostalCode: String @readonly;
|
PostalCode: String @readonly;
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
"license": "SAP SAMPLE CODE LICENSE",
|
"license": "SAP SAMPLE CODE LICENSE",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@sap/cds": "latest",
|
"@sap/cds": "latest",
|
||||||
|
"@sap/xb-msg-amqp-v100": "^0.9.31-SNAPSHOT",
|
||||||
"express": "*",
|
"express": "*",
|
||||||
"sqlite3": "^4.1.0"
|
"sqlite3": "^4.1.0"
|
||||||
},
|
},
|
||||||
@@ -17,13 +18,8 @@
|
|||||||
"kind": "odata",
|
"kind": "odata",
|
||||||
"model": "srv/external/API_BUSINESS_PARTNER"
|
"model": "srv/external/API_BUSINESS_PARTNER"
|
||||||
},
|
},
|
||||||
"db": {
|
"messaging": {
|
||||||
"kind": "sqlite",
|
"kind": "enterprise-messaging"
|
||||||
"model": [
|
|
||||||
"db/",
|
|
||||||
"srv/",
|
|
||||||
"app/"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
const cds = require('@sap/cds')
|
const cds = require('@sap/cds')
|
||||||
const { Books, ShippingAddresses, UserMappings } = cds.entities
|
const { Books, ShippingAddresses, Orders } = cds.entities
|
||||||
const RELEVANT_ADDRESS_COLUMNS = [
|
const RELEVANT_ADDRESS_COLUMNS = [
|
||||||
'AddressID',
|
'AddressID',
|
||||||
'CityName',
|
'CityName',
|
||||||
@@ -10,6 +10,44 @@ const RELEVANT_ADDRESS_COLUMNS = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
const bupaSrv = cds.connect.to('API_BUSINESS_PARTNER')
|
const bupaSrv = cds.connect.to('API_BUSINESS_PARTNER')
|
||||||
|
const messagingSrv = cds.connect.to('messaging')
|
||||||
|
|
||||||
|
messagingSrv.on('sap/messaging/ccf/BO/BusinessPartner/Changed', async msg => {
|
||||||
|
console.log('>> MSG', msg.data)
|
||||||
|
const BusinessPartner = msg.data.KEY[0].BUSINESSPARTNER
|
||||||
|
// TODO: Remove toLower hack.
|
||||||
|
// Every BusinessPartner from S/4HANA is UPPERCASE.
|
||||||
|
const ownOrders = await cds.run(SELECT.from(Orders).where('createdBy like', BusinessPartner))
|
||||||
|
console.log(ownOrders)
|
||||||
|
// const ownAddresses = await cds.run(
|
||||||
|
// SELECT(['AddressID'])
|
||||||
|
// .from(ShippingAddresses)
|
||||||
|
// .where({ BusinessPartner: businessPartner })
|
||||||
|
// )
|
||||||
|
// if (ownAddresses && ownAddresses.length > 0) {
|
||||||
|
// console.log('found business partner', businessPartner)
|
||||||
|
// }
|
||||||
|
// const tx = bupaSrv.transaction()
|
||||||
|
// const remoteAddresses = await Promise.all(
|
||||||
|
// ownAddresses.map(addressResult => {
|
||||||
|
// return tx.run(
|
||||||
|
// SELECT.one.from('API_BUSINESS_PARTNER.A_BusinessPartnerAddress')
|
||||||
|
// .columns(RELEVANT_ADDRESS_COLUMNS)
|
||||||
|
// .where({
|
||||||
|
// AddressID: addressResult.AddressID,
|
||||||
|
// BusinessPartner: businessPartner
|
||||||
|
// })
|
||||||
|
// )
|
||||||
|
// })
|
||||||
|
// )
|
||||||
|
// console.log('addresses found:', remoteAddresses)
|
||||||
|
// await Promise.all(remoteAddresses.map(address => {
|
||||||
|
// console.log('updating', address)
|
||||||
|
// if (address) {
|
||||||
|
// return cds.run(UPDATE(ShippingAddresses).set(address))
|
||||||
|
// }
|
||||||
|
// }))
|
||||||
|
})
|
||||||
|
|
||||||
/** Service implementation for CatalogService */
|
/** Service implementation for CatalogService */
|
||||||
module.exports = cds.service.impl(function () {
|
module.exports = cds.service.impl(function () {
|
||||||
@@ -19,10 +57,10 @@ module.exports = cds.service.impl(function () {
|
|||||||
})
|
})
|
||||||
|
|
||||||
async function _readAddresses (req) {
|
async function _readAddresses (req) {
|
||||||
const businessPartnerID = await _getBusinessPartnerID(req)
|
const BusinessPartner = req.user.id
|
||||||
const tx = bupaSrv.transaction(req)
|
const tx = bupaSrv.transaction(req)
|
||||||
const ql = SELECT.from('API_BUSINESS_PARTNER.A_BusinessPartnerAddress').where(
|
const ql = SELECT.from('API_BUSINESS_PARTNER.A_BusinessPartnerAddress').where(
|
||||||
{ BusinessPartner: businessPartnerID }
|
{ BusinessPartner }
|
||||||
)
|
)
|
||||||
if (req.query && req.query.SELECT && req.query.SELECT.columns) {
|
if (req.query && req.query.SELECT && req.query.SELECT.columns) {
|
||||||
ql.columns(req.query.SELECT.columns)
|
ql.columns(req.query.SELECT.columns)
|
||||||
@@ -37,35 +75,28 @@ 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) {
|
||||||
if (req.data.shippingAddress_AddressID) {
|
if (req.data.shippingAddress_AddressID) {
|
||||||
const businessPartnerID = await _getBusinessPartnerID(req)
|
const BusinessPartner = req.user.id
|
||||||
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(RELEVANT_ADDRESS_COLUMNS)
|
.columns(RELEVANT_ADDRESS_COLUMNS)
|
||||||
.where({
|
.where({
|
||||||
AddressID: req.data.shippingAddress_AddressID,
|
AddressID: req.data.shippingAddress_AddressID,
|
||||||
BusinessPartner: businessPartnerID
|
BusinessPartner
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
if (response && response.length > 0) {
|
if (response && response.length > 0) {
|
||||||
|
response[0].BusinessPartner = BusinessPartner
|
||||||
|
console.log('to be inserted: ', response)
|
||||||
const tx2 = cds.transaction(req)
|
const tx2 = cds.transaction(req)
|
||||||
try {
|
try {
|
||||||
await tx2.run(INSERT.into(ShippingAddresses).entries(response))
|
await tx2.run(INSERT.into(ShippingAddresses).entries(response))
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// already in there
|
// already in there
|
||||||
|
console.log(e)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
req.error('Shipping address not found.')
|
req.error('Shipping address not found.')
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ module.exports = db => {
|
|||||||
DELETE.from(Addresses)
|
DELETE.from(Addresses)
|
||||||
INSERT.into(Addresses).entries(
|
INSERT.into(Addresses).entries(
|
||||||
{
|
{
|
||||||
BusinessPartner: '1234567',
|
BusinessPartner: 'anonymous',
|
||||||
AddressID: '11111',
|
AddressID: '62640',
|
||||||
CityName: 'Walldorf',
|
CityName: 'Walldorf',
|
||||||
PostalCode: '69190',
|
PostalCode: '69190',
|
||||||
Country: 'Germany',
|
Country: 'Germany',
|
||||||
@@ -15,7 +15,7 @@ module.exports = db => {
|
|||||||
HouseNumber: '16'
|
HouseNumber: '16'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
BusinessPartner: '1234567',
|
BusinessPartner: 'anonymous',
|
||||||
AddressID: '22222',
|
AddressID: '22222',
|
||||||
CityName: 'St. Leon-Rot',
|
CityName: 'St. Leon-Rot',
|
||||||
PostalCode: '68789',
|
PostalCode: '68789',
|
||||||
|
|||||||
Reference in New Issue
Block a user