feat(bill): normalize attachment metadata

This commit is contained in:
gitea committed 2026-09-05 20:11:33 +08:00
1 parent 334c72617c
commit 9e1a13c1fe
8 files changed
+138 -3

No files matched your search

+2
View File
@@ -27,3 +27,5 @@ src/domains/bill/
- 保留原始 ID,并在 JSON 中提供 `name`、`code`、部门 `path` 与未解析 ID;目录在进程内按企业缓存 10 分钟。
5. **单据关联链 (`getBillRelations`)**:
- 从审批详情中的 `expenseLink`、`loan`、`requisition`、`contract` 等关系字段抽取前置单据树,保留原始对象供 JSON 下钻。
6. **附件标准化 (`getBillAttachments`)**:
- 递归识别表单中的 `attachments`、`fileId`、`fileName`、`key` 与原件 URL,统一输出附件清单并去重。
+23 -1
View File
@@ -1,5 +1,5 @@
import { describe, it, expect } from "bun:test";
import { formatSingleBill, getBillReferences, getBillRelations } from "./tools.js";
import { formatSingleBill, getBillReferences, getBillRelations, getBillAttachments } from "./tools.js";
import { RawBillItemSchema, EntitySchema } from "./types.js";
describe("Bill Domain", () => {
@@ -123,4 +123,26 @@ describe("Bill Domain", () => {
expect(result?.relations.map((item) => item.id)).toEqual(["loan-1", "req-1"]);
expect(result?.relations[0]?.amount).toContain("500");
});
it("normalizes nested bill attachments and removes duplicates", async () => {
const client = {
post: async () => new Response(JSON.stringify({ value: {
id: "backlog-3",
flowId: {
form: {
code: "E26000002",
title: "带附件报销",
attachments: [
{ fileId: "file-1", key: "receipt.pdf", fileName: "receipt.pdf" },
{ fileId: "file-1", key: "receipt.pdf", fileName: "receipt.pdf" },
],
},
},
} })),
} as any;
const result = await getBillAttachments(client, "corp-files", "E26000002");
expect(result?.attachments).toHaveLength(1);
expect(result?.attachments[0]?.fileId).toBe("file-1");
expect(result?.attachments[0]?.fileName).toBe("receipt.pdf");
});
});
+55
View File
@@ -25,6 +25,8 @@ import {
type BillReferenceReport,
type BillRelationNode,
type BillRelationsReport,
type BillAttachment,
type BillAttachmentReport,
} from "./types.js";
import { CorpContext } from "../../core/config/manager.js";
import { createEkbClient } from "../../core/auth/ekb.js";
@@ -337,6 +339,59 @@ export async function getBillRelations(
};
}
function attachmentFromValue(sourceField: string, value: unknown): BillAttachment | undefined {
if (!value || typeof value !== "object") return undefined;
const object = value as Record<string, unknown>;
const nestedFile = object.fileId && typeof object.fileId === "object" ? object.fileId as Record<string, unknown> : undefined;
const fileId = typeof object.fileId === "string" ? object.fileId : typeof nestedFile?.id === "string" ? nestedFile.id : undefined;
const key = typeof object.key === "string" ? object.key : typeof nestedFile?.key === "string" ? nestedFile.key : undefined;
const fileName = typeof object.fileName === "string" ? object.fileName : typeof object.name === "string" ? object.name : undefined;
const url = typeof object.url === "string" ? object.url : typeof nestedFile?.url === "string" ? nestedFile.url : undefined;
const thumbUrl = typeof object.thumbUrl === "string" ? object.thumbUrl : typeof nestedFile?.thumbUrl === "string" ? nestedFile.thumbUrl : undefined;
if (!fileId && !key && !fileName && !url) return undefined;
return { fileId, key, fileName, url, thumbUrl, sourceField, raw: object };
}
function collectAttachments(sourceField: string, value: unknown, output: BillAttachment[]): void {
if (Array.isArray(value)) {
for (const item of value) collectAttachments(sourceField, item, output);
return;
}
if (!value || typeof value !== "object") return;
const object = value as Record<string, unknown>;
const looksLikeAttachment = sourceField.toLowerCase().includes("attachment") || "fileId" in object || "fileName" in object || "thumbUrl" in object;
if (looksLikeAttachment) {
const attachment = attachmentFromValue(sourceField, value);
if (attachment) output.push(attachment);
}
for (const [key, nested] of Object.entries(object)) {
if (key === "fileId" || key === "fileName" || key === "url" || key === "thumbUrl" || key === "key") continue;
if (nested && typeof nested === "object") collectAttachments(sourceField === "form" ? key : `${sourceField}.${key}`, nested, output);
}
}
export async function getBillAttachments(
client: KyInstance,
corpId: string,
target: string,
): Promise<BillAttachmentReport | undefined> {
const raw = await getRawBillFlow(client, corpId, target);
if (!raw || !raw.flowId || typeof raw.flowId !== "object") return undefined;
const flow = raw.flowId as Record<string, unknown>;
const form = flow.form && typeof flow.form === "object" ? flow.form as Record<string, unknown> : {};
const attachments: BillAttachment[] = [];
collectAttachments("form", form, attachments);
const deduped = [...new Map(attachments.map((item) => [`${item.fileId || ""}|${item.key || ""}|${item.fileName || ""}`, item])).values()];
return {
target,
billId: typeof raw.id === "string" ? raw.id : undefined,
billCode: typeof form.code === "string" ? form.code : undefined,
title: typeof form.title === "string" ? form.title : undefined,
attachments: deduped,
raw: { form },
};
}
// ── 格式化单据数据 ─────────────────────────────────────────────────────────────
export function formatSingleBill(
+19
View File
@@ -199,3 +199,22 @@ export interface BillRelationsReport {
relations: BillRelationNode[];
raw: Record<string, unknown>;
}
export interface BillAttachment {
fileId?: string;
key?: string;
fileName?: string;
url?: string;
thumbUrl?: string;
sourceField: string;
raw: Record<string, unknown>;
}
export interface BillAttachmentReport {
target: string;
billId?: string;
billCode?: string;
title?: string;
attachments: BillAttachment[];
raw: Record<string, unknown>;
}