diff --git a/backend/src/repositories/referralRepository.ts b/backend/src/repositories/referralRepository.ts index c2c1c95a..b49b8177 100644 --- a/backend/src/repositories/referralRepository.ts +++ b/backend/src/repositories/referralRepository.ts @@ -763,21 +763,23 @@ export async function getAdminStats(): Promise<{ const usersByCountry = await query>( `SELECT COALESCE(c.name, CASE - WHEN u.country_of_residence IS NULL OR u.country_of_residence = 'XX' THEN 'Non renseigné' + WHEN u.country_of_residence IS NULL OR u.country_of_residence = 'XX' THEN 'UNSPECIFIED' ELSE u.country_of_residence END) AS country, COUNT(*)::text AS count FROM users u LEFT JOIN countries c ON c.code = u.country_of_residence + WHERE u.referred_by_user_id IS NOT NULL GROUP BY u.country_of_residence, c.name ORDER BY COUNT(*) DESC` ); const usersByNationality = await query>( - `SELECT COALESCE(c.name, u.nationality, 'Non renseigné') AS nationality, + `SELECT COALESCE(c.name, u.nationality, 'UNSPECIFIED') AS nationality, COUNT(*)::text AS count FROM users u LEFT JOIN countries c ON c.code = u.nationality - WHERE u.nationality IS NOT NULL + WHERE u.referred_by_user_id IS NOT NULL + AND u.nationality IS NOT NULL GROUP BY u.nationality, c.name ORDER BY COUNT(*) DESC` ); @@ -804,15 +806,26 @@ export async function getAdminStats(): Promise<{ const topReferrers = await query< Array<{ user_id: string; referral_code: string | null; direct_referrals: string; total_earned: string }> >( - `SELECT u.id AS user_id, u.referral_code, - COUNT(DISTINCT r.id)::text AS direct_referrals, - COALESCE(SUM(p.commission_amount) FILTER (WHERE p.status = 'paid'), 0)::text AS total_earned + `SELECT u.id AS user_id, + u.referral_code, + COALESCE(d.direct_referrals, 0)::text AS direct_referrals, + COALESCE(e.total_earned, 0)::text AS total_earned FROM users u - LEFT JOIN users r ON r.referred_by_user_id = u.id - LEFT JOIN referral_commission_payouts p ON p.beneficiary_user_id = u.id + LEFT JOIN ( + SELECT referred_by_user_id AS user_id, COUNT(*)::bigint AS direct_referrals + FROM users + WHERE referred_by_user_id IS NOT NULL + GROUP BY referred_by_user_id + ) d ON d.user_id = u.id + LEFT JOIN ( + SELECT beneficiary_user_id AS user_id, SUM(commission_amount)::bigint AS total_earned + FROM referral_commission_payouts + WHERE status = 'paid' + GROUP BY beneficiary_user_id + ) e ON e.user_id = u.id WHERE u.referral_code IS NOT NULL - GROUP BY u.id, u.referral_code - ORDER BY COUNT(DISTINCT r.id) DESC, COALESCE(SUM(p.commission_amount) FILTER (WHERE p.status = 'paid'), 0) DESC + AND (COALESCE(d.direct_referrals, 0) > 0 OR COALESCE(e.total_earned, 0) > 0) + ORDER BY COALESCE(d.direct_referrals, 0) DESC, COALESCE(e.total_earned, 0) DESC LIMIT 20` ); diff --git a/prediqo_web/src/features/referral/ReferralAdminPage.tsx b/prediqo_web/src/features/referral/ReferralAdminPage.tsx index f139a689..ad6ce1e8 100644 --- a/prediqo_web/src/features/referral/ReferralAdminPage.tsx +++ b/prediqo_web/src/features/referral/ReferralAdminPage.tsx @@ -30,6 +30,14 @@ import { useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { api } from '../../shared/services/api'; import { ScrollableTable } from '../../shared/components/ScrollableTable'; +import { rawToHuman } from '../wallet/utils/walletFormat'; + +const USDT_DECIMALS = 6; + +function formatUsdtRaw(raw: string | number | null | undefined): string { + const value = raw == null ? '0' : String(raw); + return `${rawToHuman(value, USDT_DECIMALS)} USDT`; +} type WalletTypeOption = { id: number; @@ -779,7 +787,11 @@ export function ReferralAdminPage() { {l.levelNumber} {l.commissionPercent}% - {l.monthlyMaxAmountRaw === '0' ? t('admin.referralAdmin.global') : l.monthlyMaxAmountRaw} + + {l.monthlyMaxAmountRaw === '0' + ? t('admin.referralAdmin.global') + : formatUsdtRaw(l.monthlyMaxAmountRaw)} + {l.monthlyMaxCount === 0 ? t('admin.referralAdmin.global') : l.monthlyMaxCount} @@ -873,7 +885,7 @@ export function ReferralAdminPage() { {t('admin.referralAdmin.walletTypeBalance', { label: p.label, - balance: p.availableBalance, + amount: formatUsdtRaw(p.availableBalance), })} ))} @@ -955,7 +967,7 @@ export function ReferralAdminPage() { {item.levelNumber} - {item.commissionAmount} + {formatUsdtRaw(item.commissionAmount)} {item.beneficiaryUserId.slice(0, 8)}… @@ -1013,12 +1025,16 @@ export function ReferralAdminPage() { - + {t('admin.referralAdmin.byCountry')} + + {t('admin.referralAdmin.byCountryHelper')} + {stats.usersByCountry.slice(0, 10).map((r) => ( - {r.country} + + {r.country === 'UNSPECIFIED' || r.country === 'Non renseigné' + ? t('admin.referralAdmin.countryUnspecified') + : r.country} + {r.count} ))} @@ -1050,6 +1073,9 @@ export function ReferralAdminPage() { {t('admin.referralAdmin.topReferrers')} + + {t('admin.referralAdmin.totalEarnedHelper')} +
@@ -1064,7 +1090,7 @@ export function ReferralAdminPage() { {r.referralCode ?? '—'} {r.directReferrals} - {r.totalEarned} + {formatUsdtRaw(r.totalEarned)} ))} diff --git a/prediqo_web/src/shared/i18n/locales/en.ts b/prediqo_web/src/shared/i18n/locales/en.ts index a230b981..50860eb3 100644 --- a/prediqo_web/src/shared/i18n/locales/en.ts +++ b/prediqo_web/src/shared/i18n/locales/en.ts @@ -1763,7 +1763,7 @@ export const en = { maxCountField: 'Max count / month (0=global)', addUpdateLevel: 'Add / update level', bonusPoolTitle: 'Bonus pool (treasury for referral payouts)', - walletTypeBalance: '{{label}}: {{balance}} raw units', + walletTypeBalance: '{{label}}: {{amount}}', walletTypeId: 'Wallet type ID', walletType: 'Wallet', amountDisplay: 'Amount (display)', @@ -1780,15 +1780,18 @@ export const en = { rejectReason: 'Rejection reason', statsTitle: 'Statistics', referredUsers: 'Referred users: {{count}}', - commissionsPaid: 'Commissions paid: {{raw}} raw', + commissionsPaid: 'Commissions paid (platform): {{amount}}', payoutEvents: 'Payout events: {{count}}', - pending: 'Pending: {{count}} ({{raw}} raw)', - review: 'Review: {{count}} ({{raw}} raw)', - byCountry: 'Signups by country of residence', + pending: 'Pending: {{count}} ({{amount}})', + review: 'Review: {{count}} ({{amount}})', + byCountry: 'Referred users by country of residence', + byCountryHelper: 'Only users who signed up with a referral code. “Not specified” = no country on profile.', + countryUnspecified: 'Not specified', byNationality: 'Signups by nationality', topReferrers: 'Top referrers', directReferrals: 'Direct referrals', - totalEarned: 'Total earned', + totalEarned: 'Total earned (this referrer)', + totalEarnedHelper: 'Sum of paid commissions credited to this referrer’s bonus wallet.', code: 'Code', commission: 'Commission', }, diff --git a/prediqo_web/src/shared/i18n/locales/fr.ts b/prediqo_web/src/shared/i18n/locales/fr.ts index 55ebc78f..bc13f177 100644 --- a/prediqo_web/src/shared/i18n/locales/fr.ts +++ b/prediqo_web/src/shared/i18n/locales/fr.ts @@ -1779,7 +1779,7 @@ export const fr = { maxCountField: 'Nombre max / mois (0=global)', addUpdateLevel: 'Ajouter / mettre à jour le niveau', bonusPoolTitle: 'Pool bonus (trésorerie pour paiements parrainage)', - walletTypeBalance: '{{label}} : {{balance}} unités brutes', + walletTypeBalance: '{{label}} : {{amount}}', walletTypeId: 'ID type wallet', walletType: 'Wallet', amountDisplay: 'Montant (affiché)', @@ -1796,15 +1796,19 @@ export const fr = { rejectReason: 'Motif de rejet', statsTitle: 'Statistiques', referredUsers: 'Utilisateurs parrainés : {{count}}', - commissionsPaid: 'Commissions payées : {{raw}} brut', + commissionsPaid: 'Commissions payées (plateforme) : {{amount}}', payoutEvents: 'Événements de paiement : {{count}}', - pending: 'En attente : {{count}} ({{raw}} brut)', - review: 'Revue : {{count}} ({{raw}} brut)', - byCountry: 'Inscriptions par pays de résidence', + pending: 'En attente : {{count}} ({{amount}})', + review: 'Revue : {{count}} ({{amount}})', + byCountry: 'Filleuls par pays de résidence', + byCountryHelper: + 'Uniquement les comptes inscrits avec un code parrain. « Non renseigné » = pas de pays sur le profil.', + countryUnspecified: 'Non renseigné', byNationality: 'Inscriptions par nationalité', topReferrers: 'Meilleurs parrains', directReferrals: 'Parrainages directs', - totalEarned: 'Total gagné', + totalEarned: 'Total gagné (ce parrain)', + totalEarnedHelper: 'Somme des commissions payées créditées sur le wallet bonus de ce parrain.', code: 'Code', commission: 'Commission', },