Add suppliers notes app

This commit is contained in:
Uwe Klinger
2021-05-11 09:52:54 +02:00
parent d04cb801c4
commit 016587094f
20 changed files with 10168 additions and 1 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,3 @@
BusinessPartner;BusinessPartnerFullName;BusinessPartnerType
11;Alice Wonder;CUSTOMER;
9980000082;Hugo Hollandaise;CUSTOMER
1 BusinessPartner;BusinessPartnerFullName;BusinessPartnerType
2 11;Alice Wonder;CUSTOMER;
3 9980000082;Hugo Hollandaise;CUSTOMER

View File

@@ -0,0 +1,13 @@
using sap.capire.notes from '../db/data-model';
using { Suppliers as MashupSuppliers } from '../db/mashup';
using { API_BUSINESS_PARTNER as BusinessPartner } from './external/API_BUSINESS_PARTNER.csn';
/**
* Notes Service
*
* Maintain notes for suppliers
*/
service NotesService {
entity Notes as projection on notes.Notes;
entity Suppliers as projection on MashupSuppliers;
}

View File

@@ -0,0 +1,61 @@
const cds = require("@sap/cds");
const s4apiKey = process.env.S4_APIKEY;
if (!s4apiKey && cds.env.profiles.indexOf("sandbox") >= 0) {
console.error(
"[ERROR] Provide API Key in env var S4_APIKEY for S/4 Sandbox: https://api.sap.com/api/API_BUSINESS_PARTNER/resource -> Show API Key"
);
process.exit(1);
}
module.exports = cds.service.impl(async function () {
const { Notes, Suppliers } = this.entities;
const bpService = await cds.connect.to("API_BUSINESS_PARTNER");
// REVISIT: This is a workaround for the missing capability to add headers to the service
const bpServiceDelegate = {
run: query => bpService.send({ query, headers: { APIKey: s4apiKey } })
};
// Suppliers?$expand=notes
this.on("READ", Suppliers, async (req, next) => {
const expandIndex = req.query.SELECT.columns.findIndex(
({ expand, ref }) => expand && ref[0] === "notes"
);
if (expandIndex < 0) return next();
req.query.SELECT.columns.splice(expandIndex, 1);
if (!req.query.SELECT.columns.find(
column => column.ref.find((ref) => ref == "ID"))
)
req.query.SELECT.columns.push({ ref: ["ID"] });
const suppliers = await next();
// Request all associated notes
const supplierIds = suppliers.map((supplier) => supplier.ID);
const notes = await this.run(SELECT.from(Notes).where({ supplier_ID: supplierIds }));
// Convert in a map for easier lookup
const notesForSuppliers = {};
for (const note of notes) {
if (!notesForSuppliers[note.supplier_ID]) notesForSuppliers[note.supplier_ID] = [];
notesForSuppliers[note.supplier_ID].push(note);
}
// Add notes to result
for (const supplier of suppliers) {
const notesForSupplier = notesForSuppliers[supplier.ID];
if (notesForSupplier) supplier.notes = notesForSupplier;
}
return suppliers;
});
this.on("READ", Suppliers, async req => {
return bpServiceDelegate.run(req.query);
});
});