Convert lib/settings.ts, lib/notifications/{in-app,email,capa-escalation,
effectiveness-recheck}.ts from Supabase PostgREST to Drizzle asAdmin queries.
Drop supabase arg from all call sites in app/api/ and cron routes. Rewrite
notification unit tests to mock @/lib/db/with-user instead of SupabaseClient.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
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<string>()
|
|
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 }
|
|
}
|