![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/backups/lavocat.quebec/backup-20250730-021618/src/pages/api/ |
import { NextApiRequest, NextApiResponse } from 'next';
import { getServerSession } from 'next-auth/next';
import { prisma } from '@/lib/prisma';
import { authOptions } from '@/lib/auth';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const session = await getServerSession(req, res, authOptions);
if (!session?.user?.id) {
return res.status(401).json({ message: 'Unauthorized' });
}
if (req.method === 'GET') {
try {
const { page = '1', limit = '20' } = req.query;
const pageNum = parseInt(page as string);
const limitNum = parseInt(limit as string);
const skip = (pageNum - 1) * limitNum;
const notifications = await prisma.notification.findMany({
where: { userId: session.user.id },
orderBy: { createdAt: 'desc' },
skip,
take: limitNum
});
return res.status(200).json(notifications);
} catch (error) {
console.error('Error fetching notifications:', error);
return res.status(500).json({ message: 'Internal server error' });
}
}
if (req.method === 'PATCH') {
try {
const { ids } = req.body;
if (!ids || (Array.isArray(ids) && ids.length === 0)) {
return res.status(400).json({ message: 'No notification IDs provided' });
}
const idArray = Array.isArray(ids) ? ids : [ids];
await prisma.notification.updateMany({
where: {
id: { in: idArray },
userId: session.user.id
},
data: {
isRead: true,
readAt: new Date()
}
});
return res.status(200).json({ message: 'Notifications marked as read' });
} catch (error) {
console.error('Error marking notifications as read:', error);
return res.status(500).json({ message: 'Internal server error' });
}
}
return res.status(405).json({ message: 'Method not allowed' });
}