-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement example how SAP Cloud SDK filter can be combined
- Loading branch information
1 parent
180160b
commit ac058ce
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using {API_BUSINESS_PARTNER as external} from './external/API_BUSINESS_PARTNER'; | ||
|
||
service BusinessPartnerService { | ||
|
||
entity BusinessPartner as projection on external.A_BusinessPartner; | ||
|
||
function getAllBusinessPartners() returns String; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const cds = require("@sap/cds"); | ||
const LOG = cds.log("bp-service"); | ||
|
||
const { | ||
BusinessPartner: sdkBusinessPartner, | ||
BusinessPartnerAddress: sdkBusinessPartnerAddress, | ||
batch: bpBatch, | ||
} = require("./odata-client/business-partner-service"); | ||
const { or, and } = require("@sap-cloud-sdk/core"); | ||
|
||
module.exports = async function () { | ||
this.on("READ", "BusinessPartner", async (req) => { | ||
const bpService = await cds.connect.to("API_BUSINESS_PARTNER"); | ||
return bpService.run(req.query); | ||
}); | ||
|
||
this.on("getAllBusinessPartners", async (req) => { | ||
LOG._info && LOG.info("getAllBusinessPartners"); | ||
// Mock filter data | ||
const bps = ["203", "1018"]; | ||
const countries = ["DE", "US"]; | ||
|
||
// Create SAP Cloud SDK Filter for BusinessPartner | ||
const bpFilter = bps.map((bp) => | ||
sdkBusinessPartnerAddress.BUSINESS_PARTNER.equals(bp) | ||
); | ||
const filter = [or(...bpFilter)]; | ||
|
||
// Create SAP Cloud SDK Filter for Country | ||
if (countries.length > 0) { | ||
const countryFilter = countries.map((country) => | ||
sdkBusinessPartnerAddress.COUNTRY.equals(country) | ||
); | ||
filter.push(or(...countryFilter)); | ||
} | ||
LOG._debug && LOG.debug("filter", JSON.stringify(filter)); | ||
const queryBuilderBPAddress = sdkBusinessPartnerAddress | ||
.requestBuilder() | ||
.getAll() | ||
.select( | ||
sdkBusinessPartnerAddress.BUSINESS_PARTNER, | ||
sdkBusinessPartnerAddress.COUNTRY, | ||
sdkBusinessPartnerAddress.FULL_NAME | ||
) | ||
// TODO: Add filter filterBusinessPartnerAddress | ||
.filter(and(filter)); | ||
const batchResponsesBPAddress = await bpBatch(queryBuilderBPAddress).execute({ | ||
destinationName: "APIBusinessHub", | ||
}); | ||
return batchResponsesBPAddress; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
### | ||
GET http://localhost:4004/odata/v4/business-partner | ||
### | ||
GET http://localhost:4004/odata/v4/business-partner/BusinessPartner | ||
?$top=10 | ||
&$select=BusinessPartner,BusinessPartnerFullName | ||
### | ||
GET http://localhost:4004/odata/v4/business-partner/getAllBusinessPartners() |