From 2f1af693c9fb80d0d482fc910b8c071d5612f7c7 Mon Sep 17 00:00:00 2001 From: nkaputnik Date: Thu, 28 Jul 2022 15:29:53 +0200 Subject: [PATCH] Finalize ToDo's --- bookshop/handlers/AdminService.Authors.CREATE.js | 1 - bookshop/srv/admin-service.js | 11 ++++++++--- ...Service-Authors-CREATE.cds => code-extensions.cds} | 10 ++++++++-- 3 files changed, 16 insertions(+), 6 deletions(-) rename bookshop/srv/{AdminService-Authors-CREATE.cds => code-extensions.cds} (81%) diff --git a/bookshop/handlers/AdminService.Authors.CREATE.js b/bookshop/handlers/AdminService.Authors.CREATE.js index a9912881..a676988f 100644 --- a/bookshop/handlers/AdminService.Authors.CREATE.js +++ b/bookshop/handlers/AdminService.Authors.CREATE.js @@ -1,4 +1,3 @@ - async function run() { //debugger //while (true) {} diff --git a/bookshop/srv/admin-service.js b/bookshop/srv/admin-service.js index d6c0675f..4c149969 100644 --- a/bookshop/srv/admin-service.js +++ b/bookshop/srv/admin-service.js @@ -33,6 +33,8 @@ class AdminService extends cds.ApplicationService { this.on("*", async (req, next) => { if (!(req.target === undefined || req.target == null)) return next() //ToDo: check whether action or event is part of an extension + // DO NOT OVERWRITE EXISTING Action Implementations! + // evaluate: Can we augment action implementation with super.next? if (req.constructor.name === "EventMessage") { const code = getCode(req.event, "ON") if (code) { @@ -63,6 +65,8 @@ var counter = 1; function newLabel() {return "VM2 - req: " + counter++} +//should only work in local exection (cds watch) +// alternative: Upon Bootstrapping, merge files into CSN function getCodeFromFile(name, operation) { const filename = name + "." + operation + ".js" const file = path.join(__dirname, "..", "handlers", filename) @@ -74,6 +78,7 @@ function getCodeFromFile(name, operation) { } } +//after push this should be the only thing that works function getCodeFromAnnotation(name, operation) { return "" } @@ -96,9 +101,9 @@ async function executeCode(code, req, result, output) { console: "inherit", timeout: 500, allowAsync: true, - sandbox: { req, - result, - output, + sandbox: { req, //todo: isolate req.data, req.reject, req.error, req.message + result, //important for READ + output, //used for Action Implementation SELECT : (class extends require('@sap/cds/lib/ql/SELECT') {then(r,e) {return srv.run(this).then(r,e)}})._api(), INSERT : (class extends require('@sap/cds/lib/ql/INSERT') {then(r,e) {return srv.run(this).then(r,e)}})._api(), UPDATE : (class extends require('@sap/cds/lib/ql/UPDATE') {then(r,e) {return srv.run(this).then(r,e)}})._api(), diff --git a/bookshop/srv/AdminService-Authors-CREATE.cds b/bookshop/srv/code-extensions.cds similarity index 81% rename from bookshop/srv/AdminService-Authors-CREATE.cds rename to bookshop/srv/code-extensions.cds index a33b9751..86ba8028 100644 --- a/bookshop/srv/AdminService-Authors-CREATE.cds +++ b/bookshop/srv/code-extensions.cds @@ -1,5 +1,11 @@ -using from '..\admin-service'; +//this file is machine-created during cds.build +namespace sap.capire.bookshop; //> important for reflection +using from '../db/schema'; +using from '../srv/cat-service'; +using from '../srv/admin-service'; annotate AdminService.Authors with @extension.logic: [{when: 'CREATE', code: 'async function run() {\r\n \/\/debugger\r\n \/\/while (true) {}\r\n \/\/process.exit()\r\n \/\/1.substring()\r\n \/\/ let res = await specialselect\r\n let res = await SELECT.one`title`.from(`Books`).where(`ID=201`)\r\n let { title } = res\r\n let Author = req.data\r\n Author.modifiedBy = \"Custom Event handler changed this!\"\r\n Author.placeOfDeath = \" --- Somewhere over \" + title + \" --- create in Sandbox\"\r\n \/\/await this.emit(\"createdAuthor\", { Author })\r\n return Author\r\n}\r\nrun()\r\n'}, {when: 'READ', code: 'function getYear(v) {\r\n return parseInt(v.substr(0, 4))\r\n}\r\nfunction getMonth(v) {\r\n return parseInt(v.substr(5, 2))\r\n}\r\nfunction getDay(v) {\r\n return parseInt(v.substr(8, 2))\r\n}\r\n\r\nfunction getAge(from, to) {\r\n if (from === undefined || from == null) return 0\r\n if (to === undefined || to == null) to = new Date().toISOString()\r\n let year = getYear(to) - getYear(from) - 1\r\n if (\r\n getMonth(to) > getMonth(from) ||\r\n (getMonth(to) === getMonth(from) && getDay(to) >= getDay(from))\r\n ) {\r\n year++\r\n }\r\n return year\r\n}\r\n\r\nconst result_ = Array.isArray(result) ? result : [result]\r\nfor (const row of result_) {\r\n row.modifiedBy += \" --- read in sandbox\"\r\n row.age = getAge(row.dateOfBirth, row.dateOfDeath)\r\n}'} -]; \ No newline at end of file +]; +annotate AdminService.Books with @extension.logic; +annotate CatalogService.ListOfBooks with @extension.logic; \ No newline at end of file