import type { KyInstance } from "ky"; import type { TaskQueryOptions, TaskView } from "./types.js"; const JOIN = [ "flowId.form.payMoney.standardNumCode,standardNumCode,/v1/basedata/enumItems", "flowId.form.expenseMoney.standardNumCode,standardNumCode,/v1/basedata/enumItems", "flowId.form.loanMoney.standardNumCode,standardNumCode,/v1/basedata/enumItems", "flowId.form.requisitionMoney.standardNumCode,standardNumCode,/v1/basedata/enumItems", "flowId.id,nodeState,/flow/v2/backlogs/state", ]; function dateFilter(options: TaskQueryOptions): string { if (options.from === undefined && options.to === undefined) return ""; const from = options.from ?? 0; const to = options.to ?? 32503680000000; return `&&((updateTime > ${from}) && (updateTime < ${to}))`; } function approvedDateFilter(options: TaskQueryOptions): string { if (options.from === undefined && options.to === undefined) return ""; const from = options.from ?? 0; const to = options.to ?? 32503680000000; return `&&((approvedFlow.updateTime > ${from}) && (approvedFlow.updateTime < ${to}))`; } function selectForBacklog(): string { return "flowId(ownerId(id,name,enName),form(title,code,specificationId(active,id,name,enName,nameSpell,code),submitDate,submitterId(id,name,enName,nickName,code,email,cellphone,note),reviewStatus,expenseMoney,loanMoney,requisitionMoney,payMoney),`...`),`...`"; } export async function fetchTaskPage( client: KyInstance, corpId: string, view: TaskView, options: TaskQueryOptions = {} ): Promise<{ count: number; items: Record[] }> { const start = options.start ?? 0; const count = options.count ?? 20; if (view === "approved") { const response = await client.post("api/flow/v1/flows/approved", { searchParams: { join: "id,nodeState,/flow/v2/backlogs/state", corpId, }, json: { query: { filterBy: `(approvedFlow.action==\"APPROVED\")&&((formType != \"permit\"))${approvedDateFilter(options)}`, limit: { start, count }, select: "ownerId(id,name,enName),form(title,code,specificationId(active,id,name,enName,nameSpell,code),submitDate,submitterId(id,name,enName,nickName,code,email,cellphone,note),reviewStatus,expenseMoney,loanMoney,requisitionMoney,payMoney,`...`),`...`", }, }, }); const json = (await response.json()) as { count?: number; items?: Record[] }; return { count: json.count ?? json.items?.length ?? 0, items: json.items ?? [] }; } const state = view === "todo" ? "APPROVING" : "PROCESSED"; const orderBy = view === "todo" ? [ { value: "flowId.state", order: "ASC" }, { value: "isUrgent", order: "DESC" }, { value: "updateTime", order: "ASC" }, ] : [{ value: "updateTime", order: "DESC" }]; const searchParams = new URLSearchParams({ corpId }); for (const join of JOIN) searchParams.append("join", join); const response = await client.post("api/flow/v2/backlogs/search", { searchParams, json: { filterBy: `(state.in(\"${state}\"))${dateFilter(options)}`, limit: { start, count }, select: selectForBacklog(), orderBy, ...(view === "todo" ? { creditSearch: true } : {}), }, }); const json = (await response.json()) as { count?: number; items?: Record[] }; return { count: json.count ?? json.items?.length ?? 0, items: json.items ?? [] }; }