import type { KyInstance } from "ky"; import { RawHoseBacklogItemSchema, type RawHoseBacklogDetailResponse, type RawHoseBacklogItem, type RawHoseBacklogSearchResponse } from "./types.js"; export const HOSE_CLOSE_LOOKUP_SELECT = "flowId(form(code,title),`...`),`...`"; export const HOSE_CLOSE_DETAIL_SELECT = "flowId(form(specificationId(id,name,code),`...`),logs(operatorId,nextOperatorId,`...`),plan(`...`),`...`),`...`"; export const HOSE_CLOSE_JOINS = [ "flowId.form.submitterId,submitterId,/v1/organization/staffs?join=defaultDepartment,defaultDepartment,/v1/organization/departments", "flowId.form.ownerId,ownerId,/v1/organization/staffs?select=id,name,code", "flowId.form.expenseDepartment,expenseDepartment,/v1/organization/departments", "flowId.logs.operatorId,operatorId,/v1/organization/staffs?select=id,name,code", "flowId.logs.nextOperatorId,nextOperatorId,/v1/organization/staffs?select=id,name,code", "flowId.plan.nodes.approverId,approverId,/v1/organization/staffs?select=id,name,code", "flowId.plan.nodes.counterSigners.signerId,signerId,/v1/organization/staffs?select=id,name,code" ] as const; export interface SearchCloseBacklogsOptions { filterBy: string; limit?: { start: number; count: number }; select?: string; } export interface CloseBacklogDetailOptions { select?: string; joins?: readonly string[] | string[]; } export async function searchCloseBacklogs( client: KyInstance, corpId: string, options: SearchCloseBacklogsOptions ): Promise { const response = await client.post("api/flow/v2/backlogs/search", { searchParams: { corpId }, json: { filterBy: options.filterBy, limit: options.limit ?? { start: 0, count: 100 }, select: options.select ?? HOSE_CLOSE_LOOKUP_SELECT } }); const data = (await response.json()) as RawHoseBacklogSearchResponse; return (data.items ?? []).map((item) => RawHoseBacklogItemSchema.parse(item)); } export async function getCloseBacklogDetailById( client: KyInstance, corpId: string, backlogId: string, options?: CloseBacklogDetailOptions ): Promise | undefined> { const params = new URLSearchParams({ corpId }); const joins = options?.joins ?? HOSE_CLOSE_JOINS; for (const join of joins) { params.append("join", join); } const response = await client.post("api/flow/v2/backlogs/id", { searchParams: params, json: { filterBy: `(id==${JSON.stringify(backlogId)})`, select: options?.select ?? HOSE_CLOSE_DETAIL_SELECT } }); const data = (await response.json()) as RawHoseBacklogDetailResponse; return data.value ?? data.items?.[0]; } export async function resolveCloseBacklogId( client: KyInstance, corpId: string, target: string ): Promise { const cleanTarget = target.trim(); if (!cleanTarget) return target; const filters = [ `(id==${JSON.stringify(cleanTarget)})||((flowId.id==${JSON.stringify(cleanTarget)})||(flowId.form.code==${JSON.stringify(cleanTarget)}))`, `(state.in("APPROVING","PROCESSED","REJECTED","DRAFT"))` ]; for (const filterBy of filters) { try { const items = await searchCloseBacklogs(client, corpId, { filterBy, limit: { start: 0, count: 100 }, select: HOSE_CLOSE_LOOKUP_SELECT }); const match = items.find((item) => { const flow = item.flowId && typeof item.flowId === "object" ? (item.flowId as Record) : {}; const form = flow.form && typeof flow.form === "object" ? (flow.form as Record) : {}; return item.id === cleanTarget || flow.id === cleanTarget || form.code === cleanTarget; }); if (match && typeof match.id === "string") { return match.id; } } catch { // Fall through to next filter or return raw target } } return cleanTarget; } export async function fetchRawCloseHoseFlow( client: KyInstance, corpId: string, target: string ): Promise | undefined> { const backlogId = await resolveCloseBacklogId(client, corpId, target); return getCloseBacklogDetailById(client, corpId, backlogId); }