Update balance tracking

This commit is contained in:
2025-10-03 16:43:20 +07:00
parent 6928770da7
commit f72bfc4ab8
11 changed files with 136 additions and 34 deletions

View File

@@ -31,9 +31,40 @@ public class AgentService : IAgentService
public async Task<AgentBalanceHistory> GetAgentBalances(string agentName, DateTime start,
DateTime? end = null)
{
// Get userId from agentName by looking up the user
var userId = await ServiceScopeHelpers.WithScopedService<IUserService, int?>(_serviceScopeFactory,
async (userService) =>
{
var user = await userService.GetUserByAgentName(agentName);
return user?.Id;
});
if (!userId.HasValue)
{
// Return empty result if user not found
return new AgentBalanceHistory
{
UserId = 0,
AgentName = agentName,
AgentBalances = new List<AgentBalance>()
};
}
// Use the UserId-based method internally
var result = await GetAgentBalancesByUserId(userId.Value, start, end);
// Override the AgentName to use the requested agentName instead of the default
result.AgentName = agentName;
return result;
}
public async Task<AgentBalanceHistory> GetAgentBalancesByUserId(int userId, DateTime start,
DateTime? end = null)
{
var effectiveEnd = end ?? DateTime.UtcNow;
string cacheKey = $"AgentBalances_{agentName}_{start:yyyyMMdd}_{effectiveEnd:yyyyMMdd}";
string cacheKey = $"AgentBalancesByUserId_{userId}_{start:yyyyMMdd}_{effectiveEnd:yyyyMMdd}";
// Check if the balances are already cached
var cachedBalances = _cacheService.GetValue<AgentBalanceHistory>(cacheKey);
@@ -43,12 +74,13 @@ public class AgentService : IAgentService
return cachedBalances;
}
var balances = await _agentBalanceRepository.GetAgentBalances(agentName, start, end);
var balances = await _agentBalanceRepository.GetAgentBalancesByUserId(userId, start, end);
// Create a single AgentBalanceHistory with all balances
var result = new AgentBalanceHistory
{
AgentName = agentName,
UserId = userId,
AgentName = $"User_{userId}",
AgentBalances = balances.OrderBy(b => b.Time).ToList()
};