diff --git a/app/api/courrier/refresh/route.ts b/app/api/courrier/refresh/route.ts index d0ed56bc..1a209450 100644 --- a/app/api/courrier/refresh/route.ts +++ b/app/api/courrier/refresh/route.ts @@ -21,18 +21,38 @@ export async function POST(request: Request) { ); } - // Extract folder from request body - const { folder = 'INBOX' } = await request.json(); + // Extract folder and account ID from request body + const { folder = 'INBOX', accountId } = await request.json(); - // First invalidate the cache for this folder - await invalidateFolderCache(session.user.id, folder); + // CRITICAL FIX: Proper folder and account ID handling + let normalizedFolder: string; + let effectiveAccountId: string; - // Then trigger a background refresh - refreshEmailsInBackground(session.user.id, folder, 1, 20); + if (folder.includes(':')) { + // Extract parts if folder already has a prefix + const parts = folder.split(':'); + const folderAccountId = parts[0]; + normalizedFolder = parts[1]; + + // If explicit accountId is provided, it takes precedence + effectiveAccountId = accountId || folderAccountId; + } else { + // No prefix in folder name + normalizedFolder = folder; + effectiveAccountId = accountId || 'default'; + } + + console.log(`[API] Refreshing folder=${normalizedFolder}, accountId=${effectiveAccountId}`); + + // First invalidate the cache for this folder with the effective account ID + await invalidateFolderCache(session.user.id, effectiveAccountId, normalizedFolder); + + // Then trigger a background refresh with explicit account ID + refreshEmailsInBackground(session.user.id, normalizedFolder, 1, 20, effectiveAccountId); // Also prefetch page 2 if this is the inbox - if (folder === 'INBOX') { - refreshEmailsInBackground(session.user.id, folder, 2, 20); + if (normalizedFolder === 'INBOX') { + refreshEmailsInBackground(session.user.id, normalizedFolder, 2, 20, effectiveAccountId); } return NextResponse.json({