Initial commit v1.1

This commit is contained in:
Rolf
2026-03-18 13:34:41 +01:00
parent cb04bf2ab8
commit b982584244
38 changed files with 3196 additions and 1103 deletions
+41 -25
View File
@@ -1,13 +1,17 @@
'use server'
import { z } from 'zod'
import { createClient } from '@/lib/supabase/server'
import { db } from '@/lib/db'
import { scores, roundPlayers, rounds } from '@/lib/db/schema'
import { eq, and } from 'drizzle-orm'
import { auth } from '@/lib/auth'
import { headers } from 'next/headers'
const UpsertScoreSchema = z.object({
roundId: z.string().uuid(),
holeId: z.string().uuid(),
playerId: z.string().uuid(),
strokes: z.number().int().min(1).max(20),
roundId: z.string().uuid(),
holeId: z.string().uuid(),
playerId: z.string(),
strokes: z.number().int().min(1).max(20),
})
export async function upsertScore(
@@ -19,27 +23,39 @@ export async function upsertScore(
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' }
const session = await auth.api.getSession({ headers: await headers() })
if (!session) 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' },
)
// Caller must be a member of the round
const [membership] = await db
.select()
.from(roundPlayers)
.where(and(eq(roundPlayers.roundId, roundId), eq(roundPlayers.userId, session.user.id)))
.limit(1)
if (!membership) return { error: 'Not a member of this round' }
// Round must be active
const [round] = await db
.select({ status: rounds.status })
.from(rounds)
.where(eq(rounds.id, roundId))
.limit(1)
if (round?.status !== 'active') return { error: 'Round is not active' }
await db
.insert(scores)
.values({
roundId,
holeId,
playerId,
strokes,
updatedBy: session.user.id,
updatedAt: new Date(),
})
.onConflictDoUpdate({
target: [scores.roundId, scores.holeId, scores.playerId],
set: { strokes, updatedBy: session.user.id, updatedAt: new Date() },
})
if (error) return { error: error.message }
return { success: true }
}