Implement multi-user WhatsApp notifications and settings UI
This commit is contained in:
38
app/api/users/profile/route.ts
Normal file
38
app/api/users/profile/route.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { auth } from "@/auth";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export async function GET() {
|
||||
const session = await auth();
|
||||
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: session.user.id }
|
||||
});
|
||||
|
||||
return NextResponse.json(user);
|
||||
}
|
||||
|
||||
export async function PATCH(req: Request) {
|
||||
const session = await auth();
|
||||
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
try {
|
||||
const body = await req.json();
|
||||
const { telegramId, telegramEnabled, whatsappNumber, whatsappEnabled } = body;
|
||||
|
||||
const user = await prisma.user.update({
|
||||
where: { id: session.user.id },
|
||||
data: {
|
||||
telegramId,
|
||||
telegramEnabled,
|
||||
whatsappNumber,
|
||||
whatsappEnabled
|
||||
}
|
||||
});
|
||||
|
||||
return NextResponse.json(user);
|
||||
} catch (error: any) {
|
||||
return NextResponse.json({ error: error.message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -67,20 +67,27 @@ export async function POST(request: Request) {
|
||||
};
|
||||
|
||||
// 3. Bildirim Gönder (Telegram)
|
||||
const notificationResult = await sendTelegramNotification(
|
||||
mapping.userId,
|
||||
to,
|
||||
mailData.from,
|
||||
mailData.subject,
|
||||
"" // Analiz bilgisini kaldırdık
|
||||
);
|
||||
let tgStatus = 'SKIPPED';
|
||||
if (mapping.user.telegramEnabled && mapping.user.telegramId) {
|
||||
const tgResult = await sendTelegramNotification(
|
||||
mapping.userId,
|
||||
to,
|
||||
mailData.from,
|
||||
mailData.subject,
|
||||
""
|
||||
);
|
||||
tgStatus = tgResult.status;
|
||||
}
|
||||
|
||||
// 4. Bildirim Gönder (WhatsApp)
|
||||
// Şu an için varsayılan numaraya gönderiyoruz, ilerde User modeline alan eklenebilir.
|
||||
const waNumber = process.env.DEFAULT_WHATSAPP_NUMBER || '905543765103';
|
||||
const waMessage = `📩 *Yeni E-posta*\n\n*Gönderen:* ${mailData.from}\n*Konu:* ${mailData.subject}\n*Alıcı:* ${to}\n\n_AyrisMail Central_`;
|
||||
|
||||
await sendWA(waNumber, waMessage);
|
||||
let waStatus = 'SKIPPED';
|
||||
if (mapping.user.whatsappEnabled && (mapping.user.whatsappNumber || process.env.DEFAULT_WHATSAPP_NUMBER)) {
|
||||
const waNumber = mapping.user.whatsappNumber || process.env.DEFAULT_WHATSAPP_NUMBER;
|
||||
const waMessage = `📩 *Yeni E-posta*\n\n*Gönderen:* ${mailData.from}\n*Konu:* ${mailData.subject}\n*Alıcı:* ${to}\n\n_AyrisMail Central_`;
|
||||
|
||||
const waResult = await sendWA(waNumber, waMessage, mapping.userId);
|
||||
waStatus = waResult.success ? 'SENT' : 'FAILED';
|
||||
}
|
||||
|
||||
// 5. Bildirim Logu
|
||||
await prisma.notificationLog.create({
|
||||
@@ -88,9 +95,9 @@ export async function POST(request: Request) {
|
||||
mailbox: to,
|
||||
sender: mailData.from,
|
||||
subject: mailData.subject,
|
||||
status: notificationResult.status,
|
||||
status: tgStatus === 'SENT' || waStatus === 'SENT' ? 'SENT' : 'FAILED',
|
||||
userId: mapping.userId,
|
||||
error: notificationResult.error
|
||||
error: tgStatus === 'FAILED' ? 'TG Failed' : (waStatus === 'FAILED' ? 'WA Failed' : null)
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
19
app/api/whatsapp/status/route.ts
Normal file
19
app/api/whatsapp/status/route.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { auth } from "@/auth";
|
||||
|
||||
export async function GET() {
|
||||
const session = await auth();
|
||||
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const userId = session.user.id;
|
||||
const workerUrl = process.env.WHATSAPP_WORKER_URL;
|
||||
const secret = process.env.WHATSAPP_SECRET;
|
||||
|
||||
try {
|
||||
const res = await fetch(`${workerUrl}/status?userId=${userId}&secret=${secret}`);
|
||||
const data = await res.json();
|
||||
return NextResponse.json(data);
|
||||
} catch (error: any) {
|
||||
return NextResponse.json({ status: 'error', error: error.message });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user