All files / Bandstand/src/app/api/bands/current route.ts

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

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                                                       
export const dynamic = 'force-dynamic'
 
import {cookies} from 'next/headers'
import {z} from 'zod'
import {requireUser} from '@/lib/guard'
import {BAND_COOKIE, myBands} from '@/lib/band'
import {route} from '@/lib/route'
 
const Body = z.object({bandId: z.string().min(1)})
 
/** Switch the band this device shows. Only to a band you're in. */
export const POST = route(async (req: Request) => {
  const {user} = await requireUser()
  const parsed = Body.safeParse(await req.json())
  if (!parsed.success) return new Response('Bad Request', {status: 400})
  const bands = await myBands(user.id)
  if (!bands.some((b) => b.id === parsed.data.bandId))
    return new Response('Not Found', {status: 404})
  cookies().set(BAND_COOKIE, parsed.data.bandId, {
    httpOnly: true,
    sameSite: 'lax',
    secure: process.env.NODE_ENV === 'production',
    path: '/',
    maxAge: 400 * 24 * 60 * 60,
  })
  return new Response(null, {status: 204})
})