feat(bill): normalize attachment metadata
This commit is contained in:
1 parent
334c72617c
commit
9e1a13c1fe
8 files changed
+138
-3
No files matched your search
@@ -38,6 +38,7 @@ metadata:
|
||||
| 单据引用 ID 二级解析(人员/部门/费用类型) | `hose bill refs <单号|ID>` | [references/bill.md](references/bill.md) |
|
||||
| 单据关联关系 / 冲借款与前置申请追溯 | `hose bill relations <单号|ID>` | [references/bill.md](references/bill.md) |
|
||||
| 单据预算状态 / 超预算与占用信息 | `hose bill budget <单号|ID>` | [references/bill.md](references/bill.md) |
|
||||
| 单据附件 / 发票原件标准列表 | `hose bill attachments <单号|ID>` | [references/bill.md](references/bill.md) |
|
||||
| 单据 Web 详情直达链接生成 | `hose bill link <单号|ID>` | [references/bill.md](references/bill.md) |
|
||||
| 单据实体模型与字段元数据查询 | `hose bill schema` | [references/bill.md](references/bill.md) |
|
||||
| 企业通讯录检索 / 员工档案 / 防爆保护 | `hose contact search`, `hose contact list`, `hose contact get` | [references/contact.md](references/contact.md) |
|
||||
@@ -78,6 +79,7 @@ hose bill get n0GMIM0dkQmKbS # 精准查询单据详情
|
||||
hose bill refs S26000001 # 解析提交人、归属人、部门、法人实体与费用类型
|
||||
hose bill relations S26000001 # 追溯冲借款、事前申请和合同关联
|
||||
hose bill budget S26000001 # 查看单据预算超限和预算接口状态
|
||||
hose bill attachments S26000001 # 查看附件和发票原件列表
|
||||
hose bill schema # 查询单据实体元数据字典
|
||||
|
||||
# 4. 待办与消息
|
||||
|
||||
@@ -92,6 +92,17 @@ hose bill budget S26000001
|
||||
hose bill budget S26000001 --json
|
||||
```
|
||||
|
||||
### 单据附件列表 (`bill attachments`)
|
||||
|
||||
从审批详情中递归提取普通附件与发票原件的文件名、`fileId`、`key`、URL 和来源字段:
|
||||
|
||||
```bash
|
||||
hose bill attachments S26000001
|
||||
hose bill attachments S26000001 --json
|
||||
```
|
||||
|
||||
当前命令只读取并标准化附件元数据;下载通道待在带真实附件的测试单据上确认前端文件服务请求后接入。
|
||||
|
||||
## 4. 单据实体元数据字典 (`bill schema`)
|
||||
|
||||
用于探查当前企业所有单据实体、字段类型与扩展属性定义:
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
getBillRisk,
|
||||
getBillReferences,
|
||||
getBillRelations,
|
||||
getBillAttachments,
|
||||
getFieldDictionary,
|
||||
} from "../../domains/bill/tools.js";
|
||||
|
||||
@@ -356,6 +357,28 @@ export async function handleBillCommand(
|
||||
return { stdout: output, stderr: "", exitCode: 0 };
|
||||
}
|
||||
|
||||
case "attachments":
|
||||
case "attachment":
|
||||
case "files": {
|
||||
const target = parsed.args[0] || stringOption(parsed.options, ["code", "id"]);
|
||||
if (!target) throw new Error("请提供要查询附件的单号或单据 ID: hose bill attachments <单号|ID>");
|
||||
const report = await getBillAttachments(client, corpId, target);
|
||||
if (!report) {
|
||||
if (runtime.jsonMode) return { stdout: printJson({ found: false, target }, runtime.outputOptions), stderr: "", exitCode: 1 };
|
||||
return { stdout: "", stderr: `[NOT FOUND] 未找到单据附件: ${target}\n`, exitCode: 1 };
|
||||
}
|
||||
if (runtime.jsonMode) {
|
||||
return { stdout: printJson({ corpId, corpName: corp.name, ...report }, runtime.outputOptions), stderr: "", exitCode: 0 };
|
||||
}
|
||||
const rows = report.attachments.map((item) => [item.fileName || "-", item.fileId || "-", item.key || "-", item.url || "-"]);
|
||||
const output = [
|
||||
`====== 单据附件 (单号: ${report.billCode || target}) ======`,
|
||||
report.attachments.length ? printTable(["文件名", "fileId", "key", "URL"], rows) : "暂无附件或发票原件",
|
||||
"",
|
||||
].join("\n");
|
||||
return { stdout: output, stderr: "", exitCode: 0 };
|
||||
}
|
||||
|
||||
case "link":
|
||||
case "url": {
|
||||
const target = parsed.args[0] || stringOption(parsed.options, ["code", "id", "c"]);
|
||||
@@ -418,6 +441,6 @@ export async function handleBillCommand(
|
||||
}
|
||||
|
||||
default:
|
||||
throw new Error(`未知 bill 子命令: ${action}。支持: list, get <单号>, refs <单号>, relations <单号>, flow <单号>, risk <单号>, budget <单号>, schema, link <单号>`);
|
||||
throw new Error(`未知 bill 子命令: ${action}。支持: list, get <单号>, refs <单号>, relations <单号>, attachments <单号>, flow <单号>, risk <单号>, budget <单号>, schema, link <单号>`);
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -119,7 +119,7 @@ export function printHelp(): string {
|
||||
corps 多企业租户管理 (list, switch, current)
|
||||
config 全局运行模式与路径诊断 (show, paths, mode <stateful|stateless>, set)
|
||||
workflow 工作流管理 (list, get, find, toggle)
|
||||
bill 单据与费用明细检索 (list, get <单号|ID>, refs <单号|ID>, relations <单号|ID>, flow <单号|ID>, risk <单号|ID>, budget <单号|ID>, schema, link <单号|ID>)
|
||||
bill 单据与费用明细检索 (list, get <单号|ID>, refs <单号|ID>, relations <单号|ID>, attachments <单号|ID>, flow <单号|ID>, risk <单号|ID>, budget <单号|ID>, schema, link <单号|ID>)
|
||||
contact 企业通讯录与人员检索 (list, search <关键词>, get <staffId>)
|
||||
task 待办中心与审批任务 (todo, approved, records, submitted, cc)
|
||||
message 消息中心与通知管理 (list, count, read, read-all)
|
||||
@@ -165,6 +165,7 @@ export function printHelp(): string {
|
||||
hose-use bill refs S26000001 # 二级解析提交人、部门、法人实体与费用类型引用
|
||||
hose-use bill relations S26000001 # 追溯冲借款、事前申请和合同关联
|
||||
hose-use bill budget S26000001 # 查看单据预算超限和预算接口状态
|
||||
hose-use bill attachments S26000001 # 查看单据附件和发票原件标准列表
|
||||
hose-use bill link CL20260901001 # 获取单据 Web 详情直达免密跳转链接
|
||||
hose-use bill list --json -o /tmp/bills.json # 导出单据报文到本地磁盘并防爆
|
||||
hose-use bill get n0GMIM0dkQmKbS # 根据单号精准查询单据结构化详情
|
||||
|
||||
@@ -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,统一输出附件清单并去重。
|
||||
@@ -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");
|
||||
});
|
||||
});
|
||||
@@ -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(
|
||||
|
||||
@@ -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>;
|
||||
}
|
||||
Reference in new issue
Block a user