Update a goal metric

Updates a goal's existing metric's `current_number_value` if one exists, otherwise responds with a 400 status code. Returns the complete updated goal metric record.

Script asana Verified

by hugo697 ยท 10/31/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Asana = {
2
  token: string;
3
};
4
/**
5
 * Update a goal metric
6
 * Updates a goal's existing metric's `current_number_value` if one exists,
7
otherwise responds with a 400 status code.
8

9
Returns the complete updated goal metric record.
10
 */
11
export async function main(
12
  auth: Asana,
13
  goal_gid: string,
14
  opt_pretty: string | undefined,
15
  opt_fields: string | undefined,
16
  body: {
17
    data?: { gid?: string; resource_type?: string; [k: string]: unknown } & {
18
      current_number_value?: number;
19
      [k: string]: unknown;
20
    };
21
    [k: string]: unknown;
22
  }
23
) {
24
  const url = new URL(
25
    `https://app.asana.com/api/1.0/goals/${goal_gid}/setMetricCurrentValue`
26
  );
27
  for (const [k, v] of [
28
    ["opt_pretty", opt_pretty],
29
    ["opt_fields", opt_fields],
30
  ]) {
31
    if (v !== undefined && v !== "") {
32
      url.searchParams.append(k, v);
33
    }
34
  }
35
  const response = await fetch(url, {
36
    method: "POST",
37
    headers: {
38
      "Content-Type": "application/json",
39
      Authorization: "Bearer " + auth.token,
40
    },
41
    body: JSON.stringify(body),
42
  });
43
  if (!response.ok) {
44
    const text = await response.text();
45
    throw new Error(`${response.status} ${text}`);
46
  }
47
  return await response.json();
48
}
49