NeahNew/app/api/courrier/delete/route.ts
2025-05-05 13:04:01 +02:00

133 lines
4.9 KiB
TypeScript

import { NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from "@/app/api/auth/options";
import { getImapConnection } from '@/lib/services/email-service';
import { invalidateFolderCache } from '@/lib/redis';
export async function POST(request: Request) {
try {
// Authenticate user
const session = await getServerSession(authOptions);
if (!session || !session.user?.id) {
return NextResponse.json(
{ error: "Not authenticated" },
{ status: 401 }
);
}
// Extract request body
const body = await request.json();
const { emailIds, folder, accountId } = body;
// Validate required parameters
if (!emailIds || !Array.isArray(emailIds) || emailIds.length === 0) {
console.error('[DELETE API] Missing or invalid emailIds parameter:', emailIds);
return NextResponse.json(
{ error: "Missing or invalid emailIds parameter" },
{ status: 400 }
);
}
if (!folder) {
console.error('[DELETE API] Missing folder parameter');
return NextResponse.json(
{ error: "Missing folder parameter" },
{ status: 400 }
);
}
// Extract account ID from folder name if present and none was explicitly provided
const folderAccountId = folder.includes(':') ? folder.split(':')[0] : undefined;
// Use the most specific account ID available
const effectiveAccountId = folderAccountId || accountId || 'default';
// Normalize folder name by removing account prefix if present
const normalizedFolder = folder.includes(':') ? folder.split(':')[1] : folder;
console.log(`[DELETE API] Deleting ${emailIds.length} emails from folder ${normalizedFolder}, account ${effectiveAccountId}`);
// Get IMAP connection
const client = await getImapConnection(session.user.id, effectiveAccountId);
try {
// Open the mailbox
await client.mailboxOpen(normalizedFolder);
// Check if we're already in the trash folder
const inTrash = normalizedFolder.toLowerCase() === 'trash' ||
normalizedFolder.toLowerCase() === 'bin' ||
normalizedFolder.toLowerCase() === 'deleted';
if (inTrash) {
// If we're in trash, mark as deleted
console.log(`[DELETE API] In trash folder, marking emails as deleted: ${emailIds.join(', ')}`);
// Mark messages as deleted
for (const emailId of emailIds) {
await client.messageFlagsAdd(emailId, ['\\Deleted']);
}
} else {
// If not in trash, move to trash
console.log(`[DELETE API] Moving emails to trash: ${emailIds.join(', ')}`);
// Try to find the trash folder
const mailboxes = await client.list();
let trashFolder = 'Trash';
// Look for common trash folder names
const trashFolderNames = ['Trash', 'TRASH', 'Bin', 'Deleted', 'Deleted Items'];
for (const folder of mailboxes) {
if (trashFolderNames.includes(folder.name) ||
trashFolderNames.some(name => folder.name.toLowerCase().includes(name.toLowerCase()))) {
trashFolder = folder.name;
break;
}
}
// Move messages to trash
for (const emailId of emailIds) {
try {
// Convert the emailId to a number if it's a string
const uid = typeof emailId === 'string' ? parseInt(emailId, 10) : emailId;
// Debug logging for troubleshooting
console.log(`[DELETE API] Moving email with UID ${uid} to trash folder "${trashFolder}"`);
// Use the correct syntax for messageMove method
await client.messageMove(uid.toString(), trashFolder, { uid: true });
console.log(`[DELETE API] Successfully moved email ${uid} to trash`);
} catch (moveError) {
console.error(`[DELETE API] Error moving email ${emailId} to trash:`, moveError);
// Continue with other emails even if one fails
}
}
}
// Invalidate cache for source folder
await invalidateFolderCache(session.user.id, effectiveAccountId, normalizedFolder);
// Also invalidate trash folder cache
await invalidateFolderCache(session.user.id, effectiveAccountId, 'Trash');
return NextResponse.json({
success: true,
message: `${emailIds.length} email(s) deleted successfully`
});
} finally {
try {
// Always close the mailbox
await client.mailboxClose();
} catch (error) {
console.error('Error closing mailbox:', error);
}
}
} catch (error) {
console.error('[DELETE API] Error processing delete request:', error);
return NextResponse.json(
{ error: "Failed to delete emails", details: error instanceof Error ? error.message : String(error) },
{ status: 500 }
);
}
}