add send noti and bulk insert (not complete)
All checks were successful
Build & Deploy on Dev / build (push) Successful in 49s
All checks were successful
Build & Deploy on Dev / build (push) Successful in 49s
This commit is contained in:
parent
0f1ec072ad
commit
9c2caa3f4a
7 changed files with 232 additions and 160 deletions
59
Services/NotificationService.cs
Normal file
59
Services/NotificationService.cs
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue