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

4.3 KB
Raw Blame
import { ActionContext, createAction, Property } from '@activepieces/pieces-framework';
import {
  HttpRequest,
  HttpMethod,
  AuthenticationType,
  httpClient,
} from '@activepieces/pieces-common';
import { googleCalendarCommon, googleCalendarAuth, getAccessToken } from '../common';
import { GoogleCalendarEvent } from '../common/types';

import { eventOutputSchema } from '../output-schemas';
export const getEventByIdProps = {
  calendar_id: googleCalendarCommon.calendarDropdown(),
  event_id: Property.ShortText({
    displayName: 'Event ID',
    description: 'Paste the ID from the event URL or a previous step.',
    required: true,
  }),
  max_attendees: Property.Number({
    displayName: 'Max Attendees',
    description: 'Above this count, only you are returned.',
    required: false,
    advanced: true,
  }),
  time_zone: Property.ShortText({
    displayName: 'Time Zone',
    description: 'IANA name, e.g. America/New_York. Defaults to the calendar zone.',
    required: false,
    advanced: true,
  }),
};

export async function runGetEventById(
  context: ActionContext<typeof googleCalendarAuth, typeof getEventByIdProps>
) {
  const {
    calendar_id: calendarId,
    event_id: eventId,
    max_attendees: maxAttendees,
    time_zone: timeZone,
  } = context.propsValue;
  const token = await getAccessToken(context.auth);

  if (
    !calendarId ||
    typeof calendarId !== 'string' ||
    calendarId.trim().length === 0
  ) {
    throw new Error('Calendar ID is required');
  }

  if (
    !eventId ||
    typeof eventId !== 'string' ||
    eventId.trim().length === 0
  ) {
    throw new Error('Event ID cannot be empty');
  }

  if (eventId.length < 5 || eventId.length > 1024) {
    throw new Error('Event ID must be between 5 and 1024 characters');
  }

  const queryParams: Record<string, string> = {};

  if (maxAttendees !== undefined && maxAttendees > 0) {
    queryParams.maxAttendees = maxAttendees.toString();
  }

  if (
    timeZone &&
    typeof timeZone === 'string' &&
    timeZone.trim().length > 0
  ) {
    queryParams.timeZone = timeZone.trim();
  }

  const url = `${googleCalendarCommon.baseUrl}/calendars/${encodeURIComponent(
    calendarId.trim()
  )}/events/${encodeURIComponent(eventId.trim())}`;

  const request: HttpRequest = {
    method: HttpMethod.GET,
    url: url,
    queryParams: queryParams,
    authentication: {
      type: AuthenticationType.BEARER_TOKEN,
      token: token,
    },
  };

  try {
    const response = await httpClient.sendRequest<GoogleCalendarEvent>(
      request
    );
    return response.body;
  } catch (error: any) {
    if (error.response?.status === 404) {
      throw new Error(
        `Event with ID "${eventId}" not found in calendar "${calendarId}". Please verify the event ID and calendar selection.`
      );
    } else if (error.response?.status === 403) {
      throw new Error(
        `Access denied to event "${eventId}" in calendar "${calendarId}". Please check your permissions.`
      );
    } else if (error.response?.status === 400) {
      throw new Error(
        `Invalid request parameters. Please check the event ID format and other parameters.`
      );
    } else if (error.response?.status === 401) {
      throw new Error(
        'Authentication failed. Please reconnect your Google Calendar account.'
      );
    } else {
      throw new Error(
        `Failed to fetch event: ${error.message || 'Unknown error occurred'}`
      );
    }
  }
}

export const getEventById = createAction({
  auth: googleCalendarAuth,
  name: 'google_calendar_get_event_by_id',
  classification: 'READ',
  displayName: 'Get Event by ID',
  description: 'Fetch event details by its unique ID from Google Calendar.',
  audience: 'human',
  aiMetadata: { description: 'Fetches the full details of a single Google Calendar event by its unique event ID within a given calendar. Use when you already have an event ID (e.g. from a trigger or a list action) and need its details; use Get all Events to search when you do not. Requires a valid event ID. Read-only and idempotent.', idempotent: true },
  propertyGroups: [
    {
      key: 'event',
      display: 'section',
      label: 'Event',
      icon: 'calendar',
      props: ['calendar_id', 'event_id'],
    },
  ],
  props: getEventByIdProps,
  outputSchema: eventOutputSchema,
  run: runGetEventById,
});