'use server' import { prisma } from '@/lib/prisma' import { auth } from '@/auth' export interface AppNotification { id: string type: string title: string message: string | null read: boolean actionUrl: string | null relatedId: string | null createdAt: Date } export async function getUnreadNotifications(): Promise { const session = await auth() if (!session?.user?.id) return [] try { const notifications = await prisma.notification.findMany({ where: { userId: session.user.id, read: false }, orderBy: { createdAt: 'desc' }, take: 20, }) return notifications } catch { return [] } } export async function markNotificationRead(id: string): Promise { const session = await auth() if (!session?.user?.id) return await prisma.notification.updateMany({ where: { id, userId: session.user.id }, data: { read: true }, }) } export async function markAllNotificationsRead(): Promise { const session = await auth() if (!session?.user?.id) return await prisma.notification.updateMany({ where: { userId: session.user.id, read: false }, data: { read: true }, }) } /** Create a notification (called from server-side code, not exposed to client) */ export async function createNotification(data: { userId: string type: string title: string message?: string actionUrl?: string relatedId?: string }): Promise { try { await prisma.notification.create({ data: { userId: data.userId, type: data.type, title: data.title, message: data.message || null, actionUrl: data.actionUrl || null, relatedId: data.relatedId || null, }, }) } catch (e) { console.error('[Notification] Failed to create:', e) } }