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

59 lines
1.6 KiB
TypeScript

import { NextResponse, NextRequest } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from "@/app/api/auth/options";
export async function GET(req: NextRequest) {
try {
const session = await getServerSession(authOptions);
if (!session?.user?.email) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const nextcloudUrl = process.env.NEXTCLOUD_URL;
if (!nextcloudUrl) {
console.error('Missing Nextcloud URL');
return NextResponse.json(
{ error: 'Nextcloud configuration is missing' },
{ status: 500 }
);
}
// Test Nextcloud connectivity
const testResponse = await fetch(`${nextcloudUrl}/status.php`);
if (!testResponse.ok) {
console.error('Nextcloud is not accessible:', await testResponse.text());
return NextResponse.json(
{
error: "Nextcloud n'est pas accessible. Veuillez contacter votre administrateur.",
emails: []
},
{ status: 503 }
);
}
// For now, return a test response
return NextResponse.json({
emails: [{
id: 'test-1',
subject: 'Test Email',
sender: {
name: 'System',
email: 'system@example.com'
},
date: new Date().toISOString(),
isUnread: true
}],
mailUrl: `${nextcloudUrl}/apps/courrier/box/unified`
});
} catch (error) {
console.error('Error:', error);
return NextResponse.json(
{
error: "Une erreur est survenue. Veuillez contacter votre administrateur.",
emails: []
},
{ status: 500 }
);
}
}