1 | |
2 |
|
3 | |
4 | * List Requisitions |
5 | * Query requisitions with optional filters. Returns up to 50 records per page; paginate with offset. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Coupa, |
9 | status: |
10 | | "draft" |
11 | | "cart" |
12 | | "pending_buyer_action" |
13 | | "pending_approval" |
14 | | "approved" |
15 | | "ordered" |
16 | | "partially_received" |
17 | | "received" |
18 | | "abandoned" |
19 | | "backgrounded" |
20 | | "withdrawn" |
21 | | undefined, |
22 | requester_login: string | undefined, |
23 | updated_after: string | undefined, |
24 | exported: boolean | undefined, |
25 | limit: number | undefined, |
26 | offset: number | undefined, |
27 | return_object: "limited" | "shallow" | undefined |
28 | ) { |
29 | const base = auth.instance_url.replace(/\/+$/, "") |
30 | const url = new URL(`${base}/api/requisitions`) |
31 | const filters: { [key: string]: string | number | boolean | undefined } = { |
32 | status, |
33 | "requester[login]": requester_login, |
34 | "updated_at[gt]": updated_after, |
35 | exported, |
36 | limit, |
37 | offset, |
38 | return_object, |
39 | } |
40 | for (const [k, v] of Object.entries(filters)) { |
41 | if (v !== undefined && v !== "") { |
42 | url.searchParams.append(k, String(v)) |
43 | } |
44 | } |
45 |
|
46 | const response = await fetch(url, { |
47 | headers: { |
48 | Authorization: `Bearer ${auth.token}`, |
49 | Accept: "application/json", |
50 | }, |
51 | }) |
52 |
|
53 | if (!response.ok) { |
54 | throw new Error(`${response.status} ${await response.text()}`) |
55 | } |
56 |
|
57 | return await response.json() |
58 | } |
59 |
|