UNPKG

@sap/low-code-event-handler

Version:

The generic event handlers for LCAP

92 lines (82 loc) 3.4 kB
const cds = require('@sap/cds'); const ExternalServiceHandler = require('./external/ExternalServiceHandler'); const AppLogicHandler = require('./app-logic/AppLogicHandler'); class LCAPApplicationService extends cds.ApplicationService { /* * Overload init() to register own handlers to be invoked first in the respective phase * note: before and after handlers are invoked in parallel! */ async init() { Object.entries(this.model.definitions).forEach(([name, definition]) => { this.dispatchEntityHandler(name, definition); }); // Ensure to call the ApplicationService's init which registers the generic handlers super.init(); } async dispatchEntityHandler(name, definition) { if (name.startsWith(`${this.name}.`)) { switch (definition.kind) { case 'entity': this.dispatchEntityEventHandler(name, definition); break; case 'action': case 'function': this.dispatchActionHandler(name); break; default: // console.debug(definition); break; } } } async dispatchEntityEventHandler(entityName, entityDefinition) { const entityEvents = [ 'CREATE', 'READ', 'UPDATE', 'DELETE' ]; this.before(entityEvents, entityName, this.beforeEventHandler); this.on(entityEvents, entityName, this.onEventHandler); this.after(entityEvents, entityName, this.afterEventHandler); if (entityDefinition.actions) { // entity bound action / function for (const entityAction in entityDefinition.actions) { this.dispatchEntityActionHandler(entityName, entityAction); } } } async dispatchEntityActionHandler(entityName, actionName) { this.before(actionName, entityName, this.beforeEventHandler); this.on(actionName, entityName, this.onEventHandler); this.after(actionName, entityName, this.afterEventHandler); } async dispatchActionHandler(actionName) { if (actionName.indexOf('.') > 0) { // cap invoke action via short name instead of full name, which is different from entity // e.g. it is 'find' not 'lcapAppLogic.find' actionName = actionName.split('.')[1]; } this.before(actionName, this.beforeEventHandler); this.on(actionName, this.onEventHandler); this.after(actionName, this.afterEventHandler); } async beforeEventHandler(request) { return AppLogicHandler.beforeEventHandler(this, request); } async onEventHandler(request, next) { const appLogicResults = await AppLogicHandler.onEventHandler(this, request, next); if ((request.event === 'CREATE' || request.event === 'UPDATE' || request.event === 'DELETE')&& ExternalServiceHandler.isExternalEntity(this, request.target)) { return ExternalServiceHandler.onEntityCUDEvent(this, request.target, request, next); } else if (request.event === 'READ' && ExternalServiceHandler.isExternalEntity(this, request.target)) { return ExternalServiceHandler.onEntityReadEvent(this, request.target, request, next); } else { if (request && request.target) { // it's entity request return next(); } else { // it's action / function request return appLogicResults; } } } async afterEventHandler(results, request) { return AppLogicHandler.afterEventHandler(this, results, request); } } module.exports = LCAPApplicationService;