import { createClient } from '@supabase/supabase-js'; import * as dotenv from 'dotenv'; import * as path from 'path'; import * as fs from 'fs'; // Load environment variables const envLocalPath = path.resolve(process.cwd(), '../.env.local'); if (fs.existsSync(envLocalPath)) { console.log(`Loading environment from: ${envLocalPath}`); dotenv.config({ path: envLocalPath }); } else { console.log(`No .env.local file found at: ${envLocalPath}`); dotenv.config(); } async function findCategoriesWithTools() { const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL; const supabaseKey = process.env.SUPABASE_SERVICE_ROLE_KEY; if (!supabaseUrl || !supabaseKey) { console.error('Missing Supabase environment variables'); process.exit(1); } const supabase = createClient(supabaseUrl, supabaseKey); // Get all tools that exist const { data: tools } = await supabase.from('tools').select('id, name, published'); if (!tools || tools.length === 0) { console.log('No tools found'); return; } // Create a Set of valid tool IDs (only published tools) const allToolIds = new Set(tools.map(t => t.id)); const publishedToolIds = new Set(tools.filter(t => t.published).map(t => t.id)); console.log(`Found ${tools.length} total tools in database (${publishedToolIds.size} published)`); // Get all junction entries const { data: junctions } = await supabase.from('tools_categories').select('*'); if (!junctions) { console.log('No junction entries found'); return; } // Filter to only valid entries (existing tools and published tools) const validJunctions = junctions.filter(j => allToolIds.has(j.tool_id)); const publishedJunctions = junctions.filter(j => publishedToolIds.has(j.tool_id)); console.log(`Found ${validJunctions.length} valid junction entries (with existing tools)`); console.log(`Found ${publishedJunctions.length} valid published junction entries`); console.log(`Found ${junctions.length - validJunctions.length} orphaned junction entries`); // Group by category for valid published tools const categoryCounts: Record = {}; for (const j of publishedJunctions) { categoryCounts[j.category_id] = (categoryCounts[j.category_id] || 0) + 1; } // Get category names console.log('\nCategories with published tools:'); const categoryIds = Object.keys(categoryCounts).map(Number).sort((a, b) => a - b); for (const categoryId of categoryIds) { const { data } = await supabase .from('categories') .select('name') .eq('id', categoryId) .single(); const name = data ? data.name : 'Unknown'; console.log(`Category ${categoryId} (${name}): ${categoryCounts[categoryId]} published tools`); } // Check specific categories mentioned by the user const specificCategories = [61, 140, 142]; for (const catId of specificCategories) { console.log(`\nTools for category ${catId}:`); // Get category name const { data: catData } = await supabase .from('categories') .select('name') .eq('id', catId) .single(); const catName = catData ? catData.name : 'Unknown'; console.log(`Category ${catId} (${catName}):`); // Get all junction entries for this category const catJunctions = junctions.filter(j => j.category_id === catId); console.log(`Total junction entries: ${catJunctions.length}`); // Filter for existing tools const existingToolJunctions = catJunctions.filter(j => allToolIds.has(j.tool_id)); console.log(`Entries with existing tools: ${existingToolJunctions.length}`); // Filter for published tools const publishedToolJunctions = catJunctions.filter(j => publishedToolIds.has(j.tool_id)); console.log(`Entries with published tools: ${publishedToolJunctions.length}`); // Show details of published tools if (publishedToolJunctions.length > 0) { console.log('Published tools:'); for (const junction of publishedToolJunctions) { const tool = tools.find(t => t.id === junction.tool_id); if (tool) { console.log(`- ${tool.name} (ID: ${tool.id})`); } } } // Show details of unpublished but existing tools const unpublishedToolJunctions = existingToolJunctions.filter(j => !publishedToolIds.has(j.tool_id)); if (unpublishedToolJunctions.length > 0) { console.log('Unpublished tools:'); for (const junction of unpublishedToolJunctions) { const tool = tools.find(t => t.id === junction.tool_id); if (tool) { console.log(`- ${tool.name} (ID: ${tool.id}, Published: ${tool.published})`); } } } } } findCategoriesWithTools().catch(console.error);