chore: migrate to PostgreSQL with Prisma

This commit is contained in:
AyrisAI
2026-05-14 14:57:15 +03:00
parent 2642d254dc
commit f328296c64
10 changed files with 253 additions and 61 deletions

View File

@@ -1,11 +1,11 @@
import { NextRequest, NextResponse } from "next/server";
import { getUsers } from "@/lib/users";
import { prisma } from "@/lib/prisma";
/**
* app/api/webhooks/mail/route.ts
*
* Webhook endpoint for incoming mail notifications (e.g. from Rspamd or Mailcow).
* Sends notifications to Telegram based on the recipient email.
* Webhook endpoint for incoming mail notifications.
* Uses Prisma to look up user mappings in the database.
*/
export async function POST(req: NextRequest) {
@@ -19,22 +19,15 @@ export async function POST(req: NextRequest) {
console.log(`[Mail Webhook] Yeni mail geldi: ${sender} -> ${aliciMail}`);
// 1. Find which USER_X owns this mail address via JSON mapping
// Format: MAIL_USER_MAPPINGS='{"email1@domain.com":"USER_0", "email2@domain.com":"USER_1"}'
const mappingsRaw = process.env.MAIL_USER_MAPPINGS || "{}";
let ownerUserKey: string | undefined = undefined;
try {
const mappings = JSON.parse(mappingsRaw);
ownerUserKey = mappings[aliciMail];
} catch (e) {
console.error("[Mail Webhook] MAIL_USER_MAPPINGS JSON ayrıştırma hatası:", e);
}
// 1. Find mapping in database
const mapping = await prisma.mailboxMapping.findUnique({
where: { email: aliciMail },
include: { user: true },
});
if (ownerUserKey) {
// 2. Get that USER's Telegram ID (e.g., USER_0_TELEGRAM_ID)
const tgIdKey = `${ownerUserKey}_TELEGRAM_ID`;
const targetChatId = process.env[tgIdKey];
if (mapping?.user) {
const { user } = mapping;
const targetChatId = user.telegramId;
if (targetChatId && process.env.TELEGRAM_BOT_TOKEN) {
const message = `🔔 *Yeni Mail Geldi!*\n\n📧 *Alıcı:* ${aliciMail}\n👤 *Gönderen:* ${sender}\n📝 *Konu:* ${subject}`;
@@ -55,7 +48,7 @@ export async function POST(req: NextRequest) {
const errorText = await res.text();
console.error(`[Mail Webhook] Telegram API hatası: ${res.status} ${errorText}`);
} else {
console.log(`[Webhook] Bildirim ${ownerUserKey} kullanıcısına (ID: ${targetChatId}) gönderildi.`);
console.log(`[Webhook] Bildirim ${user.email} kullanıcısına (ID: ${targetChatId}) gönderildi.`);
}
}
} else {