Edits history of script submission #23700 for ' Create Project (asana)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_workspace = string
    
    // Resolver: the workspaces and organizations the token can access.
    export async function workspace(auth: RT.Asana) {
      const response = await fetch(
        "https://app.asana.com/api/1.0/workspaces?limit=100",
        {
          headers: {
            Authorization: `Bearer ${auth.token}`,
            Accept: "application/json",
          },
        }
      )
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { data } = await response.json()
      return data.map((w: { gid: string; name: string }) => ({
        value: w.gid,
        label: w.name,
      }))
    }
    
    /**
     * Create Project
     * Create a project in a workspace. In an organization, `team` is required (see List Teams). Dates as YYYY-MM-DD.
     */
    export async function main(
      auth: RT.Asana,
      workspace: DynSelect_workspace,
      name: string,
      team: string | undefined,
      notes: string | undefined,
      html_notes: string | undefined,
      owner: string | undefined,
      default_view: "list" | "board" | "calendar" | "timeline" | undefined,
      privacy_setting:
        "public_to_workspace" | "private_to_team" | "private" | undefined,
      color: string | undefined,
      start_on: string | undefined,
      due_on: string | undefined,
      followers: string[] | undefined
    ) {
      const data = {
        workspace,
        name,
        team: team || undefined,
        notes: notes || undefined,
        html_notes: html_notes || undefined,
        owner: owner || undefined,
        default_view: default_view || undefined,
        privacy_setting: privacy_setting || undefined,
        color: color || undefined,
        start_on: start_on || undefined,
        due_on: due_on || undefined,
        followers: followers?.length ? followers.join(",") : undefined,
      }
    
      const response = await fetch("https://app.asana.com/api/1.0/projects", {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify({ data }),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 3 hours ago