#!/usr/bin/env node
/**
* Debug why accountId parameter returns wrong account data
*/
const axios = require('axios');
async function debugAccountIdIssue() {
console.log('π DEBUGGING ACCOUNT ID ISSUE\n');
console.log('=====================================\n');
try {
// Step 1: Authenticate
console.log('π Authenticating...\n');
const authResponse = await axios.post('https://api.umbrellacost.io/api/v1/users/signin', {
username: 'david+allcloud@umbrellacost.com',
password: 'B4*zcI7#F7poEC'
});
const token = authResponse.data.jwtToken;
const userKey = JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString()).sub;
console.log('β
Authentication successful\n');
console.log(`User Key: ${userKey}\n`);
// Step 2: Get Bank Leumi divisions
const tempApiKey = `${userKey}:15808:0`;
const divisionsResponse = await axios.get('https://api.umbrellacost.io/api/v1/users/plain-sub-users', {
headers: {
'Authorization': token,
'apikey': tempApiKey
}
});
const leumiDivisions = divisionsResponse.data.customerDivisions['Bank Leumi'];
console.log('π Bank Leumi Divisions:\n');
leumiDivisions.forEach(div => {
console.log(` ${div.accountName || 'Account'}: accountKey=${div.accountKey}, divisionId=${div.divisionId}, accountId=${div.accountId}`);
});
console.log('');
// Find the Reseller-1 division
const reseller1 = leumiDivisions.find(d => d.accountName === 'Reseller-1');
console.log('βββββββββββββββββββββββββββββββββββββ\n');
console.log('π¬ TESTING DIFFERENT PARAMETER COMBINATIONS\n');
console.log('βββββββββββββββββββββββββββββββββββββ\n');
const startDate = '2025-06-01';
const endDate = '2025-08-31';
// Test 1: With correct API key (should work)
console.log('Test 1: Using correct API key (22676:139)\n');
try {
const apiKey1 = `${userKey}:${reseller1.accountKey}:${reseller1.divisionId}`;
const response1 = await axios.get('https://api.umbrellacost.io/api/v1/invoices/caui', {
params: {
startDate: startDate,
endDate: endDate,
groupBy: 'none',
costType: ['cost', 'discount'],
excludeFilters: { chargetype: 'Tax' },
periodGranLevel: 'month',
isUnblended: true
},
headers: {
'Authorization': token,
'apikey': apiKey1,
'commonParams': JSON.stringify({ isPpApplied: true })
}
});
if (response1.data && response1.data.length > 0) {
const firstRow = response1.data[0];
const total = response1.data.reduce((sum, row) => sum + (row.total_cost || 0), 0);
console.log(` Result: ${response1.data.length} rows`);
console.log(` Account ID in response: ${firstRow.account_id}`);
console.log(` Total cost: $${total.toFixed(2)}`);
console.log(` β
Correct: Returns account 696314371547 with ~$0.01\n`);
}
} catch (error) {
console.log(` β Error: ${error.response?.status || error.message}\n`);
}
// Test 2: With accountId parameter but wrong division
console.log('Test 2: Using accountId parameter with division 0\n');
try {
const apiKey2 = `${userKey}:${reseller1.accountKey}:0`;
const response2 = await axios.get('https://api.umbrellacost.io/api/v1/invoices/caui', {
params: {
startDate: startDate,
endDate: endDate,
groupBy: 'none',
costType: ['cost', 'discount'],
excludeFilters: { chargetype: 'Tax' },
periodGranLevel: 'month',
isUnblended: true,
accountId: reseller1.accountId // Adding accountId parameter
},
headers: {
'Authorization': token,
'apikey': apiKey2,
'commonParams': JSON.stringify({ isPpApplied: true })
}
});
if (response2.data && response2.data.length > 0) {
const firstRow = response2.data[0];
const total = response2.data.reduce((sum, row) => sum + (row.total_cost || 0), 0);
console.log(` Result: ${response2.data.length} rows`);
console.log(` Account ID in response: ${firstRow.account_id}`);
console.log(` Total cost: $${total.toFixed(2)}`);
if (firstRow.account_id !== reseller1.accountId) {
console.log(` β Wrong: Returns account ${firstRow.account_id} instead of ${reseller1.accountId}\n`);
}
}
} catch (error) {
console.log(` β Error: ${error.response?.status || error.message}\n`);
}
// Test 3: With accountId parameter and correct division
console.log('Test 3: Using accountId parameter with correct division 139\n');
try {
const apiKey3 = `${userKey}:${reseller1.accountKey}:${reseller1.divisionId}`;
const response3 = await axios.get('https://api.umbrellacost.io/api/v1/invoices/caui', {
params: {
startDate: startDate,
endDate: endDate,
groupBy: 'none',
costType: ['cost', 'discount'],
excludeFilters: { chargetype: 'Tax' },
periodGranLevel: 'month',
isUnblended: true,
accountId: reseller1.accountId // Adding accountId parameter
},
headers: {
'Authorization': token,
'apikey': apiKey3,
'commonParams': JSON.stringify({ isPpApplied: true })
}
});
if (response3.data && response3.data.length > 0) {
const firstRow = response3.data[0];
const total = response3.data.reduce((sum, row) => sum + (row.total_cost || 0), 0);
console.log(` Result: ${response3.data.length} rows`);
console.log(` Account ID in response: ${firstRow.account_id}`);
console.log(` Total cost: $${total.toFixed(2)}`);
if (firstRow.account_id === reseller1.accountId) {
console.log(` β
Correct: Returns the right account with accountId parameter\n`);
} else {
console.log(` β Wrong: Returns account ${firstRow.account_id} instead of ${reseller1.accountId}\n`);
}
}
} catch (error) {
console.log(` β Error: ${error.response?.status || error.message}\n`);
}
console.log('βββββββββββββββββββββββββββββββββββββ\n');
console.log('π SUMMARY\n');
console.log('The issue appears to be that when accountId is passed as a parameter,');
console.log('it may override the division selection or cause the API to return');
console.log('data for a different account within the same customer group.\n');
console.log('The correct approach is to use the division ID in the API key,');
console.log('not pass accountId as a parameter.\n');
} catch (error) {
console.error('β Error:', error.response?.data || error.message);
}
}
// Run the test
debugAccountIdIssue().catch(console.error);