182 lines
5.2 KiB
TypeScript
182 lines
5.2 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { getServerSession } from 'next-auth';
|
|
import { authOptions } from "@/app/api/auth/options";
|
|
import { getCachedEmailCredentials, getCachedImapSession } from '@/lib/redis';
|
|
import { prisma } from '@/lib/prisma';
|
|
import { getMailboxes } from '@/lib/services/email-service';
|
|
import { ImapFlow } from 'imapflow';
|
|
|
|
export async function GET() {
|
|
// Verify auth
|
|
const session = await getServerSession(authOptions);
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json(
|
|
{ error: 'Unauthorized' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
const userId = session.user.id;
|
|
const debugData: any = {
|
|
userId,
|
|
timestamp: new Date().toISOString(),
|
|
redis: {
|
|
emailCredentials: null,
|
|
session: null
|
|
},
|
|
database: {
|
|
accounts: [],
|
|
schema: null
|
|
},
|
|
imap: {
|
|
connectionAttempt: false,
|
|
connected: false,
|
|
folders: []
|
|
}
|
|
};
|
|
|
|
// Check Redis cache for credentials
|
|
try {
|
|
const credentials = await getCachedEmailCredentials(userId);
|
|
if (credentials) {
|
|
debugData.redis.emailCredentials = {
|
|
found: true,
|
|
email: credentials.email,
|
|
host: credentials.host,
|
|
port: credentials.port,
|
|
hasPassword: !!credentials.password,
|
|
hasSmtp: !!credentials.smtp_host
|
|
};
|
|
} else {
|
|
debugData.redis.emailCredentials = { found: false };
|
|
}
|
|
} catch (e) {
|
|
debugData.redis.emailCredentials = {
|
|
error: e instanceof Error ? e.message : 'Unknown error'
|
|
};
|
|
}
|
|
|
|
// Check Redis for session data (which contains folders)
|
|
try {
|
|
const sessionData = await getCachedImapSession(userId);
|
|
if (sessionData) {
|
|
debugData.redis.session = {
|
|
found: true,
|
|
lastActive: new Date(sessionData.lastActive).toISOString(),
|
|
hasFolders: !!sessionData.mailboxes,
|
|
folderCount: sessionData.mailboxes?.length || 0,
|
|
folders: sessionData.mailboxes || []
|
|
};
|
|
} else {
|
|
debugData.redis.session = { found: false };
|
|
}
|
|
} catch (e) {
|
|
debugData.redis.session = {
|
|
error: e instanceof Error ? e.message : 'Unknown error'
|
|
};
|
|
}
|
|
|
|
// Try to get database schema information to help diagnose issues
|
|
try {
|
|
const schemaInfo = await prisma.$queryRaw`
|
|
SELECT column_name, data_type, is_nullable
|
|
FROM information_schema.columns
|
|
WHERE table_name = 'MailCredentials'
|
|
AND table_schema = 'public'
|
|
ORDER BY ordinal_position
|
|
`;
|
|
debugData.database.schema = schemaInfo;
|
|
} catch (e) {
|
|
debugData.database.schemaError = e instanceof Error ? e.message : 'Unknown error';
|
|
}
|
|
|
|
// Check database for accounts
|
|
try {
|
|
const accounts = await prisma.mailCredentials.findMany({
|
|
where: { userId },
|
|
select: {
|
|
id: true,
|
|
email: true,
|
|
host: true,
|
|
port: true
|
|
}
|
|
});
|
|
|
|
// Also try to get additional fields from raw query
|
|
const accountsWithMetadata = await Promise.all(accounts.map(async (account) => {
|
|
try {
|
|
const rawAccount = await prisma.$queryRaw`
|
|
SELECT display_name, color, smtp_host, smtp_port, smtp_secure, secure
|
|
FROM "MailCredentials"
|
|
WHERE id = ${account.id}
|
|
`;
|
|
|
|
const metadata = Array.isArray(rawAccount) && rawAccount.length > 0
|
|
? rawAccount[0]
|
|
: {};
|
|
|
|
return {
|
|
...account,
|
|
display_name: metadata.display_name,
|
|
color: metadata.color,
|
|
smtp_host: metadata.smtp_host,
|
|
smtp_port: metadata.smtp_port,
|
|
smtp_secure: metadata.smtp_secure,
|
|
secure: metadata.secure
|
|
};
|
|
} catch (e) {
|
|
return {
|
|
...account,
|
|
_queryError: e instanceof Error ? e.message : 'Unknown error'
|
|
};
|
|
}
|
|
}));
|
|
|
|
debugData.database.accounts = accountsWithMetadata;
|
|
debugData.database.accountCount = accounts.length;
|
|
} catch (e) {
|
|
debugData.database.error = e instanceof Error ? e.message : 'Unknown error';
|
|
}
|
|
|
|
// Try to get IMAP folders for the main account
|
|
if (debugData.redis.emailCredentials?.found || debugData.database.accountCount > 0) {
|
|
try {
|
|
debugData.imap.connectionAttempt = true;
|
|
|
|
// Use cached credentials
|
|
const credentials = await getCachedEmailCredentials(userId);
|
|
|
|
if (credentials && credentials.email && credentials.password) {
|
|
const client = new ImapFlow({
|
|
host: credentials.host,
|
|
port: credentials.port,
|
|
secure: true,
|
|
auth: {
|
|
user: credentials.email,
|
|
pass: credentials.password,
|
|
},
|
|
logger: false,
|
|
tls: {
|
|
rejectUnauthorized: false
|
|
}
|
|
});
|
|
|
|
await client.connect();
|
|
debugData.imap.connected = true;
|
|
|
|
// Get folders
|
|
const folders = await getMailboxes(client);
|
|
debugData.imap.folders = folders;
|
|
|
|
// Close connection
|
|
await client.logout();
|
|
} else {
|
|
debugData.imap.error = "No valid credentials found";
|
|
}
|
|
} catch (e) {
|
|
debugData.imap.error = e instanceof Error ? e.message : 'Unknown error';
|
|
}
|
|
}
|
|
|
|
return NextResponse.json(debugData);
|
|
}
|