forked from BRETT/BRETT
20 lines
907 B
TypeScript
20 lines
907 B
TypeScript
import { and, eq } from 'drizzle-orm'
|
|
import { notifications, users } from '../../../db/schema'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const user = await requireUser(event)
|
|
const id = Number(getRouterParam(event, 'id'))
|
|
if (!Number.isInteger(id) || id <= 0) {
|
|
throw createError({ statusCode: 400, statusMessage: 'Ungültige ID' })
|
|
}
|
|
await useDb().transaction(async (tx) => {
|
|
await tx.select({ id: users.id }).from(users).where(eq(users.id, user.id)).for('update')
|
|
const [notification] = await tx.select({ uploadId: notifications.uploadId }).from(notifications)
|
|
.where(and(eq(notifications.id, id), eq(notifications.userId, user.id))).limit(1)
|
|
if (!notification) return
|
|
await tx.update(notifications).set({ isRead: true })
|
|
.where(and(eq(notifications.uploadId, notification.uploadId), eq(notifications.userId, user.id)))
|
|
})
|
|
return { ok: true }
|
|
})
|