const fs = require('fs');
const path = require('path');
console.log('🔍 Tracing Customer Detection Flow for Bank Leumi Test Env\n');
// Mock the exact data structure from your JSON
const mockCustomerDivisions = {
"Bank Leumi": {
"accounts": [
{
"accountName": "Reseller-1",
"accountKey": "22676",
"divisionId": "139"
},
{
"accountName": "Allcloud-AWS-Reseller-329 - BL Test Env",
"accountKey": "24223",
"divisionId": "1"
}
]
}
};
function analyzeCustomerDetection() {
console.log('📋 Input Data:');
console.log(' Query: "show me Bank Leumi Test Env costs last 6 months"');
console.log(' Provided customer_account_key: "22676" (from Claude)');
console.log(' Available customers:', Object.keys(mockCustomerDivisions));
console.log('');
const query = "show me Bank Leumi Test Env costs last 6 months";
const queryLower = query.toLowerCase();
console.log('🔎 Step 1: Extract query words');
const queryWords = queryLower.split(/[\s-]+/).filter(w => w.length > 2);
console.log(` Query words: [${queryWords.map(w => `"${w}"`).join(', ')}]`);
console.log('');
console.log('🔍 Step 2: Customer Pattern Matching');
const customerPatterns = [
/bank[\s-]*leumi/i,
/hapoalim/i,
/mizrahi/i,
/discount/i
];
let foundCustomer = null;
customerPatterns.forEach((pattern, idx) => {
if (pattern.test(query)) {
console.log(` ✅ Pattern ${idx + 1} (${pattern}) matches query`);
// This is where we need to find the actual customer name
const customerNames = Object.keys(mockCustomerDivisions);
customerNames.forEach(customerName => {
if (pattern.test(customerName)) {
foundCustomer = customerName;
console.log(` ✅ Found matching customer: "${customerName}"`);
} else {
console.log(` ❌ Customer "${customerName}" doesn't match pattern`);
}
});
} else {
console.log(` ❌ Pattern ${idx + 1} (${pattern}) doesn't match query`);
}
});
if (!foundCustomer) {
console.log(' 🚨 NO CUSTOMER FOUND FROM PATTERNS!');
return;
}
console.log('');
console.log('🔍 Step 3: Account Matching within Customer');
const customer = mockCustomerDivisions[foundCustomer];
const accounts = customer.accounts;
console.log(` Customer: "${foundCustomer}"`);
console.log(` Available accounts: ${accounts.length}`);
accounts.forEach((account, idx) => {
console.log(`\n Account ${idx + 1}: "${account.accountName}" (key: ${account.accountKey}, division: ${account.divisionId})`);
const accountNameLower = account.accountName.toLowerCase();
const accountWords = accountNameLower.split(/[\s-]+/).filter(w => w.length > 2);
console.log(` Account words: [${accountWords.map(w => `"${w}"`).join(', ')}]`);
// Word overlap matching
const matchingWords = accountWords.filter(word =>
queryWords.some(qWord => qWord.includes(word) || word.includes(qWord))
);
console.log(` Matching words: [${matchingWords.map(w => `"${w}"`).join(', ')}]`);
// Apply the three matching conditions
const fullNameMatch = queryLower.includes(accountNameLower);
const wordCountMatch = matchingWords.length >= Math.min(2, accountWords.length);
const partialMatch = accountNameLower.split(/[\s-]+/).some(part => part.length > 3 && queryLower.includes(part));
console.log(` Full name match: ${fullNameMatch}`);
console.log(` Word count match (${matchingWords.length} >= min(2, ${accountWords.length})): ${wordCountMatch}`);
console.log(` Partial match: ${partialMatch}`);
const isMatch = fullNameMatch || wordCountMatch || partialMatch;
console.log(` ✨ RESULT: ${isMatch ? '✅ MATCH' : '❌ NO MATCH'}`);
if (isMatch) {
const expectedApiKey = `57ade50e-c9a8-49f3-8ce7-28d44536a669:${account.accountKey}:${account.divisionId}`;
console.log(` 🎯 Expected API Key: ${expectedApiKey}`);
}
});
}
function simulateServerLogic() {
console.log('\n🖥️ Simulating Server Logic\n');
// Read the actual server.ts file to check our implementation
const serverPath = path.join(__dirname, '../../src/server.ts');
const serverContent = fs.readFileSync(serverPath, 'utf8');
// Check if the always-run-detection logic is present
if (serverContent.includes('but will run detection anyway')) {
console.log('✅ Always-run-detection logic is present in server.ts');
} else {
console.log('❌ Always-run-detection logic is NOT found in server.ts');
}
// Look for the customer pattern matching logic
if (serverContent.includes('bank[\\s-]*leumi')) {
console.log('✅ Bank Leumi pattern matching is present');
} else {
console.log('❌ Bank Leumi pattern matching is NOT found');
}
// Check for the word-based account matching
if (serverContent.includes('matchingWords.length >= Math.min(2, accountWords.length)')) {
console.log('✅ Word-based account matching logic is present');
} else {
console.log('❌ Word-based account matching logic is NOT found');
}
}
function identifyPotentialIssues() {
console.log('\n🚨 Potential Issues to Investigate:\n');
console.log('1. Pattern Matching:');
console.log(' - Does /bank[\\s-]*leumi/i match "Bank Leumi"?');
console.log(' - Test:', /bank[\s-]*leumi/i.test("Bank Leumi"));
console.log('');
console.log('2. Data Flow:');
console.log(' - Is customerDivisions being populated correctly?');
console.log(' - Is the customer detection function being called?');
console.log(' - Are the detection results being applied?');
console.log('');
console.log('3. Timing Issues:');
console.log(' - Is detection running after Claude provides customer_account_key?');
console.log(' - Are multiple detection calls overriding each other?');
console.log('');
console.log('4. API Key Format:');
console.log(' - Expected: 57ade50e-c9a8-49f3-8ce7-28d44536a669:24223:1');
console.log(' - Actual: 57ade50e-c9a8-49f3-8ce7-28d44536a669:22676:139');
console.log(' - The account and division are both wrong!');
}
// Run the analysis
analyzeCustomerDetection();
simulateServerLogic();
identifyPotentialIssues();
console.log('\n💡 Next Steps:');
console.log('1. Add detailed logging to server.ts customer detection function');
console.log('2. Test the actual pattern matching with real data');
console.log('3. Verify that customerDivisions contains the correct data');
console.log('4. Check if detection results are being overridden after being set');