fix: resolve attachments from draft bills
This commit is contained in:
1 parent
7d9f656567
commit
e44b120cc2
1 file changed
+22
-6
@@ -402,17 +402,33 @@ export async function getBillAttachments(
|
||||
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> : {};
|
||||
let billId: string | undefined;
|
||||
let billCode: string | undefined;
|
||||
let title: string | undefined;
|
||||
let form: Record<string, unknown> = {};
|
||||
if (raw?.flowId && typeof raw.flowId === "object") {
|
||||
billId = typeof raw.id === "string" ? raw.id : undefined;
|
||||
const flow = raw.flowId as Record<string, unknown>;
|
||||
form = flow.form && typeof flow.form === "object" ? flow.form as Record<string, unknown> : {};
|
||||
billCode = typeof form.code === "string" ? form.code : undefined;
|
||||
title = typeof form.title === "string" ? form.title : undefined;
|
||||
} else {
|
||||
const search = await searchRawBills(client, corpId, { count: 100 });
|
||||
const item = search.items.find((candidate) => candidate.id === target || candidate.code === target);
|
||||
if (!item) return undefined;
|
||||
billId = item.id;
|
||||
billCode = item.code;
|
||||
title = item.form?.title as string | undefined;
|
||||
form = item.form || {};
|
||||
}
|
||||
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,
|
||||
billId,
|
||||
billCode,
|
||||
title,
|
||||
attachments: deduped,
|
||||
raw: { form },
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user