import { NextRequest, NextResponse } from 'next/server'; import { executeQuery } from '@/lib/clickhouse'; import type { ApiResponse } from '@/lib/types'; export async function GET(request: NextRequest) { try { // Get the url from query parameters const searchParams = request.nextUrl.searchParams; const shortUrl = searchParams.get('shortUrl'); if (!shortUrl) { return NextResponse.json({ success: false, error: 'shortUrl parameter is required' }, { status: 400 }); } console.log('Fetching shortlink by exact shortUrl:', shortUrl); // Query to fetch a single shortlink by shortUrl in attributes const query = ` SELECT id, external_id, type, slug, original_url, title, description, attributes, schema_version, creator_id, creator_email, creator_name, created_at, updated_at, deleted_at, projects, teams, tags, qr_codes AS qr_codes, channels, favorites, expires_at, click_count, unique_visitors, domain FROM shorturl_analytics.shorturl WHERE JSONHas(attributes, 'shortUrl') AND JSONExtractString(attributes, 'shortUrl') = '${shortUrl}' AND deleted_at IS NULL LIMIT 1 `; console.log('Executing query:', query); // Execute the query const result = await executeQuery(query); // If no shortlink found with the specified URL if (!Array.isArray(result) || result.length === 0) { return NextResponse.json({ success: false, error: 'Shortlink not found' }, { status: 404 }); } // Process the shortlink data const shortlink = result[0] as Record; // Extract shortUrl from attributes let shortUrlValue = ''; try { if (shortlink.attributes && typeof shortlink.attributes === 'string') { const attributes = JSON.parse(shortlink.attributes) as { shortUrl?: string }; shortUrlValue = attributes.shortUrl || ''; } } catch (e) { console.error('Error parsing shortlink attributes:', e); } // Process teams let teams: any[] = []; try { if (shortlink.teams && typeof shortlink.teams === 'string') { teams = JSON.parse(shortlink.teams); } } catch (e) { console.error('Error parsing teams:', e); } // Process tags let tags: any[] = []; try { if (shortlink.tags && typeof shortlink.tags === 'string') { tags = JSON.parse(shortlink.tags); } } catch (e) { console.error('Error parsing tags:', e); } // Process projects let projects: any[] = []; try { if (shortlink.projects && typeof shortlink.projects === 'string') { projects = JSON.parse(shortlink.projects); } } catch (e) { console.error('Error parsing projects:', e); } // Format the data to match what our store expects const formattedShortlink = { id: shortlink.id || '', externalId: shortlink.external_id || '', slug: shortlink.slug || '', originalUrl: shortlink.original_url || '', title: shortlink.title || '', shortUrl: shortUrlValue, teams: teams, projects: projects, tags: tags.map((tag: any) => tag.tag_name || ''), createdAt: shortlink.created_at, domain: shortlink.domain || (shortUrlValue ? new URL(shortUrlValue).hostname : '') }; console.log('Formatted shortlink with externalId:', shortlink.external_id); const response: ApiResponse = { success: true, data: formattedShortlink }; return NextResponse.json(response); } catch (error) { console.error('Error fetching shortlink by exact URL:', error); const response: ApiResponse = { success: false, error: error instanceof Error ? error.message : 'Unknown error occurred' }; return NextResponse.json(response, { status: 500 }); } }