NeahNew/app/api/debug/leantime-methods/route.ts
2025-05-05 13:04:01 +02:00

114 lines
3.5 KiB
TypeScript

import { NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from "@/app/api/auth/options";
// GET /api/debug/leantime-methods
export async function GET(request: Request) {
try {
// Authenticate user
const session = await getServerSession(authOptions);
if (!session || !session.user?.id) {
return NextResponse.json(
{ error: "Not authenticated" },
{ status: 401 }
);
}
// Check environment variables
if (!process.env.LEANTIME_API_URL || !process.env.LEANTIME_TOKEN) {
return NextResponse.json(
{ error: "Missing Leantime API configuration" },
{ status: 500 }
);
}
// Methods to test
const methodsToTest = [
// User related methods
'leantime.rpc.users.getAll',
// Notification methods to try
'leantime.rpc.notifications.getNotifications',
'leantime.rpc.notifications.getAllNotifications',
'leantime.rpc.notifications.getMyNotifications',
'leantime.rpc.notifications.markNotificationRead',
// Alternative paths to try
'leantime.rpc.Notifications.getNotifications',
'leantime.rpc.Notifications.getAllNotifications',
'leantime.rpc.Notifications.getMyNotifications',
// More generic paths
'notifications.getNotifications',
'notifications.getAll',
'notifications.getAllByUser',
// Alternative namespaces
'leantime.domain.notifications.getNotifications',
'leantime.domain.notifications.getAll',
];
// Test each method
const results = await Promise.all(
methodsToTest.map(async (method) => {
console.log(`[LEANTIME_DEBUG] Testing method: ${method}`);
const response = await fetch(`${process.env.LEANTIME_API_URL}/api/jsonrpc`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': process.env.LEANTIME_TOKEN || ''
},
body: JSON.stringify({
jsonrpc: '2.0',
method: method,
params: {
// Include some common parameters that might be needed
userId: 2, // Using user ID 2 since that was found in logs
status: 'open',
limit: 10
},
id: 1
})
});
const responseText = await response.text();
let parsedResponse;
try {
parsedResponse = JSON.parse(responseText);
} catch (e) {
parsedResponse = { parseError: "Invalid JSON response" };
}
return {
method,
status: response.status,
success: response.ok && !parsedResponse.error,
response: parsedResponse
};
})
);
// Find successful methods
const successfulMethods = results.filter(result => result.success);
return NextResponse.json({
success: true,
timestamp: new Date().toISOString(),
totalMethodsTested: methodsToTest.length,
successfulMethods: successfulMethods.length,
successfulMethodNames: successfulMethods.map(m => m.method),
results
});
} catch (error: any) {
console.error('[LEANTIME_DEBUG] Error testing Leantime methods:', error);
return NextResponse.json(
{
error: "Internal server error",
message: error.message,
stack: process.env.NODE_ENV === 'development' ? error.stack : undefined
},
{ status: 500 }
);
}
}