Fix db and fix endpoints

This commit is contained in:
2025-08-05 22:30:18 +07:00
parent 2dcbcc3ef2
commit 36529ae403
36 changed files with 5073 additions and 245 deletions

View File

@@ -35,52 +35,52 @@ public class AccountService : IAccountService
_userRepository = userRepository;
}
public async Task<Account> CreateAccount(User user, Account request)
public async Task<Account> CreateAccount(User user, Account account)
{
var account = await _accountRepository.GetAccountByNameAsync(request.Name);
var a = await _accountRepository.GetAccountByNameAsync(account.Name);
if (account != null)
if (a != null)
{
throw new Exception($"Account {request.Name} alreary exist");
throw new Exception($"Account {account.Name} alreary exist");
}
else
{
request.User = user;
account.User = user;
if (request.Exchange == TradingExchanges.Evm
&& request.Type == AccountType.Trader)
if (account.Exchange == TradingExchanges.Evm
&& account.Type == AccountType.Trader)
{
var keys = _evmManager.GenerateAddress();
request.Key = keys.Key;
request.Secret = keys.Secret;
account.Key = keys.Key;
account.Secret = keys.Secret;
}
else if (request.Exchange == TradingExchanges.Evm
&& request.Type == AccountType.Privy)
else if (account.Exchange == TradingExchanges.Evm
&& account.Type == AccountType.Privy)
{
if (string.IsNullOrEmpty(request.Key))
if (string.IsNullOrEmpty(account.Key))
{
// No key provided, create new privy embedded wallet.
// TODO : Fix it to create privy wallet
var privyClient = await _evmManager.CreatePrivyWallet();
request.Key = privyClient.Address;
request.Secret = privyClient.Id;
account.Key = privyClient.Address;
account.Secret = privyClient.Id;
}
else
{
request.Key = request.Key; // Address
request.Secret = request.Secret; // Privy wallet id
account.Key = account.Key; // Address
account.Secret = account.Secret; // Privy wallet id
}
}
else
{
request.Key = request.Key;
request.Secret = request.Secret;
account.Key = account.Key;
account.Secret = account.Secret;
}
await _accountRepository.InsertAccountAsync(request);
await _accountRepository.InsertAccountAsync(account);
}
return request;
return account;
}
public bool DeleteAccount(User user, string name)

View File

@@ -114,4 +114,10 @@ public class AgentService : IAgentService
{
return await _agentSummaryRepository.GetAllAsync();
}
public async Task<IEnumerable<string>> GetAllOnlineAgents()
{
var agentSummaries = await _agentSummaryRepository.GetAllAgentWithRunningBots();
return agentSummaries.Select(a => a.AgentName);
}
}

View File

@@ -1,4 +1,4 @@
using Managing.Application.Abstractions;
using Managing.Application.Abstractions.Services;
using Managing.Application.ManageBot.Commands;
using MediatR;
@@ -9,18 +9,17 @@ namespace Managing.Application.ManageBot
/// </summary>
public class GetOnlineAgentNamesCommandHandler : IRequestHandler<GetOnlineAgentNamesCommand, IEnumerable<string>>
{
private readonly IBotService _botService;
private readonly IAgentService _agentService;
public GetOnlineAgentNamesCommandHandler(IBotService botService)
public GetOnlineAgentNamesCommandHandler(IAgentService agentService)
{
_botService = botService;
_agentService = agentService;
}
public async Task<IEnumerable<string>> Handle(GetOnlineAgentNamesCommand request,
CancellationToken cancellationToken)
{
var activeBots = await _botService.GetActiveBotsNamesAsync();
return activeBots;
return await _agentService.GetAllOnlineAgents();
}
}
}

View File

@@ -116,8 +116,7 @@ namespace Managing.Application.Scenarios
public async Task<IEnumerable<Scenario>> GetScenariosByUserAsync(User user)
{
var scenarios = await _tradingService.GetScenariosAsync();
return scenarios.Where(s => s.User?.Name == user.Name);
return await _tradingService.GetScenariosByUserAsync(user);
}
public async Task<Scenario> CreateScenarioForUser(User user, string name, List<string> strategies,

View File

@@ -88,6 +88,11 @@ public class TradingService : ITradingService
return await _tradingRepository.GetScenariosAsync();
}
public async Task<IEnumerable<Scenario>> GetScenariosByUserAsync(User user)
{
return await _tradingRepository.GetScenariosByUserAsync(user);
}
public async Task<IEnumerable<IndicatorBase>> GetIndicatorsAsync()
{
return await _tradingRepository.GetStrategiesAsync();

View File

@@ -95,15 +95,16 @@ public class UserService : IUserService
}
else
{
// First login
// First login - create user first
user = new User
{
Name = name
};
await _userRepository.InsertUserAsync(user);
// Save the user first
await _userRepository.SaveOrUpdateUserAsync(user);
// Create embedded account to authenticate user
// Create embedded account to authenticate user after user is saved
var account = await _accountService.CreateAccount(user, new Account
{
Name = $"{name}-embedded",
@@ -116,9 +117,6 @@ public class UserService : IUserService
{
account
};
// Update user with the new account
await _userRepository.UpdateUser(user);
}
return user;
@@ -165,8 +163,12 @@ public class UserService : IUserService
else
{
user = await GetUserByName(user.Name);
if (!string.IsNullOrEmpty(user.AgentName) && user.AgentName.Equals(agentName))
return user;
// Update the agent name on the provided user object
user.AgentName = agentName;
await _userRepository.UpdateUser(user);
await _userRepository.SaveOrUpdateUserAsync(user);
// Initialize the AgentGrain for this user
try
@@ -204,8 +206,12 @@ public class UserService : IUserService
}
user = await GetUserByName(user.Name);
if (!string.IsNullOrEmpty(user.AvatarUrl) && user.AvatarUrl.Equals(avatarUrl))
return user;
// Update the avatar URL on the provided user object
user.AvatarUrl = avatarUrl;
await _userRepository.UpdateUser(user);
await _userRepository.SaveOrUpdateUserAsync(user);
return user;
}
@@ -222,8 +228,12 @@ public class UserService : IUserService
}
user = await GetUserByName(user.Name);
if (!string.IsNullOrEmpty(user.TelegramChannel) && user.TelegramChannel.Equals(telegramChannel))
return user;
// Update the telegram channel on the provided user object
user.TelegramChannel = telegramChannel;
await _userRepository.UpdateUser(user);
await _userRepository.SaveOrUpdateUserAsync(user);
return user;
}
@@ -247,6 +257,4 @@ public class UserService : IUserService
{
return await _userRepository.GetAllUsersAsync();
}
}