UNPKG

@tachybase/plugin-workflow-approval

Version:

Approval base in Workflow

312 lines (311 loc) 11 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 tools_exports = {}; __export(tools_exports, { getSummary: () => getSummary, parsePerson: () => parsePerson }); module.exports = __toCommonJS(tools_exports); var import_lodash = __toESM(require("lodash")); var import_constants = require("../common/constants"); var import_utils = require("../common/utils"); async function parsePerson({ node, processor, keyName }) { var _a; const configPerson = processor.getParsedValue(((_a = node.config) == null ? void 0 : _a[keyName]) ?? [], node.id).flat().filter(Boolean); const targetPerson = /* @__PURE__ */ new Set(); const UserRepo = processor.options.plugin.app.db.getRepository("users"); for (const item of configPerson) { if (typeof item === "object") { const users = await UserRepo.find({ ...item, fields: ["id"], transaction: processor.transaction }); users.forEach((userData) => targetPerson.add(userData.id)); } else { targetPerson.add(item); } } return [...targetPerson]; } function getSummary(params) { const { summaryConfig = [], data, collection, app } = params; const summaryDataSource = getSummaryDataSource({ summaryConfig, data, collection, app }); return summaryDataSource; } function getAssociationTitleFieldValue(value, fieldName, collection, app) { if (!collection && !app || typeof value !== "object" || value === null || Array.isArray(value)) { return void 0; } let field; let dataSource; let targetCollection; if (collection) { field = collection.getField(fieldName); if (!field || !field.target) { return void 0; } const db = collection.db; if (db) { targetCollection = db.getCollection(field.target); } else if (app && app.dataSourceManager) { const collectionDataSource = collection.dataSource; if (collectionDataSource) { dataSource = app.dataSourceManager.dataSources.get(collectionDataSource); } else { dataSource = app.dataSourceManager.dataSources.get("main"); } if (dataSource) { targetCollection = dataSource.collectionManager.getCollection(field.target); } } else { try { const cm = collection.collectionManager; if (cm) { dataSource = cm.dataSource; targetCollection = dataSource.collectionManager.getCollection(field.target); } } catch (e) { } } } else if (app) { return void 0; } if (!targetCollection) { return void 0; } const titleField = targetCollection.titleField || targetCollection.getPrimaryKey() || "id"; const titleValue = value[titleField]; return titleValue !== void 0 && titleValue !== null ? String(titleValue) : void 0; } function getFieldLabel(key, collection) { var _a; if (!collection) { return key; } const field = collection.getField(key); if (field) { const uiSchema = (_a = field.options) == null ? void 0 : _a.uiSchema; if (uiSchema == null ? void 0 : uiSchema.title) { return uiSchema.title; } if (field.title) { return field.title; } } return key; } function processValue(value, fieldName, collection, app) { if (value === void 0 || value === null) { return ""; } if (value instanceof Date || typeof value.toDate === "function" && value.toDate() instanceof Date) { const dateObj = value instanceof Date ? value : value.toDate(); return dateObj.toISOString(); } if (typeof value === "object" && !Array.isArray(value)) { const titleFieldValue = getAssociationTitleFieldValue(value, fieldName, collection, app); if (titleFieldValue !== void 0) { return titleFieldValue; } if (value.name !== void 0) { return String(value.name); } return JSON.stringify(value); } if (typeof value === "number") { return value; } return String(value); } function getSummaryItem(key, data, collection, app) { const value = import_lodash.default.get(data, key); const label = getFieldLabel(key, collection); if (value === void 0 || value === null) { return { key, label, type: import_constants.SUMMARY_TYPE.LITERAL, value: "" }; } if (Array.isArray(value)) { const processedArray = []; for (const item of value) { const processedValue2 = processValue(item, key, collection, app); processedArray.push(processedValue2); } return { key, label, type: import_constants.SUMMARY_TYPE.ARRAY, value: processedArray }; } if (value instanceof Date || typeof value.toDate === "function" && value.toDate() instanceof Date) { const dateObj = value instanceof Date ? value : value.toDate(); return { key, label, type: import_constants.SUMMARY_TYPE.DATE, value: dateObj.toISOString() }; } const processedValue = processValue(value, key, collection, app); const valueStr = String(processedValue); const finalType = (0, import_utils.isDateType)(valueStr) ? import_constants.SUMMARY_TYPE.DATE : import_constants.SUMMARY_TYPE.LITERAL; return { key, label, type: finalType, value: processedValue }; } function getTargetCollection(mainKey, collection, app) { if (!collection) { return void 0; } const field = collection.getField(mainKey); if (!field || !field.target) { return void 0; } let targetCollection; const db = collection.db; if (db) { targetCollection = db.getCollection(field.target); } else if (app && app.dataSourceManager) { const collectionDataSource = collection.dataSource; let dataSource; if (collectionDataSource) { dataSource = app.dataSourceManager.dataSources.get(collectionDataSource); } else { dataSource = app.dataSourceManager.dataSources.get("main"); } if (dataSource) { targetCollection = dataSource.collectionManager.getCollection(field.target); } } return targetCollection; } function isManyToManyField(mainKey, collection) { if (!collection) { return false; } const field = collection.getField(mainKey); if (!field) { return false; } return field.type === "belongsToMany" || field.type === "hasMany"; } function getSummaryDataSource({ summaryConfig = [], data, collection, app }) { const mainPathKeys = []; const subPathKeys = []; for (const key of summaryConfig) { if (key.includes(".")) { subPathKeys.push(key); } else { mainPathKeys.push(key); } } const summaryDataSource = []; for (const mainKey of mainPathKeys) { const parentValue = import_lodash.default.get(data, mainKey); const label = getFieldLabel(mainKey, collection); const isManyToMany = isManyToManyField(mainKey, collection); if (isManyToMany && Array.isArray(parentValue) && parentValue.length > 0) { const mainKeyPrefix = `${mainKey}.`; const relevantSubKeys = subPathKeys.filter((subKey) => subKey.startsWith(mainKeyPrefix)); if (relevantSubKeys.length === 0) { summaryDataSource.push(getSummaryItem(mainKey, data, collection, app)); } else { const fieldPathMap = /* @__PURE__ */ new Map(); for (const subKey of relevantSubKeys) { const relativePath = subKey.slice(mainKeyPrefix.length); const fieldName = relativePath.split(".")[0]; if (!fieldPathMap.has(fieldName)) { fieldPathMap.set(fieldName, relativePath); } else { const existingPath = fieldPathMap.get(fieldName); if (relativePath.length > existingPath.length) { fieldPathMap.set(fieldName, relativePath); } } } const targetFields = Array.from(fieldPathMap.keys()); const childValues = []; let isValidTable = true; let rowCount = 0; for (const fieldName of targetFields) { const relativePath = fieldPathMap.get(fieldName); const fullSubKey = `${mainKey}.${fieldName}`; const childArray = []; for (let i = 0; i < parentValue.length; i++) { const item = parentValue[i]; const childValue = import_lodash.default.get(item, relativePath); const processedValue = processValue(childValue, fullSubKey, collection, app); childArray.push(processedValue); } childValues.push(childArray); if (rowCount === 0) { rowCount = childArray.length; } else if (rowCount !== childArray.length) { isValidTable = false; } } if (isValidTable && rowCount > 0) { const targetCollection = getTargetCollection(mainKey, collection, app); const tableFields = targetFields.map((fieldName, idx) => { const childLabel = targetCollection ? getFieldLabel(fieldName, targetCollection) : fieldName; return { key: fieldName, label: childLabel, type: import_constants.SUMMARY_TYPE.ARRAY, value: childValues[idx] }; }); summaryDataSource.push({ key: mainKey, label, type: import_constants.SUMMARY_TYPE.TABLE, value: tableFields }); } else { summaryDataSource.push(getSummaryItem(mainKey, data, collection, app)); } } } else { summaryDataSource.push(getSummaryItem(mainKey, data, collection, app)); } } return summaryDataSource; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { getSummary, parsePerson });