|
| 1 | +import { NextResponse } from 'next/server'; |
| 2 | + |
| 3 | +import createClient from '@/lib/supabase/server'; |
| 4 | +import { ApiResponse } from '@/types/apiRoute'; |
| 5 | +import { REPORT_CATEGORIES, ReportCategory } from '@/types/report'; |
| 6 | +import { getAuthenticatedUser } from '@/utils/getAuthenticatedUser'; |
| 7 | + |
| 8 | +const POSTGRES_UNIQUE_VIOLATION = '23505'; |
| 9 | + |
| 10 | +function isReportCategory(value: unknown): value is ReportCategory { |
| 11 | + return typeof value === 'string' && REPORT_CATEGORIES.includes(value as ReportCategory); |
| 12 | +} |
| 13 | + |
| 14 | +export async function POST(request: Request): Promise<NextResponse<ApiResponse<void>>> { |
| 15 | + try { |
| 16 | + const supabase = await createClient(); |
| 17 | + const userId = await getAuthenticatedUser(supabase); |
| 18 | + |
| 19 | + const { songId, category, suggested_value } = await request.json(); |
| 20 | + |
| 21 | + if (!songId || !category) { |
| 22 | + return NextResponse.json( |
| 23 | + { success: false, error: 'Missing songId or category' }, |
| 24 | + { status: 400 }, |
| 25 | + ); |
| 26 | + } |
| 27 | + |
| 28 | + if (!isReportCategory(category)) { |
| 29 | + return NextResponse.json({ success: false, error: 'Invalid category' }, { status: 400 }); |
| 30 | + } |
| 31 | + |
| 32 | + const isNumberCategory = category === 'num_tj' || category === 'num_ky'; |
| 33 | + |
| 34 | + let normalizedSuggestedValue: string | null; |
| 35 | + if (suggested_value === null) { |
| 36 | + if (!isNumberCategory) { |
| 37 | + return NextResponse.json( |
| 38 | + { success: false, error: 'suggested_value is required for this category' }, |
| 39 | + { status: 400 }, |
| 40 | + ); |
| 41 | + } |
| 42 | + normalizedSuggestedValue = null; |
| 43 | + } else if (typeof suggested_value === 'string') { |
| 44 | + const trimmed = suggested_value.trim(); |
| 45 | + if (!trimmed) { |
| 46 | + return NextResponse.json( |
| 47 | + { success: false, error: 'Missing suggested_value' }, |
| 48 | + { status: 400 }, |
| 49 | + ); |
| 50 | + } |
| 51 | + if (isNumberCategory && !/^\d{1,5}$/.test(trimmed)) { |
| 52 | + return NextResponse.json( |
| 53 | + { success: false, error: 'Invalid number format' }, |
| 54 | + { status: 400 }, |
| 55 | + ); |
| 56 | + } |
| 57 | + normalizedSuggestedValue = trimmed; |
| 58 | + } else { |
| 59 | + return NextResponse.json( |
| 60 | + { success: false, error: 'Invalid suggested_value' }, |
| 61 | + { status: 400 }, |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + const { error: insertError } = await supabase.from('song_reports').insert({ |
| 66 | + user_id: userId, |
| 67 | + song_id: songId, |
| 68 | + category, |
| 69 | + suggested_value: normalizedSuggestedValue, |
| 70 | + }); |
| 71 | + |
| 72 | + if (insertError) { |
| 73 | + if ((insertError as { code?: string }).code === POSTGRES_UNIQUE_VIOLATION) { |
| 74 | + return NextResponse.json({ success: false, error: 'Already reported' }, { status: 409 }); |
| 75 | + } |
| 76 | + throw insertError; |
| 77 | + } |
| 78 | + |
| 79 | + return NextResponse.json({ success: true }); |
| 80 | + } catch (error) { |
| 81 | + if (error instanceof Error && error.cause === 'auth') { |
| 82 | + return NextResponse.json( |
| 83 | + { success: false, error: 'User not authenticated' }, |
| 84 | + { status: 401 }, |
| 85 | + ); |
| 86 | + } |
| 87 | + console.error('Error in report API:', error); |
| 88 | + return NextResponse.json({ success: false, error: 'Failed to post report' }, { status: 500 }); |
| 89 | + } |
| 90 | +} |
0 commit comments