39 lines
1.7 KiB
TypeScript
39 lines
1.7 KiB
TypeScript
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 } 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 });
|
|
|
|
// Hole alle E-Mail-Adressen aus den "to" Feldern für diese Domain
|
|
const mailboxData = await db.select({ to: emails.to }).from(emails).where(eq(emails.domainId, domain.id));
|
|
|
|
// Extrahiere sowohl die konvertierte Domain (mit Punkt) als auch die Original-Bucket-Domain (mit Bindestrich)
|
|
const domainNameWithDot = domain.domain; // z.B. "bayarea-cc.com"
|
|
const domainNameWithDash = bucket.replace('-emails', ''); // z.B. "bayarea-cc-com"
|
|
|
|
const uniqueMailboxes = new Set<string>();
|
|
|
|
// Filtere E-Mail-Adressen, die zu einer der beiden Domain-Varianten gehören
|
|
mailboxData.forEach(em => {
|
|
em.to?.forEach(recipient => {
|
|
const recipientLower = recipient.toLowerCase();
|
|
// Prüfe beide Varianten: mit Punkt und mit Bindestrich
|
|
if (recipientLower.endsWith(`@${domainNameWithDot}`) ||
|
|
recipientLower.endsWith(`@${domainNameWithDash}`)) {
|
|
uniqueMailboxes.add(recipientLower);
|
|
}
|
|
});
|
|
});
|
|
|
|
return NextResponse.json(Array.from(uniqueMailboxes).sort());
|
|
} |