Initial commit v1

This commit is contained in:
Rolf
2026-03-18 12:37:17 +01:00
commit 4897d3003f
55 changed files with 14241 additions and 0 deletions
+62
View File
@@ -0,0 +1,62 @@
'use server'
import { z } from 'zod'
import { createClient } from '@/lib/supabase/server'
import { revalidatePath } from 'next/cache'
const TeeInput = z.object({
name: z.string().min(1, 'Tee name required'),
color: z.string().regex(/^#[0-9a-fA-F]{6}$/),
course_rating: z.coerce.number().min(50).max(80),
slope_rating: z.coerce.number().int().min(55).max(155),
})
const HoleInput = z.object({
hole_number: z.number().int().min(1).max(18),
par: z.number().int().min(3).max(5),
stroke_index: z.number().int().min(1).max(18),
})
export const CreateCourseSchema = z.object({
name: z.string().min(1, 'Course name required').max(100),
par: z.number().int().min(27).max(90),
tees: z.array(TeeInput).min(1, 'At least one tee required'),
holes: z.array(HoleInput).min(9),
})
export type CreateCourseInput = z.infer<typeof CreateCourseSchema>
export async function createCourse(data: CreateCourseInput) {
const parsed = CreateCourseSchema.safeParse(data)
if (!parsed.success) {
return { error: parsed.error.issues[0]?.message ?? 'Invalid data' }
}
const supabase = await createClient()
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const db = supabase as any
const {
data: { user },
} = await supabase.auth.getUser()
if (!user) return { error: 'Not authenticated' }
const { data: course, error: courseError } = await db
.from('courses')
.insert({ name: parsed.data.name, par: parsed.data.par, created_by: user.id })
.select('id')
.single()
if (courseError) return { error: courseError.message }
const { error: teesError } = await db
.from('tees')
.insert(parsed.data.tees.map((t) => ({ ...t, course_id: course.id })))
if (teesError) return { error: teesError.message }
const { error: holesError } = await db
.from('holes')
.insert(parsed.data.holes.map((h) => ({ ...h, course_id: course.id })))
if (holesError) return { error: holesError.message }
revalidatePath('/rounds/new')
return { courseId: course.id as string }
}
+50
View File
@@ -0,0 +1,50 @@
'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(),
})
export async function inviteFromLobby(roundId: string, email: string) {
const parsed = InviteSchema.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 {
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()
if (!round) return { error: 'Round not found' }
if (round.created_by !== 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 }
}
+129
View File
@@ -0,0 +1,129 @@
'use server'
import { z } from 'zod'
import { createClient, createAdminClient } from '@/lib/supabase/server'
import { revalidatePath } from 'next/cache'
const CreateRoundSchema = z.object({
course_id: z.string().uuid(),
tee_id: z.string().uuid(),
name: z.string().max(100).optional(),
date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/),
holes_count: z.union([z.literal(9), z.literal(18)]),
emails: z.array(z.string().email()).default([]),
})
export type CreateRoundInput = z.infer<typeof CreateRoundSchema>
export async function createRoundWithInvites(data: CreateRoundInput) {
const parsed = CreateRoundSchema.safeParse(data)
if (!parsed.success) {
return { error: parsed.error.issues[0]?.message ?? 'Invalid data' }
}
const supabase = await createClient()
const adminClient = await createAdminClient()
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const db = supabase as any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const adminDb = adminClient as any
const {
data: { user },
} = await supabase.auth.getUser()
if (!user) return { error: 'Not authenticated' }
// Snapshot handicap and compute course handicap
const { data: profile } = await db
.from('profiles')
.select('handicap_index')
.eq('id', user.id)
.single()
const { data: tee } = await db
.from('tees')
.select('course_rating, slope_rating')
.eq('id', parsed.data.tee_id)
.single()
const { data: course } = await db
.from('courses')
.select('par')
.eq('id', parsed.data.course_id)
.single()
const handicapIndex: number | null = profile?.handicap_index ?? null
const courseHandicap =
handicapIndex !== null && tee && course
? Math.round(handicapIndex * (tee.slope_rating / 113) + (tee.course_rating - course.par))
: null
// Create round
const { data: round, error: roundError } = await db
.from('rounds')
.insert({
course_id: parsed.data.course_id,
tee_id: parsed.data.tee_id,
name: parsed.data.name || null,
date: parsed.data.date,
holes_count: parsed.data.holes_count,
created_by: user.id,
status: 'lobby',
})
.select('id')
.single()
if (roundError) return { error: roundError.message }
// Add creator as admin player
await db.from('round_players').insert({
round_id: round.id,
user_id: user.id,
role: 'admin',
handicap_index: handicapIndex,
course_handicap: courseHandicap,
})
// Send invites
const failedInvites: string[] = []
for (const email of parsed.data.emails) {
const redirectTo = `${process.env.SITE_URL}/auth/confirm?next=${encodeURIComponent(`/rounds/${round.id}/join`)}`
const { error } = await adminDb.auth.admin.inviteUserByEmail(email, { redirectTo })
if (error) {
failedInvites.push(email)
} else {
await db.from('round_invites').insert({
round_id: round.id,
email,
invited_by: user.id,
})
}
}
return {
roundId: round.id as string,
failedInvites,
}
}
export async function startRound(roundId: string) {
const parsed = z.string().uuid().safeParse(roundId)
if (!parsed.success) return { error: 'Invalid round ID' }
const supabase = await createClient()
const {
data: { user },
} = await supabase.auth.getUser()
if (!user) return { error: 'Not authenticated' }
const { error } = await (supabase as any)
.from('rounds')
.update({ status: 'active' })
.eq('id', parsed.data)
.eq('created_by', user.id)
.eq('status', 'lobby')
if (error) return { error: error.message }
revalidatePath(`/rounds/${roundId}`)
return { success: true }
}
+45
View File
@@ -0,0 +1,45 @@
'use server'
import { z } from 'zod'
import { createClient } from '@/lib/supabase/server'
const UpsertScoreSchema = z.object({
roundId: z.string().uuid(),
holeId: z.string().uuid(),
playerId: z.string().uuid(),
strokes: z.number().int().min(1).max(20),
})
export async function upsertScore(
roundId: string,
holeId: string,
playerId: string,
strokes: number,
) {
const parsed = UpsertScoreSchema.safeParse({ roundId, holeId, playerId, strokes })
if (!parsed.success) return { error: parsed.error.issues[0]?.message ?? 'Invalid data' }
const supabase = await createClient()
const {
data: { user },
} = await supabase.auth.getUser()
if (!user) return { error: 'Not authenticated' }
// Verify round is active and user is a member (RLS also enforces this)
const { error } = await (supabase as any)
.from('scores')
.upsert(
{
round_id: parsed.data.roundId,
hole_id: parsed.data.holeId,
player_id: parsed.data.playerId,
strokes: parsed.data.strokes,
updated_by: user.id,
updated_at: new Date().toISOString(),
},
{ onConflict: 'round_id,hole_id,player_id' },
)
if (error) return { error: error.message }
return { success: true }
}