barsa 26b2112fbb Refactor WHMCS client handling and improve type consistency across services
- Updated WHMCS service methods to return WhmcsClient type instead of NormalizedWhmcsClient for better alignment with domain types.
- Refactored caching logic in WhmcsCacheService to utilize WhmcsClient, enhancing type safety and consistency.
- Simplified client detail retrieval in WhmcsClientService by directly using the transformed WhmcsClient response.
- Removed deprecated utility functions and streamlined custom field normalization logic in whmcs-client.utils.ts.
- Enhanced user profile mapping in UsersService to utilize combineToUser for improved clarity and maintainability.
- Cleaned up unused imports and optimized address handling in various components for better performance.
2025-10-29 18:36:25 +09:00

62 lines
1.6 KiB
TypeScript

/**
* Shared WHMCS Provider Utilities
* Single source of truth for WHMCS data parsing
*
* Raw API types are source of truth - no fallbacks or variations expected.
*/
/**
* Parse amount from WHMCS API response
* WHMCS returns amounts as strings or numbers
*/
export function parseAmount(amount: string | number | undefined): number {
if (typeof amount === "number") return amount;
if (!amount) return 0;
const cleaned = String(amount).replace(/[^\d.-]/g, "");
const parsed = Number.parseFloat(cleaned);
return Number.isNaN(parsed) ? 0 : parsed;
}
/**
* Format date from WHMCS API to ISO string
* Returns undefined if input is invalid
*/
export function formatDate(input?: string | null): string | undefined {
if (!input) return undefined;
const date = new Date(input);
if (Number.isNaN(date.getTime())) return undefined;
return date.toISOString();
}
/**
* Normalize status using provided status map
* Generic helper for consistent status mapping
*/
export function normalizeStatus<T extends string>(
status: string | undefined,
statusMap: Record<string, T>,
defaultStatus: T
): T {
if (!status) return defaultStatus;
const mapped = statusMap[status.trim().toLowerCase()];
return mapped ?? defaultStatus;
}
/**
* Normalize billing cycle using provided cycle map
* Generic helper for consistent cycle mapping
*/
export function normalizeCycle<T extends string>(
cycle: string | undefined,
cycleMap: Record<string, T>,
defaultCycle: T
): T {
if (!cycle) return defaultCycle;
const normalized = cycle.trim().toLowerCase().replace(/[_\s-]+/g, " ");
return cycleMap[normalized] ?? defaultCycle;
}