/**
* Account Mapper Utility
* Maps accountId to accountKey using plain-sub-users data
*/
export interface PlainSubUsersData {
user_type: number; // 1 = Direct, 9/10/11 = MSP
is_reseller_mode?: boolean;
accounts?: Array<{
accountKey: string;
accountId: string;
accountName?: string;
cloudTypeId?: number; // 0 = AWS, 1 = Azure, 2 = GCP
provider?: string;
displayName?: string;
[key: string]: any;
}>;
customerDivisions?: Record<string, any>;
divisionId?: string;
[key: string]: any;
}
export class AccountMapper {
/**
* Find the accountKey for a given accountId using plain-sub-users data
*/
static findAccountKey(userData: PlainSubUsersData | undefined, accountId: string | undefined): string | null {
if (!userData || !accountId) {
console.error('[ACCOUNT-MAPPER] No userData or accountId provided');
return null;
}
// Check in accounts array
if (userData.accounts && Array.isArray(userData.accounts)) {
const account = userData.accounts.find(acc =>
acc.accountId === accountId ||
acc.accountKey === accountId ||
// Also check for partial matches (e.g., "Master-59f88c" might match "59f88c")
(typeof accountId === 'string' && (
acc.accountId?.includes(accountId) ||
acc.displayName?.toLowerCase().includes(accountId.toLowerCase())
))
);
if (account) {
console.error(`[ACCOUNT-MAPPER] Found account mapping: ${accountId} -> ${account.accountKey}`);
return account.accountKey;
}
}
// For MSP users, check in customerDivisions
if (userData.customerDivisions && userData.user_type && [9, 10, 11].includes(userData.user_type)) {
// CustomerDivisions format: { "Customer Name": { accountKey: "xxx", divisionId: "yyy" } }
for (const [customerName, customerData] of Object.entries(userData.customerDivisions)) {
if (customerName.toLowerCase().includes(accountId.toLowerCase()) ||
(customerData as any).accountKey === accountId) {
const accountKey = (customerData as any).accountKey;
console.error(`[ACCOUNT-MAPPER] Found MSP customer mapping: ${accountId} -> ${accountKey}`);
return accountKey;
}
}
}
console.error(`[ACCOUNT-MAPPER] No mapping found for accountId: ${accountId}`);
return null;
}
/**
* Get cloud context for a given accountId
*/
static getCloudContext(userData: PlainSubUsersData | undefined, accountId: string | undefined): string | null {
if (!userData || !accountId) {
return null;
}
// Check in accounts array
if (userData.accounts && Array.isArray(userData.accounts)) {
const account = userData.accounts.find(acc =>
acc.accountId === accountId ||
acc.accountKey === accountId ||
acc.accountName?.toLowerCase() === accountId?.toLowerCase() ||
// Also check for partial matches
(typeof accountId === 'string' && (
acc.accountId?.includes(accountId) ||
acc.accountName?.toLowerCase().includes(accountId.toLowerCase())
))
);
if (account && account.cloudTypeId !== undefined) {
// Map cloudTypeId to cloud context string
const cloudContextMap: Record<number, string> = {
0: 'aws',
1: 'azure',
2: 'gcp'
};
const context = cloudContextMap[account.cloudTypeId];
if (context) {
console.error(`[ACCOUNT-MAPPER] Detected cloud context for ${accountId}: ${context} (cloudTypeId: ${account.cloudTypeId})`);
return context;
}
}
}
return null;
}
/**
* Build a dynamic API key based on the requested accountId
*/
static buildDynamicApiKey(
userData: PlainSubUsersData | undefined,
baseKey: string,
requestedAccountId?: string
): string {
// Default values
let accountKey = userData?.accounts?.[0]?.accountKey || '-1';
let divisionId = userData?.divisionId || '0';
// If a specific accountId is requested, find its accountKey
if (requestedAccountId) {
const mappedKey = this.findAccountKey(userData, requestedAccountId);
if (mappedKey) {
accountKey = mappedKey;
// For MSP customers, also get the divisionId
if (userData?.customerDivisions) {
for (const customerData of Object.values(userData.customerDivisions)) {
if ((customerData as any).accountKey === mappedKey) {
divisionId = (customerData as any).divisionId || divisionId;
break;
}
}
}
}
}
const apiKey = `${baseKey}:${accountKey}:${divisionId}`;
console.error(`[ACCOUNT-MAPPER] Built API key: ${apiKey} for accountId: ${requestedAccountId || 'default'}`);
return apiKey;
}
/**
* Determine if user is MSP based on user_type
*/
static isMspUser(userData: PlainSubUsersData | undefined): boolean {
return userData?.user_type ? [9, 10, 11].includes(userData.user_type) : false;
}
/**
* Get the default account for the user
*/
static getDefaultAccount(userData: PlainSubUsersData | undefined): { accountKey: string; accountId: string } | null {
if (!userData?.accounts || userData.accounts.length === 0) {
return null;
}
const firstAccount = userData.accounts[0];
return {
accountKey: firstAccount.accountKey,
accountId: firstAccount.accountId
};
}
}