diff --git a/app/api/emails/route.ts b/app/api/emails/route.ts index f95e81bf..f568d64a 100644 --- a/app/api/emails/route.ts +++ b/app/api/emails/route.ts @@ -19,8 +19,8 @@ export async function GET(req: NextRequest) { ); } - // Fetch unread messages from Nextcloud Mail API using OIDC token - const response = await fetch(`${nextcloudUrl}/ocs/v2.php/apps/mail/api/v1/folders/inbox/messages?filter=unseen`, { + // First, let's check if the Mail app is available + const checkResponse = await fetch(`${nextcloudUrl}/ocs/v2.php/cloud/apps/mail`, { headers: { 'Authorization': `Bearer ${session.accessToken}`, 'Accept': 'application/json', @@ -29,6 +29,27 @@ export async function GET(req: NextRequest) { }, }); + if (!checkResponse.ok) { + console.error('Mail app check failed:', { + status: checkResponse.status, + statusText: checkResponse.statusText, + url: checkResponse.url + }); + return NextResponse.json( + { error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." }, + { status: 404 } + ); + } + + // If Mail app is available, fetch messages + const response = await fetch(`${nextcloudUrl}/index.php/apps/mail/api/messages?unread=true`, { + headers: { + 'Authorization': `Bearer ${session.accessToken}`, + 'Accept': 'application/json', + 'Content-Type': 'application/json', + }, + }); + if (!response.ok) { console.error('Nextcloud API error:', { status: response.status, @@ -42,7 +63,7 @@ export async function GET(req: NextRequest) { if (response.status === 404) { return NextResponse.json( - { error: 'Mail app not available on Nextcloud' }, + { error: "L'application Mail n'est pas disponible sur Nextcloud. Veuillez contacter votre administrateur." }, { status: 404 } ); } @@ -53,14 +74,14 @@ export async function GET(req: NextRequest) { const data = await response.json(); // Transform the data to match our Email interface - const emails = (data?.ocs?.data || []).map((email: any) => ({ + const emails = (data || []).map((email: any) => ({ id: email.id, subject: email.subject || '(No subject)', sender: { - name: email.from?.name || email.from?.email || 'Unknown', - email: email.from?.email || 'unknown@example.com' + name: email.from?.[0]?.label || email.from?.[0]?.email || 'Unknown', + email: email.from?.[0]?.email || 'unknown@example.com' }, - date: email.sentDate, + date: email.date, isUnread: true // Since we're only fetching unread messages }));