All files / Bandstand/src/app/api/band route.ts

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

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                                                                                                 
export const dynamic = 'force-dynamic'
 
import {prisma} from '@/lib/db'
import {requireAdmin, requireSession} from '@/lib/guard'
import {BandFields} from '@/lib/band-fields'
import {logActivity} from '@/lib/songs'
import {route} from '@/lib/route'
 
/** The current band's settings. */
export const GET = route(async () => {
  const {band} = await requireSession()
  return Response.json(band)
})
 
export const PATCH = route(async (req: Request) => {
  const {user, band} = await requireAdmin()
  const parsed = BandFields.safeParse(await req.json())
  if (!parsed.success)
    return Response.json(
      {error: parsed.error.issues[0]?.message ?? 'Bad Request'},
      {status: 400},
    )
  const changed = Object.entries(parsed.data).filter(
    ([k, v]) => v !== undefined && band[k as keyof typeof band] !== v,
  )
  if (!changed.length) return new Response(null, {status: 204})
  await prisma.$transaction(async (tx) => {
    await tx.band.update({where: {id: band.id}, data: parsed.data})
    const LABELS: Record<string, string> = {
      name: 'the band name',
      appName: 'the app name',
      timezone: 'the time zone',
      chatUrl: 'the chat link',
      tributeTo: 'who the band covers',
      voteThreshold: 'the votes needed',
      scheduling: 'the scheduling tool',
    }
    await logActivity(tx, {
      bandId: band.id,
      userId: user.id,
      action: 'band.update',
      targetType: 'band',
      targetId: band.id,
      summary: `changed ${changed.map(([k]) => LABELS[k]).join(', ')}`,
    })
  })
  return new Response(null, {status: 204})
})