import 'server-only' import { asAdmin } from '@/lib/db/with-user' import { notificationsLog } from '@/lib/db/schema' export interface InAppNotification { userId: string title: string link?: string incidentId?: string capaId?: string } export async function createInAppNotifications( notifications: InAppNotification[], ): Promise<{ created: number }> { const seen = new Set() let created = 0 for (const n of notifications) { if (!n.userId || !n.title) continue const key = `${n.userId}|${n.title}|${n.incidentId ?? ''}|${n.capaId ?? ''}` if (seen.has(key)) continue seen.add(key) try { await asAdmin(db => db.insert(notificationsLog).values({ channel: 'in_app', recipient: n.userId, recipientUserId: n.userId, title: n.title, link: n.link ?? null, incidentId: n.incidentId ?? null, capaId: n.capaId ?? null, }) ) created++ } catch (e) { console.error('in-app notification error:', e) } } return { created } }