2026-06-23 20:36:07 -04:00
|
|
|
import { NextResponse } from "next/server"
|
|
|
|
|
import { and, eq } from "drizzle-orm"
|
|
|
|
|
import { db } from "@/lib/db"
|
|
|
|
|
import { notifications } from "@/lib/db/schema"
|
|
|
|
|
import { getSessionUser } from "@/lib/session"
|
2026-07-02 13:42:34 -04:00
|
|
|
import { getAccountContext } from "@/lib/account"
|
2026-06-23 20:36:07 -04:00
|
|
|
|
|
|
|
|
// PATCH /api/notifications/read — mark all of the user's notifications as read.
|
|
|
|
|
export async function PATCH() {
|
|
|
|
|
const user = await getSessionUser()
|
|
|
|
|
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
|
|
|
|
|
2026-07-02 13:42:34 -04:00
|
|
|
const ctx = await getAccountContext(user.id)
|
|
|
|
|
const ownerId = ctx.ownerId
|
|
|
|
|
if (!ctx.canWrite) return NextResponse.json({ error: "Forbidden" }, { status: 403 })
|
|
|
|
|
|
2026-06-23 20:36:07 -04:00
|
|
|
await db
|
|
|
|
|
.update(notifications)
|
|
|
|
|
.set({ read: true })
|
2026-07-02 13:42:34 -04:00
|
|
|
.where(and(eq(notifications.user_id, ownerId), eq(notifications.read, false)))
|
2026-06-23 20:36:07 -04:00
|
|
|
|
|
|
|
|
return NextResponse.json({ ok: true })
|
|
|
|
|
}
|