Branchwell

AI Resource Pro / website / scripts / check-missing-tools.ts

1import { createClient } from '@supabase/supabase-js';
2import * as dotenv from 'dotenv';
3import * as path from 'path';
4import * as fs from 'fs';
5
6// Load environment variables
7const envLocalPath = path.resolve(process.cwd(), '../.env.local');
8if (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
16async function checkTools() {
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 // Try to find the tools by ID explicitly
28 const toolIds = [614, 615, 616, 621, 632, 637];
29
30 console.log('Checking for tools referenced in the junction table for category 61:');
31 for (const id of toolIds) {
32 const { data, error } = await supabase
33 .from('tools')
34 .select('id, name, published')
35 .eq('id', id);
36
37 if (error || !data || data.length === 0) {
38 console.log(`Tool ID ${id}: Not found or error: ${error?.message || 'No data'}`);
39 } else {
40 console.log(`Tool ID ${id}: Found - ${data[0].name} (Published: ${data[0].published})`);
41 }
42 }
43
44 // Get count of all tools
45 const { count, error: countError } = await supabase
46 .from('tools')
47 .select('*', { count: 'exact', head: true });
48
49 if (countError) {
50 console.error('Error counting tools:', countError);
51 } else {
52 console.log(`\nTotal tools in database: ${count}`);
53 }
54
55 // Get min and max tool IDs to see the range
56 const { data: minMaxData, error: minMaxError } = await supabase
57 .from('tools')
58 .select('id')
59 .order('id', { ascending: true });
60
61 if (minMaxError || !minMaxData || minMaxData.length === 0) {
62 console.error('Error getting min/max tool IDs');
63 } else {
64 const minId = minMaxData[0].id;
65 const maxId = minMaxData[minMaxData.length - 1].id;
66 console.log(`Tool ID range: ${minId} to ${maxId}`);
67 }
68
69 // Check for orphaned entries in the junction table
70 console.log('\nLooking for orphaned entries in tools_categories:');
71 const { data: allJunctions, error: junctionError } = await supabase
72 .from('tools_categories')
73 .select('*')
74 .limit(100);
75
76 if (junctionError) {
77 console.error('Error fetching junction entries:', junctionError);
78 } else if (allJunctions) {
79 let orphanedCount = 0;
80
81 for (const junction of allJunctions.slice(0, 20)) { // Check just the first 20 to avoid too many queries
82 const { data: toolData } = await supabase
83 .from('tools')
84 .select('id')
85 .eq('id', junction.tool_id)
86 .single();
87
88 if (!toolData) {
89 orphanedCount++;
90 console.log(`Orphaned entry: tool_id=${junction.tool_id}, category_id=${junction.category_id}`);
91 }
92 }
93
94 console.log(`Found ${orphanedCount} orphaned entries out of ${Math.min(20, allJunctions.length)} checked`);
95 }
96}
97
98checkTools().catch(console.error);