/* * NOTE: This endpoint is now mostly for backward compatibility. * The main email list API (/api/courrier) now fetches full email content, * so individual email fetching is typically not needed. * This is kept for cases where individual email access is still required * or when using older client code. */ import { NextResponse } from 'next/server'; import { getServerSession } from 'next-auth'; import { authOptions } from '@/app/api/auth/[...nextauth]/route'; import { getEmailContent, markEmailReadStatus } from '@/lib/services/email-service'; import { getCachedEmailContent, invalidateEmailContentCache } from '@/lib/redis'; export async function GET( request: Request, context: { params: { id: string } } ) { try { const session = await getServerSession(authOptions); if (!session || !session.user?.id) { return NextResponse.json( { error: "Not authenticated" }, { status: 401 } ); } // Await params as per Next.js requirements const params = await context.params; const id = params?.id; if (!id) { return NextResponse.json( { error: "Missing email ID" }, { status: 400 } ); } const { searchParams } = new URL(request.url); const folder = searchParams.get("folder") || "INBOX"; const accountId = searchParams.get("accountId"); try { // Try to get email from Redis cache first const cachedEmail = await getCachedEmailContent(session.user.id, accountId || 'default', id); if (cachedEmail) { console.log(`Using cached email content for ${session.user.id}:${id}`); return NextResponse.json(cachedEmail); } console.log(`Cache miss for email content ${session.user.id}:${id}, fetching from IMAP`); // Use the email service to fetch the email content const email = await getEmailContent(session.user.id, id, folder, accountId || undefined); // Return the complete email object return NextResponse.json(email); } catch (error: any) { console.error("Error fetching email content:", error); return NextResponse.json( { error: "Failed to fetch email content", message: error.message }, { status: 500 } ); } } catch (error: any) { console.error("Error in GET:", error); return NextResponse.json( { error: "Internal server error", message: error.message }, { status: 500 } ); } } // Add a route to mark email as read export async function POST( request: Request, context: { params: { id: string } } ) { try { const session = await getServerSession(authOptions); if (!session || !session.user?.id) { return NextResponse.json( { error: "Not authenticated" }, { status: 401 } ); } // Await params as per Next.js requirements const params = await context.params; const id = params?.id; if (!id) { return NextResponse.json( { error: "Missing email ID" }, { status: 400 } ); } const { action } = await request.json(); if (action !== 'mark-read' && action !== 'mark-unread') { return NextResponse.json( { error: "Invalid action. Supported actions: mark-read, mark-unread" }, { status: 400 } ); } const { searchParams } = new URL(request.url); const folder = searchParams.get("folder") || "INBOX"; const accountId = searchParams.get("accountId"); // Use the email service to mark the email const success = await markEmailReadStatus( session.user.id, id, action === 'mark-read', folder ); if (!success) { return NextResponse.json( { error: `Failed to ${action === 'mark-read' ? 'mark email as read' : 'mark email as unread'}` }, { status: 500 } ); } // Invalidate cache for this email await invalidateEmailContentCache(session.user.id, accountId || 'default', id); return NextResponse.json({ success: true }); } catch (error: any) { console.error("Error in POST:", error); return NextResponse.json( { error: "Internal server error", message: error.message }, { status: 500 } ); } }