feat(Backend): update API gateway and websocket hubs
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FinlyticCore.Models.Trades;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FinlyticBackend.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Defines a service for dispatching push notifications via Firebase Cloud Messaging (FCM).
|
||||
/// </summary>
|
||||
public interface IFirebaseNotificationService
|
||||
{
|
||||
/// <summary>
|
||||
/// Sends a push notification about a new trade proposal.
|
||||
/// </summary>
|
||||
/// <param name="proposal">The trade proposal details.</param>
|
||||
/// <param name="fcmTokens">The list of FCM device tokens.</param>
|
||||
/// <param name="cancellationToken">A cancellation token.</param>
|
||||
/// <returns>A task representing the asynchronous operation.</returns>
|
||||
Task SendTradeProposalNotificationAsync(TradeProposalDto proposal, List<string> fcmTokens, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Sends a push notification about an update to an existing trade.
|
||||
/// </summary>
|
||||
/// <param name="update">The trade update details.</param>
|
||||
/// <param name="fcmTokens">The list of FCM device tokens.</param>
|
||||
/// <param name="cancellationToken">A cancellation token.</param>
|
||||
/// <returns>A task representing the asynchronous operation.</returns>
|
||||
Task SendTradeUpdateNotificationAsync(TradeHourlyUpdateDto update, List<string> fcmTokens, CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public class FirebaseNotificationService : IFirebaseNotificationService
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly ILogger<FirebaseNotificationService> _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="FirebaseNotificationService"/> class.
|
||||
/// </summary>
|
||||
/// <param name="httpClient">The HTTP client for making API requests.</param>
|
||||
/// <param name="logger">The logger instance.</param>
|
||||
public FirebaseNotificationService(HttpClient httpClient, ILogger<FirebaseNotificationService> logger)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
||||
public async Task SendTradeProposalNotificationAsync(TradeProposalDto proposal, List<string> fcmTokens, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (fcmTokens == null || fcmTokens.Count == 0) return;
|
||||
|
||||
string title = $"🚀 Trade Signal: {proposal.SignalType} {proposal.Symbol}";
|
||||
string body = $"{proposal.CompanyName} ({proposal.Isin}) - Entry: ${proposal.EntryPrice:F2}, WinRate: {proposal.WinRate:F1}%. {proposal.Reasoning}";
|
||||
|
||||
foreach (var token in fcmTokens)
|
||||
{
|
||||
await DispatchFcmMessageAsync(token, title, body, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task SendTradeUpdateNotificationAsync(TradeHourlyUpdateDto update, List<string> fcmTokens, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (fcmTokens == null || fcmTokens.Count == 0) return;
|
||||
|
||||
string title = $"📊 Trade Update: {update.TradeId}";
|
||||
string body = $"Recommendation: {update.Recommendation} @ ${update.CurrentPrice:F2}. {update.Reasoning}";
|
||||
|
||||
foreach (var token in fcmTokens)
|
||||
{
|
||||
await DispatchFcmMessageAsync(token, title, body, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
private Task DispatchFcmMessageAsync(string fcmToken, string title, string body, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.LogInformation("[{Channel}] FCM Push Notification dispatched to Token [{TokenPrefix}...]: Title='{Title}', Body='{Body}'",
|
||||
"NotificationChannel", fcmToken.Length > 10 ? fcmToken[..10] : fcmToken, title, body);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "[{Channel}] Failed to send FCM push notification to token {Token}", "NotificationChannel", fcmToken);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user