diff --git a/src/domains/bill/tools.ts b/src/domains/bill/tools.ts index d94e326..cd2dd4b 100644 --- a/src/domains/bill/tools.ts +++ b/src/domains/bill/tools.ts @@ -402,17 +402,33 @@ export async function getBillAttachments( target: string, ): Promise { const raw = await getRawBillFlow(client, corpId, target); - if (!raw || !raw.flowId || typeof raw.flowId !== "object") return undefined; - const flow = raw.flowId as Record; - const form = flow.form && typeof flow.form === "object" ? flow.form as Record : {}; + let billId: string | undefined; + let billCode: string | undefined; + let title: string | undefined; + let form: Record = {}; + if (raw?.flowId && typeof raw.flowId === "object") { + billId = typeof raw.id === "string" ? raw.id : undefined; + const flow = raw.flowId as Record; + form = flow.form && typeof flow.form === "object" ? flow.form as Record : {}; + 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 }, };