/** * Row formatting for admin user export (XLSX/CSV). */ import type { AdminUserListItem } from '../services/adminService'; import type { AdminUserExportColumn } from '../utils/adminUserListQuery'; const USDT_DECIMALS = 6; function formatUsdtRaw(raw: string): string { try { let n = BigInt(raw); const negative = n < 0n; if (negative) n = -n; const divisor = 10n ** BigInt(USDT_DECIMALS); const whole = n / divisor; const frac = (n % divisor).toString().padStart(USDT_DECIMALS, '0').replace(/0+$/, ''); return `${negative ? '-' : ''}${whole}${frac ? `.${frac}` : ''}`; } catch { return raw; } } function formatSignedUsdtRaw(raw: string): string { try { const n = BigInt(raw); if (n === 0n) return '0'; const formatted = formatUsdtRaw(n < 0n ? (-n).toString() : raw); return n < 0n ? `-${formatted}` : formatted; } catch { return raw; } } function formatDate(value: Date | string | null | undefined): string { if (!value) return ''; const d = value instanceof Date ? value : new Date(value); return Number.isNaN(d.getTime()) ? '' : d.toISOString(); } function formatDob(dob: string | null | undefined): string { if (!dob) return ''; const match = dob.match(/^(\d{4}-\d{2}-\d{2})/); return match ? match[1] : dob; } function fullName(u: AdminUserListItem): string { const parts = [u.first_name, u.middle_name, u.last_name].filter(Boolean); return parts.length ? parts.join(' ') : ''; } function ageCategory(dob: string | null | undefined): string { if (!dob) return ''; const match = dob.match(/^(\d{4})-(\d{2})-(\d{2})/); if (!match) return ''; const birth = new Date(Date.UTC(Number(match[1]), Number(match[2]) - 1, Number(match[3]))); const now = new Date(); let age = now.getUTCFullYear() - birth.getUTCFullYear(); const m = now.getUTCMonth() - birth.getUTCMonth(); if (m < 0 || (m === 0 && now.getUTCDate() < birth.getUTCDate())) age -= 1; if (age < 0 || !Number.isFinite(age)) return ''; if (age < 18) return 'Under 18'; if (age < 25) return '18–24'; if (age < 35) return '25–34'; if (age < 45) return '35–44'; if (age < 55) return '45–54'; if (age < 65) return '55–64'; return '65+'; } function deriveAccountStatus(u: AdminUserListItem): string { return u.account_status; } export const EXPORT_COLUMN_HEADERS: Record = { id: 'ID', email: 'Email', full_name: 'Full name', account_status: 'Account status', role_name: 'Role', country_of_residence: 'Country', nationality: 'Nationality', date_of_birth: 'DOB', age_category: 'Age group', referral_code: 'Referral code', first_deposit_at: 'First deposit', first_deposit_transaction_id: 'First deposit tx ID', email_verified: 'Email verified', main_balance_raw: 'Main USDT', bonus_balance_raw: 'Bonus USDT', total_bonus_received_raw: 'Bonus received', prediction_count: 'Predictions', net_pnl_raw: 'Net P/L USDT', total_stake_raw: 'Total stake', total_payout_raw: 'Total payout', created_at: 'Created', last_login_at: 'Last login', last_activity_at: 'Last activity', is_enabled: 'Enabled', withdrawals_suspended: 'Withdrawals suspended', under_surveillance: 'Under surveillance', totp_enabled: '2FA', is_account_non_expired: 'Account non expired', is_account_non_locked: 'Account non locked', is_credentials_non_expired: 'Credentials valid', }; export function exportCellValue( column: AdminUserExportColumn, user: AdminUserListItem, countryName: (code: string | null | undefined) => string ): string | number { switch (column) { case 'id': return user.id; case 'email': return user.email ?? ''; case 'full_name': return fullName(user); case 'account_status': return deriveAccountStatus(user); case 'role_name': return user.role_name; case 'country_of_residence': return countryName(user.country_of_residence); case 'nationality': return countryName(user.nationality); case 'date_of_birth': return formatDob(user.date_of_birth); case 'age_category': return ageCategory(user.date_of_birth); case 'referral_code': return user.referral_code ?? ''; case 'first_deposit_at': return formatDate(user.first_deposit_at); case 'first_deposit_transaction_id': return user.first_deposit_transaction_id ?? ''; case 'email_verified': return user.email_verified ? 'yes' : 'no'; case 'main_balance_raw': return formatUsdtRaw(user.main_balance_raw); case 'bonus_balance_raw': return formatUsdtRaw(user.bonus_balance_raw); case 'total_bonus_received_raw': return formatUsdtRaw(user.total_bonus_received_raw); case 'prediction_count': return user.prediction_count; case 'net_pnl_raw': return formatSignedUsdtRaw(user.net_pnl_raw); case 'total_stake_raw': return formatUsdtRaw(user.total_stake_raw); case 'total_payout_raw': return formatUsdtRaw(user.total_payout_raw); case 'created_at': return formatDate(user.created_at); case 'last_login_at': return formatDate(user.last_login_at); case 'last_activity_at': return formatDate(user.last_activity_at); case 'is_enabled': return user.is_enabled ? 'true' : 'false'; case 'withdrawals_suspended': return user.withdrawals_suspended ? 'true' : 'false'; case 'under_surveillance': return user.under_surveillance ? 'true' : 'false'; case 'totp_enabled': return user.totp_enabled ? 'yes' : 'no'; case 'is_account_non_expired': return user.is_account_non_expired ? 'true' : 'false'; case 'is_account_non_locked': return user.is_account_non_locked ? 'true' : 'false'; case 'is_credentials_non_expired': return user.is_credentials_non_expired ? 'true' : 'false'; default: return ''; } } export function buildExportRows( users: AdminUserListItem[], columns: AdminUserExportColumn[], countryName: (code: string | null | undefined) => string ): unknown[][] { const header = columns.map((c) => EXPORT_COLUMN_HEADERS[c]); const rows = users.map((u) => columns.map((c) => exportCellValue(c, u, countryName))); return [header, ...rows]; }