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

5.0 KB
Raw Blame
import { Property, createAction, OAuth2PropertyValue } from '@activepieces/pieces-framework';
import { getTaskListsDropdown, getTasksInListDropdown, createTodoClient } from '../common';
import { microsoftToDoAuth } from '../auth';
import { Importance, TaskStatus, TodoTask } from '@microsoft/microsoft-graph-types';
import { createTaskOutputSchema } from '../output-schemas';

export const updateTaskAction = createAction({
	auth: microsoftToDoAuth,
	name: 'update_task',
	outputSchema: createTaskOutputSchema,
	classification: 'WRITE',
	displayName: 'Update Task',
	description: 'Update an existing task.',
	audience: 'human',
	aiMetadata: { description: 'Update fields of an existing Microsoft To Do task identified by its task list id and task id — title, body/notes, importance, status, due/reminder/start dates, or categories. Use to edit an already-created task; resolve the task id first via list/find actions. Idempotent in effect: re-sending the same field values converges the task to that state without additional side effects.', idempotent: true },
	props: {
		task_list_id: Property.Dropdown({
			auth: microsoftToDoAuth,
			displayName: 'Task List',
			description: 'The task list containing the task to update.',
			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);
			},
		}),
		task_id: Property.Dropdown({
			auth: microsoftToDoAuth,
			displayName: 'Task',
			description: 'The task to update.',
			required: true,
			refreshers: ['task_list_id'],
			options: async ({ auth, task_list_id }) => {
				const authValue = auth as OAuth2PropertyValue;
				if (!authValue?.access_token || !task_list_id) {
					return {
						disabled: true,
						placeholder: !authValue?.access_token
							? 'Connect your account first'
							: 'Select a task list first',
						options: [],
					};
				}
				return await getTasksInListDropdown(authValue, task_list_id as string);
			},
		}),
		title: Property.ShortText({
			displayName: 'Title',
			description: 'The title of the task.',
			required: false,
		}),
		body_content: Property.LongText({
			displayName: 'Body Content',
			description: 'The body or notes for the task.',
			required: false,
		}),
		importance: Property.StaticDropdown({
			displayName: 'Importance',
			description: 'The importance of the task.',
			required: false,
			options: {
				options: [
					{ label: 'Low', value: 'low' },
					{ label: 'Normal', value: 'normal' },
					{ label: 'High', value: 'high' },
				],
			},
		}),
		status: Property.StaticDropdown({
			displayName: 'Status',
			description: 'The status of the task.',
			required: false,
			options: {
				options: [
					{ label: 'Not Started', value: 'notStarted' },
					{ label: 'In Progress', value: 'inProgress' },
					{ label: 'Completed', value: 'completed' },
					{ label: 'Waiting On Others', value: 'waitingOnOthers' },
					{ label: 'Deferred', value: 'deferred' },
				],
			},
		}),
		due_date_time: Property.DateTime({
			displayName: 'Due Date',
			required: false,
		}),
		reminder_date_time: Property.DateTime({
			displayName: 'Reminder Date',
			required: false,
		}),
		start_date_time: Property.DateTime({
			displayName: 'Start Date',
			description: 'The date and time the task is scheduled to start.',
			required: false,
		}),
		categories: Property.ShortText({
			displayName: 'Categories',
			description: 'Comma-separated categories for the task (e.g., Work, Personal).',
			required: false,
		}),
	},
	async run(context) {
		const { auth, propsValue } = context;
		const {
			task_list_id,
			task_id,
			title,
			body_content,
			importance,
			status,
			due_date_time,
			reminder_date_time,
			start_date_time,
			categories,
		} = propsValue;

		const client = createTodoClient(auth);

		const taskBody: TodoTask = {
			title,
		};

		if (body_content) {
			taskBody.body = {
				content: body_content,
				contentType: 'text',
			};
		}

		if (importance) {
			taskBody.importance = importance as Importance;
		}

		if (status) {
			taskBody.status = status as TaskStatus;
		}

		if (due_date_time) {
			taskBody.dueDateTime = {
				dateTime: due_date_time,
				timeZone: 'UTC',
			};
		}

		if (reminder_date_time) {
			taskBody.isReminderOn = true;
			taskBody.reminderDateTime = { dateTime: reminder_date_time, timeZone: 'UTC' };
		}

		if (start_date_time) {
			taskBody.startDateTime = { dateTime: start_date_time, timeZone: 'UTC' };
		}

		if (categories && categories !== '') {
			taskBody.categories = categories
				.split(',')
				.map((c) => c.trim())
				.filter((c) => c.length > 0);
		}

		// Only send request if there's something to update
		if (Object.keys(taskBody).length === 0) {
			throw new Error('Please provide any field to update.');
		}

		const response = await client
			.api(`/me/todo/lists/${task_list_id}/tasks/${task_id}`)
			.update(taskBody);

		return response;
	},
});