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.0 KB
Raw Blame
import { createTrigger, TriggerStrategy } from '@activepieces/pieces-framework';
import { upgradechatAuth } from '../auth';
import { upgradechatCommon } from '../common/common';

export const newSubscription = createTrigger({
  auth: upgradechatAuth,
  name: 'newSubscription',
  displayName: 'New Subscription',
  description: 'triggers when a new subscription is created',
  aiMetadata: {
    description:
      'Fires when a subscription is created or updated in the Sperse/Upgrade.chat CRM (Subscription.CreatedOrUpdated event), delivering the contact, subscription name, dates, amount, frequency, and status. Use it to react to new or changed memberships, including renewals, plan changes, and cancellations.',
  },
  props: {},
  type: TriggerStrategy.WEBHOOK,
  sampleData: {
    eventType: 'Subscription.CreatedOrUpdated',
    contactId: 3994,
    fullname: 'Frank Micheal',
    email: '[email protected]',
    id: '536778',
    name: 'Subscription_Name',
    startDate: '2024-06-30T09:29:43.6271352Z',
    endDate: '2025-06-30T09:29:43.6271352Z',
    amount: 100,
    frequency: 'Annual',
    trialDayCount: '4',
    gracePeriodCount: '10',
    statusId: 'A',
    cancelationReason: '',
    eventTime: '2024-09-06T00:29:07',
  },
  async onEnable(context) {
    const webhookId = await upgradechatCommon.subscribeWebhook(
      'Subscription.CreatedOrUpdated',
      context.auth.props.base_url,
      context.auth.props.api_key,
      context.webhookUrl
    );

    await context.store?.put<WebhookInformation>('_new_subscription_trigger', {
      webhookId: webhookId,
    });
  },
  async onDisable(context) {
    const response = await context.store?.get<WebhookInformation>(
      '_new_subscription_trigger'
    );

    if (response !== null && response !== undefined) {
      await upgradechatCommon.unsubscribeWebhook(
        context.auth.props.base_url,
        context.auth.props.api_key,
        response.webhookId
      );
    }
  },
  async run(context) {
    return [context.payload.body];
  },
});

interface WebhookInformation {
  webhookId: number;
}