Edits history of script submission #23683 for ' Update Task (asana)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Update Task
     * Update a task. Only the fields you set are changed. `completed` defaults to `keep`; use `clear_assignee` or `clear_dates` to unassign the task or remove its start and due dates. `custom_fields` maps custom field gids to values (enum fields take the enum option gid, null clears a field).
     */
    export async function main(
      auth: RT.Asana,
      task_gid: string,
      name: string | undefined,
      notes: string | undefined,
      html_notes: string | undefined,
      assignee: string | undefined,
      due_on: string | undefined,
      due_at: string | undefined,
      start_on: string | undefined,
      completed: "keep" | "complete" | "incomplete" = "keep",
      clear_assignee: boolean | undefined,
      clear_dates: boolean | undefined,
      custom_fields: { [key: string]: any } | undefined
    ) {
      const data: { [key: string]: any } = {
        name: name || undefined,
        notes: notes || undefined,
        html_notes: html_notes || undefined,
        assignee: assignee || undefined,
        due_on: due_on || undefined,
        due_at: due_at || undefined,
        start_on: start_on || undefined,
        custom_fields:
          custom_fields && Object.keys(custom_fields).length
            ? custom_fields
            : undefined,
      }
      if (completed === "complete") data.completed = true
      if (completed === "incomplete") data.completed = false
      if (clear_assignee) data.assignee = null
      if (clear_dates) {
        data.due_on = null
        data.due_at = null
        data.start_on = null
      }
    
      const response = await fetch(
        `https://app.asana.com/api/1.0/tasks/${task_gid}`,
        {
          method: "PUT",
          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