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.4 KB
Raw Blame
import { Property, createAction, OAuth2PropertyValue } from '@activepieces/pieces-framework';
import { getTaskListsDropdown, getIncompleteTasksInListDropdown, createTodoClient } from '../common';
import { microsoftToDoAuth } from '../auth';
import { TodoTask } from '@microsoft/microsoft-graph-types';
import { completeTaskOutputSchema } from '../output-schemas';

export const completeTaskAction = createAction({
    auth: microsoftToDoAuth,
    name: 'complete_task',
    outputSchema: completeTaskOutputSchema,
    classification: 'WRITE',
    displayName: 'Complete Task',
    description: 'Marks a task as completed.',
    audience: 'human',
    aiMetadata: { description: 'Mark a specific Microsoft To Do task as completed, setting its status to completed and stamping a completion time. Use when a task should be closed out; identify it by task list id and task id (resolve via list/find actions). Idempotent in effect — completing an already-completed task leaves it completed, though it refreshes the completion timestamp.', idempotent: true },
    props: {
        task_list_id: Property.Dropdown({
   auth: microsoftToDoAuth,
            displayName: 'Task List',
            description: 'The task list containing the task you want to complete.',
            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 specific task to mark as complete.',
            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 getIncompleteTasksInListDropdown(authValue, task_list_id as string);
            },
        }),
    },
    async run(context) {
        const { auth, propsValue } = context;
        const { task_list_id, task_id } = propsValue;

        if (!task_list_id || !task_id) {
            throw new Error('Task List ID and Task ID are required');
        }

        const client = createTodoClient(auth);

        try {
            const taskUpdate: Partial<TodoTask> = {
                status: 'completed',
                completedDateTime: {
                    dateTime: new Date().toISOString(),
                    timeZone: 'UTC',
                },
            };

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

            return response as TodoTask;
        } catch (error: any) {
            throw new Error(`Failed to complete task: ${error?.message || error}`);
        }
    },
});