Edits history of script submission #23696 for ' Create Task (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 Task
     * Create a task in a workspace, optionally in projects or as a subtask of `parent`. Dates: `due_on`/`start_on` as YYYY-MM-DD, or `due_at` as an ISO 8601 date-time (set one of due_on/due_at). `custom_fields` maps custom field gids to values, e.g. {"1234": "High"}; enum fields take the enum option gid.
     */
    export async function main(
      auth: RT.Asana,
      workspace: DynSelect_workspace,
      name: string,
      notes: string | undefined,
      html_notes: string | undefined,
      assignee: string | undefined,
      projects: string[] | undefined,
      parent: string | undefined,
      due_on: string | undefined,
      due_at: string | undefined,
      start_on: string | undefined,
      tags: string[] | undefined,
      followers: string[] | undefined,
      assignee_section: string | undefined,
      completed: boolean | undefined,
      custom_fields: { [key: string]: any } | undefined
    ) {
      const data = {
        workspace,
        name,
        notes: notes || undefined,
        html_notes: html_notes || undefined,
        assignee: assignee || undefined,
        projects: projects?.length ? projects : undefined,
        parent: parent || undefined,
        due_on: due_on || undefined,
        due_at: due_at || undefined,
        start_on: start_on || undefined,
        tags: tags?.length ? tags : undefined,
        followers: followers?.length ? followers : undefined,
        assignee_section: assignee_section || undefined,
        completed: completed || undefined,
        custom_fields:
          custom_fields && Object.keys(custom_fields).length
            ? custom_fields
            : undefined,
      }
    
      const response = await fetch("https://app.asana.com/api/1.0/tasks", {
        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