diff --git a/packages/plugins/@tachybase/plugin-workflow/src/server/features/approval/actions/approvalCarbonCopy.ts b/packages/plugins/@tachybase/plugin-workflow/src/server/features/approval/actions/approvalCarbonCopy.ts index f0590a6ff..545a746f0 100644 --- a/packages/plugins/@tachybase/plugin-workflow/src/server/features/approval/actions/approvalCarbonCopy.ts +++ b/packages/plugins/@tachybase/plugin-workflow/src/server/features/approval/actions/approvalCarbonCopy.ts @@ -3,6 +3,7 @@ import { Op } from '@tachybase/database'; import { COLLECTION_WORKFLOWS_NAME } from '../../common/constants'; import { APPROVAL_STATUS } from '../constants'; +import { findUniqueObjects } from '../utils'; export const approvalCarbonCopy = { async listCentralized(context, next) { @@ -26,11 +27,14 @@ export const approvalCarbonCopy = { await actions.list(context, next); - // context.body = actions.list(context, next); - - // context.body - // actions.list(context, next); - // return - // context.body = userJob; + // NOTE: 进一步筛选, 筛选出同个用户下相同的approvalid, 只保留最新的一份. + if (context.body.rows) { + context.body.rows = findUniqueObjects( + context.body.rows, + ['userId', 'approvalId'], + 'createdAt', + (a: string, b: string) => new Date(a).getTime() - new Date(b).getTime(), + ); + } }, }; diff --git a/packages/plugins/@tachybase/plugin-workflow/src/server/features/approval/utils.ts b/packages/plugins/@tachybase/plugin-workflow/src/server/features/approval/utils.ts index 148f92a94..91c218c2f 100644 --- a/packages/plugins/@tachybase/plugin-workflow/src/server/features/approval/utils.ts +++ b/packages/plugins/@tachybase/plugin-workflow/src/server/features/approval/utils.ts @@ -18,3 +18,28 @@ export async function jsonParse(expression, scope): Promise { return [result]; } } + +// 根据一系列唯一性字段, 去重对象数组 +// result = [{id:1,name,2,weight:10, time:1}] +// 用法示例: findUniquifyObjects(result, ['name', 'weight'], 'time', (a, b) => a - b); +export function findUniqueObjects( + sourceArray = [], + uniqueByArray: string[], + compareByKey = '', + compareByFunc = (a, b) => a - b, +): any[] { + const uniqueMap = {}; + let result = []; + sourceArray.forEach((obj) => { + const uniqueKey = uniqueByArray.map((field) => obj[field]).join('|'); + const existingObj = uniqueMap[uniqueKey]; + + if (!existingObj || compareByFunc(obj[compareByKey], existingObj[compareByKey]) > 0) { + uniqueMap[uniqueKey] = obj; + } + }); + + result = [...Object.values(uniqueMap)]; + + return result; +}