@villedemontreal/workit-bpm-client
Version:
Camunda BPM client for WorkIt that works with Camunda platform powered by TypeScript
89 lines • 3.58 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.CamundaMessage = void 0;
const camundaMapperProperties_1 = require("./camundaMapperProperties");
const variables_1 = require("./variables");
const fast_safe_stringify_1 = require("fast-safe-stringify");
class CamundaMessage {
static wrap(payload) {
const { task } = payload;
const properties = camundaMapperProperties_1.CamundaMapperProperties.map(task);
const messageWithoutSpan = {
body: task.variables.getAll(),
properties,
};
// TODO: create a CamundaMessage builder
const msg = {
body: { ...messageWithoutSpan.body },
properties: { ...messageWithoutSpan.properties },
};
return [
msg,
// TODO: create a CamundaService builder
{
hasBeenThreated: false,
/**
* Acknowledge the message to Camunda platform
* Variables will be updated if change has been detected
*/
async ack(message) {
if (this.hasBeenThreated) {
return;
}
const vars = CamundaMessage.unwrap(message);
await payload.taskService.complete(task, vars);
this.hasBeenThreated = true;
},
/**
* Un acknowledge the message to Camunda platform
* This will handle failure.
* Notice that on failure, the current camunda client doesn't update the variables
*/
async nack(error) {
if (this.hasBeenThreated) {
return;
}
const { retries, retryTimeout } = error;
const retryTimeoutInMs = retryTimeout || 1000 * retries * 2;
await payload.taskService.handleFailure(task, {
errorMessage: error.message,
errorDetails: (0, fast_safe_stringify_1.default)(error),
retries,
// TODO: Add to configuration
retryTimeout: retryTimeoutInMs,
});
this.hasBeenThreated = true;
},
},
];
}
static unwrap(message) {
const { body } = message;
const vars = new variables_1.Variables(body);
Object.entries(body).forEach(([key, val]) => {
if (Object.prototype.toString.call(val) === '[object Date]') {
// Otherwise, we got invalid date
// TODO: Check the cause
vars.setTyped(key, { type: 'date', value: val.toUTCString(), valueInfo: {} });
}
else {
vars.set(key, val);
}
});
CamundaMessage._setCustomHeaders(vars, message.properties.customHeaders);
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return vars;
}
static _setCustomHeaders(vars, customHeaders) {
if (customHeaders && Object.keys(customHeaders).length > 0) {
vars.set('_meta', { customHeaders });
}
}
}
exports.CamundaMessage = CamundaMessage;
//# sourceMappingURL=camundaMessage.js.map
;