diff --git a/apps/web/app/api/auth/callback/route.ts b/apps/web/app/api/auth/callback/route.ts index e81425d..f6c8591 100644 --- a/apps/web/app/api/auth/callback/route.ts +++ b/apps/web/app/api/auth/callback/route.ts @@ -4,7 +4,7 @@ import { redirect } from 'next/navigation'; import { createClient } from '@/lib/supabase/server'; import { Resend } from 'resend'; -const CREDITS_PER_REFERRAL = 10; +const CREDITS_PER_REFERRAL = 250; async function sendWelcomeEmail( full_name: string, diff --git a/apps/web/app/api/track-referral/route.ts b/apps/web/app/api/track-referral/route.ts index d619c60..f631e3c 100644 --- a/apps/web/app/api/track-referral/route.ts +++ b/apps/web/app/api/track-referral/route.ts @@ -81,7 +81,7 @@ export async function POST(request: Request) { ...(referredUserId && { referred_user_id: referredUserId, completed_at: new Date().toISOString(), - credits_awarded: 10 + credits_awarded: 250 }), }; diff --git a/apps/web/app/layout.tsx b/apps/web/app/layout.tsx index bf84046..a84ffed 100644 --- a/apps/web/app/layout.tsx +++ b/apps/web/app/layout.tsx @@ -44,7 +44,6 @@ export default function RootLayout({ - ) diff --git a/apps/web/app/signup/page.tsx b/apps/web/app/signup/page.tsx index d611e6f..6fa02f1 100644 --- a/apps/web/app/signup/page.tsx +++ b/apps/web/app/signup/page.tsx @@ -267,7 +267,7 @@ function SignupForm() { {showReferralBanner && referralCode && (

- 🎉 You've been invited by a friend!
Referral code: {referralCode}
You will earn 10 credits. + 🎉 You've been invited by a friend!
Referral code: {referralCode}
You will earn 500 credits.

)} diff --git a/packages/supabase/migrations/20251025135210_update_profiles_default_credits.sql b/packages/supabase/migrations/20251025135210_update_profiles_default_credits.sql new file mode 100644 index 0000000..43bb551 --- /dev/null +++ b/packages/supabase/migrations/20251025135210_update_profiles_default_credits.sql @@ -0,0 +1,2 @@ +ALTER TABLE public.profiles +ALTER COLUMN credits SET DEFAULT 500; diff --git a/packages/supabase/migrations/20251025140944_update_referrals_default_credits.sql b/packages/supabase/migrations/20251025140944_update_referrals_default_credits.sql new file mode 100644 index 0000000..53cf58e --- /dev/null +++ b/packages/supabase/migrations/20251025140944_update_referrals_default_credits.sql @@ -0,0 +1,53 @@ +CREATE OR REPLACE FUNCTION public.award_referral_credits() +RETURNS trigger +LANGUAGE plpgsql +SECURITY DEFINER +AS $$ +DECLARE + referrer_profile_id UUID; + current_credits INTEGER; + current_referral_credits INTEGER; + current_total_referrals INTEGER; + credits_to_award INTEGER := 250; +BEGIN + -- Only process when status changes to 'completed' + IF NEW.status = 'completed' AND (OLD.status IS NULL OR OLD.status != 'completed') THEN + + -- Get the referrer's profile ID + referrer_profile_id := NEW.referrer_id; + + -- Lock and get current values to prevent race conditions + SELECT credits, referral_credits, total_referrals + INTO current_credits, current_referral_credits, current_total_referrals + FROM profiles + WHERE id = referrer_profile_id + FOR UPDATE; + + -- Handle NULL values safely + current_credits := COALESCE(current_credits, 0); + current_referral_credits := COALESCE(current_referral_credits, 0); + current_total_referrals := COALESCE(current_total_referrals, 0); + + -- Update the profile with new credit values + UPDATE profiles + SET + credits = current_credits + credits_to_award, + referral_credits = current_referral_credits + credits_to_award, + total_referrals = current_total_referrals + 1 + WHERE id = referrer_profile_id; + + -- Optional: Log the action for debugging + RAISE NOTICE '✅ Awarded % credits to referrer % (Profile ID: %). New totals -> Credits: %, Referral Credits: %, Total Referrals: %', + credits_to_award, + referrer_profile_id, + referrer_profile_id, + current_credits + credits_to_award, + current_referral_credits + credits_to_award, + current_total_referrals + 1; + END IF; + + RETURN NEW; +END; +$$; + +ALTER FUNCTION public.award_referral_credits() OWNER TO postgres;