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

1.9 KB
Raw Blame
import {
  AppConnectionValueForAuthProperty,
  createTrigger,
  TriggerStrategy,
} from '@activepieces/pieces-framework';
import {
  DedupeStrategy,
  httpClient,
  HttpMethod,
  Polling,
  pollingHelper,
} from '@activepieces/pieces-common';
import { timeOpsAuth } from '../..';
import { BASE_URL } from '../common';

interface Customer {
  id: number;
  vatNumber: string | null;
  name: string | null;
  defaultRate: number | null;
}

const polling: Polling<AppConnectionValueForAuthProperty<typeof timeOpsAuth>, Record<string, never>> = {
  strategy: DedupeStrategy.LAST_ITEM,
  items: async ({ auth }) => {
    const response = await httpClient.sendRequest<Customer[]>({
      method: HttpMethod.GET,
      url: `${BASE_URL}/Customers`,
      headers: {
        'x-api-key': auth.secret_text,
      },
    });

    const customers = response.body ?? [];

    return customers
      .sort((a, b) => b.id - a.id)
      .map((customer) => ({
        id: customer.id,
        data: customer,
      }));
  },
};

export const newCustomer = createTrigger({
  auth: timeOpsAuth,
  name: 'new_customer',
  displayName: 'New Customer',
  description: 'Triggers when a new customer is created.',
  aiMetadata: {
    description: 'Fires when a new customer is added in TimeOps. Polls the customer list and emits each newly seen customer record. Use to react to client onboarding, such as setting up a default project or notifying a team.',
  },
  type: TriggerStrategy.POLLING,
  props: {},
  sampleData: {
    id: 1,
    vatNumber: 'DK12345678',
    name: 'Example Customer',
    defaultRate: 100,
  },
  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);
  },
  async test(context) {
    return await pollingHelper.test(polling, context);
  },
});