import parse from 'json-templates'; import evaluators, { Evaluator } from '@nocobase/evaluators'; import { Processor } from '..'; import { JOB_STATUS } from "../constants"; import FlowNodeModel from "../models/FlowNode"; import { Instruction } from "."; interface CalculationConfig { dynamic?: boolean | string; engine?: string; expression?: string; } export default { async run(node: FlowNodeModel, prevJob, processor: Processor) { const { dynamic = false } = node.config || {}; let { engine = 'math.js', expression = '' } = node.config; let scope = processor.getScope(); if (dynamic) { const parsed = parse(dynamic)(scope) ?? {}; engine = parsed.engine; expression = parsed.expression; scope = parse(node.config.scope ?? '')(scope) ?? {}; } const evaluator = evaluators.get(engine); try { const result = evaluator && expression ? evaluator(expression, scope) : null; return { result, status: JOB_STATUS.RESOLVED }; } catch (e) { return { result: e.toString(), status: JOB_STATUS.ERROR } } } } as Instruction;