Prevent user to open multiple strategy on the same ticker
This commit is contained in:
@@ -139,6 +139,11 @@ public class BotController : BaseController
|
|||||||
await NotifyBotSubscriberAsync();
|
await NotifyBotSubscriberAsync();
|
||||||
return Ok(result);
|
return Ok(result);
|
||||||
}
|
}
|
||||||
|
catch (InvalidOperationException ex) when (ex.Message.Contains("already have a strategy"))
|
||||||
|
{
|
||||||
|
// Return 400 for validation errors about existing strategies on same ticker
|
||||||
|
return BadRequest(ex.Message);
|
||||||
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Error starting bot");
|
_logger.LogError(ex, "Error starting bot");
|
||||||
@@ -168,6 +173,11 @@ public class BotController : BaseController
|
|||||||
await NotifyBotSubscriberAsync();
|
await NotifyBotSubscriberAsync();
|
||||||
return Ok(result);
|
return Ok(result);
|
||||||
}
|
}
|
||||||
|
catch (InvalidOperationException ex) when (ex.Message.Contains("already have a strategy"))
|
||||||
|
{
|
||||||
|
// Return 400 for validation errors about existing strategies on same ticker
|
||||||
|
return BadRequest(ex.Message);
|
||||||
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Error starting copy trading bot");
|
_logger.LogError(ex, "Error starting copy trading bot");
|
||||||
@@ -192,6 +202,11 @@ public class BotController : BaseController
|
|||||||
|
|
||||||
return Ok(result);
|
return Ok(result);
|
||||||
}
|
}
|
||||||
|
catch (InvalidOperationException ex) when (ex.Message.Contains("already have a strategy"))
|
||||||
|
{
|
||||||
|
// Return 400 for validation errors about existing strategies on same ticker
|
||||||
|
return BadRequest(ex.Message);
|
||||||
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Error saving bot");
|
_logger.LogError(ex, "Error saving bot");
|
||||||
@@ -319,6 +334,11 @@ public class BotController : BaseController
|
|||||||
|
|
||||||
return Ok(result);
|
return Ok(result);
|
||||||
}
|
}
|
||||||
|
catch (InvalidOperationException ex) when (ex.Message.Contains("already have another strategy"))
|
||||||
|
{
|
||||||
|
// Return 400 for validation errors about existing strategies on same ticker
|
||||||
|
return BadRequest(ex.Message);
|
||||||
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Error restarting bot");
|
_logger.LogError(ex, "Error restarting bot");
|
||||||
|
|||||||
@@ -64,4 +64,12 @@ public interface IBotService
|
|||||||
/// <param name="account">The account to check balances for</param>
|
/// <param name="account">The account to check balances for</param>
|
||||||
/// <returns>Balance check result</returns>
|
/// <returns>Balance check result</returns>
|
||||||
Task<BalanceCheckResult> CheckAccountBalancesAsync(Account account);
|
Task<BalanceCheckResult> CheckAccountBalancesAsync(Account account);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Checks if the user already has a bot (Running or Saved) on the specified ticker
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userId">The user ID</param>
|
||||||
|
/// <param name="ticker">The ticker to check</param>
|
||||||
|
/// <returns>True if the user has a bot on this ticker, false otherwise</returns>
|
||||||
|
Task<bool> HasUserBotOnTickerAsync(int userId, Ticker ticker);
|
||||||
}
|
}
|
||||||
@@ -524,5 +524,13 @@ namespace Managing.Application.ManageBot
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<bool> HasUserBotOnTickerAsync(int userId, Ticker ticker)
|
||||||
|
{
|
||||||
|
var userBots = await _botRepository.GetBotsByUserIdAsync(userId);
|
||||||
|
return userBots.Any(bot =>
|
||||||
|
bot.Ticker == ticker &&
|
||||||
|
(bot.Status == BotStatus.Running || bot.Status == BotStatus.Saved));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -16,6 +16,27 @@ namespace Managing.Application.ManageBot
|
|||||||
|
|
||||||
public async Task<BotStatus> Handle(RestartBotCommand request, CancellationToken cancellationToken)
|
public async Task<BotStatus> Handle(RestartBotCommand request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
|
// Get the bot being restarted to check its ticker
|
||||||
|
var bot = await _botService.GetBotByIdentifier(request.Identifier);
|
||||||
|
if (bot == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"Bot with identifier {request.Identifier} not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if user already has another bot on this ticker (excluding the one being restarted)
|
||||||
|
var userBots = await _botService.GetBotsByUser(bot.User.Id);
|
||||||
|
var hasAnotherBotOnSameTicker = userBots.Any(b =>
|
||||||
|
b.Identifier != request.Identifier && // Exclude the bot being restarted
|
||||||
|
b.Ticker == bot.Ticker &&
|
||||||
|
(b.Status == BotStatus.Running || b.Status == BotStatus.Saved));
|
||||||
|
|
||||||
|
if (hasAnotherBotOnSameTicker)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException(
|
||||||
|
$"You already have another strategy running or saved on ticker {bot.Ticker}. " +
|
||||||
|
"You cannot restart this bot while you have multiple strategies on the same ticker.");
|
||||||
|
}
|
||||||
|
|
||||||
return await _botService.RestartBot(request.Identifier);
|
return await _botService.RestartBot(request.Identifier);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,6 +43,15 @@ namespace Managing.Application.ManageBot
|
|||||||
$"Bot trading balance must be greater than {Constants.GMX.Config.MinimumPositionAmount}");
|
$"Bot trading balance must be greater than {Constants.GMX.Config.MinimumPositionAmount}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if user already has a bot on this ticker
|
||||||
|
var hasExistingBotOnTicker = await _botService.HasUserBotOnTickerAsync(request.User.Id, request.Config.Ticker);
|
||||||
|
if (hasExistingBotOnTicker)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException(
|
||||||
|
$"You already have a strategy running or saved on ticker {request.Config.Ticker}. " +
|
||||||
|
"You cannot create multiple strategies on the same ticker.");
|
||||||
|
}
|
||||||
|
|
||||||
Account account;
|
Account account;
|
||||||
if (string.IsNullOrEmpty(request.Config.AccountName))
|
if (string.IsNullOrEmpty(request.Config.AccountName))
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -72,6 +72,15 @@ namespace Managing.Application.ManageBot
|
|||||||
throw new InvalidOperationException($"Could not retrieve configuration for master bot {request.MasterBotIdentifier}");
|
throw new InvalidOperationException($"Could not retrieve configuration for master bot {request.MasterBotIdentifier}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if user already has a bot on this ticker (same as master bot)
|
||||||
|
var hasExistingBotOnTicker = await _botService.HasUserBotOnTickerAsync(request.User.Id, masterConfig.Ticker);
|
||||||
|
if (hasExistingBotOnTicker)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException(
|
||||||
|
$"You already have a strategy running or saved on ticker {masterConfig.Ticker}. " +
|
||||||
|
"You cannot create multiple strategies on the same ticker.");
|
||||||
|
}
|
||||||
|
|
||||||
// Get account information from the requesting user's accounts
|
// Get account information from the requesting user's accounts
|
||||||
var userAccounts = await _accountService.GetAccountsByUserAsync(request.User, true, true);
|
var userAccounts = await _accountService.GetAccountsByUserAsync(request.User, true, true);
|
||||||
var firstAccount = userAccounts.FirstOrDefault();
|
var firstAccount = userAccounts.FirstOrDefault();
|
||||||
|
|||||||
Reference in New Issue
Block a user