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
14 changes: 7 additions & 7 deletions app/api/analyses/[id]/analyze/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { NextRequest, NextResponse } from 'next/server'
import { generateText } from 'ai'
import { getCurrentUser } from '@/lib/auth'
import { getCreditBalance, deductCredits, CREDITS } from '@/lib/credits'
import { getAnalysisById } from '@/lib/queries'

Expand All @@ -22,18 +23,17 @@ interface AppSuggestion {

export async function POST(request: NextRequest) {
try {
const { analysisId, selectedRepos, userId } = (await request.json()) as {
const { analysisId, selectedRepos } = (await request.json()) as {
analysisId: string
selectedRepos: SelectedRepository[]
userId: string
}

// Check credit balance before proceeding
if (!userId) {
return NextResponse.json({ error: 'User ID required' }, { status: 401 })
const user = await getCurrentUser()
if (!user?.id) {
return NextResponse.json({ error: 'Sign in with GitHub before running an analysis.' }, { status: 401 })
}

const currentBalance = await getCreditBalance(userId)
const currentBalance = await getCreditBalance(user.id)
if (currentBalance < CREDITS.ANALYSIS_COST) {
return NextResponse.json(
{
Expand Down Expand Up @@ -96,7 +96,7 @@ Return as JSON array of app suggestions. Focus on practical, buildable applicati
}

// Deduct credits for successful analysis
const deductResult = await deductCredits(userId, CREDITS.ANALYSIS_COST, 'analysis', {
const deductResult = await deductCredits(user.id, CREDITS.ANALYSIS_COST, 'analysis', {
analysisId,
selectedRepos: selectedRepos.map((r) => r.name),
})
Expand Down
18 changes: 9 additions & 9 deletions app/api/credits/summary/route.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { NextRequest, NextResponse } from 'next/server'
import { NextResponse } from 'next/server'
import { getCurrentUser } from '@/lib/auth'
import { getOrCreateUserCredits, getCreditUsageSummary } from '@/lib/credits'

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

if (!userId) {
const user = await getCurrentUser()
if (!user?.id) {
return NextResponse.json(
{ error: 'User ID is required' },
{ status: 400 }
{ error: 'Sign in with GitHub to view credits.' },
{ status: 401 }
)
}

// Get credits and usage summary
const [credits, summary] = await Promise.all([
getOrCreateUserCredits(userId),
getCreditUsageSummary(userId),
getOrCreateUserCredits(user.id),
getCreditUsageSummary(user.id),
])

return NextResponse.json({
Expand Down
57 changes: 27 additions & 30 deletions app/api/generate-scaffold/route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { NextRequest, NextResponse } from 'next/server'
import Anthropic from '@anthropic-ai/sdk'
import { getAnthropicModel } from '@/lib/anthropic-model'
import { getCreditBalance, deductCredits, CREDITS } from '@/lib/credits'

const client = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
})
import { getCurrentUser } from '@/lib/auth'
import { getCreditBalance, deductCredits, CREDITS } from '@/lib/credits'
import { getSubscriptionByGithubId, upsertSubscription } from '@/lib/queries'

export async function POST(request: NextRequest) {
Expand All @@ -18,9 +14,9 @@ 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) {
if (!user?.id) {
return NextResponse.json({ error: 'Sign in with GitHub to generate scaffolds.' }, { status: 401 })
}

Expand All @@ -40,19 +36,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 +134,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 || 'Insufficient 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
2 changes: 1 addition & 1 deletion components/credits-display.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function CreditsDisplay({ userId }: CreditsDisplayProps) {
const fetchCredits = async () => {
try {
setLoading(true)
const response = await fetch(`/api/credits/summary?userId=${userId}`)
const response = await fetch('/api/credits/summary')
if (!response.ok) throw new Error('Failed to fetch credits')
const data = await response.json()
setCredits(data.credits)
Expand Down
Loading