import type { KyInstance } from "ky"; import { fetchTaskPage } from "./api.js"; import type { TaskItem, TaskPageResult, TaskPerson, TaskQueryOptions, TaskView } from "./types.js"; function record(value: unknown): Record { return value && typeof value === "object" ? value as Record : {}; } function person(value: unknown): TaskPerson | undefined { const item = record(value); if (!Object.keys(item).length) return undefined; return { id: typeof item.id === "string" ? item.id : undefined, name: typeof item.name === "string" ? item.name : undefined, code: typeof item.code === "string" ? item.code : undefined, email: typeof item.email === "string" ? item.email : undefined, cellphone: typeof item.cellphone === "string" ? item.cellphone : undefined, }; } function money(value: unknown): TaskItem["amount"] { const item = record(value); if (item.standard === undefined) return undefined; return { value: String(item.standard), currency: typeof item.standardStrCode === "string" ? item.standardStrCode : undefined, symbol: typeof item.standardSymbol === "string" ? item.standardSymbol : undefined, }; } export function normalizeTask(raw: Record): TaskItem { const flow = record(raw.flowId); const form = record(flow.form ?? raw.form); const nodeState = record(raw.nodeState ?? flow.nodeState); const amount = money(form.expenseMoney ?? form.payMoney ?? form.requisitionMoney ?? form.loanMoney); const specification = record(form.specificationId); const submitter = person(form.submitterId); const owner = person(flow.ownerId ?? raw.ownerId); const actionsByStaff = record(flow.actions ?? raw.actions); const actionValues = Object.values(actionsByStaff).flatMap((value) => Array.isArray(value) ? value : []); const logs = Array.isArray(flow.logs) ? flow.logs : []; const logIndex = typeof raw.logId === "number" ? raw.logId : -1; const log = record(logs[logIndex] ?? logs[logs.length - 1]); const attributes = record(log.attributes); return { taskId: String(raw.id ?? flow.id ?? ""), flowId: typeof flow.id === "string" ? flow.id : typeof raw.id === "string" ? raw.id : undefined, billId: typeof flow.id === "string" ? flow.id : undefined, billCode: typeof form.code === "string" ? form.code : undefined, title: typeof form.title === "string" ? form.title : undefined, templateName: typeof specification.name === "string" ? specification.name : undefined, formType: typeof flow.formType === "string" ? flow.formType : typeof raw.type === "string" ? raw.type : undefined, state: typeof raw.state === "string" ? raw.state : typeof flow.state === "string" ? flow.state : undefined, action: typeof log.action === "string" ? log.action : typeof raw.action === "string" ? raw.action : undefined, actionTime: typeof log.time === "number" ? log.time : undefined, comment: typeof attributes.comment === "string" ? attributes.comment : undefined, nodeName: typeof nodeState.nodeName === "string" ? nodeState.nodeName : typeof raw.nodeName === "string" ? raw.nodeName : undefined, submitDate: typeof form.submitDate === "number" ? form.submitDate : undefined, submitter, owner, amount, actions: actionValues.filter((value): value is string => typeof value === "string"), updatedAt: typeof flow.updateTime === "number" ? flow.updateTime : typeof raw.updateTime === "number" ? raw.updateTime : undefined, raw, }; } export async function listTasks( client: KyInstance, corpId: string, view: TaskView, options: TaskQueryOptions = {} ): Promise { const page = await fetchTaskPage(client, corpId, view, options); let items = page.items.map(normalizeTask); if (options.staffId) { const target = options.staffId.toLowerCase(); items = items.filter((item) => [item.submitter?.id, item.submitter?.code, item.owner?.id, item.owner?.code] .filter(Boolean) .some((value) => String(value).toLowerCase().includes(target))); } return { view, total: page.count, start: options.start ?? 0, count: options.count ?? 20, items, }; }