Block 8 complete: streaks, notifications, referrals, premium identity, gamification

This commit is contained in:
root
2026-06-15 10:32:10 +02:00
parent 6423430275
commit 5e73d1d7ad
24 changed files with 1565 additions and 38 deletions
+82
View File
@@ -0,0 +1,82 @@
import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { requireAuth } from "@/lib/auth";
export async function GET(req: NextRequest) {
const jwtPayload = await requireAuth(req);
if (!jwtPayload) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
try {
const { searchParams } = new URL(req.url);
const unreadOnly = searchParams.get("unread") === "true";
const where: any = { userId: jwtPayload.userId };
if (unreadOnly) where.read = false;
const notifications = await prisma.notification.findMany({
where,
orderBy: { createdAt: "desc" },
take: 50,
});
return NextResponse.json({ notifications });
} catch (error) {
console.error("Notifications GET error:", error);
return NextResponse.json(
{ error: "Failed to fetch notifications" },
{ status: 500 }
);
}
}
export async function PATCH(req: NextRequest) {
const jwtPayload = await requireAuth(req);
if (!jwtPayload) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
try {
const body = await req.json();
if (body.all === true) {
await prisma.notification.updateMany({
where: { userId: jwtPayload.userId, read: false },
data: { read: true },
});
return NextResponse.json({ success: true, markedAll: true });
}
if (body.id) {
const notification = await prisma.notification.findFirst({
where: { id: body.id, userId: jwtPayload.userId },
});
if (!notification) {
return NextResponse.json(
{ error: "Notification not found" },
{ status: 404 }
);
}
await prisma.notification.update({
where: { id: body.id },
data: { read: true },
});
return NextResponse.json({ success: true });
}
return NextResponse.json(
{ error: "Provide 'id' or { all: true }" },
{ status: 400 }
);
} catch (error) {
console.error("Notifications PATCH error:", error);
return NextResponse.json(
{ error: "Failed to update notification" },
{ status: 500 }
);
}
}