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.7 KB
Raw Blame
import {
	AppConnectionValueForAuthProperty,
	createTrigger,
	PiecePropValueSchema,
	TriggerStrategy,
} from '@activepieces/pieces-framework';
import dayjs from 'dayjs';
import { todoistProjectIdDropdown } from '../common/props';
import { todoistAuth } from '../..';
import {
	DedupeStrategy,
	Polling,
	pollingHelper,
} from '@activepieces/pieces-common';
import { todoistRestClient } from '../common/client/rest-client';
import { TodoistCompletedTask } from '../common/models';
import { taskCompletedTriggerOutputSchema } from '../output-schemas';

const ISO_FORMAT = 'YYYY-MM-DDTHH:mm:ss';

const polling: Polling<AppConnectionValueForAuthProperty<typeof todoistAuth>, { project_id?: string }> = {
	strategy: DedupeStrategy.TIMEBASED,
	async items({ auth, propsValue, lastFetchEpochMS }) {
		const since =
			lastFetchEpochMS === 0
				? dayjs().subtract(5, 'minutes').format(ISO_FORMAT)
				: dayjs(lastFetchEpochMS).format(ISO_FORMAT);
		const until = dayjs().format(ISO_FORMAT);

		const tasks = await todoistRestClient.tasks.listCompleted({
			token: auth.access_token,
			since,
			until,
			project_id: propsValue.project_id,
		});

		return tasks.map((task: TodoistCompletedTask) => ({
			epochMilliSeconds: dayjs(task.completed_at).valueOf(),
			data: task,
		}));
	},
};

export const todoistTaskCompletedTrigger = createTrigger({
	auth: todoistAuth,
	name: 'task_completed',
	classification: 'READ',
	displayName: 'Task Completed',
	description: 'Fires when a task is marked complete.',
	aiMetadata: {
		description: 'Fires when a Todoist task is marked completed, emitting the completed task record. Polls for tasks closed since the last check, optionally limited to a single project (all projects if left blank).',
	},
	type: TriggerStrategy.POLLING,
	outputSchema: taskCompletedTriggerOutputSchema,

	props: {
		project_id: todoistProjectIdDropdown(
			'Leave empty to watch every project.',
		),
	},

	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: {
		id: '1899066186',
		user_id: '2671355',
		project_id: '2203306141',
		section_id: '7025',
		parent_id: null,
		added_by_uid: '2671355',
		assigned_by_uid: null,
		responsible_uid: null,
		completed_by_uid: '2671355',
		labels: [],
		checked: true,
		is_deleted: false,
		content: 'Buy Milk',
		description: '',
		priority: 1,
		note_count: 0,
		due: null,
		added_at: '2015-02-17T15:35:00.000000Z',
		completed_at: '2015-02-17T15:40:41.000000Z',
		updated_at: '2015-02-17T15:40:41.000000Z',
	},
});