export async function fetchTodoistTasks(config) { if (!config.token) throw new Error('Todoist token not configured'); const headers = { 'Authorization': `Bearer ${config.token}`, 'Content-Type': 'application/json', }; // Fetch all active tasks const tasksRes = await fetch('https://api.todoist.com/rest/v2/tasks', { headers }); if (!tasksRes.ok) { throw new Error(`Todoist API error: ${tasksRes.status} ${tasksRes.statusText}`); } const tasks = await tasksRes.json(); // Fetch projects for names const projectsRes = await fetch('https://api.todoist.com/rest/v2/projects', { headers }); const projects = projectsRes.ok ? await projectsRes.json() : []; const projectMap = new Map(projects.map((p) => [p.id, p.name])); return tasks.map((t) => ({ id: `todoist-${t.id}`, title: t.content, priority: t.priority || 1, createdAt: new Date(t.created_at || Date.now()), updatedAt: new Date(t.updated_at || t.created_at || Date.now()), completedAt: null, dueDate: t.due?.date ? new Date(t.due.date) : null, projectId: t.project_id ? `todoist-${t.project_id}` : null, projectName: projectMap.get(t.project_id) || null, labels: t.labels || [], url: `https://todoist.com/app/task/${t.id}`, source: 'todoist', raw: t, })); } export async function closeTodoistTask(taskId, token) { const realId = taskId.replace('todoist-', ''); const res = await fetch(`https://api.todoist.com/rest/v2/tasks/${realId}/close`, { method: 'POST', headers: { 'Authorization': `Bearer ${token}` }, }); if (!res.ok) throw new Error(`Failed to close Todoist task: ${res.status}`); } export async function deleteTodoistTask(taskId, token) { const realId = taskId.replace('todoist-', ''); const res = await fetch(`https://api.todoist.com/rest/v2/tasks/${realId}`, { method: 'DELETE', headers: { 'Authorization': `Bearer ${token}` }, }); if (!res.ok) throw new Error(`Failed to delete Todoist task: ${res.status}`); }