import { api, ApiError } from "@/lib/api"; import type { ApprovalItem, Decision, DecisionResult } from "@/lib/types"; export interface DecideInput { decision: Decision; note?: string; edited_args?: Record; } export function needsStepUp(item: Pick, d: Decision): boolean { return item.requires_stepup && (d === "approve" || d === "edit"); } /** POST a decision. Step-up is collected first when the item says it needs it, and once more * if the server still asks for it (e.g. the 5-minute token expired in between). */ export async function decide( item: Pick, input: DecideInput, requestStepUp: () => Promise, ): Promise { const stepup = needsStepUp(item, input.decision); if (stepup && !(await requestStepUp())) return null; const send = () => api(`/approvals/${item.id}/decision`, { method: "POST", body: input, stepup, }); try { return await send(); } catch (e) { if (e instanceof ApiError && e.status === 409 && /step-up/i.test(e.detail)) { if (!(await requestStepUp())) return null; return send(); } throw e; } }