1 | |
2 |
|
3 | |
4 | * List Invoices |
5 | * Query invoices 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 | | "new" |
11 | | "draft" |
12 | | "pending_approval" |
13 | | "approved" |
14 | | "pending_receipt" |
15 | | "processing" |
16 | | "payable_adjustment" |
17 | | "on_hold" |
18 | | "ap_hold" |
19 | | "booking_hold" |
20 | | "pending_action" |
21 | | "rejected" |
22 | | "disputed" |
23 | | "abandoned" |
24 | | "voided" |
25 | | "invalid" |
26 | | undefined, |
27 | invoice_number: string | undefined, |
28 | supplier_name: string | undefined, |
29 | created_after: string | undefined, |
30 | updated_after: string | undefined, |
31 | exported: boolean | undefined, |
32 | limit: number | undefined, |
33 | offset: number | undefined, |
34 | return_object: "limited" | "shallow" | undefined |
35 | ) { |
36 | const base = auth.instance_url.replace(/\/+$/, "") |
37 | const url = new URL(`${base}/api/invoices`) |
38 | const filters: { [key: string]: string | number | boolean | undefined } = { |
39 | status, |
40 | invoice_number, |
41 | "supplier[name]": supplier_name, |
42 | "created_at[gt]": created_after, |
43 | "updated_at[gt]": updated_after, |
44 | exported, |
45 | limit, |
46 | offset, |
47 | return_object, |
48 | } |
49 | for (const [k, v] of Object.entries(filters)) { |
50 | if (v !== undefined && v !== "") { |
51 | url.searchParams.append(k, String(v)) |
52 | } |
53 | } |
54 |
|
55 | const response = await fetch(url, { |
56 | headers: { |
57 | Authorization: `Bearer ${auth.token}`, |
58 | Accept: "application/json", |
59 | }, |
60 | }) |
61 |
|
62 | if (!response.ok) { |
63 | throw new Error(`${response.status} ${await response.text()}`) |
64 | } |
65 |
|
66 | return await response.json() |
67 | } |
68 |
|