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

3.5 KB
Raw Blame
import { AppConnectionValueForAuthProperty, OAuth2PropertyValue, Property } from '@activepieces/pieces-framework';
import { createTrigger, TriggerStrategy } from '@activepieces/pieces-framework';
import { DedupeStrategy, Polling, pollingHelper } from '@activepieces/pieces-common';
import { getTaskListsDropdown, createTodoClient } from '../common';
import { microsoftToDoAuth } from '../auth';
import { PageCollection } from '@microsoft/microsoft-graph-client';
import dayjs from 'dayjs';
import { TodoTask } from '@microsoft/microsoft-graph-types';
import { newOrUpdatedTaskOutputSchema } from '../output-schemas';

const polling: Polling<AppConnectionValueForAuthProperty<typeof microsoftToDoAuth>, { task_list_id: string }> = {
	strategy: DedupeStrategy.TIMEBASED,
	async items({ auth, propsValue, store, lastFetchEpochMS }) {
		const taskListId = propsValue.task_list_id;
		const client = createTodoClient(auth);

		const tasks = [];

		const filter =
			lastFetchEpochMS === 0
				? '$top=10'
				: `$filter=lastModifiedDateTime gt ${dayjs(lastFetchEpochMS).toISOString()}`;

		let response: PageCollection = await client
			.api(`/me/todo/lists/${taskListId}/tasks?${filter}`)
			.get();

		if (lastFetchEpochMS === 0) {
			for (const task of response.value as TodoTask[]) {
				tasks.push(task);
			}
		} else {
			while (response.value.length > 0) {
				for (const task of response.value as TodoTask[]) {
					tasks.push(task);
				}

				if (response['@odata.nextLink']) {
					response = await client.api(response['@odata.nextLink']).get();
				} else {
					break;
				}
			}
		}

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

export const newOrUpdatedTaskTrigger = createTrigger({
	name: 'new_or_updated_task',
	outputSchema: newOrUpdatedTaskOutputSchema,
	classification: 'READ',
	displayName: 'New or Updated Task',
	description: 'Triggers when a new task is created or an existing task is updated.',
	aiMetadata: {
		description: 'Fires when a task in the selected Microsoft To Do task list is created or modified in any way. Each event represents the current state of one task that was added or changed; polls on the task last-modified time, so the same task can fire again whenever it is edited.',
	},
	auth: microsoftToDoAuth,
	props: {
		task_list_id: Property.Dropdown({
   auth: microsoftToDoAuth,
			displayName: 'Task List',
			required: true,
			refreshers: [],
			options: async ({ auth }) => {
				if (!(auth as OAuth2PropertyValue)?.access_token) {
					return { disabled: true, placeholder: 'Connect your account first', options: [] };
				}
				return await getTaskListsDropdown(auth as OAuth2PropertyValue);
			},
		}),
	},
	type: TriggerStrategy.POLLING,
	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: {
		'@odata.etag': 'W/"vVwdQvxCiE6779iYhchMrAAGgwrb4w=="',
		importance: 'normal',
		isReminderOn: false,
		status: 'notStarted',
		title: 'fdfdfd',
		createdDateTime: '2025-05-08T12:40:55.3080693Z',
		lastModifiedDateTime: '2025-05-08T12:40:55.3533435Z',
		hasAttachments: false,
		categories: [],
		id: 'AQMkADAwATM3ZmYAZS0xNGVmLWNiZmYALTAwAi0wMAoARgAAAw8tTPoZEYtLvE5mK48wuvIHAL1cHUL8QohOu_-YmIXITKwABoMc5_AAAAC9XB1C-EKITrvv2JiFyEysAAaDHTv2AAAA',
		body: {
			content: '',
			contentType: 'text',
		},
	},
});