hrms-api-backend/BMA.EHR.Placement.Service/Controllers/NotifyController.cs

284 lines
11 KiB
C#
Raw Permalink Normal View History

2023-09-06 14:52:20 +07:00
using BMA.EHR.Application.Repositories;
2023-09-08 15:23:41 +07:00
using BMA.EHR.Application.Repositories.MessageQueue;
2023-09-06 14:52:20 +07:00
using BMA.EHR.Domain.Common;
2023-09-08 15:23:41 +07:00
using BMA.EHR.Domain.Models.Probation;
2023-09-06 14:52:20 +07:00
using BMA.EHR.Domain.Shared;
using BMA.EHR.Infrastructure.Persistence;
using BMA.EHR.Placement.Service.Requests;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
2024-06-30 20:52:38 +07:00
using Newtonsoft.Json;
2024-11-26 17:17:13 +07:00
using RabbitMQ.Client.Events;
using RabbitMQ.Client;
2023-09-06 14:52:20 +07:00
using Swashbuckle.AspNetCore.Annotations;
2024-06-30 20:52:38 +07:00
using System.Net.Http.Headers;
2023-09-06 14:52:20 +07:00
using System.Security.Claims;
2024-11-26 17:17:13 +07:00
using System.Text;
2023-09-06 14:52:20 +07:00
namespace BMA.EHR.Placement.Service.Controllers
{
[Route("api/v{version:apiVersion}/placement/noti")]
[ApiVersion("1.0")]
[ApiController]
[Produces("application/json")]
[Authorize]
[SwaggerTag("ระบบบรรจุ")]
public class NotifyController : BaseController
{
private readonly PlacementRepository _repository;
private readonly ApplicationDBContext _context;
private readonly MinIOService _documentService;
private readonly IHttpContextAccessor _httpContextAccessor;
2023-09-08 15:23:41 +07:00
private readonly NotificationRepository _repositoryNoti;
2024-06-30 20:52:38 +07:00
private readonly IConfiguration _configuration;
2023-09-06 14:52:20 +07:00
public NotifyController(PlacementRepository repository,
ApplicationDBContext context,
MinIOService documentService,
2023-09-08 15:23:41 +07:00
NotificationRepository repositoryNoti,
2024-06-30 20:52:38 +07:00
IHttpContextAccessor httpContextAccessor,
IConfiguration configuration)
2023-09-06 14:52:20 +07:00
{
_repository = repository;
2023-09-08 15:23:41 +07:00
_repositoryNoti = repositoryNoti;
2023-09-06 14:52:20 +07:00
_context = context;
_documentService = documentService;
_httpContextAccessor = httpContextAccessor;
2024-06-30 20:52:38 +07:00
_configuration = configuration;
2023-09-06 14:52:20 +07:00
}
#region " Properties "
private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value;
2024-06-30 20:52:38 +07:00
private string? token => _httpContextAccessor?.HttpContext?.Request.Headers["Authorization"];
2023-09-06 14:52:20 +07:00
#endregion
2024-11-26 17:17:13 +07:00
/// <summary>
/// ทดสอบ (Rabbit MQ)
/// </summary>
/// <returns></returns>
[HttpPost("test-queue")]
public async Task<ActionResult<ResponseObject>> TestRabbitMQ([FromBody] NotiRequest req)
{
var host = "localhost";
var userName = "guest";
var password = "guest";
var factory = new ConnectionFactory()
{
HostName = host,
UserName = userName,
Password = password
};
Console.WriteLine("Send to Consume!");
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "test_dotnet", durable: false, exclusive: false, autoDelete: false, arguments: null);
var jsonString = JsonConvert.SerializeObject(req);
var body = Encoding.UTF8.GetBytes(jsonString);
channel.BasicPublish(exchange: "", routingKey: "test_dotnet", basicProperties: null, body: body);
var consumer = new EventingBasicConsumer(channel);
var receivedTaskCompletionSource = new TaskCompletionSource<bool>();
consumer.Received += async (model, x) =>
{
await _repositoryNoti.PushNotificationAsync2(
Guid.Parse(req.ReceiverUserId),
req.Subject,
req.Body,
req.Payload,
"",
req.IsSendInbox,
req.IsSendMail
);
receivedTaskCompletionSource.SetResult(true);
};
channel.BasicConsume("test_dotnet", autoAck: true, consumer);
Console.WriteLine("Consume Worked!");
return Success();
}
}
2023-09-06 14:52:20 +07:00
[HttpPost()]
public async Task<ActionResult<ResponseObject>> UpdatePropertyByUser([FromBody] NotiRequest req)
{
2023-09-08 15:23:41 +07:00
await _repositoryNoti.PushNotificationAsync(
2024-10-18 16:11:47 +07:00
Guid.Parse(req.ReceiverUserId),
2023-09-08 15:23:41 +07:00
req.Subject,
req.Body,
req.Payload,
2024-10-18 16:11:47 +07:00
"",
2023-09-08 15:23:41 +07:00
req.IsSendInbox,
req.IsSendMail
2024-10-18 16:11:47 +07:00
);
2023-09-08 15:23:41 +07:00
return Success();
}
2023-12-24 15:05:44 +07:00
[HttpPost("keycloak")]
public async Task<ActionResult<ResponseObject>> UpdatePropertyByUserKeycloak([FromBody] NotiRequest req)
{
2024-07-09 17:03:54 +07:00
var apiUrl = $"{_configuration["API"]}/org/profile/keycloakid/position/" + req.ReceiverUserId;
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", ""));
2025-11-12 01:56:06 +07:00
client.DefaultRequestHeaders.Add("api-key", _configuration["API_KEY"]);
2024-07-09 17:03:54 +07:00
var _req = new HttpRequestMessage(HttpMethod.Get, apiUrl);
var _res = await client.SendAsync(_req);
var _result = await _res.Content.ReadAsStringAsync();
var org = JsonConvert.DeserializeObject<OrgRequest>(_result);
if (org != null && org.result != null)
2024-06-30 20:52:38 +07:00
{
2024-07-09 17:03:54 +07:00
await _repositoryNoti.PushNotificationAsync(
Guid.Parse(org.result.id),
req.Subject,
req.Body,
req.Payload,
2024-10-18 16:11:47 +07:00
"",
2024-07-09 17:03:54 +07:00
req.IsSendInbox,
req.IsSendMail
);
2024-06-30 20:52:38 +07:00
}
2024-07-09 17:03:54 +07:00
return Success();
}
}
[HttpPost("profile")]
2024-07-10 15:02:45 +07:00
public async Task<ActionResult<ResponseObject>> UpdatePropertyByUserProfile([FromBody] NotiRequest req)
2024-07-09 17:03:54 +07:00
{
if (req.ReceiverUserId != null)
{
await _repositoryNoti.PushNotificationAsync(
Guid.Parse(req.ReceiverUserId),
req.Subject,
req.Body,
req.Payload,
2024-10-18 16:11:47 +07:00
"",
2024-07-09 17:03:54 +07:00
req.IsSendInbox,
req.IsSendMail
);
}
return Success();
2024-10-07 23:23:00 +07:00
}
2024-10-29 17:48:41 +07:00
[HttpPost("email")]
public async Task<ActionResult<ResponseObject>> SendEmail([FromBody] SendEmailRequest req)
{
await _repositoryNoti.PushEmailAsync(
req.Subject,
req.Body,
req.Email
);
return Success();
}
2024-10-07 23:23:00 +07:00
[HttpPost("profiles")]
public async Task<ActionResult<ResponseObject>> UpdatePropertyByUserProfiles([FromBody] NotisRequest req)
{
2024-10-18 16:11:47 +07:00
await _repositoryNoti.PushNotificationsLinkAsync(
req.ReceiverUserIds,
2024-10-07 23:23:00 +07:00
req.Subject,
req.Body,
req.Payload,
req.IsSendInbox,
req.IsSendMail
);
return Success();
2023-12-24 15:05:44 +07:00
}
2024-10-19 22:29:44 +07:00
[HttpPost("profiles-send")]
public async Task<ActionResult<ResponseObject>> UpdatePropertyByUserProfiles_send([FromBody] NotisSendRequest req)
{
await _repositoryNoti.PushNotificationsLinkSendAsync(
req.ReceiverUserIds,
req.Subject,
req.Body,
req.Payload
);
return Success();
}
2023-12-24 15:05:44 +07:00
[HttpPut("{id:length(36)}")]
public async Task<ActionResult<ResponseObject>> ReplyPropertyByUser([FromBody] NotiReplyRequest req, Guid id)
{
var inbox = await _context.Inboxes.FirstOrDefaultAsync(x => x.Id == id);
if (inbox == null)
return Error(GlobalMessages.DataNotFound);
2024-07-11 00:01:49 +07:00
if (inbox.CreatedUserId == null || inbox.CreatedUserId == "")
return Error("ข้อความนี้เป็นการแจ้งเตือนจากระบบไม่สามารถตอบกลับได้");
2023-12-24 15:05:44 +07:00
2024-07-11 00:01:49 +07:00
var apiUrl = $"{_configuration["API"]}/org/profile/keycloakid/position/{inbox.CreatedUserId}";
2024-07-09 17:03:54 +07:00
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", ""));
2025-11-12 01:56:06 +07:00
client.DefaultRequestHeaders.Add("api-key", _configuration["API_KEY"]);
2024-07-09 17:03:54 +07:00
var _req = new HttpRequestMessage(HttpMethod.Get, apiUrl);
var _res = await client.SendAsync(_req);
var _result = await _res.Content.ReadAsStringAsync();
var org = JsonConvert.DeserializeObject<OrgRequest>(_result);
if (org != null && org.result != null)
{
2024-07-09 17:03:54 +07:00
await _repositoryNoti.PushNotificationAsync(
Guid.Parse(org.result.id),
req.Subject,
req.Body,
req.Payload,
2024-10-18 16:11:47 +07:00
"",
2024-07-09 17:03:54 +07:00
req.IsSendInbox,
req.IsSendMail
2024-10-18 16:11:47 +07:00
);
}
2024-07-09 17:03:54 +07:00
return Success();
}
2023-12-24 15:05:44 +07:00
}
2024-11-19 12:57:33 +07:00
[HttpPost("send-mail")]
public async Task<ActionResult<ResponseObject>> sendEmailOnly([FromBody] NotiEmailRequest req)
{
await _repositoryNoti.PushEmailAsync(
req.Subject,
req.Body,
req.Email
);
return Success();
}
2023-09-08 15:23:41 +07:00
[HttpPost("cronjob")]
public async Task<ActionResult<ResponseObject>> CornjobProbation([FromBody] NotiCronjobProbationRequest req)
{
// var profile = await _context.Profiles.FirstOrDefaultAsync(x => x.Id == req.ReceiverUserId);
// if (profile == null)
// return Error(GlobalMessages.DataNotFound);
2023-09-08 15:23:41 +07:00
_context.CronjobNotiProbations.Add(new CronjobNotiProbation
2023-09-06 14:52:20 +07:00
{
2023-09-08 15:23:41 +07:00
IsSendNoti = false,
Subject = req.Subject,
Body = req.Body,
ReceiverUserId = req.ReceiverUserId,
Payload = req.Payload,
IsSendMail = req.IsSendMail,
IsSendInbox = req.IsSendInbox,
ReceiveDate = req.ReceiveDate,
CreatedFullName = FullName ?? "System Administrator",
CreatedUserId = UserId ?? "",
2023-09-08 15:23:41 +07:00
CreatedAt = DateTime.Now,
LastUpdateFullName = FullName ?? "System Administrator",
LastUpdateUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now,
});
2023-09-06 14:52:20 +07:00
await _context.SaveChangesAsync();
return Success();
}
}
}