Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

2.9 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import { httpClient, HttpMethod } from '@activepieces/pieces-common';
import { freshdeskAuth } from '../..';
import { isNil } from '@activepieces/pieces-framework';

export const getTicketStatus = createAction({
  auth: freshdeskAuth,
  name: 'get_ticket_status',
  classification: 'READ',
  displayName: 'Get Ticket Status',
  description:
    'Get Ticket status from Freshdesk. Returns ticket_status, assigned_status, assigned_id',
  audience: 'both',
  aiMetadata: { description: 'Look up a single Freshdesk ticket by its numeric ticket ID and report its workflow state, returning a human-readable status (Open, Pending, Resolved, or Closed) plus whether the ticket is assigned to an agent. Use when you have a specific ticket ID and need just its status/assignment rather than the full ticket record. Requires a valid ticket ID. Read-only and idempotent.', idempotent: true },

  props: {
    ticketid: Property.ShortText({
      displayName: 'Ticket ID',
      description: 'The Ticket ID to return status',
      required: true,
    }),
  },

  async run(context) {
    const FDapiToken = context.auth.props.access_token;
    const FDticketID = context.propsValue.ticketid;

    const headers = {
      Authorization: FDapiToken,
      'Content-Type': 'application/json',
    };

    // Remove trailing slash from base_url
    const baseUrl = context.auth.props.base_url.replace(/\/$/, '');
    const url = `${baseUrl}/api/v2/tickets/${FDticketID}`;
    const httprequestdata = {
      method: HttpMethod.GET,
      url,
      headers,
    };
    const response = await httpClient.sendRequest(httprequestdata);

    if (response.status == 200) {
      const status = response.body.status;
      const responderid = response.body.responder_id;
      let AssignedStatusFriendlyValue = '';
      let TicketStatusFriendlyValue = '';

      if (isNil(responderid)) {
        AssignedStatusFriendlyValue = 'NOTASSIGNED';
      } else {
        AssignedStatusFriendlyValue = 'ASSIGNED';
      }

      switch (status) {
        case 2: {
          TicketStatusFriendlyValue = 'OPEN';
          break;
        }
        case 3: {
          TicketStatusFriendlyValue = 'PENDING';
          break;
        }
        case 4: {
          TicketStatusFriendlyValue = 'RESOLVED';
          break;
        }
        case 5: {
          TicketStatusFriendlyValue = 'CLOSED';
          break;
        }
        default: {
          // if anything other than 2,3,4 or 5 just assign the value - it shouldn't happen!
          TicketStatusFriendlyValue = status;
          break;
        }
      }
      const json = [
        {
          ticket_status: TicketStatusFriendlyValue,
          assigned_status: AssignedStatusFriendlyValue,
          assigned_id: responderid,
        },
      ];

      return json;
    } else {
      return response.status;
    }
  },
});