Initial commit v1.1
This commit is contained in:
+17
-37
@@ -1,50 +1,30 @@
|
||||
'use server'
|
||||
|
||||
import { z } from 'zod'
|
||||
import { createClient, createAdminClient } from '@/lib/supabase/server'
|
||||
|
||||
const InviteSchema = z.object({
|
||||
roundId: z.string().uuid(),
|
||||
email: z.string().email(),
|
||||
})
|
||||
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 = InviteSchema.safeParse({ roundId, email })
|
||||
const parsed = z.object({ roundId: z.string().uuid(), email: z.string().email() })
|
||||
.safeParse({ roundId, email })
|
||||
if (!parsed.success) return { error: 'Invalid email address' }
|
||||
|
||||
const supabase = await createClient()
|
||||
const adminClient = await createAdminClient()
|
||||
// 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) return { error: 'Not authenticated' }
|
||||
|
||||
const {
|
||||
data: { user },
|
||||
} = await supabase.auth.getUser()
|
||||
if (!user) return { error: 'Not authenticated' }
|
||||
|
||||
const { data: round } = await db
|
||||
.from('rounds')
|
||||
.select('status, created_by')
|
||||
.eq('id', parsed.data.roundId)
|
||||
.single()
|
||||
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.created_by !== user.id) return { error: 'Only the round creator can invite players' }
|
||||
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' }
|
||||
|
||||
const redirectTo = `${process.env.SITE_URL}/auth/confirm?next=${encodeURIComponent(`/rounds/${parsed.data.roundId}/join`)}`
|
||||
|
||||
const { error: inviteError } = await (adminClient as any).auth.admin.inviteUserByEmail(
|
||||
parsed.data.email,
|
||||
{ redirectTo },
|
||||
)
|
||||
if (inviteError) return { error: inviteError.message }
|
||||
|
||||
await db.from('round_invites').insert({
|
||||
round_id: parsed.data.roundId,
|
||||
email: parsed.data.email,
|
||||
invited_by: user.id,
|
||||
})
|
||||
|
||||
return { success: true }
|
||||
return inviteToRound(roundId, email, session.user.id)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user