Edits history of script submission #22690 for ' Cancel Agreement (adobe_acrobat_sign)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    async function apiBase(auth: RT.AdobeAcrobatSign): Promise<string> {
      if (auth.base_uri) return auth.base_uri.replace(/\/+$/, "")
      const r = await fetch("https://api.adobesign.com/api/rest/v6/baseUris", {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
      if (!r.ok) throw new Error(`${r.status} ${await r.text()}`)
      const { apiAccessPoint } = (await r.json()) as { apiAccessPoint: string }
      return apiAccessPoint.replace(/\/+$/, "")
    }
    
    /**
     * Cancel Agreement
     * Cancel an in-process agreement by setting its state to CANCELLED, optionally notifying the other participants.
     */
    export async function main(
      auth: RT.AdobeAcrobatSign,
      agreement_id: string,
      comment: string | undefined,
      notify_others: boolean | undefined
    ) {
      const base = await apiBase(auth)
      const url = new URL(`${base}/api/rest/v6/agreements/${agreement_id}/state`)
    
      const response = await fetch(url, {
        method: "PUT",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify({
          state: "CANCELLED",
          agreementCancellationInfo: {
            comment: comment ?? "",
            notifyOthers: notify_others ?? false,
          },
        }),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      const text = await response.text()
      return text ? JSON.parse(text) : { success: true }
    }
    

    Submitted by hugo989 5 days ago