diff --git a/app/api/leantime/status-labels/route.ts b/app/api/leantime/status-labels/route.ts index 16336d23..9986520d 100644 --- a/app/api/leantime/status-labels/route.ts +++ b/app/api/leantime/status-labels/route.ts @@ -135,29 +135,36 @@ export async function GET() { projectsData.result.map((project: any) => [project.id, project.name]) ); + // Define status code mapping + const statusMapping: Record = { + '-1': 'archived', + '0': 'new', + '1': 'in progress', + '2': 'waiting', + '3': 'in review', + '4': 'done' + }; + // Transform the nested structure into a flat array of tasks const tasks: Task[] = []; Object.entries(data.result).forEach(([projectId, statusGroups]) => { const projectName = projectsMap.get(Number(projectId)) || `Project ${projectId}`; - console.log(`Processing project ${projectId} (${projectName}):`, JSON.stringify(statusGroups, null, 2)); if (typeof statusGroups === 'object' && statusGroups !== null) { // Iterate through each status group Object.entries(statusGroups).forEach(([statusType, statusLabels]) => { - console.log(` Status type ${statusType}:`, JSON.stringify(statusLabels, null, 2)); - if (typeof statusLabels === 'object' && statusLabels !== null) { // Each status label in the group Object.values(statusLabels).forEach((label: any) => { - console.log(` Processing label:`, JSON.stringify(label, null, 2)); + const statusName = statusMapping[statusType] || `status-${statusType}`; + const headline = String(label.title || label.name || statusName); - const headline = String(label.name || label.title || 'Untitled'); tasks.push({ id: label.id?.toString() || `${projectId}-${statusType}`, headline, projectName, - status: label.type || statusType, - dueDate: null, // Status labels don't have due dates + status: statusName, + dueDate: null, details: label.description || null, milestone: label.milestone || null }); @@ -167,15 +174,14 @@ export async function GET() { } }); - console.log('Transformed tasks:', JSON.stringify(tasks, null, 2)); - // Sort tasks by status type const statusOrder: Record = { 'new': 1, 'in progress': 2, 'waiting': 3, - 'done': 4, - 'archived': 5 + 'in review': 4, + 'done': 5, + 'archived': 6 }; tasks.sort((a, b) => { @@ -184,7 +190,21 @@ export async function GET() { return statusA - statusB; }); - return NextResponse.json({ tasks }); + // Deduplicate tasks based on project and status + const uniqueTasks = tasks.reduce((acc: Task[], task) => { + const existingTask = acc.find(t => + t.projectName === task.projectName && + t.status === task.status + ); + + if (!existingTask) { + acc.push(task); + } + + return acc; + }, []); + + return NextResponse.json({ tasks: uniqueTasks }); } catch (error) { console.error('Error fetching tasks:', error); return NextResponse.json(