Add users to a portfolio

Adds the specified list of users as members of the portfolio. Returns the updated portfolio 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
 * Add users to a portfolio
6
 * Adds the specified list of users as members of the portfolio.
7
Returns the updated portfolio record.
8
 */
9
export async function main(
10
  auth: Asana,
11
  portfolio_gid: string,
12
  opt_pretty: string | undefined,
13
  opt_fields: string | undefined,
14
  body: {
15
    data?: { members: string; [k: string]: unknown };
16
    [k: string]: unknown;
17
  }
18
) {
19
  const url = new URL(
20
    `https://app.asana.com/api/1.0/portfolios/${portfolio_gid}/addMembers`
21
  );
22
  for (const [k, v] of [
23
    ["opt_pretty", opt_pretty],
24
    ["opt_fields", opt_fields],
25
  ]) {
26
    if (v !== undefined && v !== "") {
27
      url.searchParams.append(k, v);
28
    }
29
  }
30
  const response = await fetch(url, {
31
    method: "POST",
32
    headers: {
33
      "Content-Type": "application/json",
34
      Authorization: "Bearer " + auth.token,
35
    },
36
    body: JSON.stringify(body),
37
  });
38
  if (!response.ok) {
39
    const text = await response.text();
40
    throw new Error(`${response.status} ${text}`);
41
  }
42
  return await response.json();
43
}
44