courrier multi account restore compose

This commit is contained in:
alma 2025-04-28 12:04:41 +02:00
parent ddad3a12ca
commit 3e6953a4a4
2 changed files with 39 additions and 88 deletions

View File

@ -131,6 +131,8 @@ export async function GET() {
// Get additional fields that might be in the database but not in the type // Get additional fields that might be in the database but not in the type
const accountsWithMetadata = await Promise.all(allAccounts.map(async (account) => { 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 // Get the raw account data to access fields not in the type
const rawAccount = await prisma.$queryRaw` const rawAccount = await prisma.$queryRaw`
SELECT display_name, color SELECT display_name, color
@ -140,6 +142,7 @@ export async function GET() {
// Cast the raw result to an array and get the first item // Cast the raw result to an array and get the first item
const metadata = Array.isArray(rawAccount) ? rawAccount[0] : rawAccount; const metadata = Array.isArray(rawAccount) ? rawAccount[0] : rawAccount;
console.log(`[DEBUG] Metadata for ${account.email}:`, metadata);
// Get folders for this specific account // Get folders for this specific account
const accountFolders = await getAccountFolders(account.id, { const accountFolders = await getAccountFolders(account.id, {
@ -147,6 +150,8 @@ export async function GET() {
...metadata ...metadata
}); });
console.log(`[DEBUG] Found ${accountFolders.length} folders for ${account.email}`);
return { return {
...account, ...account,
display_name: metadata?.display_name || account.email, 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 not in cache and no accounts found, check database for single account (backward compatibility)
if (!credentials && allAccounts.length === 0) { if (!credentials && allAccounts.length === 0) {

View File

@ -249,99 +249,52 @@ export default function CourrierPage() {
console.log('allAccounts length:', data.allAccounts?.length || 0); console.log('allAccounts length:', data.allAccounts?.length || 0);
// Inspect each account's structure // 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.authenticated) {
if (data.hasEmailCredentials) { if (data.hasEmailCredentials) {
console.log('Session initialized, prefetch status:', data.prefetchStarted ? 'running' : 'not started'); console.log('Session initialized, prefetch status:', data.prefetchStarted ? 'running' : 'not started');
setPrefetchStarted(Boolean(data.prefetchStarted)); setPrefetchStarted(Boolean(data.prefetchStarted));
// Create a copy of the current accounts to update // 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 // Check if we have multiple accounts returned
if (data.allAccounts && Array.isArray(data.allAccounts) && data.allAccounts.length > 0) { if (data.allAccounts && Array.isArray(data.allAccounts) && data.allAccounts.length > 0) {
console.log('[DEBUG] Multiple accounts found:', data.allAccounts.length); 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) => { 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, id: account.id,
email: account.email, email: account.email,
display_name: account.display_name, display_name: account.display_name,
hasFolders: !!account.folders, hasFolders: !!account.folders,
foldersIsArray: Array.isArray(account.folders), foldersIsArray: Array.isArray(account.folders),
foldersCount: Array.isArray(account.folders) ? account.folders.length : 0, foldersCount: Array.isArray(account.folders) ? account.folders.length : 0
folders: account.folders || []
}); });
});
// 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]; // Ensure folders are valid
const accountFolders = (firstAccount.folders && Array.isArray(firstAccount.folders)) const accountFolders = (account.folders && Array.isArray(account.folders))
? firstAccount.folders ? account.folders
: (data.mailboxes && Array.isArray(data.mailboxes)) : (data.mailboxes && Array.isArray(data.mailboxes))
? data.mailboxes ? data.mailboxes
: ['INBOX', 'Sent', 'Drafts', 'Trash', 'Junk']; : ['INBOX', 'Sent', 'Drafts', 'Trash', 'Junk'];
// Add the account to the list
updatedAccounts.push({ updatedAccounts.push({
id: firstAccount.id, id: account.id || `account-${index}`,
name: firstAccount.display_name || firstAccount.email, name: account.display_name || account.email,
email: firstAccount.email, email: account.email,
color: firstAccount.color || 'bg-blue-500', color: account.color || 'bg-blue-500',
folders: accountFolders 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) { } else if (data.email) {
// Fallback to single account if allAccounts is not available // Fallback to single account if allAccounts is not available
console.log(`[DEBUG] Fallback to single account: ${data.email}`); 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) ? const folderList = (data.mailboxes && data.mailboxes.length > 0) ?
data.mailboxes : fallbackFolders; data.mailboxes : fallbackFolders;
// Update the loading account if it exists // Add the single account
if (updatedAccounts.length > 1) { updatedAccounts.push({
updatedAccounts[1] = { id: 'default-account',
id: 'default-account', // Use consistent ID name: data.displayName || data.email,
name: data.displayName || data.email, email: data.email,
email: data.email, color: 'bg-blue-500',
color: 'bg-blue-500', folders: folderList
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
});
}
} }
console.log('Setting accounts:', updatedAccounts); console.log('Setting accounts:', updatedAccounts);