AI Resource Pro / website / scripts / find-valid-tool-categories.ts
| 1 | import { createClient } from '@supabase/supabase-js'; |
| 2 | import * as dotenv from 'dotenv'; |
| 3 | import * as path from 'path'; |
| 4 | import * as fs from 'fs'; |
| 5 | |
| 6 | // Load environment variables |
| 7 | const envLocalPath = path.resolve(process.cwd(), '../.env.local'); |
| 8 | if (fs.existsSync(envLocalPath)) { |
| 9 | console.log(`Loading environment from: ${envLocalPath}`); |
| 10 | dotenv.config({ path: envLocalPath }); |
| 11 | } else { |
| 12 | console.log(`No .env.local file found at: ${envLocalPath}`); |
| 13 | dotenv.config(); |
| 14 | } |
| 15 | |
| 16 | async function findCategoriesWithTools() { |
| 17 | const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL; |
| 18 | const supabaseKey = process.env.SUPABASE_SERVICE_ROLE_KEY; |
| 19 | |
| 20 | if (!supabaseUrl || !supabaseKey) { |
| 21 | console.error('Missing Supabase environment variables'); |
| 22 | process.exit(1); |
| 23 | } |
| 24 | |
| 25 | const supabase = createClient(supabaseUrl, supabaseKey); |
| 26 | |
| 27 | // Get all tools that exist |
| 28 | const { data: tools } = await supabase.from('tools').select('id, name, published'); |
| 29 | if (!tools || tools.length === 0) { |
| 30 | console.log('No tools found'); |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | // Create a Set of valid tool IDs (only published tools) |
| 35 | const allToolIds = new Set(tools.map(t => t.id)); |
| 36 | const publishedToolIds = new Set(tools.filter(t => t.published).map(t => t.id)); |
| 37 | |
| 38 | console.log(`Found ${tools.length} total tools in database (${publishedToolIds.size} published)`); |
| 39 | |
| 40 | // Get all junction entries |
| 41 | const { data: junctions } = await supabase.from('tools_categories').select('*'); |
| 42 | if (!junctions) { |
| 43 | console.log('No junction entries found'); |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | // Filter to only valid entries (existing tools and published tools) |
| 48 | const validJunctions = junctions.filter(j => allToolIds.has(j.tool_id)); |
| 49 | const publishedJunctions = junctions.filter(j => publishedToolIds.has(j.tool_id)); |
| 50 | |
| 51 | console.log(`Found ${validJunctions.length} valid junction entries (with existing tools)`); |
| 52 | console.log(`Found ${publishedJunctions.length} valid published junction entries`); |
| 53 | console.log(`Found ${junctions.length - validJunctions.length} orphaned junction entries`); |
| 54 | |
| 55 | // Group by category for valid published tools |
| 56 | const categoryCounts: Record<number, number> = {}; |
| 57 | for (const j of publishedJunctions) { |
| 58 | categoryCounts[j.category_id] = (categoryCounts[j.category_id] || 0) + 1; |
| 59 | } |
| 60 | |
| 61 | // Get category names |
| 62 | console.log('\nCategories with published tools:'); |
| 63 | const categoryIds = Object.keys(categoryCounts).map(Number).sort((a, b) => a - b); |
| 64 | for (const categoryId of categoryIds) { |
| 65 | const { data } = await supabase |
| 66 | .from('categories') |
| 67 | .select('name') |
| 68 | .eq('id', categoryId) |
| 69 | .single(); |
| 70 | |
| 71 | const name = data ? data.name : 'Unknown'; |
| 72 | console.log(`Category ${categoryId} (${name}): ${categoryCounts[categoryId]} published tools`); |
| 73 | } |
| 74 | |
| 75 | // Check specific categories mentioned by the user |
| 76 | const specificCategories = [61, 140, 142]; |
| 77 | |
| 78 | for (const catId of specificCategories) { |
| 79 | console.log(`\nTools for category ${catId}:`); |
| 80 | |
| 81 | // Get category name |
| 82 | const { data: catData } = await supabase |
| 83 | .from('categories') |
| 84 | .select('name') |
| 85 | .eq('id', catId) |
| 86 | .single(); |
| 87 | |
| 88 | const catName = catData ? catData.name : 'Unknown'; |
| 89 | console.log(`Category ${catId} (${catName}):`); |
| 90 | |
| 91 | // Get all junction entries for this category |
| 92 | const catJunctions = junctions.filter(j => j.category_id === catId); |
| 93 | console.log(`Total junction entries: ${catJunctions.length}`); |
| 94 | |
| 95 | // Filter for existing tools |
| 96 | const existingToolJunctions = catJunctions.filter(j => allToolIds.has(j.tool_id)); |
| 97 | console.log(`Entries with existing tools: ${existingToolJunctions.length}`); |
| 98 | |
| 99 | // Filter for published tools |
| 100 | const publishedToolJunctions = catJunctions.filter(j => publishedToolIds.has(j.tool_id)); |
| 101 | console.log(`Entries with published tools: ${publishedToolJunctions.length}`); |
| 102 | |
| 103 | // Show details of published tools |
| 104 | if (publishedToolJunctions.length > 0) { |
| 105 | console.log('Published tools:'); |
| 106 | for (const junction of publishedToolJunctions) { |
| 107 | const tool = tools.find(t => t.id === junction.tool_id); |
| 108 | if (tool) { |
| 109 | console.log(`- ${tool.name} (ID: ${tool.id})`); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // Show details of unpublished but existing tools |
| 115 | const unpublishedToolJunctions = existingToolJunctions.filter(j => !publishedToolIds.has(j.tool_id)); |
| 116 | if (unpublishedToolJunctions.length > 0) { |
| 117 | console.log('Unpublished tools:'); |
| 118 | for (const junction of unpublishedToolJunctions) { |
| 119 | const tool = tools.find(t => t.id === junction.tool_id); |
| 120 | if (tool) { |
| 121 | console.log(`- ${tool.name} (ID: ${tool.id}, Published: ${tool.published})`); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | findCategoriesWithTools().catch(console.error); |