first version
This commit is contained in:
22
app/api/mailboxes/route.ts
Normal file
22
app/api/mailboxes/route.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { db } from '@/app/db/drizzle';
|
||||
import { domains, emails } from '@/app/db/schema';
|
||||
import { authenticate } from '@/app/lib/utils';
|
||||
import { eq, sql } from 'drizzle-orm';
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
if (!authenticate(req)) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
|
||||
const { searchParams } = new URL(req.url);
|
||||
const bucket = searchParams.get('bucket');
|
||||
if (!bucket) return NextResponse.json({ error: 'Missing bucket' }, { status: 400 });
|
||||
|
||||
const [domain] = await db.select().from(domains).where(eq(domains.bucket, bucket));
|
||||
if (!domain) return NextResponse.json({ error: 'Domain not found' }, { status: 404 });
|
||||
|
||||
const mailboxData = await db.select({ to: emails.to }).from(emails).where(eq(emails.domainId, domain.id));
|
||||
const uniqueMailboxes = new Set<string>();
|
||||
mailboxData.forEach(em => em.to?.forEach(r => uniqueMailboxes.add(r.toLowerCase())));
|
||||
|
||||
return NextResponse.json(Array.from(uniqueMailboxes));
|
||||
}
|
||||
Reference in New Issue
Block a user