Add feature flag check for futures trading in StartBotCommandHandler; refactor FlagsmithService to use username directly for flag retrieval and remove unused hashing logic.

This commit is contained in:
2025-12-24 21:45:32 +07:00
parent 2db6cc9033
commit 5548de6815
2 changed files with 13 additions and 24 deletions

View File

@@ -15,18 +15,28 @@ namespace Managing.Application.ManageBot
private readonly IGrainFactory _grainFactory;
private readonly IBotService _botService;
private readonly ITradingService _tradingService;
private readonly IFlagsmithService _flagsmithService;
public StartBotCommandHandler(
IAccountService accountService, IGrainFactory grainFactory, IBotService botService, ITradingService tradingService)
IAccountService accountService, IGrainFactory grainFactory, IBotService botService, ITradingService tradingService, IFlagsmithService flagsmithService)
{
_accountService = accountService;
_grainFactory = grainFactory;
_botService = botService;
_tradingService = tradingService;
_flagsmithService = flagsmithService;
}
public async Task<BotStatus> Handle(StartBotCommand request, CancellationToken cancellationToken)
{
// Check if trading_future feature flag is enabled
var isTradingFutureEnabled = await _flagsmithService.IsFeatureEnabledAsync(request.User.Name, "trading_future");
if (!isTradingFutureEnabled)
{
throw new InvalidOperationException(
"Futures trading is not enabled for your account. Please contact support to enable this feature.");
}
// Validate the configuration
if (request.Config == null)
{

View File

@@ -1,5 +1,3 @@
using System.Security.Cryptography;
using System.Text;
using Flagsmith;
using Managing.Application.Abstractions.Services;
using Microsoft.Extensions.Logging;
@@ -39,16 +37,14 @@ public class FlagsmithService : IFlagsmithService
throw new ArgumentException("Username cannot be null or empty", nameof(username));
}
var hashedIdentity = HashUsername(username);
try
{
var flags = await _flagsmithClient.GetIdentityFlags(hashedIdentity);
var flags = await _flagsmithClient.GetIdentityFlags(username);
return new FlagsmithFlagsWrapper(flags);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error getting flags for username {Username} (hashed: {HashedIdentity})", username, hashedIdentity);
_logger.LogError(ex, "Error getting flags for username {Username}", username);
throw;
}
}
@@ -100,23 +96,6 @@ public class FlagsmithService : IFlagsmithService
return null; // Default to null on error
}
}
/// <summary>
/// Hashes the username using SHA256 to create a privacy-preserving identity for Flagsmith
/// </summary>
/// <param name="username">The username to hash</param>
/// <returns>SHA256 hash of the username as a hexadecimal string</returns>
private static string HashUsername(string username)
{
if (string.IsNullOrWhiteSpace(username))
{
throw new ArgumentException("Username cannot be null or empty", nameof(username));
}
using var sha256 = SHA256.Create();
var hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(username));
return Convert.ToHexString(hashBytes).ToLowerInvariant();
}
}
/// <summary>