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

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

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                                                                                         
export const dynamic = 'force-dynamic'
 
import {z} from 'zod'
import {prisma} from '@/lib/db'
import {requireSession} from '@/lib/guard'
import {logActivity} from '@/lib/songs'
import {formatDay} from '@/lib/availability'
import {route} from '@/lib/route'
 
const Body = z.object({
  date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/),
  time: z.string().trim().max(50).nullable().optional(),
  place: z.string().trim().max(200).nullable().optional(),
  note: z.string().trim().max(500).nullable().optional(),
})
 
export const POST = route(async (req: Request) => {
  const {user, band} = await requireSession()
  const parsed = Body.safeParse(await req.json())
  if (!parsed.success) return new Response('Bad Request', {status: 400})
  const {date, time, place, note} = parsed.data
  const r = await prisma.$transaction(async (tx) => {
    const r = await tx.rehearsal.create({
      data: {
        bandId: band.id,
        date: new Date(date + 'T00:00:00Z'),
        time: time || null,
        place: place || null,
        note: note || null,
        createdById: user.id,
      },
    })
    await logActivity(tx, {
      bandId: band.id,
      userId: user.id,
      action: 'rehearsal.create',
      targetType: 'rehearsal',
      targetId: r.id,
      summary: `set a rehearsal for ${formatDay(date)}${time ? ` at ${time}` : ''}`,
    })
    return r
  })
  return Response.json({id: r.id}, {status: 201})
})