49 lines
1.6 KiB
TypeScript
49 lines
1.6 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { getServerSession } from 'next-auth';
|
|
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
|
|
import { getEmails } from '@/lib/services/email-service';
|
|
import { invalidateFolderCache } from '@/lib/redis';
|
|
import { refreshEmailsInBackground } from '@/lib/services/prefetch-service';
|
|
|
|
/**
|
|
* API endpoint to force refresh email data
|
|
* This is useful when the user wants to manually refresh or
|
|
* when the app detects that it's been a while since the last refresh
|
|
*/
|
|
export async function POST(request: Request) {
|
|
try {
|
|
// Authenticate user
|
|
const session = await getServerSession(authOptions);
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json(
|
|
{ error: "Not authenticated" },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
// Extract folder from request body
|
|
const { folder = 'INBOX' } = await request.json();
|
|
|
|
// First invalidate the cache for this folder
|
|
await invalidateFolderCache(session.user.id, folder);
|
|
|
|
// Then trigger a background refresh
|
|
refreshEmailsInBackground(session.user.id, folder, 1, 20);
|
|
|
|
// Also prefetch page 2 if this is the inbox
|
|
if (folder === 'INBOX') {
|
|
refreshEmailsInBackground(session.user.id, folder, 2, 20);
|
|
}
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: `Refresh scheduled for folder: ${folder}`
|
|
});
|
|
} catch (error) {
|
|
console.error('Error scheduling refresh:', error);
|
|
return NextResponse.json(
|
|
{ error: "Failed to schedule refresh" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|