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

const polling: Polling<DrupalAuthType, { name: string }> = {
  strategy: DedupeStrategy.LAST_ITEM,
  items: async ({ auth, propsValue, lastItemId }) => {
    if (lastItemId === undefined || lastItemId === null) {
      lastItemId = '0';
    }
    console.debug('Polling by ID', propsValue['name'], lastItemId);
    const { website_url, username, password } = auth.props;
    const body: any = {
      name: propsValue['name'],
      id: lastItemId,
    };
    const response = await httpClient.sendRequest<DrupalPollItemId[]>({
      method: HttpMethod.POST,
      url: website_url + `/orchestration/poll`,
      body: body,
      headers: {
        'Authorization': `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`,
        'Accept': 'application/vnd.api+json',
      },
    });
    return response.body.reverse().map((item) => ({
      id: item.id,
      data: item.data,
    }));
  },
};

export const drupalPollingId = createTrigger({
  auth: drupalAuth,
  name: 'drupalPollingId',
  classification: 'READ',
  displayName: 'Polling by ID',
  description: 'A trigger that polls the Drupal site by ID.',
  aiMetadata: {
    description: 'Fires for each new item pushed to a named Drupal orchestration poll, tracked by an incrementing item ID so only items newer than the last seen ID are emitted. Fires once per new item; the poll name must match the name configured on the Drupal side (e.g. an ECA poll event).',
  },
  props: {
    name: Property.ShortText({
      displayName: 'Name',
      description: 'The name identifies the poll. It must be unique. It will be used to identify the poll in the Drupal site, e.g. if you use ECA to respond to this poll, you need to use the same name in the configuration of its poll event.',
      required: true,
    }),
  },
  sampleData: {},
  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);
  },
});

interface DrupalPollItemId {
  data: any;
  id: string;
}