Branchwell

AI Resource Pro / website / scripts / debug-tools-api.ts

1import { createClient } from '@supabase/supabase-js';
2import * as dotenv from 'dotenv';
3import path from 'path';
4import fs from 'fs';
5
6// Load environment variables from .env.local
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(); // Try loading from default .env
14}
15
16async function main() {
17 const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
18 const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY;
19
20 if (!supabaseUrl || !supabaseServiceKey) {
21 console.error('Missing Supabase environment variables. Please set NEXT_PUBLIC_SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY.');
22 return;
23 }
24
25 const supabase = createClient(supabaseUrl, supabaseServiceKey);
26
27 console.log('Starting tools API debug...');
28 try {
29 console.log('Fetching tools with simplified query');
30
31 // First get all tools with a simple query - this is the same query used in the API route
32 const { data: tools, error: toolsError } = await supabase
33 .from('tools')
34 .select('id, name, description, url, image_url, category_id, featured, created_at, updated_at, slug, rating, review_count')
35 .order('name');
36
37 if (toolsError) {
38 console.error('Error fetching tools:', toolsError.message);
39 return;
40 }
41
42 console.log(`Retrieved ${tools?.length || 0} tools successfully`);
43
44 if (tools && tools.length > 0) {
45 console.log('First tool:', tools[0]);
46 }
47
48 // Then get all categories separately
49 const { data: categories, error: categoriesError } = await supabase
50 .from('categories')
51 .select('id, name, icon, slug');
52
53 if (categoriesError) {
54 console.error('Error fetching categories:', categoriesError.message);
55 return;
56 }
57
58 console.log(`Retrieved ${categories?.length || 0} categories`);
59
60 if (categories && categories.length > 0) {
61 console.log('First category:', categories[0]);
62 }
63
64 // Manually join the data
65 const toolsWithCategories = tools.map(tool => {
66 const category = categories?.find(cat => cat.id === tool.category_id) || {
67 id: 0,
68 name: 'Uncategorized',
69 icon: 'question-mark',
70 slug: 'uncategorized'
71 };
72
73 return {
74 id: tool.id,
75 name: tool.name,
76 slug: tool.slug,
77 description: tool.description,
78 url: tool.url,
79 image_url: tool.image_url,
80 rating: tool.rating,
81 reviewCount: tool.review_count,
82 featured: tool.featured,
83 created_at: tool.created_at,
84 updated_at: tool.updated_at,
85 category: category
86 };
87 });
88
89 console.log(`Processed ${toolsWithCategories.length} tools with categories`);
90
91 if (toolsWithCategories.length > 0) {
92 console.log('First tool with category:', toolsWithCategories[0]);
93 }
94 } catch (error) {
95 console.error('Exception in tools API debug:', error instanceof Error ? error.message : String(error));
96 console.error('Stack trace:', error instanceof Error ? error.stack : 'No stack trace available');
97 }
98}
99
100main().catch(console.error);