แก้บั้กออกคำสั่ง + ใส่ Payload ใน inbox + noti

This commit is contained in:
Suphonchai Phoonsawat 2023-09-11 13:53:53 +07:00
parent e451149b2f
commit 13c059e792
9 changed files with 785 additions and 222 deletions

View file

@ -1,9 +1,13 @@
using BMA.EHR.Application.Common.Interfaces;
using BMA.EHR.Application.Responses;
using BMA.EHR.Application.Responses.Messages;
using BMA.EHR.Domain.Models.HR;
using BMA.EHR.Domain.Models.Notifications;
using BMA.EHR.Domain.Shared;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
namespace BMA.EHR.Application.Repositories.MessageQueue
{
@ -29,7 +33,7 @@ namespace BMA.EHR.Application.Repositories.MessageQueue
#region " Methods "
public async Task<List<Inbox>> GetMyInboxAsync()
public async Task<List<InboxResponse>> GetMyInboxAsync()
{
try
{
@ -44,6 +48,17 @@ namespace BMA.EHR.Application.Repositories.MessageQueue
var data = await _dbContext.Set<Inbox>()
.Where(x => x.ReceiverUserId == profile.Id)
.OrderByDescending(x => x.ReceiveDate)
.Select(x => new InboxResponse
{
Subject = x.Subject,
Body = x.Body,
ReceiverUserId = x.ReceiverUserId,
IsOpen = x.IsOpen,
ReceiveDate = x.ReceiveDate,
OpenDate = x.OpenDate,
Payload = x.Payload == "" ? null : JsonConvert.DeserializeObject<CommandPayload>(Regex.Unescape(x.Payload))
})
.Take(20)
.ToListAsync();
return data;

View file

@ -1,9 +1,13 @@
using BMA.EHR.Application.Common.Interfaces;
using BMA.EHR.Application.Responses;
using BMA.EHR.Application.Responses.Messages;
using BMA.EHR.Domain.Models.HR;
using BMA.EHR.Domain.Models.Notifications;
using BMA.EHR.Domain.Shared;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
namespace BMA.EHR.Application.Repositories.MessageQueue
{
@ -29,7 +33,7 @@ namespace BMA.EHR.Application.Repositories.MessageQueue
#region " Methods "
public async Task<List<Notification>> GetMyNotificationAsync()
public async Task<List<NotificationResponse>> GetMyNotificationAsync()
{
try
{
@ -44,6 +48,17 @@ namespace BMA.EHR.Application.Repositories.MessageQueue
var data = await _dbContext.Set<Notification>()
.Where(x => x.ReceiverUserId == profile.Id)
.OrderByDescending(x => x.ReceiveDate)
.Select(x => new NotificationResponse
{
Body = x.Body,
ReceiverUserId = x.ReceiverUserId,
IsOpen = x.IsOpen,
Type = x.Type,
ReceiveDate = x.ReceiveDate,
OpenDate = x.OpenDate,
Payload = x.Payload == "" ? null : JsonConvert.DeserializeObject<CommandPayload>(Regex.Unescape(x.Payload))
})
.Take(20)
.ToListAsync();
return data;

View file

@ -0,0 +1,14 @@
namespace BMA.EHR.Application.Responses
{
public class CommandPayload
{
public List<PayloadAttachment> attachments { get; set; }
}
public class PayloadAttachment
{
public string name { get; set; }
public string url { get; set; }
}
}

View file

@ -0,0 +1,19 @@
namespace BMA.EHR.Application.Responses.Messages
{
public class InboxResponse
{
public string Subject { get; set; } = string.Empty;
public string Body { get; set; } = string.Empty;
public Guid ReceiverUserId { get; set; } = Guid.Empty;
public CommandPayload Payload { get; set; } = new CommandPayload();
public bool IsOpen { get; set; } = false;
public DateTime ReceiveDate { get; set; } = DateTime.Now;
public DateTime? OpenDate { get; set; }
}
}

View file

@ -0,0 +1,19 @@
namespace BMA.EHR.Application.Responses.Messages
{
public class NotificationResponse
{
public string Body { get; set; } = string.Empty;
public Guid ReceiverUserId { get; set; } = Guid.Empty;
public string Type { get; set; } = "TEXT";
public CommandPayload Payload { get; set; } = new();
public bool IsOpen { get; set; } = false;
public DateTime ReceiveDate { get; set; } = DateTime.Now;
public DateTime? OpenDate { get; set; }
}
}

View file

@ -12,6 +12,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="iTextSharp" Version="5.5.13.3" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.9" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.9" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.1.0" />

View file

@ -10,8 +10,11 @@ using BMA.EHR.Domain.Extensions;
using BMA.EHR.Domain.Models.Commands.Core;
using BMA.EHR.Domain.Shared;
using BMA.EHR.Infrastructure.Persistence;
using iTextSharp.text;
using iTextSharp.text.pdf;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Swashbuckle.AspNetCore.Annotations;
using System.Net.Http.Headers;
using System.Security.Claims;
@ -38,6 +41,12 @@ namespace BMA.EHR.Command.Service.Controllers
private readonly CommandStatusRepository _commandStatusRepository;
private readonly UserProfileRepository _userProfileRepository;
private readonly EmailSenderService _emailSenderService;
private readonly IWebHostEnvironment _hostingEnvironment;
private readonly MinIOService _minIOService;
private readonly IConfiguration _configuration;
// for add watermark
private BaseFont baseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
#endregion
@ -52,7 +61,10 @@ namespace BMA.EHR.Command.Service.Controllers
CommandTypeRepository commandTypeRepository,
CommandStatusRepository commandStatusRepository,
UserProfileRepository userProfileRepository,
EmailSenderService emailSenderService)
EmailSenderService emailSenderService,
IWebHostEnvironment hostingEnvironment,
MinIOService minIOService,
IConfiguration configuration)
{
_repository = repository;
_context = context;
@ -64,6 +76,9 @@ namespace BMA.EHR.Command.Service.Controllers
_commandStatusRepository = commandStatusRepository;
_userProfileRepository = userProfileRepository;
_emailSenderService = emailSenderService;
_hostingEnvironment = hostingEnvironment;
_minIOService = minIOService;
_configuration = configuration;
}
#endregion
@ -91,21 +106,58 @@ namespace BMA.EHR.Command.Service.Controllers
#region " Methods "
//[HttpGet("mail")]
//[AllowAnonymous]
//public IActionResult TestSendMail()
//{
// try
// {
// _emailSenderService.SendMail("test send mail", "test body", "suphonchai@frappet.com");
#region " Add Watermark "
// return Ok();
// }
// catch
// {
// throw;
// }
//}
private void AddWaterMarkText(PdfContentByte pdfData, string watermarkText, BaseFont font, float fontSize, float angle, BaseColor color, Rectangle realPageSize)
{
var gstate = new PdfGState { FillOpacity = 0.5f, StrokeOpacity = 0.3f };
pdfData.SaveState();
pdfData.SetGState(gstate);
pdfData.SetColorFill(color);
pdfData.BeginText();
pdfData.SetFontAndSize(font, fontSize);
var x = (realPageSize.Right + realPageSize.Left) / 2;
var y = (realPageSize.Bottom + realPageSize.Top) / 2;
pdfData.ShowTextAligned(Element.ALIGN_CENTER, watermarkText, x, y, angle);
pdfData.EndText();
pdfData.RestoreState();
}
private bool AddWatermark(string inputfilepath, string outputfilepath, string watermark_text)
{
PdfReader pdfReader = null;
PdfStamper pdfStamper = null;
try
{
pdfReader = new PdfReader(inputfilepath);
int numberOfPages = pdfReader.NumberOfPages;
iTextSharp.text.Rectangle pagesize = pdfReader.GetPageSize(1);
pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, FileMode.Create));
for (int i = 1; i <= numberOfPages; i++)
{
var dc = pdfStamper.GetOverContent(i);
AddWaterMarkText(dc, watermark_text, baseFont, 30, 45, BaseColor.DARK_GRAY, pdfReader.GetPageSizeWithRotation(i));
}
return true;
}
catch (Exception ex)
{
ex.Message.Trim();
return false;
}
finally
{
if (pdfStamper != null)
pdfStamper.Close();
if (pdfReader != null)
pdfReader.Close();
}
}
#endregion
/// <summary>
/// แสดงปีเป็นปีพุทธศักราช โดยดึงจากข้อมูลที่มีในระบบ
@ -2602,7 +2654,7 @@ namespace BMA.EHR.Command.Service.Controllers
try
{
var rawData = commandCode.Trim().ToUpper() == "C-PM-01" ? await _placementRepository.GetCompetitivePlacementAsync() :
commandCode.Trim().ToUpper() == "C-PM-02" ? await _placementRepository.GetQualifyingPlacementAsync() :
commandCode.Trim().ToUpper() == "C-PM-02" ? await _placementRepository.GetQualifyingPlacementAsync() :
await _placementRepository.GetAllPlacementAsync();
@ -3027,6 +3079,96 @@ namespace BMA.EHR.Command.Service.Controllers
#region " Documents "
/// <summary>
/// Download เอกสารแนบ
/// </summary>
/// <param name="docId">Record Id ของเอกสารแนบ</param>
/// <returns></returns>
/// <response code="200">เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpGet("download/attachment/{docId}")]
[AllowAnonymous]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> DownloadAttachment(Guid docId)
{
var now = DateTime.Now.ToString("yyyyMMdd-HHmmss");
var file = Path.Combine(_hostingEnvironment.ContentRootPath, "tmp", $"tmp-{now}.pdf");
var file_copy = Path.Combine(_hostingEnvironment.ContentRootPath, "tmp", $"tmp-{now}-copy.pdf");
try
{
var doc = await _minIOService.DownloadFileAsync(docId);
// Copy to File
System.IO.File.WriteAllBytes(file, doc.FileContent);
var watermark_text = "COPY COPY COPY COPY";
var fName = Path.GetFileName(file);
AddWatermark(file, file_copy, watermark_text);
using (var fileStream = new System.IO.FileStream(file_copy, FileMode.Open, FileAccess.Read))
{
using (var ms = new MemoryStream())
{
fileStream.CopyTo(ms);
ms.Flush();
return File(ms.ToArray(), "application/octet-stream", fName);
}
}
}
catch
{
throw;
}
finally
{
if (System.IO.File.Exists(file))
System.IO.File.Delete(file);
if (System.IO.File.Exists(file_copy))
System.IO.File.Delete(file_copy);
}
}
[HttpGet("gen-payload/{orderId}")]
[AllowAnonymous]
public async Task<ActionResult<ResponseObject>> GetPayloadStr(Guid orderId)
{
try
{
var command = await _repository.GetByIdAsync(orderId);
if (command == null)
throw new Exception(GlobalMessages.CommandNotFound);
// create command payload
var payload_attach = command.Documents
.Select(x => new PayloadAttachment
{
name = x.Category == "cover" ? "สำเนาคำสั่ง" : "สำเนาเอกสารแนบท้าย",
url = $"{_configuration["API"]}/order/download/attachment/{x.Document.Id}"
})
.ToList();
var payload = new CommandPayload()
{
attachments = payload_attach
};
var payload_str = JsonConvert.SerializeObject(payload);
return Success(payload_str);
}
catch
{
throw;
}
}
/// <summary>
/// PM7-34 : ข้อมูลรายละเอียดคำสั่งและแนบท้าย
/// </summary>

View file

@ -41,5 +41,6 @@
"Password": "nnjazjcnwhepkxdm",
"MailFrom": "suphonchai.ph@gmail.com",
"Port": "587"
}
},
"API": "https://bma-ehr.frappet.synology.me/api/v1"
}