import { getServerSession } from "next-auth/next"; import { authOptions } from "@/app/api/auth/[...nextauth]/route"; import { NextResponse } from "next/server"; export async function GET() { const session = await getServerSession(authOptions); if (!session) { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } try { // Get user's tasks from Leantime const response = await fetch('https://agilite.slm-lab.net/api/jsonrpc', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-API-Key': process.env.LEANTIME_TOKEN || '', }, body: JSON.stringify({ method: 'leantime.rpc.Tasks.Tasks.getAll', jsonrpc: '2.0', id: 1, params: { userId: session.user.id, status: ['not_started', 'in_progress'], limit: 10, sort: 'dueDate', order: 'ASC' } }) }); if (!response.ok) { throw new Error('Failed to fetch tasks from Leantime'); } const data = await response.json(); if (!data.result) { return NextResponse.json({ tasks: [] }); } // Transform the tasks to match our interface const tasks = data.result.map((task: any) => ({ id: task.id, headline: task.headline, description: task.description, status: task.status, dueDate: task.dueDate, priority: task.priority })); return NextResponse.json({ tasks }); } catch (error) { console.error('Error fetching tasks:', error); return NextResponse.json( { error: "Failed to fetch tasks" }, { status: 500 } ); } }