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.

127 lines (126 loc) 5.39 kB
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 SelectInstruction_exports = {}; __export(SelectInstruction_exports, { SelectInstruction: () => SelectInstruction, default: () => SelectInstruction_default }); module.exports = __toCommonJS(SelectInstruction_exports); var import_server = require("@tego/server"); var import_lodash = require("lodash"); var import__ = __toESM(require(".")); var import_constants = require("../constants"); var import_utils = require("../utils"); class SelectInstruction extends import__.default { async run(node, input, processor) { var _a, _b, _c, _d; const { collection, multiple, isTree, params = {}, failOnEmpty = false } = node.config; const otherOptions = {}; if (isTree) { otherOptions.tree = true; } const [dataSourceName, collectionName] = (0, import_server.parseCollectionName)(collection); const { repository, fields } = this.workflow.app.dataSourceManager.dataSources.get(dataSourceName).collectionManager.getCollection(collectionName); const { page, pageSize, sort = [], paginate = true, ...options } = processor.getParsedValue(params, node.id); const appends = ((_a = options.summary) == null ? void 0 : _a.includes(".")) ? [options.summary] : []; if (options.summary && !((_b = options.summary) == null ? void 0 : _b.includes("."))) { if (["belongsTo", "hasOne", "hasMany", "belongsToMany"].includes((_c = fields.get(options.summary)) == null ? void 0 : _c.type)) { appends.push(options.summary); } } let pageArgs = paginate ? import_server.utils.pageArgsToLimitArgs(page || import_server.DEFAULT_PAGE, pageSize || import_server.DEFAULT_PER_PAGE) : {}; const data = await (multiple ? repository.find : repository.findOne).call(repository, { ...options, ...otherOptions, ...pageArgs, sort: sort.filter((item) => item.field).map((item) => { var _a2; return `${((_a2 = item.direction) == null ? void 0 : _a2.toLowerCase()) === "desc" ? "-" : ""}${item.field}`; }), appends, transaction: this.workflow.useDataSourceTransaction(dataSourceName, processor.transaction) }); let result; const summary = ((_d = options.summary) == null ? void 0 : _d.length) ? options.summary.split(".") : null; if (summary) { if ((0, import_lodash.isArray)(data)) { result = []; data.forEach((item) => { const filteredValue = findField(item, summary[0], 0, summary, multiple, true); const value = (0, import_lodash.isArray)(filteredValue) ? filteredValue : [filteredValue]; result.push(...value); }); } else { result = findField(data, summary[0], 0, summary, multiple, true); } result = (0, import_lodash.isArray)(result) ? multiple ? result : result[0] : result; } if (failOnEmpty && (multiple ? !data.length : !data)) { return { result: summary ? result : data, status: import_constants.JOB_STATUS.FAILED }; } return { result: (0, import_utils.toJSON)(summary ? result : data), status: import_constants.JOB_STATUS.RESOLVED }; } } var SelectInstruction_default = SelectInstruction; const findField = (record, field, index, summary, multiple, isOne) => { if (record == null) { return void 0; } if (!multiple && !isOne) { return; } let isOneField = isOne; const current = record[field]; if (index === summary.length - 1) { if (!multiple && isOne) { isOneField = false; } return current; } const nextField = summary[index + 1]; if (Array.isArray(current)) { const results = []; current.forEach((item) => { const v = findField(item, nextField, index + 1, summary, multiple, isOneField); if (v === void 0) return; results.push(v); }); return results; } return findField(current, nextField, index + 1, summary, multiple, isOneField); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { SelectInstruction });