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.5 KB
Raw Blame
import {
  createTrigger,
  TriggerStrategy,
  AppConnectionValueForAuthProperty,
} from '@activepieces/pieces-framework';
import {
  DedupeStrategy,
  HttpMethod,
  Polling,
  pollingHelper,
} from '@activepieces/pieces-common';
import { echowinAuth } from '../common/auth';
import dayjs from 'dayjs';
import { makeRequest } from '../common/client';

const polling: Polling<
  AppConnectionValueForAuthProperty<typeof echowinAuth>,
  Record<string, never>
> = {
  strategy: DedupeStrategy.TIMEBASED,
  items: async ({ auth, lastFetchEpochMS }) => {
    const queryParams = new URLSearchParams({
      page: '1',
      limit: '100',
      sortBy: 'createdAt',
      sortOrder: 'desc',
    });

    const response = await makeRequest(
      auth.secret_text,
      HttpMethod.GET,
      `/contacts?${queryParams.toString()}`
    );

    const contacts = response.data;

    return contacts
      .filter((contact: any) => {
        const createdAtEpoch = dayjs(contact.createdAt).valueOf();
        return createdAtEpoch > lastFetchEpochMS;
      })
      .map((contact: any) => ({
        epochMilliSeconds: dayjs(contact.createdAt).valueOf(),
        data: contact,
      }));
  },
};

export const newContact = createTrigger({
  auth: echowinAuth,
  name: 'newContact',
  displayName: 'New Contact',
  description: 'Trigger when a new contact is created in Echowin',
  aiMetadata: {
    description:
      'Fires when a new contact is created in the Echowin CRM. Polls for contacts whose creation time is later than the last check, emitting one event per newly created contact.',
  },
  props: {},
  sampleData: {
    id: '123e4567-e89b-12d3-a456-426614174000',
    firstName: 'John',
    lastName: 'Doe',
    email: '[email protected]',
    number: '+15551234567',
    carrier: 'AT&T',
    customFields: {
      company: 'Acme Corp',
      role: 'Manager',
    },
    tags: [
      {
        id: 'tag-id-1',
        name: 'VIP',
        color: '#10B981',
      },
      {
        id: 'tag-id-2',
        name: 'Lead',
        color: '#3B82F6',
      },
    ],
    crmStage: {
      id: 'stage-id',
      name: 'Lead',
    },
    createdAt: '2024-01-01T00:00:00.000Z',
  },
  type: TriggerStrategy.POLLING,
  async test(context) {
    return await pollingHelper.test(polling, context);
  },
  async onEnable(context) {
    await pollingHelper.onEnable(polling, context);
  },
  async onDisable(context) {
    await pollingHelper.onDisable(polling, context);
  },
  async run(context) {
    return await pollingHelper.poll(polling, context);
  },
});