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,
  Property,
  TriggerStrategy,
} from '@activepieces/pieces-framework';
import {
  AuthenticationType,
  DedupeStrategy,
  httpClient,
  HttpMethod,
  Polling,
  pollingHelper,
} from '@activepieces/pieces-common';
import { freeAgentAuth } from '../auth';

const props = {
  view: Property.StaticDropdown({
    displayName: 'User Type',
    description: 'Filter users by type',
    required: false,
    options: {
      options: [
        { label: 'All Users', value: 'all' },
        { label: 'Staff', value: 'staff' },
        { label: 'Active Staff', value: 'active_staff' },
        { label: 'Advisors', value: 'advisors' },
        { label: 'Active Advisors', value: 'active_advisors' },
      ],
    },
    defaultValue: 'all',
  }),
};

type PropsValue = {
  view?: string;
};

const polling: Polling<
  AppConnectionValueForAuthProperty<typeof freeAgentAuth>,
  PropsValue
> = {
  strategy: DedupeStrategy.TIMEBASED,
  items: async ({ auth, propsValue, lastFetchEpochMS }) => {
    const { view } = propsValue;

    let url = 'https://api.freeagent.com/v2/users?';

    if (view && view !== 'all') {
      url += `view=${view}&`;
    }

    if (lastFetchEpochMS > 0) {
      const updatedSince = new Date(lastFetchEpochMS).toISOString();
      url += `updated_since=${encodeURIComponent(updatedSince)}&`;
    }

    url = url.replace(/&$/, '');

    const response = await httpClient.sendRequest({
      method: HttpMethod.GET,
      url,
      authentication: {
        type: AuthenticationType.BEARER_TOKEN,
        token: auth.access_token,
      },
    });

    const users = response.body['users'] || [];

    return users.map((user: { created_at: string }) => ({
      epochMilliSeconds: new Date(user.created_at).getTime(),
      data: user,
    }));
  },
};

export const freeAgentNewUserTrigger = createTrigger({
  auth: freeAgentAuth,
  name: 'new_user',
  displayName: 'New User',
  description: 'Triggers when a new user is added',
  aiMetadata: {
    description: 'Fires when a new user is added to the FreeAgent account, detected by polling. Can be filtered by user type (staff, advisors, active staff/advisors) or left to watch all users.',
  },
  type: TriggerStrategy.POLLING,
  props,
  sampleData: {
    url: 'https://api.freeagent.com/v2/users/1',
    first_name: 'Development',
    last_name: 'Team',
    email: '[email protected]',
    role: 'Director',
    permission_level: 8,
    ni_number: 'QQ123456C',
    unique_tax_reference: '1234567890',
    opening_mileage: 0,
    created_at: '2011-07-28T11:25:11Z',
    updated_at: '2011-08-24T08:10:23Z',
  },
  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);
  },
});