Implement database migration, notification logs, and one-click Mailcow setup

This commit is contained in:
AyrisAI
2026-05-14 16:49:11 +03:00
parent f328296c64
commit b024e20027
18 changed files with 1067 additions and 166 deletions

View File

@@ -9,15 +9,20 @@ import { prisma } from "@/lib/prisma";
*/
export async function POST(req: NextRequest) {
let aliciMail = "Bilinmiyor";
let sender = "Bilinmiyor";
let subject = "(Konu Yok)";
try {
const data = await req.json();
console.log("[Mail Webhook] Gelen Payload:", JSON.stringify(data));
// Extract basic info from the incoming payload
const aliciMail = (data.to || data.rcpt || "").toLowerCase().trim();
const sender = data.from || "Bilinmiyor";
const subject = data.subject || "(Konu Yok)";
// Extract basic info from the incoming payload (Mailcow handles these fields)
aliciMail = (data.to || data.rcpt || "").toLowerCase().trim();
sender = data.from || "Bilinmiyor";
subject = data.subject || "(Konu Yok)";
console.log(`[Mail Webhook] Yeni mail geldi: ${sender} -> ${aliciMail}`);
console.log(`[Mail Webhook] İşleniyor: ${sender} -> ${aliciMail}`);
// 1. Find mapping in database
const mapping = await prisma.mailboxMapping.findUnique({
@@ -31,33 +36,89 @@ export async function POST(req: NextRequest) {
if (targetChatId && process.env.TELEGRAM_BOT_TOKEN) {
const message = `🔔 *Yeni Mail Geldi!*\n\n📧 *Alıcı:* ${aliciMail}\n👤 *Gönderen:* ${sender}\n📝 *Konu:* ${subject}`;
const telegramUrl = `https://api.telegram.org/bot${process.env.TELEGRAM_BOT_TOKEN}/sendMessage`;
const res = await fetch(telegramUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
chat_id: targetChatId,
text: message,
parse_mode: "Markdown",
}),
});
let status = "SENT";
let errorDetail = null;
if (!res.ok) {
const errorText = await res.text();
console.error(`[Mail Webhook] Telegram API hatası: ${res.status} ${errorText}`);
} else {
console.log(`[Webhook] Bildirim ${user.email} kullanıcısına (ID: ${targetChatId}) gönderildi.`);
try {
const res = await fetch(telegramUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
chat_id: targetChatId,
text: message,
parse_mode: "Markdown",
}),
});
if (!res.ok) {
status = "FAILED";
errorDetail = `Telegram API Error: ${res.status} ${await res.text()}`;
}
} catch (err: any) {
status = "FAILED";
errorDetail = `Network Error: ${err.message}`;
}
// Log successful/failed delivery
await prisma.notificationLog.create({
data: {
mailbox: aliciMail,
sender,
subject,
status,
error: errorDetail,
userId: user.id,
},
});
} else {
// Log that user was found but notification skipped
await prisma.notificationLog.create({
data: {
mailbox: aliciMail,
sender,
subject,
status: "FAILED",
error: !process.env.TELEGRAM_BOT_TOKEN ? "Bot token missing" : "User has no Telegram ID",
userId: user.id,
},
});
}
} else {
console.log(`[Webhook] Sahibi bilinmeyen veya eşleşmeyen mail: ${aliciMail}`);
// Log unmapped mail
await prisma.notificationLog.create({
data: {
mailbox: aliciMail,
sender,
subject,
status: "FAILED",
error: "No user mapping found for this email",
},
});
}
return NextResponse.json({ status: "ok" });
} catch (error: any) {
console.error(`[Mail Webhook] Hata: ${error.message}`);
// Attempt to log the fatal error if we have enough info
try {
await prisma.notificationLog.create({
data: {
mailbox: aliciMail,
sender,
subject,
status: "FAILED",
error: `Fatal Error: ${error.message}`,
},
});
} catch (dbErr) {
console.error("[Mail Webhook] Could not even log the error to DB");
}
return NextResponse.json({ error: "İşlem başarısız" }, { status: 500 });
}
}