Neah/app/api/courrier/[id]/route.ts
2025-04-26 09:14:44 +02:00

122 lines
3.3 KiB
TypeScript

/*
* 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';
export async function GET(
request: Request,
{ params }: { params: { id: string } }
) {
try {
const session = await getServerSession(authOptions);
if (!session || !session.user?.id) {
return NextResponse.json(
{ error: "Not authenticated" },
{ status: 401 }
);
}
const { id } = params;
if (!id) {
return NextResponse.json(
{ error: "Missing email ID" },
{ status: 400 }
);
}
const { searchParams } = new URL(request.url);
const folder = searchParams.get("folder") || "INBOX";
try {
// Use the email service to fetch the email content
const email = await getEmailContent(session.user.id, id, folder);
// Return only what's needed for displaying the email
return NextResponse.json({
id,
subject: email.subject,
content: email.content,
contentFetched: true
});
} 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,
{ params }: { params: { id: string } }
) {
try {
const session = await getServerSession(authOptions);
if (!session || !session.user?.id) {
return NextResponse.json(
{ error: "Not authenticated" },
{ status: 401 }
);
}
const { id } = params;
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";
// 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 }
);
}
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 }
);
}
}