Skip to content
Draft
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
21 changes: 21 additions & 0 deletions app/api/credits/summary/route.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
import { NextRequest, NextResponse } from 'next/server'
import { getOrCreateUserCredits, getCreditUsageSummary } from '@/lib/credits'
import { getCurrentUser } from '@/lib/auth'

export async function GET(request: NextRequest) {
try {
const userId = request.nextUrl.searchParams.get('userId')
const user = await getCurrentUser()

if (!user) {
return NextResponse.json(
{ error: 'Sign in with GitHub to view credits.' },
{ status: 401 }
)
}
if (!user.id) {
return NextResponse.json(
{ error: 'Unable to load your account. Please sign in again.' },
{ status: 401 }
)
}

if (!userId) {
return NextResponse.json(
{ error: 'User ID is required' },
{ status: 400 }
)
}
if (userId !== user.id) {
return NextResponse.json(
{ error: 'You can only view credits for your own account.' },
{ status: 403 }
)
}

// Get credits and usage summary
const [credits, summary] = await Promise.all([
Expand Down
52 changes: 28 additions & 24 deletions app/api/generate-scaffold/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ export async function POST(request: NextRequest) {
)
}

const { appName, description, technologies, existingFiles, missingFiles, userId } = await request.json()
const { appName, description, technologies, existingFiles, missingFiles } = await request.json()
const user = await getCurrentUser()
if (!user) {
return NextResponse.json({ error: 'Sign in with GitHub to generate scaffolds.' }, { status: 401 })
}
if (!user.id) {
return NextResponse.json({ error: 'Unable to load your account. Please sign in again.' }, { status: 401 })
}

let sub = await getSubscriptionByGithubId(user.github_id).catch(() => null)
if (!sub) {
Expand All @@ -40,19 +43,17 @@ export async function POST(request: NextRequest) {
}

// Check credit balance before proceeding
if (userId) {
const currentBalance = await getCreditBalance(userId)
if (currentBalance < CREDITS.SCAFFOLD_COST) {
return NextResponse.json(
{
error: 'Insufficient credits',
required: CREDITS.SCAFFOLD_COST,
available: currentBalance,
message: 'Upgrade to Pro to get unlimited scaffold generation with 5,000 monthly credits.',
},
{ status: 402 }
)
}
const currentBalance = await getCreditBalance(user.id)
if (currentBalance < CREDITS.SCAFFOLD_COST) {
return NextResponse.json(
{
error: 'Insufficient credits',
required: CREDITS.SCAFFOLD_COST,
available: currentBalance,
message: 'Upgrade to Pro to get unlimited scaffold generation with 5,000 monthly credits.',
},
{ status: 402 }
)
}

console.log('[v0] Generating scaffold for app:', appName)
Expand Down Expand Up @@ -140,18 +141,21 @@ Example structure:

// Deduct credits after successful generation
let creditsUsed = 0
if (userId) {
const deductResult = await deductCredits(userId, CREDITS.SCAFFOLD_COST, 'scaffold', {
appName,
technologies,
})
if (deductResult.success) {
creditsUsed = CREDITS.SCAFFOLD_COST
console.log(`[v0] Deducted ${CREDITS.SCAFFOLD_COST} credits from user ${userId}`)
}
const deductResult = await deductCredits(user.id, CREDITS.SCAFFOLD_COST, 'scaffold', {
appName,
technologies,
})

if (!deductResult.success) {
return NextResponse.json(
{ error: deductResult.error ?? 'Failed to deduct scaffold credits' },
{ status: 402 }
)
}

creditsUsed = CREDITS.SCAFFOLD_COST
console.log(`[v0] Deducted ${CREDITS.SCAFFOLD_COST} credits from user ${user.id}`)

return NextResponse.json({
success: true,
scaffold,
Expand Down
Loading