From 5548de68151e2a3b922d4e7fb6dff3334dff65cd Mon Sep 17 00:00:00 2001 From: cryptooda Date: Wed, 24 Dec 2025 21:45:32 +0700 Subject: [PATCH] Add feature flag check for futures trading in StartBotCommandHandler; refactor FlagsmithService to use username directly for flag retrieval and remove unused hashing logic. --- .../ManageBot/StartBotCommandHandler.cs | 12 ++++++++- .../Shared/FlagsmithService.cs | 25 ++----------------- 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/src/Managing.Application/ManageBot/StartBotCommandHandler.cs b/src/Managing.Application/ManageBot/StartBotCommandHandler.cs index eb519ced..1cd4fd79 100644 --- a/src/Managing.Application/ManageBot/StartBotCommandHandler.cs +++ b/src/Managing.Application/ManageBot/StartBotCommandHandler.cs @@ -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 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) { diff --git a/src/Managing.Application/Shared/FlagsmithService.cs b/src/Managing.Application/Shared/FlagsmithService.cs index 32084afd..e98d5c6d 100644 --- a/src/Managing.Application/Shared/FlagsmithService.cs +++ b/src/Managing.Application/Shared/FlagsmithService.cs @@ -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 } } - - /// - /// Hashes the username using SHA256 to create a privacy-preserving identity for Flagsmith - /// - /// The username to hash - /// SHA256 hash of the username as a hexadecimal string - 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(); - } } ///