Files
webmailserver/app/api/mailboxes/route.ts

100 lines
3.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { NextRequest, NextResponse } from "next/server";
import { auth } from "@/auth";
import { getMailboxes, createMailbox, setupMailboxForwarding } from "@/lib/mailcow";
import { canAccessDomain } from "@/lib/users";
import { prisma } from "@/lib/prisma";
// GET /api/mailboxes?domain=example.com
export async function GET(req: NextRequest) {
// ... existing GET ...
const session = await auth();
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
const domain = req.nextUrl.searchParams.get("domain");
if (!domain) return NextResponse.json({ error: "domain parametresi gerekli" }, { status: 400 });
if (!canAccessDomain(session.user.domains ?? [], domain)) {
return NextResponse.json({ error: "Bu domaine erişim yetkiniz yok" }, { status: 403 });
}
const mailboxes = await getMailboxes(domain);
return NextResponse.json(mailboxes);
}
// POST /api/mailboxes
export async function POST(req: NextRequest) {
const session = await auth();
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
const body = await req.json();
const { local_part, domain, name, password, quota } = body;
const fullEmail = `${local_part}@${domain}`;
if (!local_part || !domain || !name || !password) {
return NextResponse.json({ error: "Eksik alan" }, { status: 400 });
}
if (!canAccessDomain(session.user.domains ?? [], domain)) {
return NextResponse.json({ error: "Bu domaine erişim yetkiniz yok" }, { status: 403 });
}
// 1. Create Mailbox in Mailcow
const result = await createMailbox({ local_part, domain, name, password, quota });
if (!result.ok) {
// Log failure to SystemLog
try {
await prisma.systemLog.create({
data: {
level: "ERROR",
message: `Mailbox creation failed: ${fullEmail}`,
details: JSON.stringify(result.data),
},
});
} catch (e) {
console.error("[SystemLog] Failed to log error", e);
}
return NextResponse.json(result.data, { status: 502 });
}
// 2. Automated "One-Click" Setup: Create Forwarding to Webhook
const webhookUrl = `${req.nextUrl.origin}/api/webhooks/mail`;
console.log(`[Setup] Setting up auto-forwarding for ${fullEmail} to ${webhookUrl}`);
const setupResult = await setupMailboxForwarding(fullEmail, webhookUrl);
if (!setupResult.ok) {
console.error(`[Setup] Failed to setup auto-forwarding for ${fullEmail}`);
try {
await prisma.systemLog.create({
data: {
level: "WARN",
message: `Auto-forwarding setup failed for ${fullEmail}`,
details: JSON.stringify(setupResult.data),
},
});
} catch (e) {
console.error("[SystemLog] Failed to log warning", e);
}
// We still return success for mailbox creation, but maybe with a warning header/prop
return NextResponse.json({
...result.data,
setup_warning: "Bildirim kurulumu otomatik yapılamadı, lütfen manuel kontrol edin."
});
}
// Log success
try {
await prisma.systemLog.create({
data: {
level: "INFO",
message: `Mailbox created and notification setup completed: ${fullEmail}`,
},
});
} catch (e) {
console.error("[SystemLog] Failed to log success", e);
}
return NextResponse.json(result.data);
}