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
2 changes: 1 addition & 1 deletion apps/web/app/api/auth/callback/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/api/track-referral/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}),
};

Expand Down
1 change: 0 additions & 1 deletion apps/web/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export default function RootLayout({
</ThemeProvider>
<SpeedInsights />
<Analytics />

</body>
</html>
)
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/signup/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ function SignupForm() {
{showReferralBanner && referralCode && (
<div className="px-6 py-3 bg-purple-50 dark:bg-purple-900/20 border border-purple-200 dark:border-purple-700 rounded-lg">
<p className="text-sm text-center text-purple-700 dark:text-purple-300">
πŸŽ‰ You've been invited by a friend! <br /> Referral code: <span className="font-mono font-bold">{referralCode}</span> <br /> You will earn 10 credits.
πŸŽ‰ You've been invited by a friend! <br /> Referral code: <span className="font-mono font-bold">{referralCode}</span> <br /> You will earn 500 credits.
</p>
</div>
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE public.profiles
ALTER COLUMN credits SET DEFAULT 500;
Original file line number Diff line number Diff line change
@@ -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;
Loading