UNPKG

@5minds/processcube_engine

Version:

The ProcessCube Engine. Stores and executes BPMNs.

173 lines 9.32 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.EndMessageEventHandler = void 0; const dayjs_1 = __importDefault(require("dayjs")); const inversify_1 = require("inversify"); 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 EndEventHandler_1 = require("./EndEventHandler"); let EndMessageEventHandler = class EndMessageEventHandler extends EndEventHandler_1.EndEventHandler { messageAndSignalService; constructor(eventMiddlewareHandler, flowNodeHandlerFactory, flowNodeInstanceDatabaseAdapter, endEventModel, processInstance, messageAndSignalService) { super(eventMiddlewareHandler, flowNodeHandlerFactory, flowNodeInstanceDatabaseAdapter, endEventModel, processInstance, 'message_end_event_handler'); this.messageAndSignalService = messageAndSignalService; } get messageEndEvent() { return this.endEvent; } async persistOnExit() { await super.persistOnExit({ type: index_1.FlowNodeInstanceDataTypes.throwEvent, eventName: this.eventName, customCorrelationId: this.customCorrelationId, }); } async startExecution() { this.logger.debug(`Executing MessageEndEvent instance.`); return this.executeHandler(); } async executeHandler() { return new Promise(async (resolve, reject) => { try { this.ensureHasClaim(); this.processToken = await this.getFinalTokenPayload(); await this.evaluateCustomCorrelationId(); await this.sendMessage(); return resolve(undefined); } catch (error) { let errorMessageToLog; if (error.category) { errorMessageToLog = `Failure in ${error.category}: Failed to run MessageEndEvent: ${error.message.replace(`Failure in ${error.category}: `, '')}`; } else { errorMessageToLog = `Failed to run MessageEndEvent: ${error.message}`; } this.logger.error(errorMessageToLog, { err: error, }); this.processToken = {}; return reject(error); } }); } async getFinalTokenPayload() { try { if (!this.messageEndEvent.inputValues || this.messageEndEvent.inputValues.trim().length === 0) { return this.processToken; } return this.executeExpression(this.messageEndEvent.inputValues, undefined, this.messageEndEvent.dataContract); } catch (error) { const errorToThrow = new processcube_engine_sdk_1.InternalServerError(`MessageEndEvent configuration for inputValues \`${this.messageEndEvent.inputValues}\` is invalid!`, error.category); errorToThrow.additionalInformation = { processInstanceId: this.processInstance.getProcessInstanceId(), correlationId: this.processInstance.getCorrelationId(), }; this.logger.error(errorToThrow.message); throw errorToThrow; } } async evaluateCustomCorrelationId() { try { if (!this.messageEndEvent.customCorrelationId) { return; } this.customCorrelationId = await this.executeExpression(`\`${this.messageEndEvent.customCorrelationId}\``, true); } catch (error) { const errorToThrow = new processcube_engine_sdk_1.InternalServerError(`EndEvent configuration for customCorrelationId is invalid!`, error.category); errorToThrow.additionalInformation = { customCorrelationId: this.messageEndEvent.customCorrelationId, processInstanceId: this.processInstance.getProcessInstanceId(), correlationId: this.processInstance.getCorrelationId(), }; this.logger.error(errorToThrow.message); throw errorToThrow; } } async sendMessage() { this.eventName = this.messageEndEvent.messageEventDefinition.name; const message = { messageReference: this.eventName, 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.endEvent.id, flowNodeName: this.endEvent.name, flowNodeType: this.endEvent.bpmnType, flowNodeLane: this.processInstance.getProcessModelFacade().getLaneForFlowNode(this.endEvent.id)?.name, flowNodeInstanceId: this.flowNodeInstanceId, identity: this.processInstance.getOwner(), currentToken: this.processToken, eventType: this.messageEndEvent.eventType, eventName: this.eventName, customCorrelationId: this.customCorrelationId, messageChannel: this.messageEndEvent.messageChannel, }; const pushMessageToInternalChannel = message.messageChannel == undefined || message.messageChannel === 'internal'; if (pushMessageToInternalChannel) { this.logger.debug(`MessageEndEvent instance now sending message ${message.messageReference} to internal channel...`); await this.messageAndSignalService.throwMessage(message); } else { this.logger.debug(`MessageEndEvent instance (ID: ${this.flowNode.id}) now sending message ${message.messageReference} to external channel ${message.messageChannel}...`); } const now = dayjs_1.default.utc().toDate(); const currentFlowNodeMetadata = processcube_engine_sdk_1.FlowNodeViewModelFactory.getViewModel(this.messageEndEvent, { processModelId: message.processModelId, processModelName: message.processModelName, }); this.logger.trace(`Posting Message End Event "OnMessageTriggered" to Event Middleware: ${message.messageReference} to external channel ${message.messageChannel}`); await this.postEventToEventMiddlewares({ 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.'); } }; exports.EndMessageEventHandler = EndMessageEventHandler; exports.EndMessageEventHandler = EndMessageEventHandler = __decorate([ (0, inversify_1.injectable)(), __metadata("design:paramtypes", [index_2.EventMiddlewareHandler, FlowNodeHandlerFactory_1.FlowNodeHandlerFactory, index_2.FlowNodeInstanceDatabaseAdapter, Object, ProcessInstance_1.ProcessInstance, MessageEventService_1.MessageEventService]) ], EndMessageEventHandler); //# sourceMappingURL=EndMessageEventHandler.js.map