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.4 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 Registration {
  id: number;
  description: string | null;
  startedAt: string;
  stoppedAt: string | null;
  projectId: number | null;
  userId: number;
  billable: boolean;
  tags: number[] | null;
}

interface RegistrationResponse {
  pageSize: number;
  page: number;
  totalResults: number;
  results: Registration[];
}

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

    const registrations = response.body.results ?? [];

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

export const newRegistration = createTrigger({
  auth: timeOpsAuth,
  name: 'new_registration',
  displayName: 'New Registration',
  description: 'Triggers when new registrations are added.',
  aiMetadata: {
    description: 'Fires when a new time-entry registration is added in TimeOps. Polls the registrations list and emits each newly seen registration, including its user, project, times, and billable flag. Use to react to logged time, such as updating reports or invoicing.',
  },
  type: TriggerStrategy.POLLING,
  props: {},
  sampleData: {
    id: 1,
    description: 'Working on project',
    startedAt: '2024-01-15T09:00:00Z',
    stoppedAt: '2024-01-15T17:00:00Z',
    projectId: 1,
    userId: 1,
    billable: true,
    tags: [],
  },
  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);
  },
});