feat: approval, carbon copy unique record (#1349)

Reviewed-on: daoyoucloud/tachybase#1349
Co-authored-by: bai.zixv <bai.zixv@foxmail.com>
Co-committed-by: bai.zixv <bai.zixv@foxmail.com>
This commit is contained in:
bai.zixv 2024-07-19 10:23:14 +08:00 committed by sealday
parent bb82ade8cf
commit e9912a00d8
2 changed files with 35 additions and 6 deletions

View File

@ -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(),
);
}
},
};

View File

@ -18,3 +18,28 @@ export async function jsonParse(expression, scope): Promise<any[]> {
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;
}