170 lines
4.8 KiB
TypeScript
170 lines
4.8 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { getServerSession } from "next-auth/next";
|
|
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
|
|
import { prisma } from "@/lib/prisma";
|
|
|
|
// Cache for Leantime user IDs
|
|
const userCache = new Map<string, number>();
|
|
|
|
interface Task {
|
|
id: string;
|
|
headline: string;
|
|
projectName: string;
|
|
projectId: number;
|
|
status: number;
|
|
dateToFinish: string | null;
|
|
milestone: string | null;
|
|
details: string | null;
|
|
createdOn: string;
|
|
editedOn: string | null;
|
|
editorId: string;
|
|
editorFirstname: string;
|
|
editorLastname: string;
|
|
}
|
|
|
|
async function getLeantimeUserId(email: string): Promise<number | null> {
|
|
// Check cache first
|
|
if (userCache.has(email)) {
|
|
return userCache.get(email)!;
|
|
}
|
|
|
|
try {
|
|
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({
|
|
jsonrpc: '2.0',
|
|
method: 'leantime.rpc.Users.Users.getUserByEmail',
|
|
id: 1,
|
|
params: {
|
|
email: email
|
|
}
|
|
}),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`Failed to fetch user from Leantime: ${response.status} ${response.statusText}`);
|
|
}
|
|
|
|
const data = await response.json();
|
|
|
|
if (!data.result || data.result === false) {
|
|
return null;
|
|
}
|
|
|
|
// Cache the user ID
|
|
userCache.set(email, data.result.id);
|
|
// Clear cache after 5 minutes
|
|
setTimeout(() => userCache.delete(email), 5 * 60 * 1000);
|
|
return data.result.id;
|
|
} catch (error) {
|
|
console.error('Error getting Leantime user ID:', error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
const session = await getServerSession(authOptions);
|
|
|
|
if (!session) {
|
|
return NextResponse.json(
|
|
{ error: "Unauthorized", message: "No session found. Please sign in." },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
if (!session.user?.email) {
|
|
return NextResponse.json(
|
|
{ error: "Unauthorized", message: "No email found in session. Please sign in again." },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
// Get Leantime user ID
|
|
const leantimeUserId = await getLeantimeUserId(session.user.email);
|
|
|
|
if (!leantimeUserId) {
|
|
return NextResponse.json(
|
|
{ error: "User not found", message: "Could not find user in Leantime. Please check your email." },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
// Get all tasks assigned to the user
|
|
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({
|
|
jsonrpc: '2.0',
|
|
method: 'leantime.rpc.Tickets.Tickets.getAll',
|
|
id: 1,
|
|
params: {
|
|
projectId: 0, // 0 means all projects
|
|
userId: leantimeUserId,
|
|
status: "all",
|
|
limit: 100
|
|
}
|
|
})
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`Failed to fetch tasks: ${response.status} ${response.statusText}`);
|
|
}
|
|
|
|
const data = await response.json();
|
|
|
|
if (!data.result) {
|
|
return NextResponse.json({ tasks: [] });
|
|
}
|
|
|
|
// Filter out tasks that are not in progress or new
|
|
const filteredTasks = data.result.filter((task: any) => {
|
|
const status = task.status.toString();
|
|
return status === '1' || status === '2'; // 1 = new, 2 = in progress
|
|
});
|
|
|
|
// Get project details to include project names
|
|
const projectsResponse = 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({
|
|
jsonrpc: '2.0',
|
|
method: 'leantime.rpc.Projects.getAll',
|
|
id: 1
|
|
})
|
|
});
|
|
|
|
if (!projectsResponse.ok) {
|
|
throw new Error(`Failed to fetch projects: ${projectsResponse.status} ${projectsResponse.statusText}`);
|
|
}
|
|
|
|
const projectsData = await projectsResponse.json();
|
|
|
|
// Map tasks to include project names
|
|
const tasksWithProjects = filteredTasks.map((task: any) => {
|
|
const project = projectsData.result.find((p: any) => p.id === task.projectId);
|
|
return {
|
|
...task,
|
|
projectName: project ? project.name : `Project ${task.projectId}`
|
|
};
|
|
});
|
|
|
|
return NextResponse.json({ tasks: tasksWithProjects });
|
|
} catch (error) {
|
|
console.error('Error fetching tasks:', error);
|
|
return NextResponse.json(
|
|
{ error: "Failed to fetch tasks", message: error instanceof Error ? error.message : "Unknown error occurred" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|