diff --git a/backend/src/repositories/transactionRepository.ts b/backend/src/repositories/transactionRepository.ts index a099f90c..b115f8a2 100644 --- a/backend/src/repositories/transactionRepository.ts +++ b/backend/src/repositories/transactionRepository.ts @@ -228,3 +228,40 @@ export async function mergeGameStakeBetStatus( [betId, JSON.stringify({ bet_status: betStatus })] ); } + +/** + * Attach candle_predict_bets.status onto game_stake metadata as bet_status + * so clients can label losses and show share consistently (even across pages). + */ +export async function enrichGameStakeBetStatus | null }>( + txs: T[] +): Promise { + const betIds = [ + ...new Set( + txs + .filter((t) => t.type === 'game_stake' && t.metadata?.bet_id) + .map((t) => String(t.metadata!.bet_id)) + ), + ]; + if (betIds.length === 0) return txs; + + const rows = await query>( + `SELECT id::text AS id, status + FROM candle_predict_bets + WHERE id = ANY($1::uuid[])`, + [betIds] + ); + const statusById = new Map(rows.map((r) => [r.id, r.status])); + + return txs.map((tx) => { + if (tx.type !== 'game_stake') return tx; + const betId = tx.metadata?.bet_id ? String(tx.metadata.bet_id) : ''; + if (!betId) return tx; + const status = statusById.get(betId); + if (status !== 'won' && status !== 'lost' && status !== 'refunded') return tx; + return { + ...tx, + metadata: { ...(tx.metadata ?? {}), bet_status: status }, + }; + }); +} diff --git a/backend/src/services/walletService.ts b/backend/src/services/walletService.ts index b57f149a..3fe6f400 100644 --- a/backend/src/services/walletService.ts +++ b/backend/src/services/walletService.ts @@ -113,7 +113,8 @@ export async function listUserTransactions( offset: number, category?: transactionRepository.TransactionActivityCategory ): Promise { - return transactionRepository.listByUserId(userId, limit, offset, category); + const items = await transactionRepository.listByUserId(userId, limit, offset, category); + return transactionRepository.enrichGameStakeBetStatus(items); } export async function listUserTransactionsPaginated( @@ -126,5 +127,10 @@ export async function listUserTransactionsPaginated( transactionRepository.listByUserId(userId, limit, offset, category), transactionRepository.countByUserId(userId, category), ]); - return { items, total, limit, offset }; + return { + items: await transactionRepository.enrichGameStakeBetStatus(items), + total, + limit, + offset, + }; } diff --git a/prediqo_mobile/lib/features/wallet/presentation/wallet_overview_screen.dart b/prediqo_mobile/lib/features/wallet/presentation/wallet_overview_screen.dart index 2e9eae99..e13392bb 100644 --- a/prediqo_mobile/lib/features/wallet/presentation/wallet_overview_screen.dart +++ b/prediqo_mobile/lib/features/wallet/presentation/wallet_overview_screen.dart @@ -1073,14 +1073,12 @@ class _PredictionTxTile extends StatelessWidget { stakeAccountKind: stakeAccountKind(tx), ) else if (tx.type == 'game_stake' && - tx.metadata?['bet_status']?.toString() != 'won' && - tx.metadata?['bet_status']?.toString() != 'refunded') + tx.metadata?['bet_status']?.toString() == 'lost') ShareResultIconButton( key: ValueKey('share-pred-${tx.id}'), betId: (tx.metadata!['bet_id']).toString(), - status: tx.metadata?['bet_status']?.toString() == 'lost' ? 'lost' : null, + status: 'lost', stakeAccountKind: stakeAccountKind(tx), - requireStatus: tx.metadata?['bet_status']?.toString() == 'lost', ), ], ], diff --git a/prediqo_mobile/lib/features/wallet/utils/transaction_display.dart b/prediqo_mobile/lib/features/wallet/utils/transaction_display.dart index a9d3ac39..cc9ad6e4 100644 --- a/prediqo_mobile/lib/features/wallet/utils/transaction_display.dart +++ b/prediqo_mobile/lib/features/wallet/utils/transaction_display.dart @@ -33,7 +33,9 @@ String transactionLabel(TransactionModel tx, AppStrings s) { ? s.txTypeTransferIn : s.txTypeTransferOut; case 'game_stake': - return s.txTypeGameStake; + return tx.metadata?['bet_status']?.toString() == 'lost' + ? s.txTypeGameLoss + : s.txTypeGameStake; case 'game_payout': return s.txTypeGamePayout; case 'game_refund': diff --git a/prediqo_mobile/lib/l10n/app_strings.dart b/prediqo_mobile/lib/l10n/app_strings.dart index c4b97178..92460587 100644 --- a/prediqo_mobile/lib/l10n/app_strings.dart +++ b/prediqo_mobile/lib/l10n/app_strings.dart @@ -213,6 +213,7 @@ class AppStrings { String get txTypeTransferIn => t('Transfer in', 'Transfert reçu'); String get txTypeTransferOut => t('Transfer out', 'Transfert envoyé'); String get txTypeGameStake => t('Stake', 'Mise'); + String get txTypeGameLoss => t('Prediction loss', 'Perte de prédiction'); String get txTypeGamePayout => t('Payout', 'Gain'); String get txTypeGameRefund => t('Refund', 'Remboursement'); String get txTypeBonus => t('Bonus', 'Bonus'); diff --git a/prediqo_web/src/features/wallet/components/TransactionList.tsx b/prediqo_web/src/features/wallet/components/TransactionList.tsx index bcb4320e..458baf10 100644 --- a/prediqo_web/src/features/wallet/components/TransactionList.tsx +++ b/prediqo_web/src/features/wallet/components/TransactionList.tsx @@ -37,10 +37,7 @@ type TransactionListProps = { emptyHint?: string; }; -function predictionShareMeta( - tx: WalletTransaction, - outcomeByBetId: Map -): { +function predictionShareMeta(tx: WalletTransaction): { betId: string; status?: 'won' | 'lost' | 'refunded'; stakeAccountKind?: string; @@ -57,29 +54,11 @@ function predictionShareMeta( if (tx.type === 'game_refund') { return { betId, status: 'refunded', stakeAccountKind }; } - // game_stake: prefer sharing from payout/refund rows when present on this page. - const siblingOutcome = outcomeByBetId.get(betId); - if (siblingOutcome) return null; - const metaStatus = tx.metadata?.bet_status; - if (metaStatus === 'won' || metaStatus === 'refunded') return null; - if (metaStatus === 'lost') { + // Losses only: stake rows for won/refunded bets share from the outcome row. + if (tx.metadata?.bet_status === 'lost') { return { betId, status: 'lost', stakeAccountKind }; } - // Legacy rows / pending: allow share; API returns the settled result (or 403 if still open). - return { betId, stakeAccountKind }; -} - -function buildPredictionOutcomeMap( - transactions: WalletTransaction[] -): Map { - const map = new Map(); - for (const tx of transactions) { - const betId = tx.metadata?.bet_id; - if (!betId) continue; - if (tx.type === 'game_payout') map.set(betId, 'won'); - else if (tx.type === 'game_refund') map.set(betId, 'refunded'); - } - return map; + return null; } function walletOpShareMeta( @@ -126,7 +105,6 @@ export function TransactionList({ const pageCount = Math.max(1, Math.ceil(total / pageSize)); const from = total === 0 ? 0 : page * pageSize + 1; const to = Math.min(total, (page + 1) * pageSize); - const outcomeByBetId = buildPredictionOutcomeMap(transactions); if (loading) { return ( @@ -184,7 +162,7 @@ export function TransactionList({ const credit = isCredit(tx); const [c1, c2] = coinColors(code); const human = rawToHuman(tx.amount, decimals); - const predictionShare = predictionShareMeta(tx, outcomeByBetId); + const predictionShare = predictionShareMeta(tx); const walletShare = walletOpShareMeta(tx); const shareButton = predictionShare ? (