@villedemontreal/workit-bpm-client
Version:
Camunda BPM client for WorkIt that works with Camunda platform powered by TypeScript
153 lines • 5.79 kB
JavaScript
/*
* Copyright (c) 2025 Ville de Montreal. All rights reserved.
* Licensed under the MIT license.
* See LICENSE file in the project root for full license information.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.CamundaBpmClient = void 0;
const workit_core_1 = require("@villedemontreal/workit-core");
const camundaMessage_1 = require("./camundaMessage");
const camundaRepository_1 = require("./repositories/camundaRepository");
const paginationUtils_1 = require("./utils/paginationUtils");
class CamundaBpmClient {
static _getWorkflowParams(options) {
const _params = {};
if (options && options.bpmnProcessId) {
_params.key = options.bpmnProcessId;
}
return paginationUtils_1.PaginationUtils.setCamundaBpmPaginationParams(_params, options);
}
constructor(config, client) {
this._client = client;
this._config = config;
this._repo = new camundaRepository_1.CamundaRepository(config);
const pluginLoader = new workit_core_1.PluginLoader(workit_core_1.IoC, this._getLogger());
if (config.plugins) {
pluginLoader.load(config.plugins);
}
}
subscribe(onMessageReceived) {
this._topicSubscription = this._client.subscribe(this._config.topicName, this._config.subscriptionOptions, async (camundaObject) => {
const [message, service] = camundaMessage_1.CamundaMessage.wrap(camundaObject);
await onMessageReceived(message, service);
});
this._startSubscriber();
return Promise.resolve();
}
unsubscribe() {
try {
if (this._topicSubscription) {
this._topicSubscription.unsubscribe();
}
this._client.stop();
return Promise.resolve();
}
catch (error) {
return Promise.reject(error);
}
}
async deployWorkflow(absPath) {
const result = await this._repo.deployWorkflow(`Deploy from ${this._config.workerId}`, absPath);
const response = result.data;
const deployedProcessDefinitionsId = Object.keys(response.deployedProcessDefinitions)[0];
const definition = response.deployedProcessDefinitions[deployedProcessDefinitionsId];
const workflows = [
{
bpmnProcessId: definition.key,
workflowKey: definition.id,
resourceName: definition.resource,
version: definition.version,
},
];
return {
workflows,
key: response.id,
};
}
async getWorkflows(options) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const params = CamundaBpmClient._getWorkflowParams(options);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const apiOptions = { params };
const requests = [this._repo.getWorkflows(apiOptions), this._repo.getWorkflowCount(apiOptions)];
const [result, repCount] = await Promise.all(requests);
const bpmns = result.data;
const workflows = bpmns.map((definition) => ({
bpmnProcessId: definition.key,
workflowKey: definition.id,
resourceName: definition.resource,
version: definition.version,
}));
return {
paging: paginationUtils_1.PaginationUtils.getPagingFromOptions(repCount.data.count, options),
items: workflows,
};
}
async getWorkflow(payload) {
let definition;
if (this._hasBpmnProcessId(payload)) {
definition = await this._repo.getWorkflow(payload.bpmnProcessId);
}
else {
definition = await this._repo.getWorkflow(payload.workflowKey);
}
return {
bpmnProcessId: definition.key,
bpmnXml: definition.bpmn20Xml,
resourceName: definition.resource,
version: definition.version,
workflowKey: definition.id,
};
}
async updateVariables(model) {
await this._repo.updateVariables(model.processInstanceId, model.variables);
}
async updateJobRetries({ jobKey, retries }) {
await this._repo.updateJobRetries(jobKey, retries);
}
publishMessage(payload) {
return this._repo.publishMessage({
messageName: payload.name,
processInstanceId: payload.messageId,
correlationKeys: payload.correlation,
variables: payload.variables,
});
}
async createWorkflowInstance(model) {
const result = await this._repo.createWorkflowInstance(model.bpmnProcessId, model.variables);
const response = result.data;
const bpmnDef = response.definitionId.split(':');
// TODO: fix this type issue
return {
bpmnProcessId: bpmnDef[0],
version: bpmnDef[1],
workflowInstanceKey: response.id,
workflowKey: response.definitionId,
};
}
cancelWorkflowInstance(instanceId) {
return this._repo.cancelWorkflowInstance(instanceId);
}
resolveIncident(incidentKey) {
return this._repo.resolveIncident(incidentKey);
}
_startSubscriber() {
if (!this._config.autoPoll) {
this._client.start();
}
}
_hasBpmnProcessId(request) {
return request.bpmnProcessId !== undefined;
}
_getLogger() {
try {
return workit_core_1.IoC.get(workit_core_1.SERVICE_IDENTIFIER.logger);
}
catch (error) {
return workit_core_1.NOOP_LOGGER;
}
}
}
exports.CamundaBpmClient = CamundaBpmClient;
//# sourceMappingURL=camundaBpmClient.js.map
;