From 4da979c51d7e27e4ded105610054b1b17cd50b35 Mon Sep 17 00:00:00 2001 From: alma Date: Mon, 28 Apr 2025 16:42:13 +0200 Subject: [PATCH] courrier multi account restore compose --- lib/services/email-service.ts | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/services/email-service.ts b/lib/services/email-service.ts index d7e916a9..d36e3681 100644 --- a/lib/services/email-service.ts +++ b/lib/services/email-service.ts @@ -66,7 +66,30 @@ export async function getImapConnection(userId: string, accountId?: string): Pro // Get credentials from cache or database const credentials = await getCachedEmailCredentials(userId, accountId || 'default'); if (!credentials) { - throw new Error('No email credentials found'); + // If no credentials found with default accountId, try to get from database + const dbCredentials = await prisma.mailCredentials.findFirst({ + where: { userId } + }); + + if (!dbCredentials) { + throw new Error('No email credentials found'); + } + + // Cache the credentials with the email as accountId + await cacheEmailCredentials(userId, dbCredentials.email, { + email: dbCredentials.email, + password: dbCredentials.password, + host: dbCredentials.host, + port: dbCredentials.port + }); + + // Try to get credentials again with the email as accountId + const retryCredentials = await getCachedEmailCredentials(userId, dbCredentials.email); + if (!retryCredentials) { + throw new Error('Failed to cache email credentials'); + } + + return getImapConnection(userId, dbCredentials.email); } // Create new connection