@5minds/processcube_engine
Version:
The ProcessCube Engine. Stores and executes BPMNs.
245 lines • 14.6 kB
JavaScript
"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 __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProcessInstanceController = void 0;
const inversify_1 = require("inversify");
const processcube_engine_sdk_1 = require("@5minds/processcube_engine_sdk");
const Contracts_1 = require("../../Contracts");
const BaseController_1 = require("./BaseController");
const BuildErrorObject_1 = require("./BuildErrorObject");
let ProcessInstanceController = class ProcessInstanceController extends BaseController_1.BaseController {
processInstanceServiceProxy;
constructor(processInstanceServiceProxy) {
super();
this.logger = new processcube_engine_sdk_1.Logger('api:http:process_instance_controller');
this.processInstanceServiceProxy = processInstanceServiceProxy;
}
async query(request, response) {
const identity = request.identity;
const offset = this.parseOffset(request);
const limit = this.parseLimit(request);
const includeXml = request.query.includeXml === 'true';
const includeCount = request.query.includeCount == null || request.query.includeCount === 'true';
const sortSettings = this.parseSortSettings(request);
this.validateProcessInstanceQuery(request.query);
const processInstanceQuery = this.buildProcessInstanceQuery(request.query);
this.logger.debug('Received `query process instances` http request', {
offset: offset,
limit: limit,
includeXml: includeXml,
sortSettings: sortSettings,
processInstanceQuery: processInstanceQuery,
includeCount: includeCount,
});
const result = await this.processInstanceServiceProxy.query(identity, processInstanceQuery, offset, limit, sortSettings, includeXml, includeCount);
const serializedResults = {
totalCount: result.totalCount,
processInstances: result.processInstances.map((processInstance) => {
return {
...processInstance,
error: (0, BuildErrorObject_1.buildErrorObject)(processInstance.error),
};
}),
};
response.status(this.httpCodeSuccessfulResponse).json(serializedResults);
}
async getProcessDefinition(request, response) {
const identity = request.identity;
const processInstanceId = request.params.process_instance_id;
const includeXml = request.query.includeXml === 'true';
this.logger.debug('Received `get process definition` http request', {
processInstanceId: processInstanceId,
includeXml: includeXml,
});
const result = await this.processInstanceServiceProxy.getProcessDefinition(identity, processInstanceId, includeXml);
response.status(this.httpCodeSuccessfulResponse).json(result);
}
async getProcessModel(request, response) {
const identity = request.identity;
const processInstanceId = request.params.process_instance_id;
this.logger.debug('Received `get process model` http request', {
processInstanceId: processInstanceId,
});
const result = await this.processInstanceServiceProxy.getProcessModel(identity, processInstanceId);
response.status(this.httpCodeSuccessfulResponse).json(result);
}
async getChildProcessInstanceIds(request, response) {
const identity = request.identity;
const processInstanceId = request.params.process_instance_id;
const parentFlowNodeType = request.query.parentFlowNodeType;
const includeNested = request.query.includeNested === 'true';
this.logger.debug('Received `get child process instances` http request', {
processInstanceId: processInstanceId,
});
const result = await this.processInstanceServiceProxy.getChildProcessInstanceIds(identity, processInstanceId, parentFlowNodeType, includeNested);
response.status(this.httpCodeSuccessfulResponse).json(result);
}
async getAncestorProcessInstanceIds(request, response) {
const identity = request.identity;
const processInstanceId = request.params.process_instance_id;
const onlyForEmbeddedProcesses = request.query.onlyForEmbeddedProcesses === 'true';
this.logger.debug('Received `get ancestor process instances` http request', {
processInstanceId: processInstanceId,
});
const result = await this.processInstanceServiceProxy.getAncestorProcessInstanceIds(identity, processInstanceId, onlyForEmbeddedProcesses);
response.status(this.httpCodeSuccessfulResponse).json(result);
}
async transferOwnership(request, response) {
const identity = request.identity;
const processInstanceId = request.params.process_instance_id;
const newOwner = request.body.newOwner;
this.logger.debug('Received `transfer process instance ownership` http request', {
processInstanceId: processInstanceId,
newOwner: newOwner,
});
await this.processInstanceServiceProxy.transferOwnership(identity, processInstanceId, newOwner);
response.status(this.httpCodeSuccessNoContentResponse).send();
}
async terminateProcessInstance(request, response) {
const identity = request.identity;
const processInstanceId = request.params.process_instance_id;
this.logger.debug('Received `terminate process instance` http request', {
processInstanceId: processInstanceId,
});
await this.processInstanceServiceProxy.terminateProcessInstance(identity, processInstanceId);
response.status(this.httpCodeSuccessNoContentResponse).send();
}
async retryProcessInstance(request, response) {
const identity = request.identity;
const processInstanceId = request.params.process_instance_id;
const flowNodeInstanceId = request.query.flow_node_instance_id;
const shouldUpdateProcessModel = request.query.update_process_model === 'true';
const newStartToken = request.body.newStartToken;
this.logger.debug('Received `retry process instance` http request', {
processInstanceId: processInstanceId,
flowNodeInstanceId: flowNodeInstanceId,
newStartToken: newStartToken,
});
await this.processInstanceServiceProxy.retryProcessInstance(identity, processInstanceId, flowNodeInstanceId, newStartToken, shouldUpdateProcessModel);
response.status(this.httpCodeSuccessNoContentResponse).send();
}
async deleteProcessInstances(request, response) {
const identity = request.identity;
const processInstanceIdsOld = request.query.process_instance_ids?.replaceAll(';', ',');
const processInstanceIdsNew = request.query.processInstanceId;
const processInstanceIds = processInstanceIdsNew || processInstanceIdsOld;
const processModelId = request.query.processModelId;
const finishedBefore = request.query.finishedBefore;
const finishedAfter = request.query.finishedAfter;
const deleteAllRelatedData = request.query.delete_all_related_data === 'true';
this.logger.debug('Received `delete process instances` http request', {
processInstanceIds: processInstanceIds,
processModelId: processModelId,
finishedBefore: finishedBefore,
finishedAfter: finishedAfter,
deleteAllRelatedData: deleteAllRelatedData,
});
const processInstanceQuery = this.buildProcessInstanceQuery({
processInstanceId: processInstanceIds,
processModelId: processModelId,
finishedBefore: finishedBefore,
finishedAfter: finishedAfter,
});
await this.processInstanceServiceProxy.deleteProcessInstances(identity, processInstanceQuery, deleteAllRelatedData);
response.status(this.httpCodeSuccessNoContentResponse).send();
}
validateProcessInstanceQuery(queryParams) {
if (queryParams.state) {
const queriedStates = queryParams.state.indexOf(',') > 0 ? queryParams.state.split(',') : [queryParams.state];
const processInstanceStates = Object.values(processcube_engine_sdk_1.ProcessInstanceState);
for (const queriedState of queriedStates) {
if (!processInstanceStates.includes(queriedState)) {
const error = new processcube_engine_sdk_1.BadRequestError(`'${queriedState}' is not a valid ProcessInstance state!`);
error.additionalInformation = {
queriedStates: queriedStates,
allowedStates: processInstanceStates,
};
throw error;
}
}
}
}
buildProcessInstanceQuery(queryParams) {
const query = {
correlationId: this.getQueryParamValueAsSearchQuery(queryParams.correlationId),
processInstanceId: this.getQueryParamValueAsSearchQuery(queryParams.processInstanceId),
processDefinitionId: this.getQueryParamValueAsSearchQuery(queryParams.processDefinitionId),
processModelId: this.getQueryParamValueAsSearchQuery(queryParams.processModelId),
processModelName: this.getQueryParamValueAsSearchQuery(queryParams.processModelName),
processModelVersion: this.getQueryParamValueAsSearchQuery(queryParams.processModelVersion),
processModelHash: this.getQueryParamValueAsSearchQuery(queryParams.processModelHash),
embeddedProcessModelId: this.getQueryParamValueAsSearchQuery(queryParams.embeddedProcessModelId),
ownerId: this.getQueryParamValueAsSearchQuery(queryParams.ownerId),
startedByRootAccessToken: this.getQueryParamValueAsBoolean(queryParams.startedByRootAccessToken),
state: this.getQueryParamValueAsEnum(queryParams.state, processcube_engine_sdk_1.ProcessInstanceState),
parentProcessInstanceId: this.getQueryParamValueAsSearchQuery(queryParams.parentProcessInstanceId),
finishedAt: this.getQueryParamValueAsDate(queryParams.finishedAt),
finishedAfter: this.getQueryParamValueAsDate(queryParams.finishedAfter),
finishedBefore: this.getQueryParamValueAsDate(queryParams.finishedBefore),
terminatedByUserId: this.getQueryParamValueAsSearchQuery(queryParams.terminatedByUserId),
createdAt: this.getQueryParamValueAsDate(queryParams.createdAt),
createdAfter: this.getQueryParamValueAsDate(queryParams.createdAfter),
createdBefore: this.getQueryParamValueAsDate(queryParams.createdBefore),
updatedAt: this.getQueryParamValueAsDate(queryParams.updatedAt),
updatedAfter: this.getQueryParamValueAsDate(queryParams.updatedAfter),
updatedBefore: this.getQueryParamValueAsDate(queryParams.updatedBefore),
durationInMillisecondsGreaterThan: this.getDurationValue(queryParams.durationInMillisecondsGreaterThan),
durationInMillisecondsLessThan: this.getDurationValue(queryParams.durationInMillisecondsLessThan),
startToken: this.getQueryParamValueAsSearchQuery(queryParams.startToken),
startEventId: this.getQueryParamValueAsSearchQuery(queryParams.startEventId),
startEventType: this.getQueryParamValueAsSearchQuery(queryParams.startEventType),
endToken: this.getQueryParamValueAsSearchQuery(queryParams.endToken),
endEventId: this.getQueryParamValueAsSearchQuery(queryParams.endEventId),
endEventType: this.getQueryParamValueAsSearchQuery(queryParams.endEventType),
correlationMetadata: this.getQueryParamValueAsSearchQuery(queryParams.correlationMetadata),
processInstanceMetadata: this.getQueryParamValueAsSearchQuery(queryParams.processInstanceMetadata),
triggeredByFlowNodeInstance: this.getQueryParamValueAsSearchQuery(queryParams.triggeredByFlowNodeInstance),
};
return query;
}
getDurationValue(durationInMilliseconds) {
const parsedDurationInMilliseconds = parseInt(durationInMilliseconds);
return Number.isNaN(parsedDurationInMilliseconds) ? undefined : parsedDurationInMilliseconds;
}
parseOffset(request) {
const parsedOffset = parseInt(request.query?.offset);
return Number.isNaN(parsedOffset) ? 0 : parsedOffset;
}
parseLimit(request) {
const parsedLimit = parseInt(request.query?.limit);
return Number.isNaN(parsedLimit) ? 0 : parsedLimit;
}
parseSortSettings(request) {
if (request.query?.sortBy || request.query?.sortDir) {
return {
sortBy: request.query?.sortBy ?? processcube_engine_sdk_1.ProcessInstanceSortableColumns.createdAt,
sortDir: request.query?.sortDir ?? 'DESC',
};
}
if (!request.query?.sortSettings) {
return {
sortBy: processcube_engine_sdk_1.ProcessInstanceSortableColumns.createdAt,
sortDir: 'DESC',
};
}
return JSON.parse(decodeURIComponent(request.query.sortSettings));
}
};
exports.ProcessInstanceController = ProcessInstanceController;
exports.ProcessInstanceController = ProcessInstanceController = __decorate([
(0, inversify_1.injectable)(),
__param(0, (0, inversify_1.inject)(Contracts_1.IocRegistrationKeys.api.services.ProcessInstanceServiceProxy)),
__metadata("design:paramtypes", [Object])
], ProcessInstanceController);
//# sourceMappingURL=ProcessInstanceController.js.map