Spaces:
Configuration error
Configuration error
| import { NextResponse } from 'next/server' | |
| import prisma from '@/lib/prisma' | |
| import { calculateBadges } from '@/lib/badges' | |
| export const dynamic = 'force-dynamic' | |
| /** | |
| * Update badges for a single prompt or all prompts | |
| * Should be run via cron job nightly | |
| */ | |
| export async function POST(req: Request) { | |
| try { | |
| const { promptId } = await req.json() | |
| if (promptId) { | |
| // Update single prompt | |
| const prompt = await prisma.prompt.findUnique({ | |
| where: { id: promptId }, | |
| }) | |
| if (!prompt) { | |
| return NextResponse.json({ error: 'Prompt not found' }, { status: 404 }) | |
| } | |
| // Get all prompts for comparison | |
| const allPrompts = await prisma.prompt.findMany({ | |
| where: { visibility: 'public' }, | |
| select: { | |
| id: true, | |
| totalRuns: true, | |
| starsCount: true, | |
| remixesCount: true, | |
| createdAt: true, | |
| framework: true, | |
| description: true, | |
| schema: true, | |
| }, | |
| }) | |
| const badges = calculateBadges(prompt as any, allPrompts as any) | |
| await prisma.prompt.update({ | |
| where: { id: promptId }, | |
| data: { badges }, | |
| }) | |
| return NextResponse.json({ promptId, badges }) | |
| } else { | |
| // Update all prompts | |
| const prompts = await prisma.prompt.findMany({ | |
| where: { visibility: 'public' }, | |
| }) | |
| let updated = 0 | |
| for (const prompt of prompts) { | |
| const badges = calculateBadges(prompt as any, prompts as any) | |
| await prisma.prompt.update({ | |
| where: { id: prompt.id }, | |
| data: { badges }, | |
| }) | |
| updated++ | |
| } | |
| return NextResponse.json({ updated, total: prompts.length }) | |
| } | |
| } catch (error) { | |
| console.error('Update badges error:', error) | |
| return NextResponse.json( | |
| { error: 'Failed to update badges' }, | |
| { status: 500 } | |
| ) | |
| } | |
| } | |