Update bot market type

This commit is contained in:
2025-12-11 23:32:06 +07:00
parent 35df25915f
commit a254db6d24
20 changed files with 1986 additions and 44 deletions

View File

@@ -1,3 +1,5 @@
using System.Security.Cryptography;
using System.Text;
using Flagsmith;
using Managing.Application.Abstractions.Services;
using Microsoft.Extensions.Logging;
@@ -30,30 +32,32 @@ public class FlagsmithService : IFlagsmithService
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public async Task<IFlagsmithFlags> GetIdentityFlagsAsync(string identity)
public async Task<IFlagsmithFlags> GetIdentityFlagsAsync(string username)
{
if (string.IsNullOrWhiteSpace(identity))
if (string.IsNullOrWhiteSpace(username))
{
throw new ArgumentException("Identity cannot be null or empty", nameof(identity));
throw new ArgumentException("Username cannot be null or empty", nameof(username));
}
var hashedIdentity = HashUsername(username);
try
{
var flags = await _flagsmithClient.GetIdentityFlags(identity);
var flags = await _flagsmithClient.GetIdentityFlags(hashedIdentity);
return new FlagsmithFlagsWrapper(flags);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error getting flags for identity {Identity}", identity);
_logger.LogError(ex, "Error getting flags for username {Username} (hashed: {HashedIdentity})", username, hashedIdentity);
throw;
}
}
public async Task<bool> IsFeatureEnabledAsync(string identity, string featureName)
public async Task<bool> IsFeatureEnabledAsync(string username, string featureName)
{
if (string.IsNullOrWhiteSpace(identity))
if (string.IsNullOrWhiteSpace(username))
{
throw new ArgumentException("Identity cannot be null or empty", nameof(identity));
throw new ArgumentException("Username cannot be null or empty", nameof(username));
}
if (string.IsNullOrWhiteSpace(featureName))
@@ -63,21 +67,21 @@ public class FlagsmithService : IFlagsmithService
try
{
var flags = await GetIdentityFlagsAsync(identity);
var flags = await GetIdentityFlagsAsync(username);
return await flags.IsFeatureEnabled(featureName);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error checking feature {FeatureName} for identity {Identity}", featureName, identity);
_logger.LogError(ex, "Error checking feature {FeatureName} for username {Username}", featureName, username);
return false; // Default to false on error
}
}
public async Task<string?> GetFeatureValueAsync(string identity, string featureName)
public async Task<string?> GetFeatureValueAsync(string username, string featureName)
{
if (string.IsNullOrWhiteSpace(identity))
if (string.IsNullOrWhiteSpace(username))
{
throw new ArgumentException("Identity cannot be null or empty", nameof(identity));
throw new ArgumentException("Username cannot be null or empty", nameof(username));
}
if (string.IsNullOrWhiteSpace(featureName))
@@ -87,15 +91,32 @@ public class FlagsmithService : IFlagsmithService
try
{
var flags = await GetIdentityFlagsAsync(identity);
var flags = await GetIdentityFlagsAsync(username);
return await flags.GetFeatureValue(featureName);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error getting feature value {FeatureName} for identity {Identity}", featureName, identity);
_logger.LogError(ex, "Error getting feature value {FeatureName} for username {Username}", featureName, username);
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>