AI Resource Pro / website / scripts / test-category-140.ts
| 1 | import fetch from 'node-fetch'; |
| 2 | import { createClient } from '@supabase/supabase-js'; |
| 3 | import * as dotenv from 'dotenv'; |
| 4 | import * as path from 'path'; |
| 5 | import * as fs from 'fs'; |
| 6 | |
| 7 | // Load environment variables |
| 8 | const envLocalPath = path.resolve(process.cwd(), '../.env.local'); |
| 9 | if (fs.existsSync(envLocalPath)) { |
| 10 | console.log(`Loading environment from: ${envLocalPath}`); |
| 11 | dotenv.config({ path: envLocalPath }); |
| 12 | } else { |
| 13 | console.log(`No .env.local file found at: ${envLocalPath}`); |
| 14 | dotenv.config(); // Try loading from default .env |
| 15 | } |
| 16 | |
| 17 | async function testCategory140() { |
| 18 | console.log('Testing Category 140 (AI Assistants) counts...'); |
| 19 | |
| 20 | // Initialize Supabase client for direct database verification |
| 21 | const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL; |
| 22 | const supabaseKey = process.env.SUPABASE_SERVICE_ROLE_KEY; |
| 23 | |
| 24 | if (!supabaseUrl || !supabaseKey) { |
| 25 | console.error('Missing Supabase environment variables. Add NEXT_PUBLIC_SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY to .env.local'); |
| 26 | process.exit(1); |
| 27 | } |
| 28 | |
| 29 | const supabase = createClient(supabaseUrl, supabaseKey); |
| 30 | const categoryId = 140; // AI Assistants |
| 31 | |
| 32 | // Adjust URL based on your local development setup |
| 33 | const baseUrl = 'http://localhost:3000'; |
| 34 | |
| 35 | try { |
| 36 | // 1. Check category details directly from the database |
| 37 | console.log('\n1. Checking category details in the database...'); |
| 38 | const { data: categoryData, error: categoryError } = await supabase |
| 39 | .from('categories') |
| 40 | .select('*') |
| 41 | .eq('id', categoryId) |
| 42 | .single(); |
| 43 | |
| 44 | if (categoryError) { |
| 45 | console.error('Error fetching category:', categoryError); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | console.log('Category in database:'); |
| 50 | console.log(categoryData); |
| 51 | |
| 52 | // 2. Check news items directly from the database |
| 53 | console.log('\n2. Checking news items in the database...'); |
| 54 | |
| 55 | // Direct news items |
| 56 | const { data: directNews, error: directNewsError } = await supabase |
| 57 | .from('news') |
| 58 | .select('id, title, published') |
| 59 | .eq('category_id', categoryId) |
| 60 | .eq('published', true); |
| 61 | |
| 62 | if (directNewsError) { |
| 63 | console.error('Error fetching direct news:', directNewsError); |
| 64 | } else { |
| 65 | console.log(`Found ${directNews?.length || 0} news items directly linked to category ${categoryId}:`); |
| 66 | directNews?.forEach(item => { |
| 67 | console.log(`- ${item.title} (ID: ${item.id})`); |
| 68 | }); |
| 69 | } |
| 70 | |
| 71 | // Junction table news items |
| 72 | console.log('\n3. Checking news_categories junction table...'); |
| 73 | const { data: junctionData, error: junctionError } = await supabase |
| 74 | .from('news_categories') |
| 75 | .select('news_id, category_id') |
| 76 | .eq('category_id', categoryId); |
| 77 | |
| 78 | if (junctionError) { |
| 79 | console.error('Error fetching junction data:', junctionError); |
| 80 | } else { |
| 81 | console.log(`Found ${junctionData?.length || 0} entries in news_categories for category ${categoryId}`); |
| 82 | |
| 83 | if (junctionData && junctionData.length > 0) { |
| 84 | // Get details of these news items |
| 85 | const newsIds = junctionData.map(item => item.news_id); |
| 86 | |
| 87 | const { data: junctionNews, error: junctionNewsError } = await supabase |
| 88 | .from('news') |
| 89 | .select('id, title, published') |
| 90 | .in('id', newsIds) |
| 91 | .eq('published', true); |
| 92 | |
| 93 | if (junctionNewsError) { |
| 94 | console.error('Error fetching news items from junction:', junctionNewsError); |
| 95 | } else { |
| 96 | console.log(`Found ${junctionNews?.length || 0} published news items via junction table:`); |
| 97 | junctionNews?.forEach(item => { |
| 98 | console.log(`- ${item.title} (ID: ${item.id})`); |
| 99 | }); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // Now also get details about tools |
| 105 | console.log('\n2a. Checking tools associated with the category...'); |
| 106 | |
| 107 | // Direct tools |
| 108 | const { data: directTools, error: directToolsError } = await supabase |
| 109 | .from('tools') |
| 110 | .select('id, name, published') |
| 111 | .eq('category_id', categoryId) |
| 112 | .eq('published', true); |
| 113 | |
| 114 | if (directToolsError) { |
| 115 | console.error('Error fetching direct tools:', directToolsError); |
| 116 | } else { |
| 117 | console.log(`Found ${directTools?.length || 0} tools directly linked to category ${categoryId}:`); |
| 118 | directTools?.forEach(item => { |
| 119 | console.log(`- ${item.name} (ID: ${item.id})`); |
| 120 | }); |
| 121 | } |
| 122 | |
| 123 | // Junction table tools |
| 124 | console.log('\n2b. Checking tools_categories junction table...'); |
| 125 | const { data: toolsJunctionData, error: toolsJunctionError } = await supabase |
| 126 | .from('tools_categories') |
| 127 | .select('tool_id, category_id') |
| 128 | .eq('category_id', categoryId); |
| 129 | |
| 130 | if (toolsJunctionError) { |
| 131 | console.error('Error fetching tools junction data:', toolsJunctionError); |
| 132 | } else { |
| 133 | console.log(`Found ${toolsJunctionData?.length || 0} entries in tools_categories for category ${categoryId}`); |
| 134 | |
| 135 | if (toolsJunctionData && toolsJunctionData.length > 0) { |
| 136 | // Get details of these tools |
| 137 | const toolIds = toolsJunctionData.map(item => item.tool_id); |
| 138 | |
| 139 | const { data: junctionTools, error: junctionToolsError } = await supabase |
| 140 | .from('tools') |
| 141 | .select('id, name, published') |
| 142 | .in('id', toolIds) |
| 143 | .eq('published', true); |
| 144 | |
| 145 | if (junctionToolsError) { |
| 146 | console.error('Error fetching tools items from junction:', junctionToolsError); |
| 147 | } else { |
| 148 | console.log(`Found ${junctionTools?.length || 0} published tools via junction table:`); |
| 149 | junctionTools?.forEach(item => { |
| 150 | console.log(`- ${item.name} (ID: ${item.id})`); |
| 151 | }); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // 3. Fetch from API and check counts |
| 157 | console.log('\n4. Fetching from categories API...'); |
| 158 | const response = await fetch(`${baseUrl}/api/categories`); |
| 159 | |
| 160 | if (!response.ok) { |
| 161 | throw new Error(`API request failed with status ${response.status}`); |
| 162 | } |
| 163 | |
| 164 | interface Category { |
| 165 | id: number; |
| 166 | name: string; |
| 167 | slug: string; |
| 168 | icon: string; |
| 169 | description: string; |
| 170 | tools_count: number; |
| 171 | news_count: number; |
| 172 | learning_count: number; |
| 173 | posts_count: number; |
| 174 | total_count: number; |
| 175 | [key: string]: any; |
| 176 | } |
| 177 | |
| 178 | const categories = await response.json() as Category[]; |
| 179 | const apiCategory = categories.find((c: Category) => c.id === categoryId); |
| 180 | |
| 181 | if (!apiCategory) { |
| 182 | console.error('Category not found in API response'); |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | console.log('Category from API:'); |
| 187 | console.log(JSON.stringify(apiCategory, null, 2)); |
| 188 | |
| 189 | // 4. Detailed verification of counts |
| 190 | console.log('\n5. Verifying counts...'); |
| 191 | |
| 192 | // Count tools via junction table (that are published) |
| 193 | let publishedToolsJunctionCount = 0; |
| 194 | const { data: publishedTools } = await supabase |
| 195 | .from('tools') |
| 196 | .select('id') |
| 197 | .eq('published', true); |
| 198 | |
| 199 | if (publishedTools && publishedTools.length > 0) { |
| 200 | const publishedToolIds = publishedTools.map(item => item.id); |
| 201 | |
| 202 | const { count, error } = await supabase |
| 203 | .from('tools_categories') |
| 204 | .select('*', { count: 'exact', head: true }) |
| 205 | .eq('category_id', categoryId) |
| 206 | .in('tool_id', publishedToolIds); |
| 207 | |
| 208 | if (!error) { |
| 209 | publishedToolsJunctionCount = count || 0; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | console.log(`Published tools via junction table: ${publishedToolsJunctionCount}`); |
| 214 | console.log(`API tools_count: ${apiCategory.tools_count}`); |
| 215 | |
| 216 | if (publishedToolsJunctionCount === apiCategory.tools_count) { |
| 217 | console.log('✅ Tool counts match!'); |
| 218 | } else { |
| 219 | console.log('❌ Tool counts do not match!'); |
| 220 | } |
| 221 | |
| 222 | // Count news via junction table (that are published) |
| 223 | let publishedJunctionNewsCount = 0; |
| 224 | const { data: publishedNews } = await supabase |
| 225 | .from('news') |
| 226 | .select('id') |
| 227 | .eq('published', true); |
| 228 | |
| 229 | if (publishedNews && publishedNews.length > 0) { |
| 230 | const publishedNewsIds = publishedNews.map(item => item.id); |
| 231 | |
| 232 | const { count, error } = await supabase |
| 233 | .from('news_categories') |
| 234 | .select('*', { count: 'exact', head: true }) |
| 235 | .eq('category_id', categoryId) |
| 236 | .in('news_id', publishedNewsIds); |
| 237 | |
| 238 | if (!error) { |
| 239 | publishedJunctionNewsCount = count || 0; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | console.log(`Published news via junction table: ${publishedJunctionNewsCount}`); |
| 244 | console.log(`API news_count: ${apiCategory.news_count}`); |
| 245 | |
| 246 | if (publishedJunctionNewsCount === apiCategory.news_count) { |
| 247 | console.log('✅ Counts match!'); |
| 248 | } else { |
| 249 | console.log('❌ Counts do not match!'); |
| 250 | } |
| 251 | |
| 252 | } catch (error) { |
| 253 | console.error('Error during testing:', error); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // Run the function |
| 258 | testCategory140() |
| 259 | .then(() => { |
| 260 | console.log('Testing completed'); |
| 261 | process.exit(0); |
| 262 | }) |
| 263 | .catch((error) => { |
| 264 | console.error('Testing failed:', error); |
| 265 | process.exit(1); |
| 266 | }); |