From e10dd6e94a5676a38ae2699d7ff9b99ff47c9bab Mon Sep 17 00:00:00 2001 From: alma Date: Thu, 1 May 2025 21:04:38 +0200 Subject: [PATCH] courrier preview --- app/api/courrier/account-details/route.ts | 64 +++++++++++++++++++++++ app/courrier/page.tsx | 61 +++++++++++++++------ 2 files changed, 110 insertions(+), 15 deletions(-) create mode 100644 app/api/courrier/account-details/route.ts diff --git a/app/api/courrier/account-details/route.ts b/app/api/courrier/account-details/route.ts new file mode 100644 index 00000000..52e2055a --- /dev/null +++ b/app/api/courrier/account-details/route.ts @@ -0,0 +1,64 @@ +import { NextResponse } from 'next/server'; +import { getServerSession } from 'next-auth'; +import { authOptions } from '@/app/api/auth/[...nextauth]/route'; +import { prisma } from '@/lib/prisma'; + +export async function GET(request: Request) { + try { + // Authenticate user + const session = await getServerSession(authOptions); + if (!session?.user?.id) { + return NextResponse.json( + { error: 'Unauthorized' }, + { status: 401 } + ); + } + + // Get accountId from query params + const { searchParams } = new URL(request.url); + const accountId = searchParams.get('accountId'); + + if (!accountId) { + return NextResponse.json( + { error: 'Account ID is required' }, + { status: 400 } + ); + } + + // Get account details from database, including connection details + const account = await prisma.mailCredentials.findFirst({ + where: { + id: accountId, + userId: session.user.id + }, + select: { + id: true, + email: true, + host: true, + port: true, + secure: true, + display_name: true, + color: true, + // Don't include the password in the response + } + }); + + if (!account) { + return NextResponse.json( + { error: 'Account not found' }, + { status: 404 } + ); + } + + return NextResponse.json(account); + } catch (error) { + console.error('Error fetching account details:', error); + return NextResponse.json( + { + error: 'Failed to fetch account details', + details: error instanceof Error ? error.message : 'Unknown error' + }, + { status: 500 } + ); + } +} \ No newline at end of file diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index b943f100..a07938c9 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -520,15 +520,6 @@ export default function CourrierPage() { router.push('/courrier/login'); }; - // Helper function for logging - const logEmailOp = (operation: string, details: string, data?: any) => { - const timestamp = new Date().toISOString().split('T')[1].substring(0, 12); - console.log(`[${timestamp}][EMAIL-APP][${operation}] ${details}`); - if (data) { - console.log(`[${timestamp}][EMAIL-APP][DATA]`, data); - } - }; - // Update the accounts from state - fix type issues const setAccounts = (newAccounts: Account[]) => { console.log('[DEBUG] Setting accounts:', newAccounts); @@ -901,8 +892,8 @@ export default function CourrierPage() { {/* Edit Password Modal */} { if (!open) setShowEditModal(false); }}> - - Edit Account Settings + + Edit Account Settings
{ e.preventDefault(); if (!accountToEdit) return; @@ -912,6 +903,46 @@ export default function CourrierPage() { const displayName = (formElement.querySelector('#display-name') as HTMLInputElement).value; const color = (formElement.querySelector('input[name="color"]:checked') as HTMLInputElement)?.value || accountToEdit.color; + // If password is changed, test the connection first + if (newPassword) { + try { + // First get the account's connection details + const accountDetailsRes = await fetch(`/api/courrier/account-details?accountId=${accountToEdit.id}`); + if (!accountDetailsRes.ok) { + throw new Error('Failed to fetch account connection details'); + } + const accountDetails = await accountDetailsRes.json(); + + // Test connection with new password before saving + const testResponse = await fetch('/api/courrier/test-connection', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + email: accountToEdit.email, + password: newPassword, + // Use the account's connection details from the API + host: accountDetails.host, + port: accountDetails.port || 993, + secure: accountDetails.secure || true + }) + }); + + const testResult = await testResponse.json(); + + if (!testResponse.ok) { + throw new Error(testResult.error || 'Connection test failed with new password'); + } + + console.log('Connection test successful with new password'); + } catch (error) { + console.error('Error testing connection:', error); + throw new Error(`Password test failed: ${error instanceof Error ? error.message : 'Unknown error'}`); + } + } + + // Continue with the update if test passed or no password change const res = await fetch('/api/courrier/account', { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, @@ -940,7 +971,7 @@ export default function CourrierPage() { } }}>
- +
- +
- +
{colorPalette.map((color, index) => (
@@ -1002,7 +1033,7 @@ export default function CourrierPage() { disabled={editLoading} > {editLoading ? : null} - Save Changes + Save