#!/usr/bin/env node
/**
* Get the actual MSP customers using the plain-sub-users API
*/
const { spawn } = require('child_process');
async function getActualMspCustomers() {
console.log('π₯ FETCHING ACTUAL MSP CUSTOMERS (DIVISIONS)');
console.log('=' .repeat(60));
const server = spawn('node', ['dist/index.js'], {
stdio: ['pipe', 'pipe', 'pipe'],
env: process.env
});
let responses = {};
let responseBuffer = '';
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();
console.log('π Debug:', msg.trim());
});
await new Promise(resolve => setTimeout(resolve, 3000));
// Initialize
console.log('π‘ Initializing...');
server.stdin.write(JSON.stringify({
jsonrpc: "2.0",
id: 1,
method: "initialize",
params: {
protocolVersion: "0.1.0",
capabilities: {},
clientInfo: { name: "msp-customers", version: "1.0.0" }
}
}) + '\n');
await new Promise(resolve => setTimeout(resolve, 2000));
// Authenticate with AllCloud user
console.log('π Authenticating with AllCloud MSP user...');
server.stdin.write(JSON.stringify({
jsonrpc: "2.0",
id: 2,
method: "tools/call",
params: {
name: "authenticate_user",
arguments: {
username: "david+allcloud@umbrellacost.com",
password: "B4*zcI7#F7poEC"
}
}
}) + '\n');
await new Promise(resolve => setTimeout(resolve, 8000));
// Check auth
console.log('π All responses received:', Object.keys(responses).map(id => `${id}: ${responses[id] ? 'exists' : 'missing'}`).join(', '));
console.log('π Auth response:', JSON.stringify(responses[2], null, 2));
if (responses[2]?.error) {
console.log('β Authentication failed:', responses[2].error);
server.kill();
return;
}
if (!responses[2]?.result) {
console.log('β No auth result received');
server.kill();
return;
}
// Get MSP customers using plain-sub-users API
console.log('π₯ Calling api___users_plain_sub_users...');
server.stdin.write(JSON.stringify({
jsonrpc: "2.0",
id: 3,
method: "tools/call",
params: {
name: "api___users_plain_sub_users",
arguments: {}
}
}) + '\n');
await new Promise(resolve => setTimeout(resolve, 10000));
// Show results
console.log('\nπ ACTUAL MSP CUSTOMERS (DIVISIONS):');
console.log('=' .repeat(70));
const customerResponse = responses[3];
if (customerResponse) {
if (customerResponse.error) {
console.log('β Error:', JSON.stringify(customerResponse.error, null, 2));
} else if (customerResponse.result?.content?.[0]?.text) {
const responseText = customerResponse.result.content[0].text;
console.log(responseText);
// Try to extract customer data from the response
console.log('\nπ PARSING CUSTOMER DATA:');
// Look for JSON in the response
const jsonMatch = responseText.match(/```json\s*([\s\S]*?)\s*```/);
if (jsonMatch) {
try {
const data = JSON.parse(jsonMatch[1]);
// The data might have customers in different structures
if (data.divisionsIds && Array.isArray(data.divisionsIds)) {
console.log(`π Found division structure with ${data.divisionsIds.length} division IDs`);
}
// Look for accounts array which might contain customer divisions
if (data.accounts && Array.isArray(data.accounts)) {
console.log(`π Found ${data.accounts.length} accounts`);
// Group accounts by division
const divisions = new Map();
data.accounts.forEach((account, index) => {
const divisionId = account.divisionId;
const accountName = account.accountName;
const accountKey = account.accountKey;
if (divisionId && divisionId !== 0) {
if (!divisions.has(divisionId)) {
divisions.set(divisionId, []);
}
divisions.get(divisionId).push({
accountName,
accountKey,
accountId: account.accountId
});
}
console.log(` ${index + 1}. "${accountName}" (Key: ${accountKey}, Division: ${divisionId})`);
});
if (divisions.size > 0) {
console.log(`\nπ― FOUND ${divisions.size} CUSTOMER DIVISIONS:`);
for (const [divisionId, accounts] of divisions.entries()) {
console.log(` Division ${divisionId}: ${accounts.length} accounts`);
accounts.forEach((acc, i) => {
console.log(` ${i + 1}. "${acc.accountName}" β ${acc.accountKey}`);
});
}
} else {
console.log('\nβ οΈ No customer divisions found (all accounts in division 0)');
}
}
} catch (e) {
console.log('β Failed to parse JSON:', e.message);
}
} else {
console.log('β No JSON block found in response');
console.log('Raw response snippet:', responseText.substring(0, 500) + '...');
}
} else {
console.log('β No content in response');
}
} else {
console.log('β No response received');
}
server.kill();
console.log('\nβ
Done');
}
getActualMspCustomers().catch(console.error);