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

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

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 {requireUser} from '@/lib/guard'
import {setSharing} from '@/lib/availability-server'
import {route} from '@/lib/route'
 
const Body = z.object({
  shareAvailability: z.boolean().optional(),
  blockOtherBands: z.boolean().optional(),
  /** The first-time tour: done (finished or skipped), or show it again */
  tourDone: z.boolean().optional(),
  /** What push notifications tell them about (on every device) */
  notifyCharts: z.boolean().optional(),
  notifySetlists: z.boolean().optional(),
  notifyRehearsals: z.boolean().optional(),
  notifyProposals: z.boolean().optional(),
})
 
/** Personal settings that span every band someone is in. */
export const PATCH = 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 {shareAvailability, blockOtherBands, tourDone, ...notify} = parsed.data
  await prisma.$transaction(async (tx) => {
    if (
      shareAvailability !== undefined &&
      shareAvailability !== user.shareAvailability
    )
      await setSharing(tx, user.id, shareAvailability)
    if (blockOtherBands !== undefined)
      await tx.user.update({where: {id: user.id}, data: {blockOtherBands}})
    if (Object.keys(notify).length)
      await tx.user.update({where: {id: user.id}, data: notify})
    if (tourDone !== undefined)
      await tx.user.update({
        where: {id: user.id},
        data: {tourDoneAt: tourDone ? new Date() : null},
      })
  })
  return new Response(null, {status: 204})
})