courrier preview

This commit is contained in:
alma 2025-05-01 21:04:38 +02:00
parent 4a2297f4b0
commit e10dd6e94a
2 changed files with 110 additions and 15 deletions

View File

@ -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 }
);
}
}

View File

@ -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 */}
<Dialog open={showEditModal} onOpenChange={open => { if (!open) setShowEditModal(false); }}>
<DialogContent className="sm:max-w-[500px]">
<DialogTitle>Edit Account Settings</DialogTitle>
<DialogContent className="sm:max-w-[500px] bg-white text-gray-800">
<DialogTitle className="text-gray-800">Edit Account Settings</DialogTitle>
<form onSubmit={async e => {
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() {
}
}}>
<div className="mb-4">
<Label htmlFor="display-name">Account Name</Label>
<Label htmlFor="display-name" className="text-gray-800">Account Name</Label>
<Input
id="display-name"
type="text"
@ -950,7 +981,7 @@ export default function CourrierPage() {
</div>
<div className="mb-4">
<Label htmlFor="new-password">New Password (optional)</Label>
<Label htmlFor="new-password" className="text-gray-800">New Password (optional)</Label>
<Input
id="new-password"
type="password"
@ -962,7 +993,7 @@ export default function CourrierPage() {
</div>
<div className="mb-4">
<Label className="block mb-2">Account Color</Label>
<Label className="block mb-2 text-gray-800">Account Color</Label>
<div className="grid grid-cols-5 gap-2">
{colorPalette.map((color, index) => (
<div key={index} className="flex items-center">
@ -1002,7 +1033,7 @@ export default function CourrierPage() {
disabled={editLoading}
>
{editLoading ? <Loader2 className="h-4 w-4 animate-spin mr-2" /> : null}
Save Changes
Save
</Button>
</div>
</form>