File size: 1,416 Bytes
865237e
 
 
 
 
de686dc
 
865237e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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[] }>;
}