working leantime widget 16

This commit is contained in:
Alma 2025-04-12 13:27:57 +02:00
parent e114d647ae
commit 0372dd7aed

View File

@ -133,27 +133,46 @@ export async function GET() {
projectsData.result.map((project: any) => [project.id, project.name]) projectsData.result.map((project: any) => [project.id, project.name])
); );
// Transform and categorize the tasks // Transform the nested structure into a flat array of tasks
const tasks = data.result.map((task: any) => { const tasks: Task[] = [];
const dueDate = task.dateToFinish ? new Date(task.dateToFinish * 1000) : null; Object.entries(data.result).forEach(([projectId, statusGroups]) => {
const projectName = projectsMap.get(Number(projectId)) || `Project ${projectId}`;
return { if (typeof statusGroups === 'object' && statusGroups !== null) {
id: task.id, // Iterate through each status group
headline: task.headline, Object.entries(statusGroups).forEach(([statusType, statusLabels]) => {
projectName: projectsMap.get(task.projectId) || `Project ${task.projectId}`, if (typeof statusLabels === 'object' && statusLabels !== null) {
status: task.status, // Each status label in the group
dueDate: dueDate ? dueDate.toISOString() : null, Object.values(statusLabels).forEach((label: any) => {
details: task.description, const headline = String(label.name || label.title || 'Untitled');
milestone: task.milestoneName tasks.push({
}; id: label.id?.toString() || `${projectId}-${statusType}`,
headline,
projectName,
status: label.type || statusType,
dueDate: null, // Status labels don't have due dates
details: label.description || null,
milestone: label.milestone || null
});
});
}
});
}
}); });
// Sort tasks by due date // Sort tasks by status type
tasks.sort((a: Task, b: Task) => { const statusOrder: Record<string, number> = {
if (!a.dueDate && !b.dueDate) return 0; 'new': 1,
if (!a.dueDate) return 1; 'in progress': 2,
if (!b.dueDate) return -1; 'waiting': 3,
return new Date(a.dueDate).getTime() - new Date(b.dueDate).getTime(); 'done': 4,
'archived': 5
};
tasks.sort((a, b) => {
const statusA = statusOrder[a.status.toLowerCase()] || 99;
const statusB = statusOrder[b.status.toLowerCase()] || 99;
return statusA - statusB;
}); });
return NextResponse.json({ tasks }); return NextResponse.json({ tasks });