|
| 1 | +import { db } from '@sim/db' |
| 2 | +import { templateCreators, user } from '@sim/db/schema' |
| 3 | +import { eq } from 'drizzle-orm' |
| 4 | +import { type NextRequest, NextResponse } from 'next/server' |
| 5 | +import { getSession } from '@/lib/auth' |
| 6 | +import { createLogger } from '@/lib/logs/console/logger' |
| 7 | +import { generateRequestId } from '@/lib/utils' |
| 8 | + |
| 9 | +const logger = createLogger('CreatorVerificationAPI') |
| 10 | + |
| 11 | +export const revalidate = 0 |
| 12 | + |
| 13 | +// POST /api/creators/[id]/verify - Verify a creator (super users only) |
| 14 | +export async function POST(request: NextRequest, { params }: { params: Promise<{ id: string }> }) { |
| 15 | + const requestId = generateRequestId() |
| 16 | + const { id } = await params |
| 17 | + |
| 18 | + try { |
| 19 | + const session = await getSession() |
| 20 | + if (!session?.user?.id) { |
| 21 | + logger.warn(`[${requestId}] Unauthorized verification attempt for creator: ${id}`) |
| 22 | + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) |
| 23 | + } |
| 24 | + |
| 25 | + // Check if user is a super user |
| 26 | + const currentUser = await db.select().from(user).where(eq(user.id, session.user.id)).limit(1) |
| 27 | + |
| 28 | + if (!currentUser[0]?.isSuperUser) { |
| 29 | + logger.warn(`[${requestId}] Non-super user attempted to verify creator: ${id}`) |
| 30 | + return NextResponse.json({ error: 'Only super users can verify creators' }, { status: 403 }) |
| 31 | + } |
| 32 | + |
| 33 | + // Check if creator exists |
| 34 | + const existingCreator = await db |
| 35 | + .select() |
| 36 | + .from(templateCreators) |
| 37 | + .where(eq(templateCreators.id, id)) |
| 38 | + .limit(1) |
| 39 | + |
| 40 | + if (existingCreator.length === 0) { |
| 41 | + logger.warn(`[${requestId}] Creator not found for verification: ${id}`) |
| 42 | + return NextResponse.json({ error: 'Creator not found' }, { status: 404 }) |
| 43 | + } |
| 44 | + |
| 45 | + // Update creator verified status to true |
| 46 | + await db |
| 47 | + .update(templateCreators) |
| 48 | + .set({ verified: true, updatedAt: new Date() }) |
| 49 | + .where(eq(templateCreators.id, id)) |
| 50 | + |
| 51 | + logger.info(`[${requestId}] Creator verified: ${id} by super user: ${session.user.id}`) |
| 52 | + |
| 53 | + return NextResponse.json({ |
| 54 | + message: 'Creator verified successfully', |
| 55 | + creatorId: id, |
| 56 | + }) |
| 57 | + } catch (error) { |
| 58 | + logger.error(`[${requestId}] Error verifying creator ${id}`, error) |
| 59 | + return NextResponse.json({ error: 'Internal server error' }, { status: 500 }) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// DELETE /api/creators/[id]/verify - Unverify a creator (super users only) |
| 64 | +export async function DELETE( |
| 65 | + request: NextRequest, |
| 66 | + { params }: { params: Promise<{ id: string }> } |
| 67 | +) { |
| 68 | + const requestId = generateRequestId() |
| 69 | + const { id } = await params |
| 70 | + |
| 71 | + try { |
| 72 | + const session = await getSession() |
| 73 | + if (!session?.user?.id) { |
| 74 | + logger.warn(`[${requestId}] Unauthorized unverification attempt for creator: ${id}`) |
| 75 | + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) |
| 76 | + } |
| 77 | + |
| 78 | + // Check if user is a super user |
| 79 | + const currentUser = await db.select().from(user).where(eq(user.id, session.user.id)).limit(1) |
| 80 | + |
| 81 | + if (!currentUser[0]?.isSuperUser) { |
| 82 | + logger.warn(`[${requestId}] Non-super user attempted to unverify creator: ${id}`) |
| 83 | + return NextResponse.json({ error: 'Only super users can unverify creators' }, { status: 403 }) |
| 84 | + } |
| 85 | + |
| 86 | + // Check if creator exists |
| 87 | + const existingCreator = await db |
| 88 | + .select() |
| 89 | + .from(templateCreators) |
| 90 | + .where(eq(templateCreators.id, id)) |
| 91 | + .limit(1) |
| 92 | + |
| 93 | + if (existingCreator.length === 0) { |
| 94 | + logger.warn(`[${requestId}] Creator not found for unverification: ${id}`) |
| 95 | + return NextResponse.json({ error: 'Creator not found' }, { status: 404 }) |
| 96 | + } |
| 97 | + |
| 98 | + // Update creator verified status to false |
| 99 | + await db |
| 100 | + .update(templateCreators) |
| 101 | + .set({ verified: false, updatedAt: new Date() }) |
| 102 | + .where(eq(templateCreators.id, id)) |
| 103 | + |
| 104 | + logger.info(`[${requestId}] Creator unverified: ${id} by super user: ${session.user.id}`) |
| 105 | + |
| 106 | + return NextResponse.json({ |
| 107 | + message: 'Creator unverified successfully', |
| 108 | + creatorId: id, |
| 109 | + }) |
| 110 | + } catch (error) { |
| 111 | + logger.error(`[${requestId}] Error unverifying creator ${id}`, error) |
| 112 | + return NextResponse.json({ error: 'Internal server error' }, { status: 500 }) |
| 113 | + } |
| 114 | +} |
0 commit comments