working leantime widget 44

This commit is contained in:
Alma 2025-04-12 14:55:57 +02:00
parent 6709db5c29
commit 849480b3cf
2 changed files with 143 additions and 21 deletions

View File

@ -0,0 +1,100 @@
import { NextRequest, NextResponse } from "next/server";
import { getServerSession } from "next-auth/next";
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
interface Task {
id: string;
headline: string;
projectName: string;
projectId: number;
status: number;
dueDate: string | null;
milestone: string | null;
details: string | null;
}
async function getLeantimeUserId(email: string): Promise<number | null> {
try {
const response = await fetch(`${process.env.LEANTIME_API_URL}/api/jsonrpc`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.LEANTIME_TOKEN}`,
},
body: JSON.stringify({
jsonrpc: '2.0',
method: 'leantime.rpc.users.getAll',
id: 1,
}),
});
if (!response.ok) {
console.error('Failed to fetch Leantime users');
return null;
}
const data = await response.json();
const users = data.result;
const user = users.find((u: any) => u.email === email);
return user ? user.id : null;
} catch (error) {
console.error('Error fetching Leantime user ID:', error);
return null;
}
}
export async function GET(request: NextRequest) {
try {
const session = await getServerSession(authOptions);
if (!session?.user?.email) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const userId = await getLeantimeUserId(session.user.email);
if (!userId) {
return NextResponse.json({ error: "User not found in Leantime" }, { status: 404 });
}
// Fetch tasks assigned to the user
const response = await fetch(`${process.env.LEANTIME_API_URL}/api/jsonrpc`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.LEANTIME_TOKEN}`,
},
body: JSON.stringify({
jsonrpc: '2.0',
method: 'leantime.rpc.tickets.getAll',
params: {
userId: userId,
status: "all",
},
id: 1,
}),
});
if (!response.ok) {
throw new Error('Failed to fetch tasks from Leantime');
}
const data = await response.json();
const tasks = data.result.map((task: any) => ({
id: task.id.toString(),
headline: task.headline,
projectName: task.projectName,
projectId: task.projectId,
status: task.status,
dueDate: task.dateToFinish || null,
milestone: task.milestoneName || null,
details: task.description || null,
}));
return NextResponse.json({ tasks });
} catch (error) {
console.error('Error in tasks route:', error);
return NextResponse.json(
{ error: "Failed to fetch tasks" },
{ status: 500 }
);
}
}

View File

@ -10,7 +10,7 @@ interface Task {
headline: string;
projectName: string;
projectId: number;
status: string;
status: number;
dueDate: string | null;
milestone: string | null;
details: string | null;
@ -59,9 +59,9 @@ export function Flow() {
}, {});
// Convert to array and sort tasks by due date within each project
const sortedProjectTasks = Object.entries(groupedTasks).map(([projectName, tasks]) => ({
const sortedProjectTasks = Object.entries(groupedTasks as { [key: string]: Task[] }).map(([projectName, tasks]) => ({
projectName,
tasks: tasks.sort((a, b) => {
tasks: tasks.sort((a: Task, b: Task) => {
if (!a.dueDate) return 1;
if (!b.dueDate) return -1;
return new Date(a.dueDate).getTime() - new Date(b.dueDate).getTime();
@ -108,6 +108,22 @@ export function Flow() {
});
};
const getStatusInfo = (status: number): { label: string; className: string } => {
switch (status) {
case 1:
return { label: 'NEW', className: 'bg-blue-100 text-blue-800' };
case 2:
return { label: 'IN PROGRESS', className: 'bg-yellow-100 text-yellow-800' };
case 3:
return { label: 'DONE', className: 'bg-green-100 text-green-800' };
case 4:
return { label: 'BLOCKED', className: 'bg-red-100 text-red-800' };
case 0:
default:
return { label: 'UNKNOWN', className: 'bg-gray-100 text-gray-800' };
}
};
return (
<Card className="transition-transform duration-500 ease-in-out transform hover:scale-105">
<CardHeader className="flex flex-row items-center justify-between pb-2">
@ -137,25 +153,31 @@ export function Flow() {
<div key={project.projectName} className="space-y-2">
<h3 className="font-medium text-gray-900">{project.projectName}</h3>
<div className="space-y-1">
{project.tasks.map((task) => (
<div key={task.id} className="flex items-center justify-between p-2 rounded-lg bg-white shadow-sm">
<span className="text-sm font-medium text-gray-700">
{task.headline}
</span>
<div className="flex items-center space-x-2">
<span className={`px-2 py-1 rounded-full text-xs font-medium ${
task.status === 'NEW' ? 'bg-blue-100 text-blue-800' :
task.status === 'INPROGRESS' ? 'bg-yellow-100 text-yellow-800' :
'bg-green-100 text-green-800'
}`}>
{task.status}
</span>
<span className="text-xs text-gray-500">
{formatDate(task.dueDate)}
</span>
{project.tasks.map((task) => {
const statusInfo = getStatusInfo(task.status);
return (
<div key={task.id} className="flex items-center justify-between p-2 rounded-lg bg-white shadow-sm hover:bg-gray-50 transition-colors">
<div className="flex-1 min-w-0 mr-4">
<span className="text-sm font-medium text-gray-700 block truncate" title={task.headline}>
{task.headline}
</span>
{task.milestone && (
<span className="text-xs text-gray-500 block truncate" title={task.milestone}>
{task.milestone}
</span>
)}
</div>
<div className="flex items-center space-x-2 flex-shrink-0">
<span className={`px-2 py-1 rounded-full text-xs font-medium ${statusInfo.className}`}>
{statusInfo.label}
</span>
<span className="text-xs text-gray-500 whitespace-nowrap">
{formatDate(task.dueDate)}
</span>
</div>
</div>
</div>
))}
);
})}
</div>
</div>
))}