Prevent user to open multiple strategy on the same ticker
This commit is contained in:
@@ -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)
|
||||
{
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,6 +43,15 @@ namespace Managing.Application.ManageBot
|
||||
$"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;
|
||||
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}");
|
||||
}
|
||||
|
||||
// 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
|
||||
var userAccounts = await _accountService.GetAccountsByUserAsync(request.User, true, true);
|
||||
var firstAccount = userAccounts.FirstOrDefault();
|
||||
|
||||
Reference in New Issue
Block a user