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.8 KB
Raw Blame
import { createTrigger, TriggerStrategy } from '@activepieces/pieces-framework';
import { parseurAuth, parseurCommon } from '../common';
import { parserDropdown } from '../common/properties';
import { WebhookInformation } from '../common/types';

export const newDocumentProcessed = createTrigger({
  auth: parseurAuth,
  name: 'newDocumentProcessed',
  classification: 'READ',
  displayName: 'New Document Processed',
  description:
    'Fires when a new document is successfully processed and parsed by Parseur.',
  aiMetadata: {
    description:
      'Fires when a document in the selected Parseur mailbox is successfully parsed, emitting the document with its extracted/structured fields. Use to react to newly available parsed data.',
  },
  props: {
    mailboxId: parserDropdown({ required: true }),
  },
  sampleData: {},
  type: TriggerStrategy.WEBHOOK,
  async onEnable(context) {
    const webhookCreateResponse = await parseurCommon.createWebhook({
      apiKey: context.auth.secret_text,
      event: 'document.processed',
      target: context.webhookUrl,
      category: 'CUSTOM',
    });
    await parseurCommon.enableWebhook({
      apiKey: context.auth.secret_text,
      webhookId: webhookCreateResponse.id,
      mailboxId: context.propsValue.mailboxId as number,
    });
    await context.store.put<WebhookInformation>('_newDocumentProcessed', {
      webhookId: webhookCreateResponse.id,
    });
  },
  async onDisable(context) {
    const webhookInfo = await context.store.get<WebhookInformation>(
      '_newDocumentProcessed'
    );
    if (!webhookInfo) {
      return;
    }
    await parseurCommon.deleteWebhook({
      apiKey: context.auth.secret_text,
      webhookId: webhookInfo.webhookId,
    });
    await context.store.delete('_newDocumentProcessed');
  },
  async run(context) {
    return [context.payload.body];
  },
});