Add SendEmail ไปที่ระบบออกคำสั่ง

This commit is contained in:
Suphonchai Phoonsawat 2023-09-05 14:43:46 +07:00
parent 648d940b57
commit 68a8ca9d9f
6 changed files with 69 additions and 16 deletions

View file

@ -1,4 +1,5 @@
using BMA.EHR.Application.Repositories; using BMA.EHR.Application.Messaging;
using BMA.EHR.Application.Repositories;
using BMA.EHR.Application.Repositories.Commands; using BMA.EHR.Application.Repositories.Commands;
using BMA.EHR.Application.Repositories.MessageQueue; using BMA.EHR.Application.Repositories.MessageQueue;
using BMA.EHR.Application.Repositories.Reports; using BMA.EHR.Application.Repositories.Reports;
@ -28,6 +29,8 @@ namespace BMA.EHR.Application
services.AddTransient<InsigniaReportRepository>(); services.AddTransient<InsigniaReportRepository>();
services.AddTransient<CommandReportRepository>(); services.AddTransient<CommandReportRepository>();
services.AddTransient<EmailSenderService>();
return services; return services;
} }
} }

View file

@ -3,7 +3,7 @@ using Microsoft.Extensions.Configuration;
using System.Net; using System.Net;
using System.Net.Mail; using System.Net.Mail;
namespace BMA.EHR.Infrastructure.Messaging namespace BMA.EHR.Application.Messaging
{ {
public class EmailSenderService public class EmailSenderService
{ {

View file

@ -1,4 +1,5 @@
using BMA.EHR.Application.Common.Interfaces; using BMA.EHR.Application.Common.Interfaces;
using BMA.EHR.Application.Messaging;
using BMA.EHR.Application.Requests.Commands; using BMA.EHR.Application.Requests.Commands;
using BMA.EHR.Application.Responses; using BMA.EHR.Application.Responses;
using BMA.EHR.Domain.Extensions; using BMA.EHR.Domain.Extensions;
@ -30,6 +31,7 @@ namespace BMA.EHR.Application.Repositories.Commands
private readonly OrganizationCommonRepository _organizationCommonRepository; private readonly OrganizationCommonRepository _organizationCommonRepository;
private readonly UserProfileRepository _userProfileRepository; private readonly UserProfileRepository _userProfileRepository;
private readonly IConfiguration _configuration; private readonly IConfiguration _configuration;
private readonly EmailSenderService _emailSenderService;
#endregion #endregion
@ -39,13 +41,15 @@ namespace BMA.EHR.Application.Repositories.Commands
IHttpContextAccessor httpContextAccessor, IHttpContextAccessor httpContextAccessor,
OrganizationCommonRepository organizationCommonRepository, OrganizationCommonRepository organizationCommonRepository,
UserProfileRepository userProfileRepository, UserProfileRepository userProfileRepository,
IConfiguration configuration) : base(dbContext, httpContextAccessor) IConfiguration configuration,
EmailSenderService emailSenderService) : base(dbContext, httpContextAccessor)
{ {
_dbContext = dbContext; _dbContext = dbContext;
_httpContextAccessor = httpContextAccessor; _httpContextAccessor = httpContextAccessor;
_organizationCommonRepository = organizationCommonRepository; _organizationCommonRepository = organizationCommonRepository;
_userProfileRepository = userProfileRepository; _userProfileRepository = userProfileRepository;
_configuration = configuration; _configuration = configuration;
_emailSenderService = emailSenderService;
} }
#endregion #endregion
@ -1758,7 +1762,13 @@ namespace BMA.EHR.Application.Repositories.Commands
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync();
// TODO: ต้องเปลี่ยนเป็น Email จริงนะ ตอนนี้ Hardcode อยู่
// Send noti inbox and email // Send noti inbox and email
var subject = $"คุณได้รับคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}";
var body = $"คุณได้รับบรรจุเป็นข้าราชการกรุงเทพมหานครสามัญ ตามคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}";
_emailSenderService.SendMail(subject, body, "dev@frappet.com");
var inbox = new Inbox var inbox = new Inbox
{ {
Subject = $"คุณได้รับคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", Subject = $"คุณได้รับคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}",
@ -1777,6 +1787,8 @@ namespace BMA.EHR.Application.Repositories.Commands
}; };
_dbContext.Set<Notification>().Add(noti); _dbContext.Set<Notification>().Add(noti);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync();
} }
// send cc noti inbox // send cc noti inbox
@ -1785,14 +1797,27 @@ namespace BMA.EHR.Application.Repositories.Commands
var pf = await _dbContext.Set<Profile>().FirstOrDefaultAsync(x => x.CitizenId == cc.CitizenId); var pf = await _dbContext.Set<Profile>().FirstOrDefaultAsync(x => x.CitizenId == cc.CitizenId);
if (pf != null) if (pf != null)
{ {
var inbox = new Inbox if (cc.IsSendInbox)
{ {
Subject = $"คุณได้รับสำเนาคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", var inbox = new Inbox
Body = $"คำสั่งบบรรจุเป็นข้าราชการกรุงเทพมหานครสามัญ คำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", {
ReceiverUserId = pf.Id, Subject = $"คุณได้รับสำเนาคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}",
Payload = "", Body = $"คำสั่งบบรรจุเป็นข้าราชการกรุงเทพมหานครสามัญ คำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}",
}; ReceiverUserId = pf.Id,
_dbContext.Set<Inbox>().Add(inbox); Payload = "",
};
_dbContext.Set<Inbox>().Add(inbox);
}
if (cc.IsSendMail)
{
// TODO: ต้องเปลี่ยนเป็น Email จริงนะ ตอนนี้ Hardcode อยู่
// Send noti inbox and email
var subject = $"คุณได้รับสำเนาคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}";
var body = $"คำสั่งบบรรจุเป็นข้าราชการกรุงเทพมหานครสามัญ คำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}";
_emailSenderService.SendMail(subject, body, "dev@frappet.com");
}
var noti = new Notification var noti = new Notification
{ {

View file

@ -1,4 +1,5 @@
using BMA.EHR.Application.Repositories; using BMA.EHR.Application.Messaging;
using BMA.EHR.Application.Repositories;
using BMA.EHR.Application.Repositories.Commands; using BMA.EHR.Application.Repositories.Commands;
using BMA.EHR.Application.Requests.Commands; using BMA.EHR.Application.Requests.Commands;
using BMA.EHR.Application.Responses; using BMA.EHR.Application.Responses;
@ -12,7 +13,6 @@ using BMA.EHR.Infrastructure.Persistence;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations; using Swashbuckle.AspNetCore.Annotations;
using System.Net;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Security.Claims; using System.Security.Claims;
@ -37,6 +37,7 @@ namespace BMA.EHR.Command.Service.Controllers
private readonly CommandTypeRepository _commandTypeRepository; private readonly CommandTypeRepository _commandTypeRepository;
private readonly CommandStatusRepository _commandStatusRepository; private readonly CommandStatusRepository _commandStatusRepository;
private readonly UserProfileRepository _userProfileRepository; private readonly UserProfileRepository _userProfileRepository;
private readonly EmailSenderService _emailSenderService;
#endregion #endregion
@ -50,7 +51,8 @@ namespace BMA.EHR.Command.Service.Controllers
PrefixRepository prefixRepository, PrefixRepository prefixRepository,
CommandTypeRepository commandTypeRepository, CommandTypeRepository commandTypeRepository,
CommandStatusRepository commandStatusRepository, CommandStatusRepository commandStatusRepository,
UserProfileRepository userProfileRepository) UserProfileRepository userProfileRepository,
EmailSenderService emailSenderService)
{ {
_repository = repository; _repository = repository;
_context = context; _context = context;
@ -61,6 +63,7 @@ namespace BMA.EHR.Command.Service.Controllers
_commandTypeRepository = commandTypeRepository; _commandTypeRepository = commandTypeRepository;
_commandStatusRepository = commandStatusRepository; _commandStatusRepository = commandStatusRepository;
_userProfileRepository = userProfileRepository; _userProfileRepository = userProfileRepository;
_emailSenderService = emailSenderService;
} }
#endregion #endregion
@ -88,6 +91,22 @@ namespace BMA.EHR.Command.Service.Controllers
#region " Methods " #region " Methods "
//[HttpGet("mail")]
//[AllowAnonymous]
//public IActionResult TestSendMail()
//{
// try
// {
// _emailSenderService.SendMail("test send mail", "test body", "suphonchai@frappet.com");
// return Ok();
// }
// catch
// {
// throw;
// }
//}
/// <summary> /// <summary>
/// แสดงปีเป็นปีพุทธศักราช โดยดึงจากข้อมูลที่มีในระบบ /// แสดงปีเป็นปีพุทธศักราช โดยดึงจากข้อมูลที่มีในระบบ
/// </summary> /// </summary>

View file

@ -34,5 +34,12 @@
"Protocol": "HTTPS", "Protocol": "HTTPS",
"Node": { "Node": {
"API": "https://bma-ehr.frappet.synology.me/api/v1/probation" "API": "https://bma-ehr.frappet.synology.me/api/v1/probation"
},
"Mail": {
"Server": "smtp.gmail.com",
"User": "suphonchai.ph@gmail.com",
"Password": "nnjazjcnwhepkxdm",
"MailFrom": "suphonchai.ph@gmail.com",
"Port": "587"
} }
} }

View file

@ -1,6 +1,5 @@
using BMA.EHR.Application.Common.Interfaces; using BMA.EHR.Application.Common.Interfaces;
using BMA.EHR.Application.Repositories; using BMA.EHR.Application.Repositories;
using BMA.EHR.Infrastructure.Messaging;
using BMA.EHR.Infrastructure.Persistence; using BMA.EHR.Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
@ -14,7 +13,7 @@ namespace BMA.EHR.Infrastructure
IConfiguration configuration) IConfiguration configuration)
{ {
services.AddTransient<MinIOService>(); services.AddTransient<MinIOService>();
services.AddTransient<EmailSenderService>();
var connectionString = configuration.GetConnectionString("DefaultConnection"); var connectionString = configuration.GetConnectionString("DefaultConnection");