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

147 lines
5.4 KiB
C#
Raw 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;
using BMA.EHR.Domain.Extensions;
using BMA.EHR.Domain.Models.MetaData;
using BMA.EHR.Domain.Models.Notifications;
using BMA.EHR.Domain.Models.Placement;
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;
using Swashbuckle.AspNetCore.Annotations;
using System.Security.Claims;
using System.Security.Cryptography;
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;
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,
2023-09-06 14:52:20 +07:00
IHttpContextAccessor httpContextAccessor)
{
_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;
}
#region " Properties "
private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value;
#endregion
[HttpPost()]
public async Task<ActionResult<ResponseObject>> UpdatePropertyByUser([FromBody] NotiRequest 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
await _repositoryNoti.PushNotificationAsync(
req.ReceiverUserId,
req.Subject,
req.Body,
req.Payload,
req.IsSendInbox,
req.IsSendMail
);
return Success();
}
2023-12-24 15:05:44 +07:00
[HttpPost("keycloak")]
public async Task<ActionResult<ResponseObject>> UpdatePropertyByUserKeycloak([FromBody] NotiRequest req)
{
var profile = await _context.Profiles.FirstOrDefaultAsync(x => x.KeycloakId == req.ReceiverUserId);
if (profile == null)
return Error(GlobalMessages.DataNotFound);
await _repositoryNoti.PushNotificationAsync(
profile.Id,
req.Subject,
req.Body,
req.Payload,
req.IsSendInbox,
req.IsSendMail
);
return Success();
}
[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);
var profile = await _context.Profiles.FirstOrDefaultAsync(x => x.KeycloakId == Guid.Parse(inbox.CreatedUserId));
if (profile == null)
return Error(GlobalMessages.DataNotFound);
await _repositoryNoti.PushNotificationAsync(
profile.Id,
req.Subject,
req.Body,
req.Payload,
req.IsSendInbox,
req.IsSendMail
);
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);
_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();
}
}
}