1 | |
2 | type Persona = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Update an Inquiry |
7 | * Updates an existing Inquiry. |
8 |
|
9 | Note that if you use webhooks, updates to inquiries that are not in progress can result in data getting out of sync. For example, updating a completed Inquiry will not cause your Inquiry completed webhook to retrigger. |
10 |
|
11 | Inquiries represent a snapshot of data collected from an individual, so we generally do not recommend updating an Inquiry's data after the Inquiry has been finalized. |
12 | */ |
13 | export async function main( |
14 | auth: Persona, |
15 | inquiry_id: string, |
16 | body: { |
17 | data?: { |
18 | attributes?: { |
19 | note?: string; |
20 | fields?: |
21 | | {} |
22 | | ({ |
23 | birthdate?: string; |
24 | "name-first"?: string; |
25 | "name-middle"?: string; |
26 | "name-last"?: string; |
27 | "phone-number"?: string; |
28 | "email-address"?: string; |
29 | } & { |
30 | "address-street-1"?: string; |
31 | "address-street-2"?: string; |
32 | "address-city"?: string; |
33 | "address-subdivision"?: string; |
34 | "address-postal-code"?: string; |
35 | } & { "address-country-code"?: string }); |
36 | tags?: string[]; |
37 | }; |
38 | }; |
39 | }, |
40 | include?: string, |
41 | fields?: string, |
42 | Key_Inflection?: string, |
43 | Idempotency_Key?: string, |
44 | Persona_Version?: string, |
45 | ) { |
46 | const url = new URL( |
47 | `https://api.withpersona.com/api/v1/inquiries/${inquiry_id}`, |
48 | ); |
49 | for (const [k, v] of [ |
50 | ["include", include], |
51 | ["fields", fields], |
52 | ]) { |
53 | if (v !== undefined && v !== "" && k !== undefined) { |
54 | url.searchParams.append(k, v); |
55 | } |
56 | } |
57 | const headers: Record<string, string> = { |
58 | Authorization: `Bearer ${auth.apiKey}`, |
59 | "Content-Type": "application/json", |
60 | }; |
61 | if (Key_Inflection) { |
62 | headers["Key-Inflection"] = Key_Inflection; |
63 | } |
64 | if (Idempotency_Key) { |
65 | headers["Idempotency-Key"] = Idempotency_Key; |
66 | } |
67 | if (Persona_Version) { |
68 | headers["Persona-Version"] = Persona_Version; |
69 | } |
70 | const response = await fetch(url, { |
71 | method: "PATCH", |
72 | headers, |
73 | body: JSON.stringify(body), |
74 | }); |
75 | if (!response.ok) { |
76 | const text = await response.text(); |
77 | throw new Error(`${response.status} ${text}`); |
78 | } |
79 | return await response.json(); |
80 | } |
81 |
|