28-08-24_19.30 Vyhledavani - Po zamotani Claudem
unknown
plain_text
19 days ago
2.2 kB
2
Indexable
Never
// ts-nocheck import { supabase } from "$lib/supabaseClient"; import { countries as categories } from "$lib/autocomplete/countriesData.js"; export async function load({ url }) { const searchTerm = url.searchParams.get('s') || ''; const sortBy = url.searchParams.get('sortBy') || 'Best'; let lawyerQuery; let isCategory = false; // Check if the search term matches a category isCategory = categories.some(category => category.toLowerCase() === searchTerm.toLowerCase()); if (isCategory) { // Search by category lawyerQuery = supabase .from('lawyer_specialization') .select(` lawyers(id, jmeno, slug, zamereni) `) .eq('category_name', searchTerm); } else { // Search by lawyer name lawyerQuery = supabase .from('lawyers') .select(`id, jmeno, slug, zamereni`) .ilike('bez_titulu', `%${searchTerm}%`); } const { data: lawyersData, error: lawyersError } = await lawyerQuery; if (lawyersError) { console.error("Error fetching lawyers data:", lawyersError); return { lawyers: [], searchTerm }; } // Fetch lawyer stats const lawyerIds = lawyersData.map(lawyer => lawyer.id); const { data: statsData, error: statsError } = await supabase .from('lawyer_stats') .select('lawyer_id, review_count, avg_rating, bayesian_avg') .in('lawyer_id', lawyerIds); if (statsError) { console.error("Error fetching lawyer stats:", statsError); return { lawyers: [], searchTerm }; } // Combine lawyer data with stats const lawyers = lawyersData.map(lawyer => { const stats = statsData.find(stat => stat.lawyer_id === lawyer.id) || { review_count: 0, avg_rating: 0, bayesian_avg: 0 }; return { ...lawyer, num_reviews: stats.review_count, rating: stats.avg_rating, bayesian_avg: stats.bayesian_avg }; }); // Sort the results lawyers.sort((a, b) => { if (sortBy === 'Best') { return b.bayesian_avg - a.bayesian_avg; } else { return b.num_reviews - a.num_reviews; } }); return { lawyers, searchTerm }; }
Leave a Comment