UNPKG

@5minds/processcube_engine

Version:

The ProcessCube Engine. Stores and executes BPMNs.

218 lines • 12.3 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.IntermediateMessageThrowEventHandler = 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 MessageEventService_1 = require("../../MessageEventService"); const ProcessInstance_1 = require("../../ProcessInstance"); const FlowNodeHandlerFactory_1 = require("../FlowNodeHandlerFactory"); const EventHandler_1 = require("./EventHandler"); let IntermediateMessageThrowEventHandler = class IntermediateMessageThrowEventHandler extends EventHandler_1.EventHandler { customCorrelationId; messageEventService; constructor(eventMiddlewareHandler, flowNodeHandlerFactory, flowNodeInstanceDatabaseAdapter, intermediateMessageThrowEventModel, processInstance, messageEventService) { super(eventMiddlewareHandler, flowNodeHandlerFactory, flowNodeInstanceDatabaseAdapter, intermediateMessageThrowEventModel, processInstance, 'message_throw_event_handler'); this.messageEventService = messageEventService; } get messageThrowEvent() { return this.flowNode; } async persistOnExit() { await super.persistOnExit({ type: index_1.FlowNodeInstanceDataTypes.throwEvent, eventName: this.messageThrowEvent.messageEventDefinition.name, customCorrelationId: this.customCorrelationId, }); this.publishIntermediateThrowEventTriggeredNotification(); } async persistOnError(error) { await super.persistOnError(error, { type: index_1.FlowNodeInstanceDataTypes.throwEvent, eventName: this.messageThrowEvent.messageEventDefinition?.name, customCorrelationId: this.customCorrelationId, }); this.publishIntermediateThrowEventTriggeredNotification(); } async persistOnTerminate() { await super.persistOnTerminate({ type: index_1.FlowNodeInstanceDataTypes.throwEvent, eventName: this.messageThrowEvent.messageEventDefinition.name, customCorrelationId: this.customCorrelationId, }); } async startExecution() { this.logger.debug(`Executing MessageThrowEvent instance.`); return this.executeHandler(); } async executeHandler() { try { this.ensureHasClaim(); this.processToken = await this.getTokenPayloadFromInputValues(); this.validateMessage(); const messageName = this.messageThrowEvent.messageEventDefinition.name; await this.evaluateCustomCorrelationId(); const message = { messageReference: messageName, 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.messageThrowEvent.id, flowNodeName: this.messageThrowEvent.name, flowNodeType: this.messageThrowEvent.bpmnType, flowNodeLane: this.processInstance.getProcessModelFacade().getLaneForFlowNode(this.messageThrowEvent.id)?.name, flowNodeInstanceId: this.flowNodeInstanceId, identity: this.processInstance.getOwner(), currentToken: this.processToken, eventName: this.messageThrowEvent.messageEventDefinition.name, eventType: this.flowNode.eventType, customCorrelationId: this.customCorrelationId, messageChannel: this.messageThrowEvent.messageChannel, }; const pushMessageToInternalChannel = message.messageChannel == undefined || message.messageChannel === 'internal'; if (pushMessageToInternalChannel) { this.logger.debug(`MessageThrowEvent instance now sending message ${messageName} to internal channel...`); await this.messageEventService.throwMessage(message, true); } else { this.logger.debug(`MessageThrowEvent instance now sending message ${messageName} to external channel ${message.messageChannel}...`); } const now = dayjs_1.default.utc().toDate(); const currentFlowNodeMetadata = processcube_engine_sdk_1.FlowNodeViewModelFactory.getViewModel(this.messageThrowEvent, { processModelId: message.processModelId, processModelName: message.processModelName, }); this.logger.trace(`Posting Message Intermediate Throw Event (ID: ${this.flowNode.id}) "OnMessageTriggered" to Event Middleware: ${message.messageReference} to external channel ${message.messageChannel}`); await this.postEventToEventMiddlewares((0, lodash_clonedeep_1.default)({ eventType: processcube_engine_sdk_1.EngineEventType.OnMessageTriggered, timestamp: now, processModelId: message.processModelId, processModelName: message.processModelName, processDefinitionId: message.processDefinitionId, processDefinitionHash: this.processInstance.getProcessDefinitionHash(), flowNodeId: message.flowNodeId, flowNodeName: message.flowNodeName, flowNodeType: message.flowNodeType, flowNodeLane: message.flowNodeLane, flowNodeInstanceId: this.flowNodeInstanceId, processInstanceId: message.processInstanceId, parentProcessInstanceId: message.parentProcessInstanceId, correlationId: message.correlationId, currentFlowNode: currentFlowNodeMetadata, logLevel: processcube_engine_sdk_1.LogLevel.debug, tokenPayload: this.processToken, messageChannel: message.messageChannel, })); this.logger.debug('Done.'); return this.processInstance.getProcessModelFacade().getNextFlowNodesFor(this.messageThrowEvent); } catch (error) { let errorMessageToLog; if (error.category) { errorMessageToLog = `Failure in ${error.category}: Failed to send message: ${error.message.replace(`Failure in ${error.category}: `, '')}`; } else { errorMessageToLog = `Failed to send message: ${error.message}`; } this.logger.error(errorMessageToLog, { correlationId: this.processInstance.getCorrelationId(), processInstanceId: this.processInstance.getProcessInstanceId(), flowNodeInstanceId: this.flowNodeInstanceId, flowNodeId: this.flowNode.id, }); this.processToken = {}; throw error; } } validateMessage() { if (this.messageThrowEvent.messageEventDefinition?.name === undefined) { const errorToThrow = new processcube_engine_sdk_1.BadRequestError('Intermediate Message Throw Event does not have a message name assigned.', 'process'); errorToThrow.additionalInformation = { flowNodeInstanceId: this.flowNodeInstanceId, flowNodeId: this.messageThrowEvent.id, flowNodeName: this.messageThrowEvent.name, processInstanceId: this.processInstance.getProcessInstanceId(), processModelId: this.processInstance.getProcessModelId(), embeddedProcessModelId: this.processInstance.getEmbeddedProcessModelId(), correlationId: this.processInstance.getCorrelationId(), }; throw errorToThrow; } } async evaluateCustomCorrelationId() { if (!this.messageThrowEvent.customCorrelationId) { return; } try { this.customCorrelationId = await this.processInstance.executeRuntimeExpression({ expression: `\`${this.messageThrowEvent.customCorrelationId}\``, currentFlowNode: this.messageThrowEvent, currentToken: this.processToken, previousFlowNode: this.processInstance.getProcessModelFacade().getPreviousFlowNodesFor(this.flowNode)?.pop(), allowNonObjectResults: true, }); } catch (error) { const errorToThrow = new processcube_engine_sdk_1.BadRequestError(`MessageThrowEvent configuration for customCorrelationId is invalid!`, error.category); errorToThrow.additionalInformation = { customCorrelationId: this.messageThrowEvent.customCorrelationId, processInstanceId: this.processInstance.getProcessInstanceId(), correlationId: this.processInstance.getCorrelationId(), }; this.logger.error(errorToThrow.message); throw errorToThrow; } } async getTokenPayloadFromInputValues() { try { if (!this.messageThrowEvent.inputValues || this.messageThrowEvent.inputValues.trim().length === 0) { return this.processToken; } return this.processInstance.executeRuntimeExpression({ expression: this.messageThrowEvent.inputValues, currentFlowNode: this.messageThrowEvent, currentToken: this.processToken, previousFlowNode: this.processInstance.getProcessModelFacade().getPreviousFlowNodesFor(this.flowNode)?.pop(), allowNonObjectResults: false, dataContract: this.messageThrowEvent.dataContract, }); } catch (error) { const errorToThrow = new processcube_engine_sdk_1.BadRequestError(`MessageThrowEvent configuration for inputValues \`${this.messageThrowEvent.inputValues}\` is invalid!`, error.category); errorToThrow.additionalInformation = { processInstanceId: this.processInstance.getProcessInstanceId(), correlationId: this.processInstance.getCorrelationId(), }; this.logger.error(errorToThrow.message); throw errorToThrow; } } }; exports.IntermediateMessageThrowEventHandler = IntermediateMessageThrowEventHandler; exports.IntermediateMessageThrowEventHandler = IntermediateMessageThrowEventHandler = __decorate([ (0, inversify_1.injectable)(), __metadata("design:paramtypes", [index_2.EventMiddlewareHandler, FlowNodeHandlerFactory_1.FlowNodeHandlerFactory, index_2.FlowNodeInstanceDatabaseAdapter, Object, ProcessInstance_1.ProcessInstance, MessageEventService_1.MessageEventService]) ], IntermediateMessageThrowEventHandler); //# sourceMappingURL=IntermediateMessageThrowEventHandler.js.map