import { NextResponse } from "next/server"; import { getServiceSupabase } from "@/lib/supabase/client"; export const dynamic = "force-dynamic"; export async function GET() { try { const supabase = getServiceSupabase(); // Fetch tasks with due dates, ordered by due date const { data, error } = await supabase .from("tasks") .select(` id, title, status, priority, due_date, assignee_name, projects:project_id (name) `) .not("due_date", "is", null) .order("due_date", { ascending: true }) .limit(50); if (error) { console.error("Error fetching tasks with due dates:", error); return NextResponse.json( { error: "Failed to fetch tasks" }, { status: 500 } ); } // Transform the data const tasks = (data || []).map((task: Record) => ({ id: String(task.id ?? ""), title: String(task.title ?? ""), status: String(task.status ?? "todo"), priority: String(task.priority ?? "medium"), due_date: task.due_date ? String(task.due_date) : null, assignee_name: task.assignee_name ? String(task.assignee_name) : null, project_name: task.projects && typeof task.projects === "object" && task.projects !== null ? String((task.projects as Record).name ?? "") : null, })); return NextResponse.json(tasks); } catch (error) { console.error("Error in tasks API:", error); return NextResponse.json( { error: "Internal server error" }, { status: 500 } ); } }