58 lines
2 KiB
C#
58 lines
2 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using System.Net.Http.Headers;
|
|
using System.Text;
|
|
|
|
namespace BMA.EHR.Leave.Services;
|
|
|
|
public class NotificationService
|
|
{
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
private readonly ILogger<NotificationService> _logger;
|
|
private readonly IConfiguration _configuration;
|
|
|
|
private const string NotifyEndpoint = "https://hrmsbkk.case-collection.com/api/v1/org/through-socket/notify-from-token";
|
|
|
|
public NotificationService(IHttpClientFactory httpClientFactory, ILogger<NotificationService> logger, IConfiguration configuration)
|
|
{
|
|
_httpClientFactory = httpClientFactory;
|
|
_logger = logger;
|
|
_configuration = configuration;
|
|
}
|
|
|
|
public async Task SendNotificationAsync(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);
|
|
}
|
|
}
|
|
}
|