const fs = require('fs');
const path = require('path');
console.log('🕵️ Investigating Why Customer Detection Logs Are Missing\n');
function analyzeServerCode() {
console.log('đź“‹ Step 1: Analyzing server.ts for customer detection code...\n');
const serverPath = path.join(__dirname, '../../src/server.ts');
const serverContent = fs.readFileSync(serverPath, 'utf8');
const lines = serverContent.split('\n');
// Look for the key areas we need to check
const keyPatterns = [
'MSP-CUSTOMER-DETECTION',
'Claude provided customer_account_key',
'but will run detection anyway',
'detectCustomerFromQuery',
'authMethod === \'cognito\'',
'currentSession.userEmail',
'CUSTOMER-DETECTION'
];
keyPatterns.forEach(pattern => {
console.log(`🔍 Looking for pattern: "${pattern}"`);
let found = false;
lines.forEach((line, idx) => {
if (line.includes(pattern)) {
found = true;
console.log(` âś… Line ${idx + 1}: ${line.trim()}`);
// Show surrounding context for important patterns
if (pattern.includes('Claude provided') || pattern.includes('detectCustomerFromQuery')) {
console.log(` Context:`);
for (let i = Math.max(0, idx - 2); i <= Math.min(lines.length - 1, idx + 2); i++) {
if (i !== idx) {
console.log(` ${i + 1}: ${lines[i].trim()}`);
}
}
}
}
});
if (!found) {
console.log(` ❌ Pattern "${pattern}" NOT FOUND!`);
}
console.log('');
});
}
function checkConditionLogic() {
console.log('đź“‹ Step 2: Checking the conditional logic that triggers detection...\n');
const serverPath = path.join(__dirname, '../../src/server.ts');
const serverContent = fs.readFileSync(serverPath, 'utf8');
// Look for the specific conditional logic around customer detection
const authMethodChecks = [];
const lines = serverContent.split('\n');
lines.forEach((line, idx) => {
if (line.includes('authMethod') && (line.includes('cognito') || line.includes('keycloak'))) {
authMethodChecks.push({
line: idx + 1,
content: line.trim(),
context: lines.slice(Math.max(0, idx - 3), Math.min(lines.length, idx + 4))
});
}
});
if (authMethodChecks.length === 0) {
console.log('❌ NO authMethod checks found! This could be the problem.');
} else {
console.log(`âś… Found ${authMethodChecks.length} authMethod check(s):`);
authMethodChecks.forEach((check, idx) => {
console.log(`\n Check ${idx + 1} at line ${check.line}:`);
console.log(` ${check.content}`);
console.log(` Context:`);
check.context.forEach((contextLine, contextIdx) => {
const lineNum = check.line - 3 + contextIdx;
const marker = lineNum === check.line ? '>>> ' : ' ';
console.log(` ${marker}${lineNum}: ${contextLine.trim()}`);
});
});
}
}
function identifyPossibleIssues() {
console.log('\nđź“‹ Step 3: Identifying possible issues...\n');
const serverPath = path.join(__dirname, '../../src/server.ts');
const serverContent = fs.readFileSync(serverPath, 'utf8');
// Check if the detection code is in a try-catch that might be silently failing
if (serverContent.includes('try') && serverContent.includes('detectCustomerFromQuery')) {
console.log('⚠️ Detection code is in a try-catch block - could be silently failing');
}
// Check if there are early returns that might skip detection
const lines = serverContent.split('\n');
const suspiciousReturns = [];
lines.forEach((line, idx) => {
if (line.includes('return') &&
(line.includes('validatedParams') || line.includes('params'))) {
suspiciousReturns.push(`Line ${idx + 1}: ${line.trim()}`);
}
});
if (suspiciousReturns.length > 0) {
console.log('⚠️ Found potential early returns that might skip detection:');
suspiciousReturns.forEach(ret => console.log(` ${ret}`));
}
// Check if detection is conditional on having customers data
if (serverContent.includes('customerDivisions') && serverContent.includes('length')) {
console.log('âś… Detection appears to check for customerDivisions data');
} else {
console.log('❌ No checks for customerDivisions data found - might fail silently');
}
console.log('\n🔍 Key questions to investigate:');
console.log('1. Is the authMethod being detected correctly as "cognito"?');
console.log('2. Is the customerDivisions data being populated?');
console.log('3. Is the detection function actually being called?');
console.log('4. Are there any exceptions being thrown and caught silently?');
console.log('5. Is the user session data correct?');
}
function generateDebuggingStrategy() {
console.log('\nđź“‹ Step 4: Debugging Strategy\n');
console.log('🎯 Immediate steps to debug:');
console.log('');
console.log('1. Add console.error logs BEFORE the authMethod check:');
console.log(' console.error(`[DEBUG] authMethod: ${authMethod}, userEmail: ${currentSession.userEmail}`);');
console.log('');
console.log('2. Add console.error logs for customerDivisions:');
console.log(' console.error(`[DEBUG] customerDivisions keys: ${Object.keys(this.customerDivisions)}`);');
console.log('');
console.log('3. Add console.error log at the start of detection function:');
console.log(' console.error(`[DEBUG] detectCustomerFromQuery called with: ${userQuery}`);');
console.log('');
console.log('4. Check if the conditional logic path is reached:');
console.log(' console.error(`[DEBUG] About to check customer detection condition`);');
console.log('');
console.log('đź’ˇ Expected behavior:');
console.log('- authMethod should be "cognito"');
console.log('- customerDivisions should contain "Bank Leumi" key');
console.log('- userQuery should be "Bank Leumi test environment costs last 3 months"');
console.log('- Detection should run and find account 24223 instead of 22676');
}
// Run the analysis
analyzeServerCode();
checkConditionLogic();
identifyPossibleIssues();
generateDebuggingStrategy();
console.log('\n🚀 Next: Add debugging logs to server.ts and restart to trace execution');