103 lines
3.2 KiB
TypeScript
103 lines
3.2 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { executeQuery } from '@/lib/clickhouse';
|
|
import { NextRequest } from 'next/server';
|
|
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
// Get pagination and filter parameters from the URL
|
|
const searchParams = request.nextUrl.searchParams;
|
|
const page = parseInt(searchParams.get('page') || '1', 10);
|
|
const pageSize = parseInt(searchParams.get('page_size') || '10', 10);
|
|
const search = searchParams.get('search');
|
|
const team = searchParams.get('team');
|
|
|
|
// Calculate OFFSET
|
|
const offset = (page - 1) * pageSize;
|
|
|
|
// Build WHERE conditions
|
|
const whereConditions = ['deleted_at IS NULL'];
|
|
|
|
if (search) {
|
|
// Expand search to include more fields: slug, shortUrl in attributes, team name, tag name, original_url
|
|
whereConditions.push(`(
|
|
slug ILIKE '%${search}%' OR
|
|
original_url ILIKE '%${search}%' OR
|
|
title ILIKE '%${search}%' OR
|
|
JSONHas(attributes, 'shortUrl') AND JSONExtractString(attributes, 'shortUrl') ILIKE '%${search}%' OR
|
|
arrayExists(x -> JSONExtractString(x, 'team_name') ILIKE '%${search}%', JSONExtractArrayRaw(teams)) OR
|
|
arrayExists(x -> JSONExtractString(x, 'tag_name') ILIKE '%${search}%', JSONExtractArrayRaw(tags))
|
|
)`);
|
|
}
|
|
|
|
if (team) {
|
|
whereConditions.push(`arrayExists(x -> JSONExtractString(x, 'team_id') = '${team}', JSONExtractArrayRaw(teams))`);
|
|
}
|
|
|
|
const whereClause = whereConditions.join(' AND ');
|
|
|
|
// First query to get total count
|
|
const countQuery = `
|
|
SELECT count(*) as total
|
|
FROM shorturl_analytics.shorturl
|
|
WHERE ${whereClause}
|
|
`;
|
|
|
|
const countResult = await executeQuery(countQuery);
|
|
// Handle the result safely by using an explicit type check
|
|
const total = Array.isArray(countResult) && countResult.length > 0 && typeof countResult[0] === 'object' && countResult[0] !== null && 'total' in countResult[0]
|
|
? Number(countResult[0].total)
|
|
: 0;
|
|
const totalPages = Math.ceil(total / pageSize);
|
|
|
|
// Main query with pagination
|
|
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
|
|
FROM shorturl_analytics.shorturl
|
|
WHERE ${whereClause}
|
|
ORDER BY created_at DESC
|
|
LIMIT ${pageSize} OFFSET ${offset}
|
|
`;
|
|
|
|
// Execute the query using the shared client
|
|
const rows = await executeQuery(query);
|
|
|
|
// Return the data with pagination metadata
|
|
return NextResponse.json({
|
|
links: rows,
|
|
total: total,
|
|
total_pages: totalPages,
|
|
page: page,
|
|
page_size: pageSize
|
|
});
|
|
} catch (error) {
|
|
console.error('Error fetching shortlinks from ClickHouse:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch shortlinks' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|