All files / Bandstand/src/app/(band)/rehearsals page.tsx

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

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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73                                                                                                                                                 
export const dynamic = 'force-dynamic'
 
import {prisma} from '@/lib/db'
import {pageSession} from '@/lib/guard'
import {addDays, dayRange, keyOf, todayKey} from '@/lib/availability'
import {bandEntries, otherBandBlocks} from '@/lib/availability-server'
import {displayName} from '@/lib/songs'
import {avatarUrl} from '@/lib/avatars'
import Rehearsals from '@/components/Rehearsals'
 
export const metadata = {title: 'Rehearsals'}
 
// How far ahead to look for a day everyone can make
const HORIZON_DAYS = 730
 
export default async function RehearsalsPage() {
  const {user, band, isAdmin} = await pageSession()
  const today = todayKey(new Date(), band.timezone)
  const from = new Date(today + 'T00:00:00Z')
  const to = new Date(addDays(today, HORIZON_DAYS) + 'T00:00:00Z')
  const [members, rehearsals, myBandCount] = await Promise.all([
    prisma.membership.findMany({
      where: {bandId: band.id},
      select: {user: true},
      orderBy: {user: {name: 'asc'}},
    }),
    prisma.rehearsal.findMany({
      where: {bandId: band.id, date: {gte: from}},
      orderBy: {date: 'asc'},
      include: {
        createdBy: {select: {name: true, displayName: true, email: true}},
      },
    }),
    prisma.membership.count({where: {userId: user.id}}),
  ])
  const users = members.map((m) => m.user)
  // Scheduling tool off: just the rehearsals, no one's days off
  const [entries, blocks] = band.scheduling
    ? await Promise.all([
        bandEntries(band.id, users, from, to),
        otherBandBlocks(band.id, users, user.id, from, to),
      ])
    : [[], []]
  return (
    <Rehearsals
      me={user.id}
      isAdmin={isAdmin}
      horizon={dayRange(today, HORIZON_DAYS)}
      members={users.map((u) => ({
        id: u.id,
        name: displayName(u),
        answered: Boolean(u.availabilityUpdatedAt),
        avatar: avatarUrl(u),
      }))}
      entries={entries}
      blocks={blocks}
      rehearsals={rehearsals.map((r) => ({
        id: r.id,
        date: keyOf(r.date),
        time: r.time,
        place: r.place,
        note: r.note,
        by: displayName(r.createdBy),
        mine: r.createdById === user.id,
      }))}
      bandName={band.name}
      scheduling={band.scheduling}
      timezone={band.timezone}
      blocksOn={user.blockOtherBands && myBandCount > 1}
    />
  )
}