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 {
	AuthenticationType,
	DedupeStrategy,
	httpClient,
	HttpMethod,
	Polling,
	pollingHelper,
	QueryParams,
} from '@activepieces/pieces-common';
import { getHubspotAccessToken, hubspotAuth } from '../auth';
import {
	AppConnectionValueForAuthProperty,
	createTrigger,
	TriggerStrategy,
} from '@activepieces/pieces-framework';
import { newEmailSubscriptionsTimelineTriggerOutputSchema } from '../output-schemas';

type SubscriptionTimeLineResponse = {
	hasMore: boolean;
	offset: string;
	timeline: Array<Record<string, any>>;
};

const polling: Polling<AppConnectionValueForAuthProperty<typeof hubspotAuth>, Record<string, any>> = {
	strategy: DedupeStrategy.TIMEBASED,
	async items({ auth, lastFetchEpochMS }) {
		const qs: QueryParams = { limit: '100' };
		if (lastFetchEpochMS) {
			qs.startTimestamp = lastFetchEpochMS.toString();
		}
		const items = [];

		let hasMore = true;
		do {
			const response = await httpClient.sendRequest<SubscriptionTimeLineResponse>({
				method: HttpMethod.GET,
				url: 'https://api.hubapi.com/email/public/v1/subscriptions/timeline',
				queryParams: qs,
				authentication: {
					type: AuthenticationType.BEARER_TOKEN,
					token: getHubspotAccessToken(auth),
				},
			});
			hasMore = response.body.hasMore;
			qs.offset = response.body.offset;
			for (const item of response.body.timeline) {
				items.push(item);
			}
		} while (hasMore);

		return items.map((item) => ({
			epochMilliSeconds: item.timestamp as number,
			data: item,
		}));
	},
};

export const newEmailSubscriptionsTimelineTrigger = createTrigger({
	auth: hubspotAuth,
	name: 'new-email-subscriptions-timeline',
	classification: 'READ',
	displayName: 'New Email Subscription Change',
	description: "Triggers when a contact's email subscription status changes.",
	aiMetadata: {
		description:
			'Fires when a new email-subscription timeline event is recorded for the HubSpot portal. Each event represents one subscription change (such as a bounce, unsubscribe, or opt-in) for a recipient, including the change type, source, and the underlying event that caused it. Polls the portal-wide email subscription timeline by timestamp.',
	},
	outputSchema: newEmailSubscriptionsTimelineTriggerOutputSchema,
	type: TriggerStrategy.POLLING,
	props: {},
	async onEnable(context) {
		await pollingHelper.onEnable(polling, context);
	},
	async onDisable(context) {
		await pollingHelper.onDisable(polling, context);
	},
	async test(context) {
		return await pollingHelper.test(polling, context);
	},
	async run(context) {
		return await pollingHelper.poll(polling, context);
	},
	sampleData: {
		timestamp: 1401975207000,
		portalId: 62515,
		recipient: '[email protected]',
		changes: [
			{
				change: 'BOUNCED',
				source: 'SOURCE_NON_DELIVERY_REPORT',
				portalId: 62515,
				changeType: 'PORTAL_BOUNCE',
				causedByEvent: {
					id: '6d72d39c-87da-3ced-bfdf-5f0213363827',
					created: 1401975207000,
				},
			},
		],
	},
});