刘光辉
118 分钟以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import type { AuditEventDetail, AuditOperation, AuditOperationPage } from '../../audit/events/types';
 
import type { AuditEventDetailDTO, AuditEventId, DmsFolderAuditPageQuery } from '#/api/x/audit/events';
 
import { getDmsFolderAuditDetail, getDmsFolderAuditPage, getDmsFolderAuditTimeline } from '#/api/x/audit/events';
import { parseAuditEventFieldDiffs } from '#/components/FormExtraPanel/auditDiff';
 
import { groupTimelineOperations, normalizeOperation } from '../../audit/events/auditOperation';
 
const detailCache = new Map<string, AuditEventDetail>();
 
export const folderAuditOperationQuery = {
  async detail(eventId: AuditEventId): Promise<AuditEventDetail> {
    const cacheKey = String(eventId);
    const cached = detailCache.get(cacheKey);
    if (cached) return cached;
 
    const response = await getDmsFolderAuditDetail(eventId);
    const bundle = response.data;
    if (!bundle?.business) throw new Error('DMS_FOLDER_AUDIT_NOT_FOUND');
 
    const business = normalizeDetail(bundle.business);
    const signatures = Array.isArray(bundle.signatures) ? bundle.signatures.map((detail) => normalizeDetail(detail)) : [];
    detailCache.clear();
    detailCache.set(String(business.id), business);
    for (const signature of signatures) detailCache.set(String(signature.id), signature);
    return business;
  },
 
  async page(query: DmsFolderAuditPageQuery): Promise<AuditOperationPage> {
    const response = await getDmsFolderAuditPage(query);
    const page = response.data;
    return {
      list: Array.isArray(page?.list) ? page.list.map((event) => normalizeOperation(event)) : [],
      pagination: page?.pagination || {
        currentPage: query.currentPage,
        pageSize: query.pageSize,
        total: 0,
      },
    };
  },
 
  async timeline(folderId: string): Promise<AuditOperation[]> {
    const response = await getDmsFolderAuditTimeline(folderId);
    return groupTimelineOperations(Array.isArray(response.data) ? response.data : []);
  },
};
 
function normalizeDetail(detail: AuditEventDetailDTO): AuditEventDetail {
  const parsedDiffs = parseAuditEventFieldDiffs(detail.fieldDiffList, detail.fieldDiffs);
  return {
    ...detail,
    fieldDiffList: parsedDiffs.diffs,
    fieldDiffParseError: parsedDiffs.parseError,
  };
}