#!/usr/bin/env node
const { spawn } = require('child_process');
require('dotenv').config();
async function analyzeMspCustomers() {
console.log('🔍 ANALYZING MSP CUSTOMERS FOR DIVISIONS/REAL CUSTOMERS');
console.log('=' .repeat(60));
const server = spawn('node', ['dist/index.js'], {
stdio: ['pipe', 'pipe', 'pipe'],
env: process.env
});
let responseBuffer = '';
let responses = {};
server.stdout.on('data', (data) => {
responseBuffer += data.toString();
const lines = responseBuffer.split('\n');
for (let i = 0; i < lines.length - 1; i++) {
const line = lines[i].trim();
if (line) {
try {
const response = JSON.parse(line);
if (response.id) {
responses[response.id] = response;
}
} catch (e) {
// Not JSON, continue
}
}
}
responseBuffer = lines[lines.length - 1];
});
server.stderr.on('data', (data) => {
const msg = data.toString();
if (msg.includes('started successfully')) {
console.log('✅ Server started');
}
});
await new Promise(resolve => setTimeout(resolve, 2000));
// Initialize
server.stdin.write('{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"0.1.0","capabilities":{},"clientInfo":{"name":"analyze","version":"1.0.0"}}}\n');
await new Promise(resolve => setTimeout(resolve, 1000));
// Authenticate with working credentials (Saola MSP)
server.stdin.write(JSON.stringify({
jsonrpc: "2.0",
id: 2,
method: "tools/call",
params: {
name: "authenticate_user",
arguments: {
username: process.env.UMBRELLA_TEST_MSP_USERNAME,
password: process.env.UMBRELLA_TEST_MSP_PASSWORD
}
}
}) + '\n');
await new Promise(resolve => setTimeout(resolve, 3000));
// Get MSP customers
server.stdin.write('{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"api___msp_customers","arguments":{}}}\n');
await new Promise(resolve => setTimeout(resolve, 5000));
// Analyze the MSP customers response for division structure
const response = responses[3];
if (response?.result?.content?.[0]?.text) {
const responseText = response.result.content[0].text;
console.log('✅ Got MSP customers response');
// Extract JSON
const jsonMatch = responseText.match(/```json\s*([\s\S]*?)\s*```/);
if (jsonMatch) {
try {
const customers = JSON.parse(jsonMatch[1]);
console.log(`\n📊 ANALYZING ${customers.length} MSP CUSTOMERS:`);
// Check structure of each customer object
if (customers.length > 0) {
console.log('\n🔍 CUSTOMER OBJECT STRUCTURE:');
const firstCustomer = customers[0];
Object.keys(firstCustomer).forEach(key => {
const value = firstCustomer[key];
console.log(` ${key}: ${typeof value} = ${Array.isArray(value) ? `[${value.length} items]` : JSON.stringify(value).substring(0, 50)}`);
});
// Look for division-related fields
console.log('\n🎯 LOOKING FOR DIVISION/CUSTOMER RELATIONSHIPS:');
customers.forEach((customer, index) => {
const name = customer.customerName || customer.customer_name;
const code = customer.customerCode || customer.customer_code;
const id = customer.customerId || customer.customer_id;
const linkedAccounts = customer.linkedAccountIds || customer.linked_account_ids || [];
console.log(`\n${index + 1}. Customer: "${name}"`);
console.log(` Code: ${code}`);
console.log(` ID: ${id}`);
console.log(` Linked Accounts: ${linkedAccounts.length} accounts`);
// These MSP "customers" might actually be divisions themselves
if (linkedAccounts.length > 1) {
console.log(` 🏢 MULTI-ACCOUNT CUSTOMER (potential real customer division)`);
linkedAccounts.forEach((accId, i) => {
console.log(` ${i + 1}. Account: ${accId}`);
});
}
// Check for bank-like names
if (name.toLowerCase().includes('bank') ||
name.toLowerCase().includes('leumi') ||
name.toLowerCase().includes('hapoalim')) {
console.log(` 🏦 BANK CUSTOMER DETECTED!`);
}
});
// Summary
const multiAccountCustomers = customers.filter(c =>
(c.linkedAccountIds || c.linked_account_ids || []).length > 1
);
console.log(`\n📋 SUMMARY:`);
console.log(` Total MSP "Customers": ${customers.length}`);
console.log(` Multi-account customers: ${multiAccountCustomers.length}`);
console.log(` Single-account customers: ${customers.length - multiAccountCustomers.length}`);
// Search specifically for Bank Leumi
const leumiCustomer = customers.find(c =>
(c.customerName || '').toLowerCase().includes('leumi')
);
if (leumiCustomer) {
console.log(`\n🎯 BANK LEUMI FOUND:`);
console.log(` Name: "${leumiCustomer.customerName}"`);
console.log(` Account Key: ${leumiCustomer.accountKey}`);
console.log(` Linked Accounts: ${leumiCustomer.linkedAccountIds?.length || 0}`);
} else {
console.log(`\n❌ Bank Leumi not found in MSP customers`);
console.log(` This confirms Bank Leumi is likely a division name in a different structure`);
}
}
} catch (e) {
console.log('❌ Failed to parse MSP customers JSON:', e.message);
}
} else {
console.log('❌ No JSON found in MSP customers response');
}
} else {
console.log('❌ No MSP customers response');
}
server.kill();
console.log('\n✅ Analysis complete');
}
analyzeMspCustomers().catch(console.error);