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 {
  AppConnectionValueForAuthProperty,
  createTrigger,
  MarkdownVariant,
  Property,
  StaticPropsValue,
  TriggerStrategy,
} from '@activepieces/pieces-framework';
import {
  DedupeStrategy,
  HttpMethod,
  Polling,
  pollingHelper,
} from '@activepieces/pieces-common';
import { calltidycalapi } from '../common';
import { tidyCalAuth } from '../auth';
import dayjs from 'dayjs';

const props = {
  info: Property.MarkDown({
    value: 'Checks TidyCal periodically and runs once for each new booking.',
    variant: MarkdownVariant.INFO,
  }),
};

export const tidycalnewbooking = createTrigger({
  auth: tidyCalAuth,
  name: 'new_booking',
  classification: 'READ',
  displayName: 'New Booking',
  description: 'Triggers when a new booking is created',
  aiMetadata: {
    description: 'Fires when a new (non-canceled) booking is created in TidyCal. The event represents the scheduled appointment, including its id, booking type, start/end times, timezone, meeting URL, intake question answers, creation timestamp, and the associated contact. Use this to react to newly scheduled meetings, such as sending confirmations or creating calendar events.',
  },
  props,
  sampleData: {
    id: 1,
    contact_id: 1,
    booking_type_id: 1,
    starts_at: '2022-01-01T00:00:00.000000Z',
    ends_at: '2022-02-01T00:00:00.000000Z',
    cancelled_at: null,
    created_at: '2022-02-01T00:00:00.000000Z',
    updated_at: '2022-02-01T00:00:00.000000Z',
    timezone: 'America/Los_Angeles',
    meeting_url: 'https://zoom.us/j/949494949494',
    meeting_id: 'fw44lkj48fks',
    questions: [],
    contact: {
      id: 1,
      email: '[email protected]',
      name: 'John Doe',
      created_at: '2022-01-01T00:00:00.000000Z',
      updated_at: '2022-01-01T00:00:00.000000Z',
    },
  },
  type: TriggerStrategy.POLLING,
  onEnable: async (context) => {
    await pollingHelper.onEnable(polling, context);
  },
  onDisable: async (context) => {
    await pollingHelper.onDisable(polling, context);
  },
  run: async (context) => {
    return await pollingHelper.poll(polling, context);
  },
  test: async (context) => {
    return await pollingHelper.test(polling, context);
  },
});

const polling: Polling<
  AppConnectionValueForAuthProperty<typeof tidyCalAuth>,
  StaticPropsValue<typeof props>
> = {
  strategy: DedupeStrategy.TIMEBASED,
  items: async ({ auth, lastFetchEpochMS }) => {
    const currentValues = await calltidycalapi<{
      data: {
        id: number;
        created_at: string;
      }[];
    }>(HttpMethod.GET, `bookings?cancelled=false`, auth, undefined);

    const createdBookings = currentValues.body;
    const bookings = createdBookings.data.filter((item) => {
      const created_at = dayjs(item.created_at);
      return created_at.isAfter(lastFetchEpochMS);
    });
    return bookings.map((item) => {
      return {
        epochMilliSeconds: dayjs(item.created_at).valueOf(),
        data: item,
      };
    });
  },
};