Files
managing-apps/src/Managing.Api/Controllers/UserController.cs
cryptooda fb49190346 Add agent summary update functionality and improve user controller
- Introduced a new endpoint in UserController to update the agent summary, ensuring balance data is refreshed after transactions.
- Implemented ForceUpdateSummaryImmediate method in IAgentGrain to allow immediate updates without cooldown checks.
- Enhanced StartBotCommandHandler to force update the agent summary before starting the bot, ensuring accurate balance data.
- Updated TypeScript API client to include the new update-agent-summary method for frontend integration.
2026-01-03 03:09:44 +07:00

196 lines
7.7 KiB
C#

using Managing.Api.Authorization;
using Managing.Api.Models.Requests;
using Managing.Application.Abstractions.Models;
using Managing.Application.Abstractions.Services;
using Managing.Application.ManageBot.Commands;
using Managing.Domain.Users;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Managing.Api.Controllers;
/// <summary>
/// Provides authentication-related actions, including token creation for user authentication.
/// </summary>
[ApiController]
[Route("[controller]")]
[Produces("application/json")]
public class UserController : BaseController
{
private IConfiguration _config;
private readonly IJwtUtils _jwtUtils;
private readonly IWebhookService _webhookService;
private readonly IMediator _mediator;
/// <summary>
/// Initializes a new instance of the <see cref="UserController"/> class.
/// </summary>
/// <param name="config">Configuration settings.</param>
/// <param name="userService">Service for user-related operations.</param>
/// <param name="jwtUtils">Utility for JWT token operations.</param>
/// <param name="webhookService">Service for webhook operations.</param>
/// <param name="mediator">Mediator for handling commands.</param>
public UserController(IConfiguration config, IUserService userService, IJwtUtils jwtUtils,
IWebhookService webhookService, IMediator mediator)
: base(userService)
{
_config = config;
_jwtUtils = jwtUtils;
_webhookService = webhookService;
_mediator = mediator;
}
/// <summary>
/// Creates a JWT token for a user based on the provided login credentials.
/// </summary>
/// <param name="login">The login request containing user credentials.</param>
/// <returns>A JWT token if authentication is successful; otherwise, an Unauthorized result.</returns>
[AllowAnonymous]
[HttpPost("create-token")]
public async Task<ActionResult<string>> CreateToken([FromBody] LoginRequest login)
{
var user = await _userService.Authenticate(login.Name, login.Address, login.Message, login.Signature, login.OwnerWalletAddress);
if (user != null)
{
var tokenString = _jwtUtils.GenerateJwtToken(user, login.Address);
return Ok(tokenString);
}
return Unauthorized();
}
/// <summary>
/// Gets the current user's information.
/// </summary>
/// <returns>The current user's information.</returns>
[Authorize]
[HttpGet]
public async Task<ActionResult<User>> GetCurrentUser()
{
var user = await base.GetUser();
return Ok(user);
}
/// <summary>
/// Updates the agent name for the current user.
/// </summary>
/// <param name="agentName">The new agent name to set.</param>
/// <returns>The updated user with the new agent name.</returns>
[Authorize]
[HttpPut("agent-name")]
public async Task<ActionResult<User>> UpdateAgentName([FromBody] string agentName)
{
var user = await GetUser();
var updatedUser = await _userService.UpdateAgentName(user, agentName);
return Ok(updatedUser);
}
/// <summary>
/// Updates the avatar URL for the current user.
/// </summary>
/// <param name="avatarUrl">The new avatar URL to set.</param>
/// <returns>The updated user with the new avatar URL.</returns>
[Authorize]
[HttpPut("avatar")]
public async Task<ActionResult<User>> UpdateAvatarUrl([FromBody] string avatarUrl)
{
var user = await GetUser();
var updatedUser = await _userService.UpdateAvatarUrl(user, avatarUrl);
return Ok(updatedUser);
}
/// <summary>
/// Updates the Telegram channel for the current user.
/// </summary>
/// <param name="telegramChannel">The new Telegram channel to set.</param>
/// <returns>The updated user with the new Telegram channel.</returns>
[Authorize]
[HttpPut("telegram-channel")]
public async Task<ActionResult<User>> UpdateTelegramChannel([FromBody] string telegramChannel)
{
var user = await GetUser();
var updatedUser = await _userService.UpdateTelegramChannel(user, telegramChannel);
return Ok(updatedUser);
}
/// <summary>
/// Tests the Telegram channel configuration by sending a test message.
/// </summary>
/// <returns>A message indicating the test result.</returns>
[Authorize]
[HttpPost("telegram-channel/test")]
public async Task<ActionResult<string>> TestTelegramChannel()
{
var user = await GetUser();
if (string.IsNullOrEmpty(user.TelegramChannel))
{
return BadRequest("No Telegram channel configured for this user. Please set a Telegram channel first.");
}
try
{
var testMessage = $"🚀 **Trading Bot - Channel Test**\n\n" +
$"🎯 **Agent:** {user.Name}\n" +
$"📡 **Channel ID:** {user.TelegramChannel}\n" +
$"⏰ **Test Time:** {DateTime.UtcNow:MMM dd, yyyy • HH:mm:ss} UTC\n\n" +
$"🔔 **You will receive notifications for:**\n" +
$"• 📈 Position Opens & Closes\n" +
$"• 🤖 Bot configuration changes\n\n" +
$"🎉 **Ready to trade!** Your notifications are now active.";
await _webhookService.SendMessage(testMessage, user.TelegramChannel);
return Ok(
$"Test message sent successfully to Telegram channel {user.TelegramChannel}. Please check your Telegram to verify delivery.");
}
catch (Exception ex)
{
return StatusCode(500, $"Failed to send test message: {ex.Message}");
}
}
/// <summary>
/// Updates the user settings for the current user.
/// </summary>
/// <param name="settings">The user settings to update.</param>
/// <returns>The updated user with the new settings.</returns>
[Authorize]
[HttpPut("settings")]
public async Task<ActionResult<User>> UpdateUserSettings([FromBody] UpdateUserSettingsRequest settings)
{
var user = await GetUser();
// Map API request to DTO
// Note: IsGmxEnabled and DefaultExchange are not updatable via settings endpoint
var settingsDto = new UserSettingsDto
{
LowEthAmountAlert = settings.LowEthAmountAlert,
EnableAutoswap = settings.EnableAutoswap,
AutoswapAmount = settings.AutoswapAmount,
MaxWaitingTimeForPositionToGetFilledSeconds = settings.MaxWaitingTimeForPositionToGetFilledSeconds,
MaxTxnGasFeePerPosition = settings.MaxTxnGasFeePerPosition,
GmxSlippage = settings.GmxSlippage,
MinimumConfidence = settings.MinimumConfidence,
TrendStrongAgreementThreshold = settings.TrendStrongAgreementThreshold,
SignalAgreementThreshold = settings.SignalAgreementThreshold,
AllowSignalTrendOverride = settings.AllowSignalTrendOverride
};
var updatedUser = await _userService.UpdateUserSettings(user, settingsDto);
return Ok(updatedUser);
}
/// <summary>
/// Updates the agent summary by refreshing balance data and recalculating metrics.
/// Should be called after a topup/deposit to ensure the balance is up to date.
/// </summary>
/// <returns>Success response.</returns>
[HttpPost("update-agent-summary")]
public async Task<ActionResult> UpdateAgentSummary()
{
var user = await GetUser();
await _mediator.Send(new UpdateAgentSummaryCommand(user));
return Ok(new { message = "Agent summary updated successfully" });
}
}