#!/usr/bin/env node
import dotenv from 'dotenv';
import { UmbrellaAuth } from './auth.js';
import { UmbrellaApiClient } from './api-client.js';
dotenv.config();
async function testCostEndpoints() {
console.log('🔍 TESTING COST ENDPOINTS AND PARAMETERS');
console.log('=======================================');
const baseURL = process.env.UMBRELLA_API_BASE_URL || 'https://api.umbrellacost.io/api/v1';
const directCustomerCreds = {
username: 'elisha@umbrellacost.net',
password: 'G37oi57Kp@cNzx'
};
try {
// Authenticate
const auth = new UmbrellaAuth(baseURL);
await auth.authenticate(directCustomerCreds);
const apiClient = new UmbrellaApiClient(baseURL);
apiClient.setAuthToken(auth.getAuthHeaders());
// Get date range
const now = new Date();
const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);
const startDate = startOfMonth.toISOString().split('T')[0];
const endDate = now.toISOString().split('T')[0];
console.log(`Date range: ${startDate} to ${endDate}\n`);
// Get accounts
const accountsResponse = await apiClient.makeRequest('/user-management/accounts');
if (!accountsResponse.success) {
console.log(`❌ Cannot get accounts: ${accountsResponse.error}`);
return;
}
const accounts = accountsResponse.data;
const numericAccount = accounts.find((acc: any) => /^\d+$/.test(acc.accountId));
console.log(`Found numeric account: ${numericAccount?.accountName} (ID: ${numericAccount?.accountId})\n`);
// Test different cost endpoints and parameter combinations
const testConfigurations = [
// Test with no account parameter - maybe it gives total costs?
{
endpoint: '/invoices/caui',
params: { startDate, endDate },
description: 'CAUI without accountId (maybe total costs?)'
},
// Test with numeric account
{
endpoint: '/invoices/caui',
params: { startDate, endDate, accountId: numericAccount?.accountId },
description: `CAUI with numeric accountId: ${numericAccount?.accountId}`
},
// Test different parameter names
{
endpoint: '/invoices/caui',
params: { startDate, endDate, account: numericAccount?.accountId },
description: `CAUI with 'account' parameter`
},
{
endpoint: '/invoices/caui',
params: { startDate, endDate, accountKey: numericAccount?.accountKey },
description: `CAUI with 'accountKey' parameter`
},
// Test other cost endpoints
{
endpoint: '/invoices/cost',
params: { startDate, endDate },
description: 'Cost endpoint without account'
},
{
endpoint: '/invoices/cost',
params: { startDate, endDate, accountId: numericAccount?.accountId },
description: 'Cost endpoint with accountId'
},
// Test cost summary
{
endpoint: '/invoices/summary',
params: { startDate, endDate },
description: 'Summary endpoint without account'
},
// Test just getting invoice data
{
endpoint: '/invoices',
params: { startDate, endDate },
description: 'Invoices endpoint without account'
},
// Test billing
{
endpoint: '/billing',
params: { startDate, endDate },
description: 'Billing endpoint'
}
];
for (const config of testConfigurations) {
console.log(`Testing: ${config.description}`);
console.log(`Endpoint: ${config.endpoint}`);
console.log(`Params: ${JSON.stringify(config.params)}`);
try {
const response = await apiClient.makeRequest(config.endpoint, config.params);
if (response.success) {
console.log(`✅ SUCCESS!`);
console.log(` Data type: ${typeof response.data}`);
if (Array.isArray(response.data)) {
console.log(` Items: ${response.data.length}`);
if (response.data.length > 0) {
console.log(` Sample keys: ${Object.keys(response.data[0] || {}).join(', ')}`);
}
} else if (response.data && typeof response.data === 'object') {
console.log(` Keys: ${Object.keys(response.data).join(', ')}`);
}
} else {
console.log(`❌ FAILED: ${response.error}`);
}
} catch (error: any) {
console.log(`❌ ERROR: ${error.message}`);
}
console.log('');
// Brief pause between requests
await new Promise(resolve => setTimeout(resolve, 200));
}
} catch (error: any) {
console.log(`❌ Test failed: ${error.message}`);
}
}
testCostEndpoints().catch(console.error);