final
This commit is contained in:
39
app/api/referrals/link/route.ts
Normal file
39
app/api/referrals/link/route.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { cookies } from 'next/headers'
|
||||
|
||||
// GET /api/referrals/link - Get referral link for current user
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const cookieStore = cookies()
|
||||
const buyerIdCookie = cookieStore.get('buyer_id')?.value
|
||||
|
||||
if (!buyerIdCookie) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Authentication required' },
|
||||
{ status: 401 }
|
||||
)
|
||||
}
|
||||
|
||||
const buyer_id = parseInt(buyerIdCookie, 10)
|
||||
|
||||
// Get base URL
|
||||
const baseUrl = process.env.NEXT_PUBLIC_BASE_URL ||
|
||||
request.headers.get('origin') ||
|
||||
'http://localhost:3000'
|
||||
|
||||
// Create referral link with buyer_id as referral parameter
|
||||
const referralLink = `${baseUrl}?ref=${buyer_id}`
|
||||
|
||||
return NextResponse.json({
|
||||
referralLink,
|
||||
referralId: buyer_id,
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Error generating referral link:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to generate referral link' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
50
app/api/referrals/status/route.ts
Normal file
50
app/api/referrals/status/route.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { cookies } from 'next/headers'
|
||||
import pool from '@/lib/db'
|
||||
|
||||
// GET /api/referrals/status - Get referral count and unlock status for current user
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const cookieStore = cookies()
|
||||
const buyerIdCookie = cookieStore.get('buyer_id')?.value
|
||||
|
||||
if (!buyerIdCookie) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
referralCount: 0,
|
||||
isUnlocked: false,
|
||||
referralsNeeded: 3,
|
||||
referralsRemaining: 3
|
||||
},
|
||||
{ status: 200 }
|
||||
)
|
||||
}
|
||||
|
||||
const buyer_id = parseInt(buyerIdCookie, 10)
|
||||
|
||||
// Count referrals for this user
|
||||
const [referralRows] = await pool.execute(
|
||||
'SELECT COUNT(*) as count FROM referrals WHERE referrer = ?',
|
||||
[buyer_id]
|
||||
)
|
||||
|
||||
const referralCount = (referralRows as any[])[0]?.count || 0
|
||||
const isUnlocked = referralCount >= 3
|
||||
const referralsNeeded = 3
|
||||
const referralsRemaining = Math.max(0, referralsNeeded - referralCount)
|
||||
|
||||
return NextResponse.json({
|
||||
referralCount,
|
||||
isUnlocked,
|
||||
referralsNeeded,
|
||||
referralsRemaining,
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Error fetching referral status:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch referral status' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user