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.2 KB
Raw Blame
import {
  createTrigger,
  TriggerStrategy,
  PiecePropValueSchema,
  AppConnectionValueForAuthProperty,
} from '@activepieces/pieces-framework';
import {
  DedupeStrategy,
  Polling,
  pollingHelper,
} from '@activepieces/pieces-common';
import { weblingAuth } from '../auth';
import { weblingCommon } from '../common';
import { WeblingCalendarEvent } from '../common/types';
import { getUpdatedOrNewEvents } from '../common/helpers';

const polling: Polling<
  AppConnectionValueForAuthProperty<typeof weblingAuth>,
  { calendarId?: string }
> = {
  strategy: DedupeStrategy.TIMEBASED,
  items: async ({ auth, propsValue: { calendarId }, lastFetchEpochMS }) => {
    // implement the logic to fetch the items
    const items: WeblingCalendarEvent[] =
      (await getUpdatedOrNewEvents(auth, calendarId!, lastFetchEpochMS)) ?? [];
    return items.map((item) => ({
      epochMilliSeconds: new Date(item.meta.lastmodified).getTime(),
      data: item,
    }));
  },
};

export const onEventChanged = createTrigger({
  auth: weblingAuth,
  name: 'onEventChanged',
  displayName: 'New or Updated Event',
  description: 'Triggers when an event is added or updated.',
  aiMetadata: {
    description:
      'Fires when a calendar event in Webling is created or updated since the last poll, optionally scoped to a specific calendar. Represents a new or modified event you may want to act on.',
  },
  props: {
    calendarId: weblingCommon.calendarDropdown(),
  },
  sampleData: {},
  type: TriggerStrategy.POLLING,
  async test(context) {
    const { propsValue } = context;
    return await pollingHelper.test(polling, { ...context, propsValue: {
        calendarId: propsValue.calendarId,
      } });
  },
  async onEnable(context) {
    const { propsValue } = context;
    await pollingHelper.onEnable(polling, { ...context, propsValue: {
        calendarId: propsValue.calendarId,
      } });
  },

  async onDisable(context) {
    const { propsValue } = context;
    await pollingHelper.onDisable(polling, { ...context, propsValue: {
        calendarId: propsValue.calendarId,
      } });
  },

  async run(context) {
    const { propsValue } = context;
    return await pollingHelper.poll(polling, { ...context, propsValue: {
        calendarId: propsValue.calendarId,
      } });
  },
});