hrms-api-backend/BMA.EHR.Insignia/Services/InsigniaRequestProcessService.cs
waruneeauy a047787fb4 Merge branch 'develop'
* develop:
  fix: skip notification if no commander is assigned; update connection strings in appsettings
  edit permission normal
  fix สิทธิ์เมนูจำนวนสิทธิ์การลาที่ใช้ไป #2094
  Fix รายงานใบสมัครสอบส่วนเนื้อหาคำรับรองเพี้ยน #2099
  migration files
  migrate to CM Server
  no message
  fix role
  แก้ไขข้อความให้ตรงกับฝั่ง ui (ทาง กทม. แจ้งให้แก้ไขข้อความค่ะ)
  แก้ไขข้อความให้ตรงกับฝั่ง ui (ทาง กทม. แจ้งให้แก้ไขข้อความค่ะ)
  no message
  fix bug เพิ่ม-แก้ไขข้อมูลประวัติการศึกษาไม่อัปเดต #2022, #2025
  fix #2016, #2017
  fix ใบสมัครสอบ
  fix ยืมคืนเครื่องราชย?มาใช้่ RootDnaId แทน rootId
  fix ใบสมัครสอบคัดเลือก #2016, #2017, #2018
  fix issue #2007
  fix issues 1972
  migrate
2025-12-08 10:11:10 +07:00

129 lines
4.1 KiB
C#

using BMA.EHR.Application.Repositories;
using BMA.EHR.Insignia.Service.Configuration;
using SocketIOClient;
using System.Security.Claims;
namespace BMA.EHR.Insignia.Service.Services;
public class InsigniaRequestProcessService : BackgroundService
{
private readonly IBackgroundTaskQueue _queue;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IServiceScopeFactory _serviceScopeFactory;
public InsigniaRequestProcessService(
IBackgroundTaskQueue queue,
IHttpContextAccessor httpContextAccessor,
IServiceScopeFactory serviceScopeFactory)
{
_queue = queue;
_httpContextAccessor = httpContextAccessor;
_serviceScopeFactory = serviceScopeFactory;
}
#region " Properties "
protected string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
protected string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value;
protected bool? IsPlacementAdmin => _httpContextAccessor?.HttpContext?.User?.IsInRole("placement1");
protected string? AccessToken => _httpContextAccessor?.HttpContext?.Request.Headers["Authorization"];
#endregion
public override async Task StartAsync(CancellationToken cancellationToken)
{
var client = new SocketIO("wss://hrmsbkk.case-collection.com",
new SocketIOOptions
{
// เพิ่ม token ใน handshake.auth
Auth = new { token = AccessToken ?? "" },
Path = "/api/v1/org-socket"
});
client.OnConnected += async (sender, e) =>
{
Console.WriteLine("Connected to Socket.IO server");
await client.EmitAsync("eventName", "Hello from .NET client");
};
await client.ConnectAsync();
await base.StartAsync(cancellationToken);
}
//public override async Task StartAsync(CancellationToken cancellationToken)
//{
//var client = new SocketIO("https://bma-ehr.frappet.synology.me/api/v1/org-socket",
// new SocketIOOptions
// {
// // เพิ่ม token ใน handshake.auth
// Auth = new { token = AccessToken ?? "" }
// });
//client.OnConnected += async (sender, e) =>
//{
// Console.WriteLine("Connected to Socket.IO server");
// await client.EmitAsync("eventName", "Hello from .NET client");
//};
//await client.ConnectAsync();
//await base.StartAsync(cancellationToken);
//}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
try
{
var workItem = await _queue.DequeueAsync(stoppingToken);
if (workItem != null)
{
var startTime = DateTime.Now;
await workItem(stoppingToken);
var endTime = DateTime.Now;
var duration = endTime - startTime;
// Resolve repository from a scope because DbContext is scoped/transient.
using var scope = _serviceScopeFactory.CreateScope();
var userProfileRepository = (UserProfileRepository)scope.ServiceProvider.GetService(typeof(UserProfileRepository));
if (userProfileRepository != null)
{
await userProfileRepository.PostInsigniaMessageToSocket("Task completed", UserId ?? "", AccessToken);
}
}
}
catch (OperationCanceledException)
{
break;
}
catch (Exception ex)
{
// รอสักครู่ก่อนประมวลผล task ถัดไป เพื่อป้องกันการวนลูปข้อผิดพลาด
await Task.Delay(1000, stoppingToken);
}
}
}
public override async Task StopAsync(CancellationToken cancellationToken)
{
await base.StopAsync(cancellationToken);
}
}