UNPKG

@5minds/processcube_engine

Version:

The ProcessCube Engine. Stores and executes BPMNs.

218 lines • 12.2 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.IntermediateSignalThrowEventHandler = void 0; const dayjs_1 = __importDefault(require("dayjs")); const inversify_1 = require("inversify"); const lodash_clonedeep_1 = __importDefault(require("lodash.clonedeep")); const processcube_engine_sdk_1 = require("@5minds/processcube_engine_sdk"); const index_1 = require("../../../Contracts/index"); const index_2 = require("../../../Tools/index"); const ProcessInstance_1 = require("../../ProcessInstance"); const SignalEventService_1 = require("../../SignalEventService"); const FlowNodeHandlerFactory_1 = require("../FlowNodeHandlerFactory"); const EventHandler_1 = require("./EventHandler"); let IntermediateSignalThrowEventHandler = class IntermediateSignalThrowEventHandler extends EventHandler_1.EventHandler { customCorrelationId; signalEventService; constructor(eventMiddlewareHandler, flowNodeHandlerFactory, flowNodeInstanceDatabaseAdapter, intermediateSingalThrowEventHandler, processInstance, signalEventService) { super(eventMiddlewareHandler, flowNodeHandlerFactory, flowNodeInstanceDatabaseAdapter, intermediateSingalThrowEventHandler, processInstance, 'signal_throw_event_handler'); this.signalEventService = signalEventService; } get signalThrowEvent() { return this.flowNode; } async persistOnExit() { await super.persistOnExit({ type: index_1.FlowNodeInstanceDataTypes.throwEvent, eventName: this.signalThrowEvent.signalEventDefinition.name, customCorrelationId: this.customCorrelationId, }); this.publishIntermediateThrowEventTriggeredNotification(); } async persistOnError(error) { await super.persistOnError(error, { type: index_1.FlowNodeInstanceDataTypes.throwEvent, eventName: this.signalThrowEvent.signalEventDefinition?.name, customCorrelationId: this.customCorrelationId, }); this.publishIntermediateThrowEventTriggeredNotification(); } async persistOnTerminate() { await super.persistOnTerminate({ type: index_1.FlowNodeInstanceDataTypes.throwEvent, eventName: this.signalThrowEvent.signalEventDefinition.name, customCorrelationId: this.customCorrelationId, }); } async startExecution() { this.logger.debug(`Executing SignalThrowEvent instance.`); return this.executeHandler(); } async executeHandler() { try { this.ensureHasClaim(); this.validateSignal(); this.processToken = await this.getTokenPayloadFromInputValues(); const signalName = this.signalThrowEvent.signalEventDefinition.name; await this.evaluateCustomCorrelationId(); const signal = { signalReference: signalName, correlationId: this.processInstance.getCorrelationId(), processDefinitionId: this.processInstance.getProcessDefinitionId(), processModelId: this.processInstance.getProcessModelId(), embeddedProcessModelId: this.processInstance.getEmbeddedProcessModelId(), processModelName: this.processInstance.getProcessModelName(), processInstanceId: this.processInstance.getProcessInstanceId(), parentProcessInstanceId: this.processInstance.getParentProcessInstance()?.getProcessInstanceId(), flowNodeId: this.signalThrowEvent.id, flowNodeName: this.signalThrowEvent.name, flowNodeType: this.signalThrowEvent.bpmnType, flowNodeLane: this.processInstance.getProcessModelFacade().getLaneForFlowNode(this.signalThrowEvent.id)?.name, flowNodeInstanceId: this.flowNodeInstanceId, identity: this.processInstance.getOwner(), currentToken: this.processToken, eventName: this.signalThrowEvent.signalEventDefinition.name, eventType: this.flowNode.eventType, customCorrelationId: this.customCorrelationId, signalChannel: this.signalThrowEvent.signalChannel, }; const pushSignalToInternalChannel = signal.signalChannel === undefined || signal.signalChannel === 'internal'; if (pushSignalToInternalChannel) { this.logger.debug(`SignalThrowEvent instance now sending signal ${signal.signalReference} to internal channel...`); await this.signalEventService.throwSignal(signal, true); } else { this.logger.debug(`SignalThrowEvent instance now sending signal ${signal.signalReference} to external channel ${signal.signalChannel}...`); } const now = dayjs_1.default.utc().toDate(); const currentFlowNodeMetadata = processcube_engine_sdk_1.FlowNodeViewModelFactory.getViewModel(this.signalThrowEvent, { processModelId: signal.processModelId, processModelName: signal.processModelName, }); this.logger.trace(`Posting Signal Intermediate Throw Event (ID: ${this.flowNode.id}) "OnSignalTriggered" to Event Middleware: ${signal.signalReference} to external channel ${signal.signalChannel}`); await this.postEventToEventMiddlewares((0, lodash_clonedeep_1.default)({ eventType: processcube_engine_sdk_1.EngineEventType.OnSignalTriggered, timestamp: now, processModelId: signal.processModelId, processModelName: signal.processModelName, processDefinitionId: signal.processDefinitionId, processDefinitionHash: this.processInstance.getProcessDefinitionHash(), flowNodeId: signal.flowNodeId, flowNodeName: signal.flowNodeName, flowNodeType: signal.flowNodeType, flowNodeLane: signal.flowNodeLane, flowNodeInstanceId: this.flowNodeInstanceId, processInstanceId: signal.processInstanceId, parentProcessInstanceId: signal.parentProcessInstanceId, correlationId: signal.correlationId, currentFlowNode: currentFlowNodeMetadata, logLevel: processcube_engine_sdk_1.LogLevel.debug, tokenPayload: this.processToken, signalChannel: signal.signalChannel, })); return this.processInstance.getProcessModelFacade().getNextFlowNodesFor(this.signalThrowEvent); } catch (error) { let errorMessageToLog; if (error.category) { errorMessageToLog = `Failure in ${error.category}: Failed to send signal: ${error.message.replace(`Failure in ${error.category}: `, '')}`; } else { errorMessageToLog = `Failed to send signal: ${error.message}`; } this.logger.error(errorMessageToLog); this.processToken = {}; if (!error.additionalInformation) { error.additionalInformation = { processInstanceId: this.processInstance.getProcessInstanceId(), correlationId: this.processInstance.getCorrelationId(), }; } throw error; } } validateSignal() { if (this.signalThrowEvent.signalEventDefinition?.name === undefined) { const errorToThrow = new processcube_engine_sdk_1.BadRequestError('Intermediate Signal Throw Event does not have a signal name assigned.', 'process'); errorToThrow.additionalInformation = { flowNodeInstanceId: this.flowNodeInstanceId, flowNodeId: this.signalThrowEvent.id, flowNodeName: this.signalThrowEvent.name, processInstanceId: this.processInstance.getProcessInstanceId(), processModelId: this.processInstance.getProcessModelId(), embeddedProcessModelId: this.processInstance.getEmbeddedProcessModelId(), correlationId: this.processInstance.getCorrelationId(), }; throw errorToThrow; } } async evaluateCustomCorrelationId() { if (!this.signalThrowEvent.customCorrelationId) { return; } try { this.customCorrelationId = await this.processInstance.executeRuntimeExpression({ expression: `\`${this.signalThrowEvent.customCorrelationId}\``, currentFlowNode: this.signalThrowEvent, currentToken: this.processToken, previousFlowNode: this.processInstance.getProcessModelFacade().getPreviousFlowNodesFor(this.flowNode)?.pop(), allowNonObjectResults: true, }); } catch (error) { const errorToThrow = new processcube_engine_sdk_1.BadRequestError(`SignalThrowEvent configuration for customCorrelationId is invalid!`, error.category); errorToThrow.additionalInformation = { customCorrelationId: this.signalThrowEvent.customCorrelationId, processInstanceId: this.processInstance.getProcessInstanceId(), correlationId: this.processInstance.getCorrelationId(), }; this.logger.error(errorToThrow.message); throw errorToThrow; } } async getTokenPayloadFromInputValues() { try { if (!this.signalThrowEvent.inputValues || this.signalThrowEvent.inputValues.trim().length === 0) { return this.processToken; } return this.processInstance.executeRuntimeExpression({ expression: this.signalThrowEvent.inputValues, currentFlowNode: this.signalThrowEvent, currentToken: this.processToken, previousFlowNode: this.processInstance.getProcessModelFacade().getPreviousFlowNodesFor(this.flowNode)?.pop(), allowNonObjectResults: true, dataContract: this.signalThrowEvent.dataContract, }); } catch (error) { const errorToThrow = new processcube_engine_sdk_1.BadRequestError(`SignalThrowEvent configuration for inputValues \`${this.signalThrowEvent.inputValues}\` is invalid!`, error.category); errorToThrow.additionalInformation = { processInstanceId: this.processInstance.getProcessInstanceId(), correlationId: this.processInstance.getCorrelationId(), }; this.logger.error(errorToThrow.message); throw errorToThrow; } } }; exports.IntermediateSignalThrowEventHandler = IntermediateSignalThrowEventHandler; exports.IntermediateSignalThrowEventHandler = IntermediateSignalThrowEventHandler = __decorate([ (0, inversify_1.injectable)(), __metadata("design:paramtypes", [index_2.EventMiddlewareHandler, FlowNodeHandlerFactory_1.FlowNodeHandlerFactory, index_2.FlowNodeInstanceDatabaseAdapter, Object, ProcessInstance_1.ProcessInstance, SignalEventService_1.SignalEventService]) ], IntermediateSignalThrowEventHandler); //# sourceMappingURL=IntermediateSignalThrowEventHandler.js.map