After changing about commission dispatch and adding sharing card5
This commit is contained in:
parent
f08fe0ca8f
commit
b7df0da453
@ -1651,6 +1651,7 @@ async function payOutBetsAtMaturity(
|
||||
stake_account_kind: parseStakeAccountKind(bet.stake_account_kind),
|
||||
},
|
||||
});
|
||||
await transactionRepository.mergeGameStakeBetStatus(bet.id, 'refunded');
|
||||
queueUserAdminStatsRefresh(bet.user_id);
|
||||
continue;
|
||||
}
|
||||
@ -1686,9 +1687,11 @@ async function payOutBetsAtMaturity(
|
||||
stake_account_kind: parseStakeAccountKind(bet.stake_account_kind),
|
||||
},
|
||||
});
|
||||
await transactionRepository.mergeGameStakeBetStatus(bet.id, 'won');
|
||||
queuePredictionReferralCommission(bet, 'won');
|
||||
} else {
|
||||
await repo.updateBetOutcome(bet.id, 'lost', 0n);
|
||||
await transactionRepository.mergeGameStakeBetStatus(bet.id, 'lost');
|
||||
if (countInDaily) housePnlRaw += stakeRaw;
|
||||
void predictionAuditService.logPredictionBetSettled({
|
||||
userId: bet.user_id,
|
||||
|
||||
@ -213,3 +213,18 @@ export async function countByUserId(userId: string, category?: TransactionActivi
|
||||
);
|
||||
return parseInt(rows[0]?.count ?? '0', 10);
|
||||
}
|
||||
|
||||
/** Mirror settled bet outcome onto the original game_stake row for Wallet Activity share UX. */
|
||||
export async function mergeGameStakeBetStatus(
|
||||
betId: string,
|
||||
betStatus: 'won' | 'lost' | 'refunded'
|
||||
): Promise<void> {
|
||||
await query(
|
||||
`UPDATE transactions
|
||||
SET metadata = COALESCE(metadata, '{}'::jsonb) || $2::jsonb,
|
||||
updated_at = NOW()
|
||||
WHERE type = 'game_stake'
|
||||
AND metadata->>'bet_id' = $1`,
|
||||
[betId, JSON.stringify({ bet_status: betStatus })]
|
||||
);
|
||||
}
|
||||
|
||||
@ -262,6 +262,7 @@ export async function processMultiBetsAtMaturity(
|
||||
stake_account_kind: parseStakeAccountKind(bet.stake_account_kind),
|
||||
},
|
||||
});
|
||||
await transactionRepository.mergeGameStakeBetStatus(bet.id, 'refunded');
|
||||
queueUserAdminStatsRefresh(bet.user_id);
|
||||
await notifyMultiBetUpdate(bet.user_id, bet.interval_minutes, null);
|
||||
continue;
|
||||
@ -270,6 +271,7 @@ export async function processMultiBetsAtMaturity(
|
||||
const actual = outcome.winner;
|
||||
if (!expected || actual !== expected) {
|
||||
await repo.updateBetOutcome(bet.id, 'lost', 0n);
|
||||
await transactionRepository.mergeGameStakeBetStatus(bet.id, 'lost');
|
||||
if (countInDaily) housePnlRaw += stakeRaw;
|
||||
void predictionAuditService.logPredictionBetSettled({
|
||||
userId: bet.user_id,
|
||||
@ -334,6 +336,7 @@ export async function processMultiBetsAtMaturity(
|
||||
stake_account_kind: parseStakeAccountKind(bet.stake_account_kind),
|
||||
},
|
||||
});
|
||||
await transactionRepository.mergeGameStakeBetStatus(bet.id, 'won');
|
||||
queuePredictionReferralCommission(bet, 'won');
|
||||
queueUserAdminStatsRefresh(bet.user_id);
|
||||
await notifyMultiBetUpdate(bet.user_id, bet.interval_minutes, null);
|
||||
|
||||
@ -23,6 +23,7 @@ Future<void> openShareResultDialog(
|
||||
return showDialog<void>(
|
||||
context: context,
|
||||
builder: (ctx) => _ShareResultDialog(
|
||||
key: ValueKey(transactionId ?? betId),
|
||||
betId: betId,
|
||||
transactionId: transactionId,
|
||||
),
|
||||
@ -30,7 +31,7 @@ Future<void> openShareResultDialog(
|
||||
}
|
||||
|
||||
class _ShareResultDialog extends StatefulWidget {
|
||||
const _ShareResultDialog({this.betId, this.transactionId});
|
||||
const _ShareResultDialog({super.key, this.betId, this.transactionId});
|
||||
final String? betId;
|
||||
final String? transactionId;
|
||||
|
||||
@ -55,13 +56,20 @@ class _ShareResultDialogState extends State<_ShareResultDialog> {
|
||||
setState(() {
|
||||
_loading = true;
|
||||
_error = null;
|
||||
_payload = null;
|
||||
});
|
||||
try {
|
||||
final repo = sl<ShareCardRepository>();
|
||||
final payload = widget.transactionId != null
|
||||
? await repo.getTransactionShareCard(widget.transactionId!)
|
||||
: await repo.getBetShareCard(widget.betId!);
|
||||
final requestedBetId = widget.betId;
|
||||
final requestedTxId = widget.transactionId;
|
||||
final payload = requestedTxId != null
|
||||
? await repo.getTransactionShareCard(requestedTxId)
|
||||
: await repo.getBetShareCard(requestedBetId!);
|
||||
if (!mounted) return;
|
||||
// Ignore stale responses if the dialog target changed (shouldn't, but safe).
|
||||
if (requestedTxId != widget.transactionId || requestedBetId != widget.betId) {
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_payload = payload;
|
||||
_loading = false;
|
||||
@ -82,6 +90,8 @@ class _ShareResultDialogState extends State<_ShareResultDialog> {
|
||||
if (boundary == null || payload == null) return;
|
||||
setState(() => _busy = true);
|
||||
try {
|
||||
// Wait for the latest frame so we never export a previous card paint.
|
||||
await WidgetsBinding.instance.endOfFrame;
|
||||
final image = await boundary.toImage(pixelRatio: 3);
|
||||
final bytes = await image.toByteData(format: ui.ImageByteFormat.png);
|
||||
if (bytes == null) throw Exception('export failed');
|
||||
@ -140,7 +150,13 @@ class _ShareResultDialogState extends State<_ShareResultDialog> {
|
||||
if (_payload != null)
|
||||
RepaintBoundary(
|
||||
key: _cardKey,
|
||||
child: ShareResultCard(payload: _payload!),
|
||||
child: ShareResultCard(
|
||||
key: ValueKey(
|
||||
'${_payload!.isWallet ? _payload!.transactionId : _payload!.betId}-'
|
||||
'${_payload!.resultSignedDisplay ?? _payload!.amountSignedDisplay ?? ''}',
|
||||
),
|
||||
payload: _payload!,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
@ -1057,17 +1057,32 @@ class _PredictionTxTile extends StatelessWidget {
|
||||
color: credit ? AppColors.success : AppColors.danger,
|
||||
),
|
||||
),
|
||||
if ((tx.metadata?['bet_id'] ?? '').toString().isNotEmpty)
|
||||
ShareResultIconButton(
|
||||
betId: (tx.metadata!['bet_id']).toString(),
|
||||
status: tx.type == 'game_payout'
|
||||
? 'won'
|
||||
: tx.type == 'game_refund'
|
||||
? 'refunded'
|
||||
: null,
|
||||
stakeAccountKind: stakeAccountKind(tx),
|
||||
requireStatus: tx.type == 'game_payout' || tx.type == 'game_refund',
|
||||
),
|
||||
if ((tx.metadata?['bet_id'] ?? '').toString().isNotEmpty) ...[
|
||||
if (tx.type == 'game_payout')
|
||||
ShareResultIconButton(
|
||||
key: ValueKey('share-pred-${tx.id}'),
|
||||
betId: (tx.metadata!['bet_id']).toString(),
|
||||
status: 'won',
|
||||
stakeAccountKind: stakeAccountKind(tx),
|
||||
)
|
||||
else if (tx.type == 'game_refund')
|
||||
ShareResultIconButton(
|
||||
key: ValueKey('share-pred-${tx.id}'),
|
||||
betId: (tx.metadata!['bet_id']).toString(),
|
||||
status: 'refunded',
|
||||
stakeAccountKind: stakeAccountKind(tx),
|
||||
)
|
||||
else if (tx.type == 'game_stake' &&
|
||||
tx.metadata?['bet_status']?.toString() != 'won' &&
|
||||
tx.metadata?['bet_status']?.toString() != 'refunded')
|
||||
ShareResultIconButton(
|
||||
key: ValueKey('share-pred-${tx.id}'),
|
||||
betId: (tx.metadata!['bet_id']).toString(),
|
||||
status: tx.metadata?['bet_status']?.toString() == 'lost' ? 'lost' : null,
|
||||
stakeAccountKind: stakeAccountKind(tx),
|
||||
requireStatus: tx.metadata?['bet_status']?.toString() == 'lost',
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
@ -31,6 +31,8 @@ export function ShareResultButton(props: Props) {
|
||||
const { t } = useTranslation();
|
||||
const configQ = useShareCardConfig();
|
||||
const [open, setOpen] = useState(false);
|
||||
/** Bumps on each open so the dialog remounts and never reuses a previous card. */
|
||||
const [session, setSession] = useState(0);
|
||||
const size = props.size ?? 'small';
|
||||
|
||||
const eligible =
|
||||
@ -46,6 +48,9 @@ export function ShareResultButton(props: Props) {
|
||||
|
||||
if (!eligible) return null;
|
||||
|
||||
const targetKey =
|
||||
props.kind === 'wallet' ? props.transactionId : props.betId;
|
||||
|
||||
return (
|
||||
<>
|
||||
<Tooltip title={t('shareCard.share')}>
|
||||
@ -54,6 +59,7 @@ export function ShareResultButton(props: Props) {
|
||||
color="primary"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
setSession((n) => n + 1);
|
||||
setOpen(true);
|
||||
}}
|
||||
aria-label={t('shareCard.share')}
|
||||
@ -62,6 +68,7 @@ export function ShareResultButton(props: Props) {
|
||||
</IconButton>
|
||||
</Tooltip>
|
||||
<ShareResultDialog
|
||||
key={`${targetKey}-${session}`}
|
||||
open={open}
|
||||
betId={props.kind === 'wallet' ? null : props.betId}
|
||||
transactionId={props.kind === 'wallet' ? props.transactionId : null}
|
||||
|
||||
@ -141,17 +141,29 @@ export function ShareResultDialog({ open, betId, transactionId, onClose }: Props
|
||||
setHint(null);
|
||||
setCachedExport(null);
|
||||
setMenuAnchor(null);
|
||||
setLoading(false);
|
||||
setBusy(false);
|
||||
return;
|
||||
}
|
||||
let cancelled = false;
|
||||
// Always drop the previous card before loading the next target.
|
||||
setPayload(null);
|
||||
setCachedExport(null);
|
||||
setHint(null);
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
const targetId = transactionId ?? betId!;
|
||||
const loader = transactionId
|
||||
? fetchWalletShareCardPayload(transactionId)
|
||||
: fetchShareCardPayload(betId!);
|
||||
void loader
|
||||
.then((data) => {
|
||||
if (!cancelled) setPayload(data);
|
||||
if (cancelled) return;
|
||||
// Guard against out-of-order responses if the target changed mid-flight.
|
||||
const dataId =
|
||||
data.kind === 'wallet' ? data.transactionId : data.betId;
|
||||
if (dataId !== targetId) return;
|
||||
setPayload(data);
|
||||
})
|
||||
.catch((e: unknown) => {
|
||||
if (!cancelled) {
|
||||
@ -167,6 +179,10 @@ export function ShareResultDialog({ open, betId, transactionId, onClose }: Props
|
||||
};
|
||||
}, [open, betId, transactionId, t]);
|
||||
|
||||
useEffect(() => {
|
||||
setCachedExport(null);
|
||||
}, [payload]);
|
||||
|
||||
const buildExport = async () => {
|
||||
if (!cardRef.current || !payload) return null;
|
||||
if (cachedExport) return cachedExport;
|
||||
@ -275,12 +291,20 @@ export function ShareResultDialog({ open, betId, transactionId, onClose }: Props
|
||||
<CircularProgress size={32} />
|
||||
</Box>
|
||||
)}
|
||||
{error && <Alert severity="error">{error}</Alert>}
|
||||
{payload && (
|
||||
{error && !loading && <Alert severity="error">{error}</Alert>}
|
||||
{!loading && payload ? (
|
||||
<Stack sx={{ alignItems: 'center' }}>
|
||||
<ShareResultCard payload={payload} cardRef={cardRef} />
|
||||
<ShareResultCard
|
||||
key={
|
||||
payload.kind === 'wallet'
|
||||
? `${payload.transactionId}-${payload.amountSignedDisplay}`
|
||||
: `${payload.betId}-${payload.resultSignedDisplay}-${payload.status}`
|
||||
}
|
||||
payload={payload}
|
||||
cardRef={cardRef}
|
||||
/>
|
||||
</Stack>
|
||||
)}
|
||||
) : null}
|
||||
{hint && (
|
||||
<Alert severity="info" sx={{ mt: 2 }}>
|
||||
{hint}
|
||||
|
||||
@ -37,7 +37,10 @@ type TransactionListProps = {
|
||||
emptyHint?: string;
|
||||
};
|
||||
|
||||
function predictionShareMeta(tx: WalletTransaction): {
|
||||
function predictionShareMeta(
|
||||
tx: WalletTransaction,
|
||||
outcomeByBetId: Map<string, 'won' | 'refunded'>
|
||||
): {
|
||||
betId: string;
|
||||
status?: 'won' | 'lost' | 'refunded';
|
||||
stakeAccountKind?: string;
|
||||
@ -54,9 +57,31 @@ function predictionShareMeta(tx: WalletTransaction): {
|
||||
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') {
|
||||
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<string, 'won' | 'refunded'> {
|
||||
const map = new Map<string, 'won' | 'refunded'>();
|
||||
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;
|
||||
}
|
||||
|
||||
function walletOpShareMeta(
|
||||
tx: WalletTransaction
|
||||
): {
|
||||
@ -101,6 +126,7 @@ 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 (
|
||||
@ -158,7 +184,7 @@ export function TransactionList({
|
||||
const credit = isCredit(tx);
|
||||
const [c1, c2] = coinColors(code);
|
||||
const human = rawToHuman(tx.amount, decimals);
|
||||
const predictionShare = predictionShareMeta(tx);
|
||||
const predictionShare = predictionShareMeta(tx, outcomeByBetId);
|
||||
const walletShare = walletOpShareMeta(tx);
|
||||
const shareButton = predictionShare ? (
|
||||
<ShareResultButton
|
||||
|
||||
@ -62,6 +62,8 @@ export type WalletTransaction = {
|
||||
reference?: string;
|
||||
bet_id?: string;
|
||||
stake_account_kind?: 'main' | 'bonus' | 'test';
|
||||
/** Settled prediction outcome mirrored onto game_stake rows. */
|
||||
bet_status?: 'won' | 'lost' | 'refunded';
|
||||
outcome?: string;
|
||||
round_id?: string;
|
||||
candle_open_ms?: number;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user