@tachybase/module-workflow
Version:
A powerful BPM tool that provides foundational support for business automation, with the capability to extend unlimited triggers and nodes.
168 lines (167 loc) • 6.28 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var workflows_helpers_exports = {};
__export(workflows_helpers_exports, {
getCategoriesForWorkflow: () => getCategoriesForWorkflow,
getEventSourceNameForWorkflow: () => getEventSourceNameForWorkflow,
getLatestExecutedTimeForWorkflow: () => getLatestExecutedTimeForWorkflow,
getWorkflowData: () => getWorkflowData,
setCategories: () => setCategories,
setEventSourceName: () => setEventSourceName,
setLatestExecutedTime: () => setLatestExecutedTime
});
module.exports = __toCommonJS(workflows_helpers_exports);
var import_dayjs = __toESM(require("dayjs"));
function getWorkflowData(workflow) {
return workflow && typeof workflow.toJSON === "function" ? workflow.toJSON() : workflow;
}
async function getLatestExecutedTimeForWorkflow(ctx, workflowId, workflowKey) {
if (!workflowId && !workflowKey) {
return null;
}
try {
const ExecutionRepo = ctx.db.getRepository("executions");
const filter = {};
if (workflowId) {
filter.workflowId = workflowId;
} else if (workflowKey) {
filter.key = workflowKey;
}
const latestExecution = await ExecutionRepo.findOne({
filter,
fields: ["id", "key", "createdAt", "workflowId"],
sort: ["-createdAt"],
context: ctx
});
return (latestExecution == null ? void 0 : latestExecution.createdAt) ? (0, import_dayjs.default)(latestExecution.createdAt).utc().toISOString() : null;
} catch (error) {
ctx.log.error("Failed to fetch latest executed time:", error);
return null;
}
}
function setLatestExecutedTime(row, executedTime) {
row.latestExecutedTime = executedTime;
if (row && typeof row.setDataValue === "function") {
row.setDataValue("latestExecutedTime", executedTime);
}
}
async function getEventSourceNameForWorkflow(ctx, workflowKey) {
if (!workflowKey) {
return null;
}
try {
const WebhookRepo = ctx.db.getRepository("webhooks");
const eventSource = await WebhookRepo.findOne({
filter: { workflowKey },
fields: ["id", "name"],
context: ctx
});
return (eventSource == null ? void 0 : eventSource.name) || null;
} catch (error) {
ctx.log.error("Failed to fetch event source name:", error);
return null;
}
}
function setEventSourceName(row, eventSourceName) {
row.eventSourceName = eventSourceName;
if (row && typeof row.setDataValue === "function") {
row.setDataValue("eventSourceName", eventSourceName);
}
}
async function getCategoriesForWorkflow(ctx, workflow, workflowKey) {
var _a;
if (workflow) {
const workflowData = workflow && typeof workflow.toJSON === "function" ? workflow.toJSON() : workflow;
if ((workflowData == null ? void 0 : workflowData.category) && Array.isArray(workflowData.category)) {
return workflowData.category.map((cat) => {
const data = cat && typeof cat.toJSON === "function" ? cat.toJSON() : cat;
return {
id: data == null ? void 0 : data.id,
name: data == null ? void 0 : data.name,
color: data == null ? void 0 : data.color
};
});
}
}
const key = workflowKey || workflow && ((_a = getWorkflowData(workflow)) == null ? void 0 : _a.key);
if (!key) {
return null;
}
try {
const WorkflowCategoryRepo = ctx.db.getRepository("workflowCategory");
const CategoryRepo = ctx.db.getRepository("workflowCategories");
const workflowCategories = await WorkflowCategoryRepo.find({
filter: { workflowKey: key },
fields: ["categoryId"],
context: ctx
});
if (!workflowCategories || workflowCategories.length === 0) {
return [];
}
const categoryIds = workflowCategories.map((wc) => {
const data = wc && typeof wc.toJSON === "function" ? wc.toJSON() : wc;
return data == null ? void 0 : data.categoryId;
}).filter(Boolean);
if (categoryIds.length === 0) {
return [];
}
const categories = await CategoryRepo.find({
filter: { id: categoryIds },
fields: ["id", "name", "color"],
sort: ["sort"],
context: ctx
});
return categories.map((cat) => {
const data = cat && typeof cat.toJSON === "function" ? cat.toJSON() : cat;
return {
id: data == null ? void 0 : data.id,
name: data == null ? void 0 : data.name,
color: data == null ? void 0 : data.color
};
});
} catch (error) {
ctx.log.error("Failed to fetch categories:", error);
return null;
}
}
function setCategories(row, categories) {
row.category = categories || [];
if (row && typeof row.setDataValue === "function") {
row.setDataValue("category", categories || []);
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
getCategoriesForWorkflow,
getEventSourceNameForWorkflow,
getLatestExecutedTimeForWorkflow,
getWorkflowData,
setCategories,
setEventSourceName,
setLatestExecutedTime
});