Edits history of script submission #22743 for ' Add Prospect to Sequence (outreach)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_sequence = string
    
    // Resolver: lists the org's sequences for the dropdown.
    export async function sequence(auth: RT.Outreach) {
      const url = new URL("https://api.outreach.io/api/v2/sequences")
      url.searchParams.append("sort", "name")
      url.searchParams.append("page[size]", "100")
      const response = await fetch(url, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/vnd.api+json",
        },
      })
      const { data } = (await response.json()) as {
        data: { id: number; attributes: { name: string } }[]
      }
      return data.map((s) => ({ value: String(s.id), label: s.attributes.name }))
    }
    
    export type DynSelect_mailbox = string
    
    // Resolver: lists the org's sending mailboxes for the dropdown.
    export async function mailbox(auth: RT.Outreach) {
      const url = new URL("https://api.outreach.io/api/v2/mailboxes")
      url.searchParams.append("page[size]", "100")
      const response = await fetch(url, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/vnd.api+json",
        },
      })
      const { data } = (await response.json()) as {
        data: { id: number; attributes: { email: string } }[]
      }
      return data.map((m) => ({ value: String(m.id), label: m.attributes.email }))
    }
    
    /**
     * Add Prospect to Sequence
     * Adds an existing prospect to a sequence, sending from the selected mailbox.
     */
    export async function main(
      auth: RT.Outreach,
      prospect_id: number,
      sequence: DynSelect_sequence,
      mailbox: DynSelect_mailbox
    ) {
      const response = await fetch(
        "https://api.outreach.io/api/v2/sequenceStates",
        {
          method: "POST",
          headers: {
            Authorization: `Bearer ${auth.token}`,
            "Content-Type": "application/vnd.api+json",
            Accept: "application/vnd.api+json",
          },
          body: JSON.stringify({
            data: {
              type: "sequenceState",
              relationships: {
                prospect: { data: { type: "prospect", id: prospect_id } },
                sequence: { data: { type: "sequence", id: Number(sequence) } },
                mailbox: { data: { type: "mailbox", id: Number(mailbox) } },
              },
            },
          }),
        }
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 hours ago