Add suppliers notes app
This commit is contained in:
2847
notes/srv/external/API_BUSINESS_PARTNER.csn
vendored
Normal file
2847
notes/srv/external/API_BUSINESS_PARTNER.csn
vendored
Normal file
File diff suppressed because it is too large
Load Diff
6614
notes/srv/external/API_BUSINESS_PARTNER.edmx
vendored
Normal file
6614
notes/srv/external/API_BUSINESS_PARTNER.edmx
vendored
Normal file
File diff suppressed because it is too large
Load Diff
3
notes/srv/external/data/API_BUSINESS_PARTNER-A_BusinessPartner.csv
vendored
Normal file
3
notes/srv/external/data/API_BUSINESS_PARTNER-A_BusinessPartner.csv
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
BusinessPartner;BusinessPartnerFullName;BusinessPartnerType
|
||||
11;Alice Wonder;CUSTOMER;
|
||||
9980000082;Hugo Hollandaise;CUSTOMER
|
||||
|
13
notes/srv/notes-service.cds
Normal file
13
notes/srv/notes-service.cds
Normal 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;
|
||||
}
|
||||
61
notes/srv/notes-service.js
Normal file
61
notes/srv/notes-service.js
Normal 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);
|
||||
});
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user