116 lines
2.8 KiB
TypeScript
116 lines
2.8 KiB
TypeScript
|
|
import { NextRequest, NextResponse } from 'next/server';
|
||
|
|
import { prisma } from '@/lib/prisma';
|
||
|
|
import { getCurrentUser } from '@/lib/auth';
|
||
|
|
|
||
|
|
// GET - Récupérer les notifications de l'utilisateur
|
||
|
|
export async function GET(request: NextRequest) {
|
||
|
|
try {
|
||
|
|
const user = await getCurrentUser();
|
||
|
|
if (!user) {
|
||
|
|
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
|
||
|
|
}
|
||
|
|
|
||
|
|
const searchParams = request.nextUrl.searchParams;
|
||
|
|
const limit = parseInt(searchParams.get('limit') || '50');
|
||
|
|
const unreadOnly = searchParams.get('unreadOnly') === 'true';
|
||
|
|
|
||
|
|
const where: any = {
|
||
|
|
userId: user.id,
|
||
|
|
};
|
||
|
|
|
||
|
|
if (unreadOnly) {
|
||
|
|
where.read = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
const notifications = await prisma.notification.findMany({
|
||
|
|
where,
|
||
|
|
orderBy: {
|
||
|
|
createdAt: 'desc',
|
||
|
|
},
|
||
|
|
take: limit,
|
||
|
|
});
|
||
|
|
|
||
|
|
const unreadCount = await prisma.notification.count({
|
||
|
|
where: {
|
||
|
|
userId: user.id,
|
||
|
|
read: false,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
return NextResponse.json({
|
||
|
|
notifications,
|
||
|
|
unreadCount,
|
||
|
|
});
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Erreur lors de la récupération des notifications:', error);
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: 'Erreur serveur' },
|
||
|
|
{ status: 500 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// POST - Marquer une notification comme lue
|
||
|
|
export async function POST(request: NextRequest) {
|
||
|
|
try {
|
||
|
|
const user = await getCurrentUser();
|
||
|
|
if (!user) {
|
||
|
|
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
|
||
|
|
}
|
||
|
|
|
||
|
|
const body = await request.json();
|
||
|
|
const { notificationId, markAllAsRead } = body;
|
||
|
|
|
||
|
|
if (markAllAsRead) {
|
||
|
|
// Marquer toutes les notifications comme lues
|
||
|
|
await prisma.notification.updateMany({
|
||
|
|
where: {
|
||
|
|
userId: user.id,
|
||
|
|
read: false,
|
||
|
|
},
|
||
|
|
data: {
|
||
|
|
read: true,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
return NextResponse.json({ success: true });
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!notificationId) {
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: 'ID de notification requis' },
|
||
|
|
{ status: 400 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Vérifier que la notification appartient à l'utilisateur
|
||
|
|
const notification = await prisma.notification.findFirst({
|
||
|
|
where: {
|
||
|
|
id: notificationId,
|
||
|
|
userId: user.id,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!notification) {
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: 'Notification non trouvée' },
|
||
|
|
{ status: 404 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Marquer comme lue
|
||
|
|
await prisma.notification.update({
|
||
|
|
where: { id: notificationId },
|
||
|
|
data: { read: true },
|
||
|
|
});
|
||
|
|
|
||
|
|
return NextResponse.json({ success: true });
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Erreur lors de la mise à jour de la notification:', error);
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: 'Erreur serveur' },
|
||
|
|
{ status: 500 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|