All files / web/src/app/api/settings/notification-preferences/push-subscriptions route.ts

0% Statements 0/65
0% Branches 0/1
0% Functions 0/1
0% Lines 0/65

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66                                                                                                                                   
import { NextResponse, type NextRequest } from 'next/server'
import { withAuth } from '@/lib/auth/withAuth'
import { db } from '@/db'
import { userPushSubscriptions } from '@/db/schema'
import { and, eq } from 'drizzle-orm'

/** POST /api/settings/notification-preferences/push-subscriptions — register a push endpoint */
export const POST = withAuth(async (request: NextRequest, { userId }) => {
  const body = await request.json()
  const { endpoint, keys, deviceLabel } = body as {
    endpoint: string
    keys: { p256dh: string; auth: string }
    deviceLabel?: string
  }

  if (!endpoint || !keys?.p256dh || !keys?.auth) {
    return NextResponse.json({ error: 'endpoint and keys are required' }, { status: 400 })
  }

  // Upsert: if this endpoint already exists for this user, just update it
  const [existing] = await db
    .select({ id: userPushSubscriptions.id })
    .from(userPushSubscriptions)
    .where(
      and(eq(userPushSubscriptions.userId, userId), eq(userPushSubscriptions.endpoint, endpoint))
    )
    .limit(1)

  if (existing) {
    await db
      .update(userPushSubscriptions)
      .set({ keys, deviceLabel: deviceLabel ?? null, lastUsedAt: new Date() })
      .where(eq(userPushSubscriptions.id, existing.id))

    return NextResponse.json({ id: existing.id, created: false })
  }

  const [sub] = await db
    .insert(userPushSubscriptions)
    .values({
      userId,
      endpoint,
      keys,
      deviceLabel: deviceLabel ?? null,
    })
    .returning({ id: userPushSubscriptions.id })

  return NextResponse.json({ id: sub.id, created: true }, { status: 201 })
})

/** DELETE /api/settings/notification-preferences/push-subscriptions — remove a push endpoint */
export const DELETE = withAuth(async (request: NextRequest, { userId }) => {
  const { searchParams } = new URL(request.url)
  const id = searchParams.get('id')

  if (!id) {
    return NextResponse.json({ error: 'id is required' }, { status: 400 })
  }

  await db
    .delete(userPushSubscriptions)
    .where(and(eq(userPushSubscriptions.id, id), eq(userPushSubscriptions.userId, userId)))

  return NextResponse.json({ success: true })
})