Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions app/actions/spin-wheel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@
import { createClient } from '@/lib/supabase/server'
import { revalidatePath } from 'next/cache'

interface UserCoupon {
title: string;
current_usage: number | null;
usage_limit: number | null;
}

interface WheelPrize {
title: string;
chance: number | string;
code_prefix: string;
description: string | null;
gradient: string | null;
}

export async function spinWheel() {
const supabase = await createClient()
if (!supabase) return { error: 'Usługa tymczasowo niedostępna.' }
Expand Down Expand Up @@ -47,18 +61,18 @@ export async function spinWheel() {
.eq('user_id', user.id)
.eq('is_active', true)

const unspentCoupons = userCurrentCoupons?.filter((c: any) => (c.current_usage || 0) < (c.usage_limit || 1)) || [];
const usedTitles = unspentCoupons.map((c: any) => c.title);
const unspentCoupons = userCurrentCoupons?.filter((c: UserCoupon) => (c.current_usage || 0) < (c.usage_limit || 1)) || [];
const usedTitles = unspentCoupons.map((c: UserCoupon) => c.title);

// Filtrujemy nagrody
const availablePrizes = prizes.filter((p: any) => !usedTitles.includes(p.title));
const availablePrizes = prizes.filter((p: WheelPrize) => !usedTitles.includes(p.title));

if (availablePrizes.length === 0) {
return { error: 'Posiadasz już wszystkie rabaty! Zużyj przynajmniej jeden z nich pod kasą, by zwolnić miejsce.' }
}

// 4. ALGORYTM LOSOWANIA (Ważony) z puli tylko dostępnych nagród
const totalWeight = availablePrizes.reduce((sum: number, p: any) => sum + Number(p.chance), 0)
const totalWeight = availablePrizes.reduce((sum: number, p: WheelPrize) => sum + Number(p.chance), 0)
let randomNum = Math.random() * totalWeight
let winningPrize = availablePrizes[0]

Expand Down
42 changes: 21 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"@types/three": "^0.182.0",
"@types/web-push": "^3.6.4",
"esbuild": "^0.27.4",
"eslint": "^9",
"eslint": "^9.39.4",
"eslint-config-next": "16.1.6",
"serwist": "^9.5.7",
"shadcn": "^3.8.4",
Expand Down