88 lines
3.2 KiB
TypeScript
88 lines
3.2 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { getServerSession } from 'next-auth';
|
|
import { authOptions } from "@/app/api/auth/options";
|
|
import { getEmails } from '@/lib/services/email-service';
|
|
import {
|
|
getCachedEmailList,
|
|
cacheEmailList,
|
|
invalidateFolderCache
|
|
} from '@/lib/redis';
|
|
|
|
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 }
|
|
);
|
|
}
|
|
|
|
// Extract query parameters
|
|
const { searchParams } = new URL(request.url);
|
|
const page = parseInt(searchParams.get("page") || "1");
|
|
const perPage = parseInt(searchParams.get("perPage") || "20");
|
|
const folder = searchParams.get("folder") || "INBOX";
|
|
const searchQuery = searchParams.get("search") || "";
|
|
const accountId = searchParams.get("accountId") || "";
|
|
const checkOnly = searchParams.get("checkOnly") === "true";
|
|
|
|
// Log exact parameters received by the API
|
|
console.log(`[API/emails] Received request with: folder=${folder}, accountId=${accountId}, page=${page}, checkOnly=${checkOnly}`);
|
|
|
|
// Parameter normalization
|
|
// If folder contains an account prefix, extract it but DO NOT use it
|
|
// Always prioritize the explicit accountId parameter
|
|
let normalizedFolder = folder;
|
|
let effectiveAccountId = accountId || 'default';
|
|
|
|
if (folder.includes(':')) {
|
|
const parts = folder.split(':');
|
|
normalizedFolder = parts[1];
|
|
|
|
console.log(`[API/emails] Folder has prefix, normalized to ${normalizedFolder}`);
|
|
}
|
|
|
|
console.log(`[API/emails] Using normalized parameters: folder=${normalizedFolder}, accountId=${effectiveAccountId}`);
|
|
|
|
// Try to get from Redis cache first, but only if it's not a search query and not checkOnly
|
|
if (!searchQuery && !checkOnly) {
|
|
console.log(`[API/emails] Checking Redis cache for ${session.user.id}:${effectiveAccountId}:${normalizedFolder}:${page}:${perPage}`);
|
|
const cachedEmails = await getCachedEmailList(
|
|
session.user.id,
|
|
effectiveAccountId,
|
|
normalizedFolder,
|
|
page,
|
|
perPage
|
|
);
|
|
if (cachedEmails) {
|
|
console.log(`[API/emails] Using Redis cached emails for ${session.user.id}:${effectiveAccountId}:${normalizedFolder}:${page}:${perPage}`);
|
|
return NextResponse.json(cachedEmails);
|
|
}
|
|
}
|
|
|
|
console.log(`[API/emails] Redis cache miss for ${session.user.id}:${effectiveAccountId}:${normalizedFolder}:${page}:${perPage}, fetching emails from IMAP`);
|
|
|
|
// Use the email service to fetch emails
|
|
const emailsResult = await getEmails(
|
|
session.user.id,
|
|
normalizedFolder,
|
|
page,
|
|
perPage,
|
|
effectiveAccountId,
|
|
checkOnly
|
|
);
|
|
|
|
console.log(`[API/emails] Successfully fetched ${emailsResult.emails.length} emails from IMAP for account ${effectiveAccountId}`);
|
|
|
|
// Return result
|
|
return NextResponse.json(emailsResult);
|
|
} catch (error: any) {
|
|
console.error("[API/emails] Error fetching emails:", error);
|
|
return NextResponse.json(
|
|
{ error: "Failed to fetch emails", message: error.message },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|