From 3e6953a4a446f8455bde9ed1dffdfedd067289ad Mon Sep 17 00:00:00 2001 From: alma Date: Mon, 28 Apr 2025 12:04:41 +0200 Subject: [PATCH] courrier multi account restore compose --- app/api/courrier/session/route.ts | 12 +++- app/courrier/page.tsx | 115 ++++++++---------------------- 2 files changed, 39 insertions(+), 88 deletions(-) diff --git a/app/api/courrier/session/route.ts b/app/api/courrier/session/route.ts index 9171a932..2ccfd6dc 100644 --- a/app/api/courrier/session/route.ts +++ b/app/api/courrier/session/route.ts @@ -131,6 +131,8 @@ export async function GET() { // Get additional fields that might be in the database but not in the type const accountsWithMetadata = await Promise.all(allAccounts.map(async (account) => { + console.log(`[DEBUG] Processing metadata for account ${account.email}`); + // Get the raw account data to access fields not in the type const rawAccount = await prisma.$queryRaw` SELECT display_name, color @@ -140,6 +142,7 @@ export async function GET() { // Cast the raw result to an array and get the first item const metadata = Array.isArray(rawAccount) ? rawAccount[0] : rawAccount; + console.log(`[DEBUG] Metadata for ${account.email}:`, metadata); // Get folders for this specific account const accountFolders = await getAccountFolders(account.id, { @@ -147,6 +150,8 @@ export async function GET() { ...metadata }); + console.log(`[DEBUG] Found ${accountFolders.length} folders for ${account.email}`); + return { ...account, display_name: metadata?.display_name || account.email, @@ -155,7 +160,12 @@ export async function GET() { }; })); - console.log(`Found ${allAccounts.length} email accounts for user ${userId}`); + console.log(`[DEBUG] Final processed accounts:`, accountsWithMetadata.map(acc => ({ + id: acc.id, + email: acc.email, + display_name: acc.display_name, + folderCount: acc.folders?.length || 0 + }))); // If not in cache and no accounts found, check database for single account (backward compatibility) if (!credentials && allAccounts.length === 0) { diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index 208547f3..b61e1297 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -249,99 +249,52 @@ export default function CourrierPage() { console.log('allAccounts length:', data.allAccounts?.length || 0); // Inspect each account's structure - if (data.allAccounts && Array.isArray(data.allAccounts)) { - data.allAccounts.forEach((account: any, idx: number) => { - console.log(`Account ${idx + 1}:`, { - id: account.id, - email: account.email, - display_name: account.display_name, - foldersExist: !!account.folders, - foldersIsArray: Array.isArray(account.folders), - foldersLength: account.folders?.length || 0, - folders: account.folders - }); - }); - } - - if (!isMounted) return; - if (data.authenticated) { if (data.hasEmailCredentials) { console.log('Session initialized, prefetch status:', data.prefetchStarted ? 'running' : 'not started'); setPrefetchStarted(Boolean(data.prefetchStarted)); // Create a copy of the current accounts to update - const updatedAccounts = [...accounts]; + let updatedAccounts: Account[] = [{ id: 'all-accounts', name: 'All', email: '', color: 'bg-gray-500' }]; // Check if we have multiple accounts returned if (data.allAccounts && Array.isArray(data.allAccounts) && data.allAccounts.length > 0) { console.log('[DEBUG] Multiple accounts found:', data.allAccounts.length); - // First, validate the structure of each account + // Process each account data.allAccounts.forEach((account: any, index: number) => { - console.log(`[DEBUG] Account ${index+1} structure check:`, { + console.log(`[DEBUG] Processing account ${index + 1}:`, { id: account.id, email: account.email, display_name: account.display_name, hasFolders: !!account.folders, foldersIsArray: Array.isArray(account.folders), - foldersCount: Array.isArray(account.folders) ? account.folders.length : 0, - folders: account.folders || [] + foldersCount: Array.isArray(account.folders) ? account.folders.length : 0 }); - }); - - // Keep the All account at position 0 - if (updatedAccounts.length > 0) { - // Replace the loading account with the real one - data.allAccounts.forEach((account: any, index: number) => { - // Ensure folders are valid - const accountFolders = (account.folders && Array.isArray(account.folders)) - ? account.folders - : (data.mailboxes && Array.isArray(data.mailboxes)) - ? data.mailboxes - : ['INBOX', 'Sent', 'Drafts', 'Trash', 'Junk']; - - // If we're updating the first real account (index 0 in API, position 1 in our array) - if (index === 0 && updatedAccounts.length > 1) { - // Update the loading account in place to maintain references - updatedAccounts[1] = { - id: account.id, // Use the real account ID - name: account.display_name || account.email, - email: account.email, - color: account.color || 'bg-blue-500', - folders: accountFolders - }; - console.log(`[DEBUG] Updated loading account to real account: ${account.email} with ID ${account.id}`); - } else { - // Add additional accounts as new entries - updatedAccounts.push({ - id: account.id || `account-${index}`, - name: account.display_name || account.email, - email: account.email, - color: account.color || 'bg-blue-500', - folders: accountFolders - }); - } - }); - } else { - // Fallback if accounts array is empty for some reason - updatedAccounts.push({ id: 'all-accounts', name: 'All', email: '', color: 'bg-gray-500' }); - const firstAccount = data.allAccounts[0]; - const accountFolders = (firstAccount.folders && Array.isArray(firstAccount.folders)) - ? firstAccount.folders + // Ensure folders are valid + const accountFolders = (account.folders && Array.isArray(account.folders)) + ? account.folders : (data.mailboxes && Array.isArray(data.mailboxes)) ? data.mailboxes : ['INBOX', 'Sent', 'Drafts', 'Trash', 'Junk']; + // Add the account to the list updatedAccounts.push({ - id: firstAccount.id, - name: firstAccount.display_name || firstAccount.email, - email: firstAccount.email, - color: firstAccount.color || 'bg-blue-500', + id: account.id || `account-${index}`, + name: account.display_name || account.email, + email: account.email, + color: account.color || 'bg-blue-500', folders: accountFolders }); - } + }); + + console.log('[DEBUG] Final accounts array:', updatedAccounts.map(acc => ({ + id: acc.id, + name: acc.name, + email: acc.email, + folderCount: acc.folders?.length || 0 + }))); } else if (data.email) { // Fallback to single account if allAccounts is not available console.log(`[DEBUG] Fallback to single account: ${data.email}`); @@ -353,26 +306,14 @@ export default function CourrierPage() { const folderList = (data.mailboxes && data.mailboxes.length > 0) ? data.mailboxes : fallbackFolders; - // Update the loading account if it exists - if (updatedAccounts.length > 1) { - updatedAccounts[1] = { - id: 'default-account', // Use consistent ID - name: data.displayName || data.email, - email: data.email, - color: 'bg-blue-500', - folders: folderList - }; - } else { - // Fallback if accounts array is empty - updatedAccounts.push({ id: 'all-accounts', name: 'All', email: '', color: 'bg-gray-500' }); - updatedAccounts.push({ - id: 'default-account', - name: data.displayName || data.email, - email: data.email, - color: 'bg-blue-500', - folders: folderList - }); - } + // Add the single account + updatedAccounts.push({ + id: 'default-account', + name: data.displayName || data.email, + email: data.email, + color: 'bg-blue-500', + folders: folderList + }); } console.log('Setting accounts:', updatedAccounts);