#!/usr/bin/env node
import dotenv from 'dotenv';
import { UmbrellaAuth } from './auth.js';
import { UmbrellaApiClient } from './api-client.js';
import { getTestCredentialSets } from './auth-helper.js';
dotenv.config();
async function discoverWorkingEndpoints() {
console.log('π ENDPOINT DISCOVERY FOR MSP ACCOUNT');
console.log('====================================');
const baseURL = process.env.UMBRELLA_API_BASE_URL || 'https://api.umbrellacost.io/api/v1';
const credentialSets = getTestCredentialSets();
if (!credentialSets || credentialSets.length === 0) {
console.error('β No test credentials available');
return;
}
// Use the MSP account (first credential set)
const mspCreds = credentialSets[0];
console.log(`\nπ Discovering endpoints for MSP Account: ${mspCreds.username}`);
console.log('='.repeat(60));
const auth = new UmbrellaAuth(baseURL);
const apiClient = new UmbrellaApiClient(baseURL);
try {
// Authenticate with MSP account
console.log('π Authenticating MSP account...');
await auth.authenticate(mspCreds);
apiClient.setAuthToken(auth.getAuthHeaders());
console.log('β
MSP authentication successful');
// Test all known working endpoints first
const workingEndpoints = [
'/recommendations/report',
'/recommendationsNew/views',
'/anomaly-detection',
'/invoices/service-names/distinct'
];
console.log('\nπ Testing Known Working Endpoints:');
for (const endpoint of workingEndpoints) {
try {
const response = await apiClient.makeRequest(endpoint);
if (response.success) {
console.log(` β
${endpoint}`);
} else {
console.log(` β ${endpoint}: ${response.error}`);
}
} catch (error: any) {
console.log(` β ${endpoint}: ${error.message}`);
}
}
// Test potential customer management endpoints with variations
const customerEndpointVariations = [
// Direct customer endpoints
'/customers',
'/customer',
'/customer-management',
'/customer-management/customers',
'/customer/list',
'/msp/customers',
'/msp-customers',
// User management variations
'/user-management',
'/user-management/customers',
'/user-management/organizations',
'/user-management/accounts',
'/user-management/clients',
// Organization endpoints
'/organizations',
'/organization',
'/orgs',
'/org',
// Account management endpoints
'/accounts',
'/account',
'/account-management',
'/account-management/customers',
// Partner/MSP specific endpoints
'/partners',
'/partner',
'/partner-management',
'/msp',
'/msp/organizations',
'/msp-management',
// Billing/rebilling endpoints that might contain customer info
'/billing',
'/billing/customers',
'/rebilling',
'/rebilling/customers',
// Admin endpoints
'/admin',
'/admin/customers',
'/admin/organizations'
];
console.log('\nπ Testing Customer Management Endpoint Variations:');
const workingCustomerEndpoints: string[] = [];
for (const endpoint of customerEndpointVariations) {
try {
console.log(` Testing: ${endpoint}`);
const response = await apiClient.makeRequest(endpoint);
if (response.success) {
console.log(` β
FOUND: ${endpoint}`);
workingCustomerEndpoints.push(endpoint);
// Show some details about the response
if (response.data) {
if (Array.isArray(response.data)) {
console.log(` β Array with ${response.data.length} items`);
if (response.data.length > 0) {
console.log(` β Sample keys: ${Object.keys(response.data[0]).slice(0, 5).join(', ')}`);
}
} else if (typeof response.data === 'object') {
console.log(` β Object with keys: ${Object.keys(response.data).slice(0, 8).join(', ')}`);
}
}
} else {
console.log(` β ${endpoint}: ${response.error}`);
}
} catch (error: any) {
console.log(` β ${endpoint}: ${error.message}`);
}
// Small delay to be nice to the API
await new Promise(resolve => setTimeout(resolve, 100));
}
console.log('\nπ DISCOVERY RESULTS:');
console.log('===================');
if (workingCustomerEndpoints.length > 0) {
console.log(`β
Found ${workingCustomerEndpoints.length} working customer-related endpoints:`);
workingCustomerEndpoints.forEach(endpoint => {
console.log(` - ${endpoint}`);
});
} else {
console.log('β No customer management endpoints found');
console.log(' This could mean:');
console.log(' - The MSP account lacks customer management permissions');
console.log(' - Customer data is accessed through different endpoints');
console.log(' - Customer management is handled via a different API version');
console.log(' - The account is not actually an MSP account');
}
} catch (error: any) {
console.error(`β Authentication failed: ${error.message}`);
}
}
discoverWorkingEndpoints().catch(console.error);