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

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

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                                                                                     
export const dynamic = 'force-dynamic'
 
import {prisma} from '@/lib/db'
import {requireSession} from '@/lib/guard'
import {SetlistBody, saveSetlist} from '@/lib/setlists'
import {logActivity} from '@/lib/songs'
import {route} from '@/lib/route'
 
export const GET = route(async () => {
  const {band} = await requireSession()
  const sets = await prisma.setlist.findMany({
    where: {bandId: band.id},
    orderBy: [{gigDate: {sort: 'asc', nulls: 'last'}}, {updatedAt: 'desc'}],
    include: {_count: {select: {items: true}}},
  })
  return Response.json(sets)
})
 
export const POST = route(async (req: Request) => {
  const {user, band} = await requireSession()
  const parsed = SetlistBody.safeParse(await req.json())
  if (!parsed.success) return new Response('Bad Request', {status: 400})
  const set = await prisma.$transaction(async (tx) => {
    const s = await tx.setlist.create({
      data: {name: parsed.data.name, bandId: band.id, updatedById: user.id},
    })
    await logActivity(tx, {
      bandId: band.id,
      userId: user.id,
      action: 'setlist.create',
      targetType: 'setlist',
      targetId: s.id,
      summary: `created the setlist ${s.name}`,
    })
    return s
  })
  // Dates, venue and songs go through the same path as later edits
  const {name: _n, ...rest} = parsed.data
  if (Object.keys(rest).length)
    await saveSetlist(band.id, set.id, user.id, {...rest, name: set.name})
  return Response.json({id: set.id}, {status: 201})
})