143 lines
3.9 KiB
TypeScript
143 lines
3.9 KiB
TypeScript
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 url = searchParams.get('url');
|
|
|
|
if (!url) {
|
|
return NextResponse.json({
|
|
success: false,
|
|
error: 'URL parameter is required'
|
|
}, { status: 400 });
|
|
}
|
|
|
|
console.log('Fetching shortlink by URL:', url);
|
|
|
|
// 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') = '${url}'
|
|
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];
|
|
|
|
// Extract shortUrl from attributes
|
|
let shortUrl = '';
|
|
try {
|
|
if (shortlink.attributes && typeof shortlink.attributes === 'string') {
|
|
const attributes = JSON.parse(shortlink.attributes);
|
|
shortUrl = attributes.shortUrl || '';
|
|
}
|
|
} catch (e) {
|
|
console.error('Error parsing shortlink attributes:', e);
|
|
}
|
|
|
|
// Process teams
|
|
let teams = [];
|
|
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 = [];
|
|
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 = [];
|
|
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: shortUrl,
|
|
teams: teams,
|
|
projects: projects,
|
|
tags: tags.map((tag) => tag.tag_name || ''),
|
|
createdAt: shortlink.created_at,
|
|
domain: shortlink.domain || (shortUrl ? new URL(shortUrl).hostname : '')
|
|
};
|
|
|
|
console.log('Shortlink data formatted with externalId:', shortlink.external_id, 'Final object:', formattedShortlink);
|
|
|
|
const response: ApiResponse<typeof formattedShortlink> = {
|
|
success: true,
|
|
data: formattedShortlink
|
|
};
|
|
|
|
return NextResponse.json(response);
|
|
} catch (error) {
|
|
console.error('Error fetching shortlink by URL:', error);
|
|
const response: ApiResponse<null> = {
|
|
success: false,
|
|
error: error instanceof Error ? error.message : 'Unknown error occurred'
|
|
};
|
|
return NextResponse.json(response, { status: 500 });
|
|
}
|
|
}
|