// Task relations: dependencies and checklists import { query, queryOne, execute } from '../db.js'; interface TaskLinkArgs { from_id: string; to_id: string; link_type: string; } interface ChecklistAddArgs { task_id: string; item: string; } interface ChecklistToggleArgs { item_id: number; checked: boolean; } /** * Create a dependency between tasks */ export async function taskLink(args: TaskLinkArgs): Promise { const { from_id, to_id, link_type } = args; try { await execute( `INSERT INTO task_links (from_task_id, to_task_id, link_type) VALUES ($1, $2, $3) ON CONFLICT (from_task_id, to_task_id, link_type) DO NOTHING`, [from_id, to_id, link_type] ); return `Linked: ${from_id} ${link_type} ${to_id}`; } catch (error) { return `Error creating link: ${error}`; } } /** * Add a checklist item to a task */ export async function checklistAdd(args: ChecklistAddArgs): Promise { const { task_id, item } = args; // Get next position const result = await queryOne<{ max: number }>( `SELECT COALESCE(MAX(position), 0) + 1 as max FROM task_checklist WHERE task_id = $1`, [task_id] ); const position = result?.max || 1; await execute( `INSERT INTO task_checklist (task_id, item, position) VALUES ($1, $2, $3)`, [task_id, item, position] ); return `Added to ${task_id}: ${item}`; } /** * Toggle a checklist item */ export async function checklistToggle(args: ChecklistToggleArgs): Promise { const { item_id, checked } = args; const result = await execute( `UPDATE task_checklist SET checked = $1 WHERE id = $2`, [checked, item_id] ); if (result === 0) { return `Checklist item not found: ${item_id}`; } return `${checked ? 'Checked' : 'Unchecked'}: item #${item_id}`; }