All files / Bandstand/src/app/api/account/calendar route.ts

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

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 {randomBytes} from 'crypto'
import {prisma} from '@/lib/db'
import {requireUser} from '@/lib/guard'
import {route} from '@/lib/route'
import {requestOrigin} from '@/lib/band'
 
// On the address they're using, so each band's members see their own domain
const feedUrl = (token: string) =>
  `${requestOrigin()}/api/calendar/${token}.ics`
 
/** This person's calendar-feed link, or null if they haven't made one. */
export const GET = route(async () => {
  const {user} = await requireUser()
  return Response.json({
    url: user.calendarToken ? feedUrl(user.calendarToken) : null,
  })
})
 
/** Make the link, or replace it (the old one stops working). */
export const POST = route(async () => {
  const {user} = await requireUser()
  const token = randomBytes(24).toString('base64url')
  await prisma.user.update({where: {id: user.id}, data: {calendarToken: token}})
  return Response.json({url: feedUrl(token)}, {status: 201})
})