Initial commit v1.1
This commit is contained in:
@@ -1,63 +1,84 @@
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { auth } from '@/lib/auth'
|
||||
import { db } from '@/lib/db'
|
||||
import { rounds, roundPlayers, roundInvites, courses, tees, user } from '@/lib/db/schema'
|
||||
import { eq } from 'drizzle-orm'
|
||||
import { redirect, notFound } from 'next/navigation'
|
||||
import { headers } from 'next/headers'
|
||||
import { RoundLobby } from '@/components/round/round-lobby'
|
||||
|
||||
export default async function RoundPage({ params }: { params: Promise<{ id: string }> }) {
|
||||
const { id } = await params
|
||||
const supabase = await createClient()
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const db = supabase as any
|
||||
const session = await auth.api.getSession({ headers: await headers() })
|
||||
if (!session) redirect('/login')
|
||||
|
||||
const {
|
||||
data: { user },
|
||||
} = await supabase.auth.getUser()
|
||||
if (!user) redirect('/login')
|
||||
|
||||
const { data: round } = await db
|
||||
.from('rounds')
|
||||
.select(
|
||||
`
|
||||
id, name, date, status, holes_count, created_by,
|
||||
course:courses (id, name, par),
|
||||
tee:tees (id, name, color, course_rating, slope_rating)
|
||||
`,
|
||||
)
|
||||
.eq('id', id)
|
||||
.single()
|
||||
const [round] = await db
|
||||
.select({
|
||||
id: rounds.id,
|
||||
name: rounds.name,
|
||||
date: rounds.date,
|
||||
status: rounds.status,
|
||||
holesCount: rounds.holesCount,
|
||||
createdBy: rounds.createdBy,
|
||||
courseName: courses.name,
|
||||
teeName: tees.name,
|
||||
teeColor: tees.color,
|
||||
courseRating: tees.courseRating,
|
||||
slopeRating: tees.slopeRating,
|
||||
})
|
||||
.from(rounds)
|
||||
.innerJoin(courses, eq(rounds.courseId, courses.id))
|
||||
.innerJoin(tees, eq(rounds.teeId, tees.id))
|
||||
.where(eq(rounds.id, id))
|
||||
.limit(1)
|
||||
|
||||
if (!round) notFound()
|
||||
|
||||
const { data: players } = await db
|
||||
.from('round_players')
|
||||
.select(
|
||||
`
|
||||
id, role, handicap_index, course_handicap, joined_at,
|
||||
profile:profiles (id, display_name, avatar_url)
|
||||
`,
|
||||
)
|
||||
.eq('round_id', id)
|
||||
.order('joined_at')
|
||||
|
||||
const { data: invites } = await db
|
||||
.from('round_invites')
|
||||
.select('id, email, accepted_at, created_at')
|
||||
.eq('round_id', id)
|
||||
.is('accepted_at', null)
|
||||
.order('created_at')
|
||||
|
||||
const isCreator = round.created_by === user.id
|
||||
|
||||
if (round.status === 'active' || round.status === 'completed') {
|
||||
redirect(`/rounds/${id}/scorecard`)
|
||||
}
|
||||
|
||||
const players = await db
|
||||
.select({
|
||||
id: roundPlayers.id,
|
||||
userId: roundPlayers.userId,
|
||||
role: roundPlayers.role,
|
||||
handicapIndex: roundPlayers.handicapIndex,
|
||||
courseHandicap: roundPlayers.courseHandicap,
|
||||
joinedAt: roundPlayers.joinedAt,
|
||||
displayName: user.name,
|
||||
avatarUrl: user.image,
|
||||
})
|
||||
.from(roundPlayers)
|
||||
.innerJoin(user, eq(roundPlayers.userId, user.id))
|
||||
.where(eq(roundPlayers.roundId, id))
|
||||
.orderBy(roundPlayers.joinedAt)
|
||||
|
||||
const pendingInvites = await db
|
||||
.select({ id: roundInvites.id, email: roundInvites.email, createdAt: roundInvites.createdAt })
|
||||
.from(roundInvites)
|
||||
.where(eq(roundInvites.roundId, id))
|
||||
|
||||
return (
|
||||
<RoundLobby
|
||||
round={round}
|
||||
players={players ?? []}
|
||||
pendingInvites={invites ?? []}
|
||||
currentUserId={user.id}
|
||||
isCreator={isCreator}
|
||||
round={{
|
||||
id: round.id,
|
||||
name: round.name,
|
||||
date: round.date,
|
||||
status: round.status,
|
||||
holesCount: round.holesCount,
|
||||
createdBy: round.createdBy,
|
||||
course: { name: round.courseName },
|
||||
tee: { name: round.teeName, color: round.teeColor },
|
||||
}}
|
||||
players={players.map((p) => ({
|
||||
id: p.id,
|
||||
role: p.role,
|
||||
handicapIndex: p.handicapIndex,
|
||||
courseHandicap: p.courseHandicap,
|
||||
profile: { id: p.userId, displayName: p.displayName, avatarUrl: p.avatarUrl },
|
||||
}))}
|
||||
pendingInvites={pendingInvites.map((i) => ({ id: i.id, email: i.email, createdAt: i.createdAt, acceptedAt: null }))}
|
||||
currentUserId={session.user.id}
|
||||
isCreator={round.createdBy === session.user.id}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user