0

Update a Case

by
Published Apr 8, 2025

Updates the fields and add attachments on a > that has been previously created.

Script persona Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Persona = {
3
  apiKey: string;
4
};
5
/**
6
 * Update a Case
7
 * Updates the fields and add attachments on a > that has been previously created.
8
 */
9
export async function main(
10
  auth: Persona,
11
  case_id: string,
12
  body: {
13
    data?: {
14
      attributes?: {
15
        attachments?: { data?: string; filename?: string }[];
16
        "case-queue-id"?: string;
17
        fields?: {};
18
      };
19
    };
20
    meta?: { "sla-expires-in-seconds"?: number };
21
  },
22
  include?: string,
23
  fields?: string,
24
  Key_Inflection?: string,
25
  Idempotency_Key?: string,
26
  Persona_Version?: string,
27
) {
28
  const url = new URL(`https://api.withpersona.com/api/v1/cases/${case_id}`);
29
  for (const [k, v] of [
30
    ["include", include],
31
    ["fields", fields],
32
  ]) {
33
    if (v !== undefined && v !== "" && k !== undefined) {
34
      url.searchParams.append(k, v);
35
    }
36
  }
37
  const headers: Record<string, string> = {
38
    Authorization: `Bearer ${auth.apiKey}`,
39
    "Content-Type": "application/json",
40
  };
41
  if (Key_Inflection) {
42
    headers["Key-Inflection"] = Key_Inflection;
43
  }
44
  if (Idempotency_Key) {
45
    headers["Idempotency-Key"] = Idempotency_Key;
46
  }
47
  if (Persona_Version) {
48
    headers["Persona-Version"] = Persona_Version;
49
  }
50
  const response = await fetch(url, {
51
    method: "PATCH",
52
    headers,
53
    body: JSON.stringify(body),
54
  });
55
  if (!response.ok) {
56
    const text = await response.text();
57
    throw new Error(`${response.status} ${text}`);
58
  }
59
  return await response.json();
60
}
61