138 lines
3.9 KiB
TypeScript
138 lines
3.9 KiB
TypeScript
import { getServerSession } from "next-auth/next";
|
|
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
|
|
import { NextResponse } from "next/server";
|
|
|
|
interface StatusLabel {
|
|
id: string;
|
|
name: string;
|
|
class: string;
|
|
statusType: string;
|
|
kanbanCol: boolean | string;
|
|
sortKey: number | string;
|
|
}
|
|
|
|
interface Project {
|
|
id: number;
|
|
name: string;
|
|
labels: StatusLabel[];
|
|
}
|
|
|
|
// Cache for user IDs to avoid repeated lookups
|
|
const userCache = new Map<string, number>();
|
|
|
|
export async function GET() {
|
|
try {
|
|
const session = await getServerSession(authOptions);
|
|
|
|
if (!session?.user?.email) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
// Check cache first
|
|
let leantimeUserId = userCache.get(session.user.email);
|
|
|
|
// If not in cache, fetch from API
|
|
if (!leantimeUserId) {
|
|
const userResponse = 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: session.user.email
|
|
}
|
|
})
|
|
});
|
|
|
|
if (!userResponse.ok) {
|
|
throw new Error('Failed to fetch user from Leantime');
|
|
}
|
|
|
|
const userData = await userResponse.json();
|
|
|
|
if (!userData.result || !userData.result.id) {
|
|
return NextResponse.json({ error: "User not found in Leantime" }, { status: 404 });
|
|
}
|
|
|
|
leantimeUserId = userData.result.id;
|
|
// Cache the user ID for 5 minutes
|
|
userCache.set(session.user.email, leantimeUserId);
|
|
setTimeout(() => userCache.delete(session.user.email!), 5 * 60 * 1000);
|
|
}
|
|
|
|
// Get projects
|
|
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 from Leantime');
|
|
}
|
|
|
|
const projectsData = await projectsResponse.json();
|
|
|
|
// Get status labels
|
|
const labelsResponse = 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.getAllStatusLabelsByUserId',
|
|
id: 1,
|
|
params: {
|
|
userId: leantimeUserId
|
|
}
|
|
})
|
|
});
|
|
|
|
if (!labelsResponse.ok) {
|
|
throw new Error('Failed to fetch status labels from Leantime');
|
|
}
|
|
|
|
const labelsData = await labelsResponse.json();
|
|
|
|
// Transform the data into the required format
|
|
const projects: Project[] = projectsData.result.map((project: any) => {
|
|
const projectLabels = labelsData.result[project.id];
|
|
const labels: StatusLabel[] = projectLabels ? Object.entries(projectLabels).map(([key, value]: [string, any]) => ({
|
|
id: key,
|
|
name: value.name,
|
|
class: value.class,
|
|
statusType: value.statusType,
|
|
kanbanCol: value.kanbanCol,
|
|
sortKey: value.sortKey
|
|
})).sort((a: StatusLabel, b: StatusLabel) => Number(a.sortKey) - Number(b.sortKey)) : [];
|
|
|
|
return {
|
|
id: project.id,
|
|
name: project.name,
|
|
labels
|
|
};
|
|
}).filter((project: Project) => project.labels.length > 0);
|
|
|
|
return NextResponse.json({ projects });
|
|
} catch (error) {
|
|
console.error('Error fetching status labels:', error);
|
|
return NextResponse.json(
|
|
{ error: "Failed to fetch status labels" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|