#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
console.log('🔍 DEEP ANALYSIS: Recommendations API Key Issue\n');
console.log('='.repeat(80));
// Read the key files
const serverWithAuthPath = path.join(__dirname, '../../src/server-with-auth.ts');
const apiClientPath = path.join(__dirname, '../../src/api-client.ts');
const typesPath = path.join(__dirname, '../../src/types.ts');
const serverCode = fs.readFileSync(serverWithAuthPath, 'utf8');
const apiClientCode = fs.readFileSync(apiClientPath, 'utf8');
const typesCode = fs.readFileSync(typesPath, 'utf8');
console.log('\n📋 STEP 1: Analyzing Parameter Flow from Claude to API\n');
console.log('-'.repeat(80));
// 1. Check what Claude sends
console.log('1.1 What Claude Desktop sends for recommendations:');
console.log(' /v1/recommendationsNew/heatmap/summary: {accountKey: "24223", divisionId: "1"}');
console.log(' /v2/recommendations/list: {accountKey: "24223", divisionId: "1"}');
// 2. Check if accountKey is in the schema
console.log('\n1.2 Checking QueryParamsSchema in types.ts:');
const schemaMatch = typesCode.match(/QueryParamsSchema = z\.object\(\{[\s\S]*?\}\);/);
if (schemaMatch) {
const hasAccountKey = schemaMatch[0].includes('accountKey:');
const hasDivisionId = schemaMatch[0].includes('divisionId:');
console.log(` ✅ Has divisionId: ${hasDivisionId}`);
console.log(` ${hasAccountKey ? '✅' : '❌'} Has accountKey: ${hasAccountKey}`);
if (!hasAccountKey) {
console.log(' ⚠️ PROBLEM: accountKey is NOT in schema - will be stripped during validation!');
}
}
// 3. Analyze parameter validation flow
console.log('\n1.3 Parameter Validation Flow in server-with-auth.ts:');
const validationLine = serverCode.match(/validatedParams = \{.*?QueryParamsSchema\.partial\(\)\.parse.*?\}/);
if (validationLine) {
console.log(' Line ~749: validatedParams = {...QueryParamsSchema.partial().parse(regularParams), ...}');
console.log(' ⚠️ This strips out any parameters NOT in the schema!');
}
// 4. Check parameter mapping
console.log('\n1.4 Parameter Mapping for recommendations endpoints:');
const mappingSection = serverCode.match(/if \(ENDPOINTS_REQUIRING_CUSTOMER_PARAMS\.includes\(path\)\) \{[\s\S]*?if \(validatedParams\.accountKey && validatedParams\.divisionId\)/);
if (mappingSection) {
console.log(' Found mapping code at lines 770-786');
console.log(' Code expects: validatedParams.accountKey && validatedParams.divisionId');
console.log(' Maps to: customer_account_key and customer_division_id');
console.log(' ⚠️ BUT accountKey never makes it here because it was stripped!');
}
console.log('\n📋 STEP 2: Analyzing API Client Flow\n');
console.log('-'.repeat(80));
// 5. Check how api-client uses the parameters
console.log('2.1 API Client MSP Check:');
const mspCheckMatch = apiClientCode.match(/MSP Check: customerAccountKey=(.*?), useFrontendApi=/g);
if (mspCheckMatch) {
console.log(' api-client.ts checks for customerAccountKey parameter');
console.log(' When undefined, uses default API key: 57ade50e-c9a8-49f3-8ce7-28d44536a669:15808:0');
}
// 6. Check getAuthHeaders function
console.log('\n2.2 getAuthHeaders Function Analysis:');
const getAuthHeadersMatch = apiClientCode.match(/private getAuthHeaders\([\s\S]*?\n \}/);
if (getAuthHeadersMatch) {
console.log(' Function builds API key based on customerAccountKey');
console.log(' If customerAccountKey exists: builds {baseKey}:{accountKey}:{divisionId}');
console.log(' If not: uses default apikey');
}
console.log('\n📋 STEP 3: The Complete Problem Chain\n');
console.log('-'.repeat(80));
console.log(`
PROBLEM FLOW:
1. Claude sends: {accountKey: "24223", divisionId: "1"} ✅
2. server-with-auth.ts receives the parameters ✅
3. QueryParamsSchema.partial().parse() strips accountKey ❌ (NOT IN SCHEMA)
4. validatedParams has no accountKey ❌
5. Parameter mapping never happens (line 772 check fails) ❌
6. customer_account_key is never set ❌
7. api-client receives customerAccountKey=undefined ❌
8. Default API key used: 57ade50e-c9a8-49f3-8ce7-28d44536a669:15808:0 ❌
9. Wrong recommendations returned ❌
`);
console.log('\n📋 STEP 4: The Solution\n');
console.log('-'.repeat(80));
console.log(`
SOLUTION:
1. Add 'accountKey' to QueryParamsSchema in types.ts ✅ (Already done)
2. Rebuild the TypeScript: npm run build
3. Restart the server
4. Now the flow will be:
- accountKey passes validation
- Gets mapped to customer_account_key
- API client receives it
- Builds correct apikey: 57ade50e-c9a8-49f3-8ce7-28d44536a669:24223:1
`);
console.log('\n📋 STEP 5: Current Status Check\n');
console.log('-'.repeat(80));
// Check if the fix is already applied
const currentHasAccountKey = typesCode.includes('accountKey: z.string().optional()');
if (currentHasAccountKey) {
console.log('✅ FIX APPLIED: accountKey is now in QueryParamsSchema');
console.log(' Just need to rebuild and restart the server');
} else {
console.log('❌ FIX NOT APPLIED: accountKey is still missing from QueryParamsSchema');
}
console.log('\n📋 STEP 6: Verification of All Recommendation Endpoints\n');
console.log('-'.repeat(80));
const endpoints = [
'/v1/recommendationsNew/heatmap/summary',
'/v2/recommendations/list'
];
console.log('Checking ENDPOINTS_REQUIRING_CUSTOMER_PARAMS list:');
endpoints.forEach(endpoint => {
const isIncluded = serverCode.includes(`'${endpoint}'`);
console.log(` ${isIncluded ? '✅' : '❌'} ${endpoint}`);
});
console.log('\n' + '='.repeat(80));
console.log('ANALYSIS COMPLETE');
console.log('='.repeat(80));