Edits history of script submission #16273 for ' Retrieve a statistic (gorgias)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Gorgias = {
      username: string;
      apiKey: string;
      domain: string;
    };
    /**
     * Retrieve a statistic
     *
     */
    export async function main(
      auth: Gorgias,
      name:
        | "overview"
        | "total-tickets-created"
        | "total-tickets-replied"
        | "total-tickets-closed"
        | "total-messages-sent"
        | "total-messages-received"
        | "median-first-response-time"
        | "median-resolution-time"
        | "total-one-touch-tickets"
        | "support-volume"
        | "resolution-time"
        | "first-response-time"
        | "tickets-created-per-hour-per-weekday"
        | "tickets-per-tag"
        | "tickets-created-per-channel"
        | "tickets-created-per-channel-per-day"
        | "tickets-closed-per-agent"
        | "tickets-closed-per-agent-per-day"
        | "messages-sent-per-macro"
        | "satisfaction-surveys"
        | "latest-satisfaction-surveys"
        | "intents-occurrence"
        | "intents-overview"
        | "intents-breakdown-per-day"
        | "revenue-overview"
        | "revenue-per-agent"
        | "revenue-per-day"
        | "revenue-per-ticket"
        | "automation-overview"
        | "automation-flow"
        | "automation-per-channel"
        | "self-service-overview"
        | "self-service-flows-distribution"
        | "self-service-product-with-most-issues"
        | "self-service-top-reported-issues"
        | "self-service-most-returned-products",
      body: {
        filters: {
          channels?: string[];
          period?: { end_datetime: string; start_datetime: string };
          score?: number[];
          integrations?: number[];
          agents?: number[];
          tags?: number[];
        };
      },
    ) {
      const url = new URL(`https://${auth.domain}.gorgias.com/api/stats/${name}`);
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: "Basic " + btoa(`${auth.username}:${auth.apiKey}`),
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 235 days ago