Implement database migration, notification logs, and one-click Mailcow setup
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { auth } from "@/auth";
|
||||
import { getMailboxes, createMailbox } from "@/lib/mailcow";
|
||||
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 });
|
||||
|
||||
@@ -26,6 +28,7 @@ export async function POST(req: NextRequest) {
|
||||
|
||||
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 });
|
||||
@@ -35,6 +38,62 @@ export async function POST(req: NextRequest) {
|
||||
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 });
|
||||
return NextResponse.json(result.data, { status: result.ok ? 200 : 502 });
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user