hrms-api-recruit/Services/NotificationService.cs
Suphonchai Phoonsawat 9c2caa3f4a
All checks were successful
Build & Deploy on Dev / build (push) Successful in 49s
add send noti and bulk insert (not complete)
2026-05-14 16:29:16 +07:00

59 lines
2.1 KiB
C#

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<NotificationService> _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<NotificationService> 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);
}
}
}