Spaces:
Running
Running
File size: 1,277 Bytes
03608f2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import { api, ApiError } from "@/lib/api";
import type { ApprovalItem, Decision, DecisionResult } from "@/lib/types";
export interface DecideInput {
decision: Decision;
note?: string;
edited_args?: Record<string, unknown>;
}
export function needsStepUp(item: Pick<ApprovalItem, "requires_stepup">, 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<ApprovalItem, "id" | "requires_stepup">,
input: DecideInput,
requestStepUp: () => Promise<boolean>,
): Promise<DecisionResult | null> {
const stepup = needsStepUp(item, input.decision);
if (stepup && !(await requestStepUp())) return null;
const send = () =>
api<DecisionResult>(`/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;
}
}
|