31 lines
1.1 KiB
TypeScript
31 lines
1.1 KiB
TypeScript
'use server'
|
|
|
|
import { z } from 'zod'
|
|
import { db } from '@/lib/db'
|
|
import { rounds } from '@/lib/db/schema'
|
|
import { eq } from 'drizzle-orm'
|
|
import { auth } from '@/lib/auth'
|
|
import { headers } from 'next/headers'
|
|
import { inviteToRound } from './rounds'
|
|
|
|
export async function inviteFromLobby(roundId: string, email: string) {
|
|
const parsed = z.object({ roundId: z.string().uuid(), email: z.string().email() })
|
|
.safeParse({ roundId, email })
|
|
if (!parsed.success) return { error: 'Invalid email address' }
|
|
|
|
const session = await auth.api.getSession({ headers: await headers() })
|
|
if (!session) return { error: 'Not authenticated' }
|
|
|
|
const [round] = await db
|
|
.select({ status: rounds.status, createdBy: rounds.createdBy })
|
|
.from(rounds)
|
|
.where(eq(rounds.id, roundId))
|
|
.limit(1)
|
|
|
|
if (!round) return { error: 'Round not found' }
|
|
if (round.createdBy !== session.user.id) return { error: 'Only the round creator can invite players' }
|
|
if (round.status !== 'lobby') return { error: 'Cannot invite after the round has started' }
|
|
|
|
return inviteToRound(roundId, email, session.user.id)
|
|
}
|