feat(my-bets): personal bet journal with CLV tracking
Adds a manual bet-tracking journal that turns the analyzer into an actual bet tracker. Users record wagers; the journal auto-grades them when event results land and computes per-bet Closing-Line-Value against the latest pre-match snapshot — the strongest long-run indicator of betting skill. Domain: - PlacedBet entity (reuses Bet vocabulary for Scope/Type/Side/Value/Rate) with stake, placed-at, outcome, and notes. Derived GrossReturn / NetProfit. - BetOutcome enum (Pending / Won / Lost / Void). - BetOutcomeResolver: pure function grading any Match-scope bet against an EventResult. Handles 1X2, draws, handicap (incl. push), and totals. Period-scope bets stay manual since EventResult only carries full-time. Application: - IPlacedBetRepository abstraction. - ClosingLineValueCalculator: pure CLV math (implied-probability delta) + snapshot-matching predicate by Scope/Type/Side/Value. - BetJournalReport + BetJournalStats records. - Four use cases: Record / ResolvePending / BuildReport / Delete. - New ISnapshotRepository.GetLatestPreMatchAsync pushes the closing-line pick into a single SQLite query rather than materialising the 30-day window in memory per event. - ROI turnover excludes Void stakes — pushes are not real turnover and including them would dilute the user's edge. Infrastructure: - PlacedBetEntity / Configuration / Repository / Mapping helpers. - 20260516 migration adding the PlacedBets table with EventCode and Outcome indices. Intentionally NO foreign key to Events — the journal is user data and must survive snapshot-retention pruning. Covered by an explicit round-trip test. UI: - Pages/MyBets/Journal.razor: hero header, 4-card KPI strip (ROI / strike rate / avg CLV / net profit, tinted by tone), inline add-bet form with the same invariants as the Bet record, drill-down table with per-row outcome pills, CLV percentage-points column, P&L, notes underline, and inline-confirm delete. RU + EN i18n. - Nav entry under Analysis. Tests: +55 across Domain / Application / Infrastructure (resolver math including handicap push and total push boundaries, PlacedBet invariants and derived properties, CLV math + null-handling, four use cases under NSubstitute, EF round-trip including survives-event-deletion). All 379 tests pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
using Marathon.Application.Storage;
|
||||
using Marathon.Domain.Entities;
|
||||
using Marathon.Domain.Enums;
|
||||
using Marathon.Domain.ValueObjects;
|
||||
|
||||
namespace Marathon.Application.Abstractions;
|
||||
|
||||
/// <summary>
|
||||
/// Repository for <see cref="PlacedBet"/> domain entities — the user-tracked
|
||||
/// betting journal.
|
||||
/// </summary>
|
||||
public interface IPlacedBetRepository : IRepository<Guid, PlacedBet>
|
||||
{
|
||||
/// <summary>
|
||||
/// Bets matching <paramref name="outcome"/>. Used by the resolver use case
|
||||
/// to scan only <see cref="BetOutcome.Pending"/> rows on each pass.
|
||||
/// </summary>
|
||||
Task<IReadOnlyList<PlacedBet>> ListByOutcomeAsync(BetOutcome outcome, CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// Bets whose <see cref="PlacedBet.PlacedAt"/> falls within
|
||||
/// <paramref name="range"/>. Used by the journal page when the user filters
|
||||
/// by date.
|
||||
/// </summary>
|
||||
Task<IReadOnlyList<PlacedBet>> ListByDateRangeAsync(DateRange range, CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// Every bet recorded against <paramref name="eventId"/>. Used by the event
|
||||
/// detail page to show "you have N bets on this match".
|
||||
/// </summary>
|
||||
Task<IReadOnlyList<PlacedBet>> ListByEventAsync(EventId eventId, CancellationToken ct = default);
|
||||
}
|
||||
@@ -36,4 +36,19 @@ public interface ISnapshotRepository
|
||||
Task AddAsync(OddsSnapshot entity, CancellationToken ct = default);
|
||||
|
||||
Task SaveChangesAsync(CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// Returns the latest pre-match snapshot for <paramref name="eventId"/> whose
|
||||
/// <see cref="OddsSnapshot.CapturedAt"/> is at or before
|
||||
/// <paramref name="atOrBefore"/>, or <c>null</c> if none exists. Used by the
|
||||
/// bet-journal use case as the "closing line" reference for CLV.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Pushes the ORDER BY + LIMIT 1 down to SQLite so we do not materialise
|
||||
/// every snapshot in the 30-day pre-match window just to pick one.
|
||||
/// </remarks>
|
||||
Task<OddsSnapshot?> GetLatestPreMatchAsync(
|
||||
EventId eventId,
|
||||
DateTimeOffset atOrBefore,
|
||||
CancellationToken ct = default);
|
||||
}
|
||||
|
||||
@@ -32,6 +32,11 @@ public static class ApplicationModule
|
||||
services.AddScoped<DetectAnomaliesUseCase>();
|
||||
services.AddScoped<EvaluateAnomalyOutcomesUseCase>();
|
||||
|
||||
services.AddScoped<RecordPlacedBetUseCase>();
|
||||
services.AddScoped<ResolvePendingBetsUseCase>();
|
||||
services.AddScoped<BuildBetJournalReportUseCase>();
|
||||
services.AddScoped<DeletePlacedBetUseCase>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
using Marathon.Domain.Entities;
|
||||
using Marathon.Domain.Enums;
|
||||
|
||||
namespace Marathon.Application.Betting;
|
||||
|
||||
/// <summary>
|
||||
/// Aggregate report on the user's bet-tracking journal — totals, P&L, and
|
||||
/// per-bet CLV. Consumed by the Journal page; built by
|
||||
/// <see cref="UseCases.BuildBetJournalReportUseCase"/>.
|
||||
/// </summary>
|
||||
/// <param name="Stats">Roll-up of stake / profit / hit rate / CLV across all bets in scope.</param>
|
||||
/// <param name="Bets">
|
||||
/// Every bet paired with its computed CLV (null when no closing snapshot was
|
||||
/// available). Ordered most-recent <see cref="PlacedBet.PlacedAt"/> first.
|
||||
/// </param>
|
||||
public sealed record BetJournalReport(
|
||||
BetJournalStats Stats,
|
||||
IReadOnlyList<BetJournalRow> Bets);
|
||||
|
||||
/// <summary>
|
||||
/// One row in the journal — a domain <see cref="PlacedBet"/> plus the CLV
|
||||
/// computed against the closing pre-match snapshot.
|
||||
/// </summary>
|
||||
/// <param name="Bet">The domain bet exactly as persisted.</param>
|
||||
/// <param name="ClvProbabilityDelta">
|
||||
/// Closing-line value as an implied-probability delta in roughly [-1, 1].
|
||||
/// Positive means the user took a better price than the closing line; null
|
||||
/// when no matching bet existed in the closing snapshot.
|
||||
/// </param>
|
||||
public sealed record BetJournalRow(
|
||||
PlacedBet Bet,
|
||||
decimal? ClvProbabilityDelta);
|
||||
|
||||
/// <summary>
|
||||
/// Aggregate statistics across a set of <see cref="PlacedBet"/>.
|
||||
/// All money values share the user's currency — the domain does not encode one.
|
||||
/// </summary>
|
||||
/// <param name="TotalBets">Every bet in scope, regardless of outcome.</param>
|
||||
/// <param name="PendingCount">Bets still awaiting settlement.</param>
|
||||
/// <param name="WonCount">Settled wins.</param>
|
||||
/// <param name="LostCount">Settled losses.</param>
|
||||
/// <param name="VoidCount">Settled pushes / void grades.</param>
|
||||
/// <param name="TotalStaked">
|
||||
/// Turnover that contributes to ROI: sum of <see cref="PlacedBet.Stake"/> across
|
||||
/// <b>Won and Lost</b> bets only. Void (push) and Pending bets are excluded — a
|
||||
/// returned stake is not real turnover and counting it would dilute ROI.
|
||||
/// </param>
|
||||
/// <param name="TotalReturned">
|
||||
/// Sum of <see cref="PlacedBet.GrossReturn"/> across the same Won + Lost subset
|
||||
/// that feeds <see cref="TotalStaked"/>.
|
||||
/// </param>
|
||||
/// <param name="NetProfit"><c>TotalReturned − TotalStaked</c>.</param>
|
||||
/// <param name="RoiPercent">
|
||||
/// <c>NetProfit / TotalStaked × 100</c>. Null when no bets have resolved yet.
|
||||
/// </param>
|
||||
/// <param name="StrikeRatePercent">
|
||||
/// <c>WonCount / (WonCount + LostCount) × 100</c> — excludes voids and pendings.
|
||||
/// Null when no settled win/loss exists yet.
|
||||
/// </param>
|
||||
/// <param name="AverageClvProbabilityDelta">
|
||||
/// Mean CLV across bets where CLV was computable. Null when no comparable
|
||||
/// closing snapshot was available for any bet.
|
||||
/// </param>
|
||||
public sealed record BetJournalStats(
|
||||
int TotalBets,
|
||||
int PendingCount,
|
||||
int WonCount,
|
||||
int LostCount,
|
||||
int VoidCount,
|
||||
decimal TotalStaked,
|
||||
decimal TotalReturned,
|
||||
decimal NetProfit,
|
||||
decimal? RoiPercent,
|
||||
decimal? StrikeRatePercent,
|
||||
decimal? AverageClvProbabilityDelta)
|
||||
{
|
||||
/// <summary>Convenience: WonCount + LostCount + VoidCount.</summary>
|
||||
public int ResolvedCount => WonCount + LostCount + VoidCount;
|
||||
|
||||
public static BetJournalStats Empty { get; } = new(
|
||||
TotalBets: 0,
|
||||
PendingCount: 0,
|
||||
WonCount: 0,
|
||||
LostCount: 0,
|
||||
VoidCount: 0,
|
||||
TotalStaked: 0m,
|
||||
TotalReturned: 0m,
|
||||
NetProfit: 0m,
|
||||
RoiPercent: null,
|
||||
StrikeRatePercent: null,
|
||||
AverageClvProbabilityDelta: null);
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
using Marathon.Domain.Entities;
|
||||
using Marathon.Domain.ValueObjects;
|
||||
|
||||
namespace Marathon.Application.Betting;
|
||||
|
||||
/// <summary>
|
||||
/// Pure helper that computes Closing Line Value (CLV) for a placed bet.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// CLV measures how much better (or worse) the rate the user took was compared
|
||||
/// with the bookmaker's last pre-match price on the same selection. It is the
|
||||
/// single best long-run indicator of betting skill — positive CLV correlates
|
||||
/// with positive expected value regardless of any individual bet's outcome.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Formula (implied-probability delta):
|
||||
/// <list type="bullet">
|
||||
/// <item>Taken implied probability: <c>p_t = 1 / takenRate</c></item>
|
||||
/// <item>Closing implied probability: <c>p_c = 1 / closeRate</c></item>
|
||||
/// <item><c>CLV = p_c − p_t</c></item>
|
||||
/// </list>
|
||||
/// Positive CLV means the closing price implied higher probability for the
|
||||
/// selection than the price the user took — i.e. the line moved in the user's
|
||||
/// favour after they placed the bet.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Returns <c>null</c> when no matching bet (same Scope / Type / Side / Value)
|
||||
/// can be found in the closing snapshot — typically because the market closed
|
||||
/// before the bookmaker exposed a comparable line, or the snapshot store has
|
||||
/// gaps. UI consumers must distinguish "no data" from "0% CLV".
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public static class ClosingLineValueCalculator
|
||||
{
|
||||
/// <summary>
|
||||
/// Computes CLV (implied-probability delta) given the rate the user took
|
||||
/// and the rate present in the closing pre-match snapshot for the same
|
||||
/// selection. Both must be positive — invariants on <see cref="OddsRate"/>
|
||||
/// already guarantee this for inputs sourced from the domain.
|
||||
/// </summary>
|
||||
public static decimal Compute(decimal takenRate, decimal closingRate)
|
||||
{
|
||||
if (takenRate <= 0m)
|
||||
throw new ArgumentOutOfRangeException(nameof(takenRate), takenRate, "Must be positive.");
|
||||
if (closingRate <= 0m)
|
||||
throw new ArgumentOutOfRangeException(nameof(closingRate), closingRate, "Must be positive.");
|
||||
|
||||
var takenProb = 1m / takenRate;
|
||||
var closingProb = 1m / closingRate;
|
||||
|
||||
// Round to 6 decimals — beyond that is noise from the round-trip.
|
||||
return Math.Round(closingProb - takenProb, 6);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convenience overload: finds the matching <see cref="Bet"/> in
|
||||
/// <paramref name="closingSnapshot"/> by Scope / Type / Side / Value, then
|
||||
/// computes CLV against <paramref name="takenRate"/>. Returns <c>null</c>
|
||||
/// when no comparable bet is present.
|
||||
/// </summary>
|
||||
public static decimal? TryCompute(
|
||||
decimal takenRate,
|
||||
Bet placedSelection,
|
||||
OddsSnapshot? closingSnapshot)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(placedSelection);
|
||||
if (closingSnapshot is null) return null;
|
||||
|
||||
var match = closingSnapshot.Bets.FirstOrDefault(b =>
|
||||
b.Scope.Equals(placedSelection.Scope) &&
|
||||
b.Type == placedSelection.Type &&
|
||||
b.Side == placedSelection.Side &&
|
||||
NullableValuesEqual(b.Value, placedSelection.Value));
|
||||
|
||||
return match is null ? null : Compute(takenRate, match.Rate.Value);
|
||||
}
|
||||
|
||||
private static bool NullableValuesEqual(OddsValue? a, OddsValue? b)
|
||||
{
|
||||
if (a is null && b is null) return true;
|
||||
if (a is null || b is null) return false;
|
||||
return a.Value == b.Value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
using Marathon.Application.Abstractions;
|
||||
using Marathon.Application.Betting;
|
||||
using Marathon.Domain.Entities;
|
||||
using Marathon.Domain.Enums;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using DomainEventId = Marathon.Domain.ValueObjects.EventId;
|
||||
|
||||
namespace Marathon.Application.UseCases;
|
||||
|
||||
/// <summary>
|
||||
/// Builds a <see cref="BetJournalReport"/>: every persisted bet paired with its
|
||||
/// Closing-Line-Value, plus aggregate <see cref="BetJournalStats"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Closing-line lookup: for each distinct event in the journal, this use case
|
||||
/// queries pre-match snapshots within a window that ends at the event's
|
||||
/// <see cref="Event.ScheduledAt"/> and picks the latest snapshot whose
|
||||
/// <see cref="OddsSnapshot.CapturedAt"/> is still before kickoff. That snapshot
|
||||
/// is the "close" for CLV purposes.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// If the snapshot store has nothing within the lookback window, the bet
|
||||
/// receives a null CLV. Stats then exclude it from the average.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public sealed class BuildBetJournalReportUseCase
|
||||
{
|
||||
private readonly IPlacedBetRepository _bets;
|
||||
private readonly IEventRepository _events;
|
||||
private readonly ISnapshotRepository _snapshots;
|
||||
private readonly ILogger<BuildBetJournalReportUseCase> _logger;
|
||||
|
||||
public BuildBetJournalReportUseCase(
|
||||
IPlacedBetRepository bets,
|
||||
IEventRepository events,
|
||||
ISnapshotRepository snapshots,
|
||||
ILogger<BuildBetJournalReportUseCase> logger)
|
||||
{
|
||||
_bets = bets ?? throw new ArgumentNullException(nameof(bets));
|
||||
_events = events ?? throw new ArgumentNullException(nameof(events));
|
||||
_snapshots = snapshots ?? throw new ArgumentNullException(nameof(snapshots));
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
|
||||
public async Task<BetJournalReport> ExecuteAsync(CancellationToken ct = default)
|
||||
{
|
||||
var bets = await _bets.ListAsync(ct).ConfigureAwait(false);
|
||||
if (bets.Count == 0)
|
||||
{
|
||||
_logger.LogInformation("BuildBetJournalReportUseCase: no bets — empty report");
|
||||
return new BetJournalReport(BetJournalStats.Empty, Array.Empty<BetJournalRow>());
|
||||
}
|
||||
|
||||
var distinctEventIds = bets.Select(b => b.EventId).Distinct().ToList();
|
||||
|
||||
// Resolve closing snapshot per event using a single-row repo call —
|
||||
// pushes the ORDER BY / LIMIT 1 down to SQLite rather than materialising
|
||||
// every snapshot in a 30-day window.
|
||||
var closingByEvent = new Dictionary<DomainEventId, OddsSnapshot?>(distinctEventIds.Count);
|
||||
foreach (var eventId in distinctEventIds)
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
|
||||
var ev = await _events.GetAsync(eventId, ct).ConfigureAwait(false);
|
||||
if (ev is null)
|
||||
{
|
||||
closingByEvent[eventId] = null;
|
||||
continue;
|
||||
}
|
||||
|
||||
var closing = await _snapshots
|
||||
.GetLatestPreMatchAsync(eventId, ev.ScheduledAt, ct)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
closingByEvent[eventId] = closing;
|
||||
}
|
||||
|
||||
var rows = new List<BetJournalRow>(bets.Count);
|
||||
foreach (var bet in bets)
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
closingByEvent.TryGetValue(bet.EventId, out var closing);
|
||||
|
||||
var clv = ClosingLineValueCalculator.TryCompute(
|
||||
takenRate: bet.Selection.Rate.Value,
|
||||
placedSelection: bet.Selection,
|
||||
closingSnapshot: closing);
|
||||
|
||||
rows.Add(new BetJournalRow(bet, clv));
|
||||
}
|
||||
|
||||
rows.Sort((a, b) => b.Bet.PlacedAt.CompareTo(a.Bet.PlacedAt));
|
||||
|
||||
var stats = ComputeStats(rows);
|
||||
|
||||
_logger.LogInformation(
|
||||
"BuildBetJournalReportUseCase: report built — {Total} bets, {Resolved} resolved, ROI={Roi:0.##}%",
|
||||
stats.TotalBets, stats.ResolvedCount, stats.RoiPercent ?? 0m);
|
||||
|
||||
return new BetJournalReport(stats, rows);
|
||||
}
|
||||
|
||||
private static BetJournalStats ComputeStats(IReadOnlyList<BetJournalRow> rows)
|
||||
{
|
||||
if (rows.Count == 0) return BetJournalStats.Empty;
|
||||
|
||||
var pending = 0;
|
||||
var won = 0;
|
||||
var lost = 0;
|
||||
var voided = 0;
|
||||
// Industry-standard ROI excludes pushes from turnover — staking on a Void
|
||||
// bet returns the stake and is functionally a no-op, so counting it as
|
||||
// turnover dilutes the ROI denominator and understates the user's edge.
|
||||
// Only Won + Lost contribute to TotalStaked / TotalReturned.
|
||||
var totalStaked = 0m;
|
||||
var totalReturned = 0m;
|
||||
decimal clvSum = 0m;
|
||||
var clvCount = 0;
|
||||
|
||||
foreach (var row in rows)
|
||||
{
|
||||
switch (row.Bet.Outcome)
|
||||
{
|
||||
case BetOutcome.Pending: pending++; break;
|
||||
case BetOutcome.Won: won++; break;
|
||||
case BetOutcome.Lost: lost++; break;
|
||||
case BetOutcome.Void: voided++; break;
|
||||
}
|
||||
|
||||
if (row.Bet.Outcome is BetOutcome.Won or BetOutcome.Lost)
|
||||
{
|
||||
totalStaked += row.Bet.Stake;
|
||||
totalReturned += row.Bet.GrossReturn ?? 0m;
|
||||
}
|
||||
|
||||
if (row.ClvProbabilityDelta is { } clv)
|
||||
{
|
||||
clvSum += clv;
|
||||
clvCount++;
|
||||
}
|
||||
}
|
||||
|
||||
var netProfit = totalReturned - totalStaked;
|
||||
var winLoss = won + lost;
|
||||
|
||||
decimal? roi = totalStaked > 0m
|
||||
? Math.Round((netProfit / totalStaked) * 100m, 2)
|
||||
: null;
|
||||
decimal? strikeRate = winLoss > 0
|
||||
? Math.Round(((decimal)won / winLoss) * 100m, 2)
|
||||
: null;
|
||||
// CLV inputs are already 6-decimal-rounded by ClosingLineValueCalculator;
|
||||
// round the mean only at the display boundary to avoid compounding bias.
|
||||
decimal? avgClv = clvCount > 0
|
||||
? clvSum / clvCount
|
||||
: null;
|
||||
|
||||
return new BetJournalStats(
|
||||
TotalBets: rows.Count,
|
||||
PendingCount: pending,
|
||||
WonCount: won,
|
||||
LostCount: lost,
|
||||
VoidCount: voided,
|
||||
TotalStaked: totalStaked,
|
||||
TotalReturned: totalReturned,
|
||||
NetProfit: netProfit,
|
||||
RoiPercent: roi,
|
||||
StrikeRatePercent: strikeRate,
|
||||
AverageClvProbabilityDelta: avgClv);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using Marathon.Application.Abstractions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Marathon.Application.UseCases;
|
||||
|
||||
/// <summary>
|
||||
/// Removes a <see cref="Marathon.Domain.Entities.PlacedBet"/> from the journal
|
||||
/// by its identifier. Silent no-op when the id does not exist.
|
||||
/// </summary>
|
||||
public sealed class DeletePlacedBetUseCase
|
||||
{
|
||||
private readonly IPlacedBetRepository _bets;
|
||||
private readonly ILogger<DeletePlacedBetUseCase> _logger;
|
||||
|
||||
public DeletePlacedBetUseCase(
|
||||
IPlacedBetRepository bets,
|
||||
ILogger<DeletePlacedBetUseCase> logger)
|
||||
{
|
||||
_bets = bets ?? throw new ArgumentNullException(nameof(bets));
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
|
||||
public async Task ExecuteAsync(Guid betId, CancellationToken ct = default)
|
||||
{
|
||||
await _bets.DeleteAsync(betId, ct).ConfigureAwait(false);
|
||||
await _bets.SaveChangesAsync(ct).ConfigureAwait(false);
|
||||
_logger.LogInformation("DeletePlacedBetUseCase: removed bet {BetId}", betId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
using Marathon.Application.Abstractions;
|
||||
using Marathon.Domain.Entities;
|
||||
using Marathon.Domain.Enums;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using DomainEventId = Marathon.Domain.ValueObjects.EventId;
|
||||
|
||||
namespace Marathon.Application.UseCases;
|
||||
|
||||
/// <summary>
|
||||
/// Records a new <see cref="PlacedBet"/> entered manually via the Journal UI.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// The use case validates that the referenced event exists, then persists the
|
||||
/// bet. If the event already has a final result the bet is graded on the spot
|
||||
/// via <see cref="Marathon.Domain.Betting.BetOutcomeResolver"/> — saves the
|
||||
/// user a round-trip to the resolver page when entering historical wagers.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public sealed class RecordPlacedBetUseCase
|
||||
{
|
||||
private readonly IPlacedBetRepository _bets;
|
||||
private readonly IEventRepository _events;
|
||||
private readonly IResultRepository _results;
|
||||
private readonly ILogger<RecordPlacedBetUseCase> _logger;
|
||||
|
||||
public RecordPlacedBetUseCase(
|
||||
IPlacedBetRepository bets,
|
||||
IEventRepository events,
|
||||
IResultRepository results,
|
||||
ILogger<RecordPlacedBetUseCase> logger)
|
||||
{
|
||||
_bets = bets ?? throw new ArgumentNullException(nameof(bets));
|
||||
_events = events ?? throw new ArgumentNullException(nameof(events));
|
||||
_results = results ?? throw new ArgumentNullException(nameof(results));
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Persists <paramref name="bet"/>. Returns the bet as stored — if the
|
||||
/// event already has a result, the returned instance reflects the graded
|
||||
/// <see cref="BetOutcome"/>.
|
||||
/// </summary>
|
||||
/// <exception cref="InvalidOperationException">
|
||||
/// The bet references an unknown event. The journal does not allow free-form
|
||||
/// event codes — wagers must be on events the scraper has captured so the
|
||||
/// CLV calculator can compare against the closing snapshot.
|
||||
/// </exception>
|
||||
public async Task<PlacedBet> ExecuteAsync(PlacedBet bet, CancellationToken ct = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(bet);
|
||||
|
||||
// Confirm the event exists in the local store.
|
||||
var ev = await _events.GetAsync(bet.EventId, ct).ConfigureAwait(false);
|
||||
if (ev is null)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Cannot record a bet on unknown event '{bet.EventId.Value}'. " +
|
||||
"The event must already be present in the scrape store.");
|
||||
}
|
||||
|
||||
var toPersist = bet;
|
||||
|
||||
// Auto-grade if a result is already available.
|
||||
if (bet.Outcome == BetOutcome.Pending)
|
||||
{
|
||||
var result = await _results.GetAsync(bet.EventId, ct).ConfigureAwait(false);
|
||||
if (result is not null)
|
||||
{
|
||||
var graded = Marathon.Domain.Betting.BetOutcomeResolver.Resolve(bet.Selection, result);
|
||||
if (graded is not null)
|
||||
{
|
||||
toPersist = bet.WithOutcome(graded.Value);
|
||||
_logger.LogInformation(
|
||||
"RecordPlacedBetUseCase: bet {BetId} on event {EventId} auto-graded as {Outcome}",
|
||||
toPersist.Id, ((DomainEventId)toPersist.EventId).Value, graded.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await _bets.AddAsync(toPersist, ct).ConfigureAwait(false);
|
||||
await _bets.SaveChangesAsync(ct).ConfigureAwait(false);
|
||||
|
||||
_logger.LogInformation(
|
||||
"RecordPlacedBetUseCase: persisted bet {BetId} on event {EventId} stake={Stake} rate={Rate}",
|
||||
toPersist.Id, ((DomainEventId)toPersist.EventId).Value, toPersist.Stake, toPersist.Selection.Rate.Value);
|
||||
|
||||
return toPersist;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
using Marathon.Application.Abstractions;
|
||||
using Marathon.Domain.Betting;
|
||||
using Marathon.Domain.Entities;
|
||||
using Marathon.Domain.Enums;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using DomainEventId = Marathon.Domain.ValueObjects.EventId;
|
||||
|
||||
namespace Marathon.Application.UseCases;
|
||||
|
||||
/// <summary>
|
||||
/// Sweeps the journal for <see cref="BetOutcome.Pending"/> bets whose events
|
||||
/// have been graded, and updates them in bulk via
|
||||
/// <see cref="BetOutcomeResolver"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Called on demand from the Journal page's "Resolve pending" button. The
|
||||
/// design is idempotent — bets that cannot be auto-graded (period-scope, or
|
||||
/// no result yet) are left untouched and surface again on the next pass.
|
||||
/// </remarks>
|
||||
public sealed class ResolvePendingBetsUseCase
|
||||
{
|
||||
private readonly IPlacedBetRepository _bets;
|
||||
private readonly IResultRepository _results;
|
||||
private readonly ILogger<ResolvePendingBetsUseCase> _logger;
|
||||
|
||||
public ResolvePendingBetsUseCase(
|
||||
IPlacedBetRepository bets,
|
||||
IResultRepository results,
|
||||
ILogger<ResolvePendingBetsUseCase> logger)
|
||||
{
|
||||
_bets = bets ?? throw new ArgumentNullException(nameof(bets));
|
||||
_results = results ?? throw new ArgumentNullException(nameof(results));
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the number of bets that were transitioned out of Pending in this pass.
|
||||
/// </summary>
|
||||
public async Task<int> ExecuteAsync(CancellationToken ct = default)
|
||||
{
|
||||
var pending = await _bets.ListByOutcomeAsync(BetOutcome.Pending, ct).ConfigureAwait(false);
|
||||
if (pending.Count == 0)
|
||||
{
|
||||
_logger.LogInformation("ResolvePendingBetsUseCase: no pending bets");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Cache results per event so we do not re-query for each bet on the same event.
|
||||
var resultCache = new Dictionary<DomainEventId, EventResult?>();
|
||||
var resolvedCount = 0;
|
||||
|
||||
foreach (var bet in pending)
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
|
||||
if (!resultCache.TryGetValue(bet.EventId, out var result))
|
||||
{
|
||||
result = await _results.GetAsync(bet.EventId, ct).ConfigureAwait(false);
|
||||
resultCache[bet.EventId] = result;
|
||||
}
|
||||
|
||||
if (result is null) continue;
|
||||
|
||||
var graded = BetOutcomeResolver.Resolve(bet.Selection, result);
|
||||
if (graded is null) continue;
|
||||
|
||||
var updated = bet.WithOutcome(graded.Value);
|
||||
await _bets.UpdateAsync(updated, ct).ConfigureAwait(false);
|
||||
resolvedCount++;
|
||||
}
|
||||
|
||||
// Save before logging — if the batch fails, an exception bubbles out and
|
||||
// the success-count log is never emitted; we never report a graded count
|
||||
// that was rolled back.
|
||||
if (resolvedCount > 0)
|
||||
await _bets.SaveChangesAsync(ct).ConfigureAwait(false);
|
||||
|
||||
_logger.LogInformation(
|
||||
"ResolvePendingBetsUseCase: graded {Resolved} of {Pending} pending bets",
|
||||
resolvedCount, pending.Count);
|
||||
|
||||
return resolvedCount;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user