using Microsoft.Extensions.Logging; using Newtonsoft.Json; using System.Net.Http.Headers; using System.Text; namespace BMA.EHR.Recruit.Services; public class NotificationService { private readonly IHttpClientFactory _httpClientFactory; private readonly ILogger _logger; private readonly IConfiguration _configuration; private string NotifyEndpoint = "https://hrmsbkk.case-collection.com/api/v1/org/through-socket/notify-from-token"; public NotificationService(IHttpClientFactory httpClientFactory, ILogger logger, IConfiguration configuration) { _httpClientFactory = httpClientFactory; _logger = logger; _configuration = configuration; NotifyEndpoint = $"{_configuration["API"]}/org/through-socket/notify-from-token"; } public async Task SendImportNotificationAsync(string? token, bool error, string message) { if (string.IsNullOrEmpty(token)) { _logger.LogWarning("Cannot send import notification: token is null or empty."); return; } try { var client = _httpClientFactory.CreateClient("default"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", "")); var payload = new { error, message }; var json = JsonConvert.SerializeObject(payload); var content = new StringContent(json, Encoding.UTF8, "application/json"); var response = await client.PostAsync(NotifyEndpoint, content); if (!response.IsSuccessStatusCode) { var responseBody = await response.Content.ReadAsStringAsync(); _logger.LogWarning("Import notification failed with status {StatusCode}: {Body}", response.StatusCode, responseBody); } } catch (Exception ex) { _logger.LogError(ex, "Failed to send import notification: {Message}", ex.Message); } } }