56 lines
1.4 KiB
TypeScript
56 lines
1.4 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { prisma } from "@/lib/prisma";
|
|
import { requireAuth } from "@/lib/session";
|
|
|
|
export async function GET(req: NextRequest) {
|
|
const session = await requireAuth();
|
|
if (!session) return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
|
|
|
|
try {
|
|
const messages = await prisma.message.findMany({
|
|
orderBy: { createdAt: "desc" },
|
|
include: {
|
|
readBy: {
|
|
where: { userId: session.user.id },
|
|
},
|
|
},
|
|
});
|
|
|
|
const result = messages.map((m) => ({
|
|
...m,
|
|
isRead: m.readBy.length > 0,
|
|
}));
|
|
|
|
return NextResponse.json(result);
|
|
} catch {
|
|
return NextResponse.json({ error: "Erreur" }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function POST(req: NextRequest) {
|
|
const session = await requireAuth();
|
|
if (!session) return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
|
|
|
|
const { messageId } = await req.json();
|
|
if (!messageId) return NextResponse.json({ error: "messageId requis" }, { status: 400 });
|
|
|
|
try {
|
|
await prisma.messageRead.upsert({
|
|
where: {
|
|
messageId_userId: {
|
|
messageId,
|
|
userId: session.user.id,
|
|
},
|
|
},
|
|
update: {},
|
|
create: {
|
|
messageId,
|
|
userId: session.user.id,
|
|
},
|
|
});
|
|
return NextResponse.json({ ok: true });
|
|
} catch {
|
|
return NextResponse.json({ error: "Erreur" }, { status: 500 });
|
|
}
|
|
}
|