Move simulation history into profile

This commit is contained in:
hz
2026-07-14 21:38:03 -05:00
parent 52e82391ee
commit 3b946c2bc5
14 changed files with 312 additions and 68 deletions
@@ -35,6 +35,8 @@ public class ExamSimulationStore
Builders<ExamSimulation>.IndexKeys.Ascending(item => item.UserId).Ascending(item => item.State)));
_simulations.Indexes.CreateOne(new CreateIndexModel<ExamSimulation>(
Builders<ExamSimulation>.IndexKeys.Ascending(item => item.UserId).Ascending(item => item.State).Descending(item => item.CompletedUtc)));
_simulations.Indexes.CreateOne(new CreateIndexModel<ExamSimulation>(
Builders<ExamSimulation>.IndexKeys.Ascending(item => item.UserId).Descending(item => item.CreatedUtc)));
_simulations.Indexes.CreateOne(new CreateIndexModel<ExamSimulation>(
Builders<ExamSimulation>.IndexKeys.Ascending(item => item.UpdatedUtc)));
_outcomes.Indexes.CreateOne(new CreateIndexModel<Step3Outcome>(
@@ -183,6 +185,37 @@ public class ExamSimulationStore
};
}
public async Task<ExamSimulationHistoryPage> GetHistoryAsync(string userId, int limit, DateTime? before)
{
limit = Math.Clamp(limit, 1, 50);
var filter = Builders<ExamSimulation>.Filter.Eq(item => item.UserId, userId);
if (before is not null)
filter &= Builders<ExamSimulation>.Filter.Lt(item => item.CreatedUtc, before.Value);
var simulations = await _simulations.Find(filter).SortByDescending(item => item.CreatedUtc).Limit(limit + 1).ToListAsync();
var hasMore = simulations.Count > limit;
if (hasMore) simulations.RemoveAt(simulations.Count - 1);
return new ExamSimulationHistoryPage
{
Items = simulations.Select(ToHistorySummary).ToList(),
NextBefore = hasMore && simulations.Count > 0 ? simulations[^1].CreatedUtc : null
};
}
public static ExamSimulationHistorySummary ToHistorySummary(ExamSimulation item) => new()
{
Id = item.Id ?? "",
State = item.State,
CreatedUtc = item.CreatedUtc,
UpdatedUtc = item.UpdatedUtc,
CompletedUtc = item.CompletedUtc,
CurrentBlock = item.CurrentBlock,
PredictedScore = item.Score?.PredictedScore,
RangeLow = item.Score?.RangeLow,
RangeHigh = item.Score?.RangeHigh,
Correct = item.Score?.Correct,
Total = item.Score?.Total ?? item.Items.Count
};
public async Task<ExamSimulation> StartNextBlockAsync(ExamSimulation simulation)
{
if (simulation.State != ExamSimulationStates.BetweenBlocks)