UNPKG

@tachybase/module-workflow

Version:

A powerful BPM tool that provides foundational support for business automation, with the capability to extend unlimited triggers and nodes.

176 lines (175 loc) 5.39 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var ConditionInstruction_exports = {}; __export(ConditionInstruction_exports, { BRANCH_INDEX: () => BRANCH_INDEX, ConditionInstruction: () => ConditionInstruction, calculators: () => calculators, default: () => ConditionInstruction_default }); module.exports = __toCommonJS(ConditionInstruction_exports); var import_server = require("@tego/server"); var import__ = require("."); var import_constants = require("../constants"); const BRANCH_INDEX = { DEFAULT: null, ON_TRUE: 1, ON_FALSE: 0 }; const calculators = new import_server.Registry(); function equal(a, b) { return a === b; } function notEqual(a, b) { return a !== b; } function gt(a, b) { return a > b; } function gte(a, b) { return a >= b; } function lt(a, b) { return a < b; } function lte(a, b) { return a <= b; } calculators.register("equal", equal); calculators.register("notEqual", notEqual); calculators.register("gt", gt); calculators.register("gte", gte); calculators.register("lt", lt); calculators.register("lte", lte); calculators.register("==", equal); calculators.register("!=", notEqual); calculators.register(">", gt); calculators.register(">=", gte); calculators.register("<", lt); calculators.register("<=", lte); function includes(a, b) { return a.includes(b); } function notIncludes(a, b) { return !a.includes(b); } function startsWith(a, b) { return a.startsWith(b); } function notStartsWith(a, b) { return !a.startsWith(b); } function endsWith(a, b) { return a.endsWith(b); } function notEndsWith(a, b) { return !a.endsWith(b); } calculators.register("includes", includes); calculators.register("notIncludes", notIncludes); calculators.register("startsWith", startsWith); calculators.register("notStartsWith", notStartsWith); calculators.register("endsWith", endsWith); calculators.register("notEndsWith", notEndsWith); function isEmpty(a) { return a == null; } function isNotEmpty(a) { return !isEmpty(a); } function isTrue(a) { return a === true; } function isFalse(a) { return a === false; } calculators.register("isEmpty", isEmpty); calculators.register("isNotEmpty", isNotEmpty); calculators.register("isTrue", isTrue); calculators.register("isFalse", isFalse); function calculate(calculation = {}) { let fn; if (!(calculation.calculator && (fn = calculators.get(calculation.calculator)))) { throw new Error(`no calculator function registered for "${calculation.calculator}"`); } return Boolean(fn(...calculation.operands ?? [])); } function logicCalculate(calculation) { if (!calculation) { return true; } if (typeof calculation["group"] === "object") { const method = calculation["group"].type === "and" ? "every" : "some"; return (calculation["group"].calculations ?? [])[method]((item) => logicCalculate(item)); } return calculate(calculation); } class ConditionInstruction extends import__.Instruction { async run(node, prevJob, processor) { const start = prevJob.createdAt || Date.now(); const { engine, calculation, expression, rejectOnFalse } = node.config || {}; const evaluator = import_server.evaluators.get(engine); let result = true; try { result = evaluator ? evaluator(expression, processor.getScope(node.id)) : logicCalculate(processor.getParsedValue(calculation, node.id)); } catch (e) { return { result: e.toString(), status: import_constants.JOB_STATUS.ERROR }; } if (!result && rejectOnFalse) { return { status: import_constants.JOB_STATUS.FAILED, result }; } const job = { status: import_constants.JOB_STATUS.RESOLVED, result, // TODO(optimize): try unify the building of job nodeId: node.id, nodeKey: node.key, upstreamId: prevJob && prevJob.id || null, cost: Date.now() - start }; const branchNode = processor.nodes.find( (item) => item.upstreamId === node.id && item.branchIndex != null && Boolean(item.branchIndex) === result ); if (!branchNode) { return job; } const savedJob = await processor.saveJob(job); await processor.run(branchNode, savedJob); return null; } async resume(node, branchJob, processor) { const job = processor.findBranchParentJob(branchJob, node); if (branchJob.status === import_constants.JOB_STATUS.RESOLVED) { return job; } return processor.exit(branchJob.status); } } var ConditionInstruction_default = ConditionInstruction; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { BRANCH_INDEX, ConditionInstruction, calculators });