PR-AGENT / src /api /form.ts
Seth
Update
de686dc
export type DynamicField = {
id: string;
label: string;
type: "select" | "number" | "text" | "chips" | "textarea";
options?: string[];
/** Shown beside numeric inputs (from schema or inferred from label). */
unit?: string;
};
export type FormSchemaResponse = {
fields: DynamicField[];
interval_options?: string[];
source?: string;
error?: string;
};
export type PrRow = {
pr_line_item: number;
mat_grp: number;
pr_short_text: string;
pr_long_text: string;
pr_quantity: number;
delivery_date: string;
};
const base = () =>
(import.meta.env.VITE_API_BASE as string | undefined)?.replace(/\/$/, "") ?? "";
export async function fetchFormSchema(
commodityCode: number
): Promise<FormSchemaResponse> {
const res = await fetch(`${base()}/api/form-schema/${commodityCode}`);
if (!res.ok) throw new Error(await res.text());
return res.json() as Promise<FormSchemaResponse>;
}
export async function buildPrLines(body: {
commodity_code: number;
dynamic_values: Record<string, string | number>;
deliveries: number;
interval: string;
other_spec: string;
year: number;
}): Promise<{ rows: PrRow[] }> {
const res = await fetch(`${base()}/api/build-pr`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
if (!res.ok) throw new Error(await res.text());
return res.json() as Promise<{ rows: PrRow[] }>;
}