Add SendEmail ไปที่ระบบออกคำสั่ง
This commit is contained in:
parent
648d940b57
commit
68a8ca9d9f
6 changed files with 69 additions and 16 deletions
|
|
@ -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.MessageQueue;
|
||||
using BMA.EHR.Application.Repositories.Reports;
|
||||
|
|
@ -28,6 +29,8 @@ namespace BMA.EHR.Application
|
|||
services.AddTransient<InsigniaReportRepository>();
|
||||
services.AddTransient<CommandReportRepository>();
|
||||
|
||||
services.AddTransient<EmailSenderService>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
60
BMA.EHR.Application/Messaging/EmailSenderService.cs
Normal file
60
BMA.EHR.Application/Messaging/EmailSenderService.cs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
using BMA.EHR.Domain.Extensions;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System.Net;
|
||||
using System.Net.Mail;
|
||||
|
||||
namespace BMA.EHR.Application.Messaging
|
||||
{
|
||||
public class EmailSenderService
|
||||
{
|
||||
#region " Fields "
|
||||
|
||||
private readonly IConfiguration _configuration;
|
||||
|
||||
#endregion
|
||||
|
||||
#region " Constructor and Destructor "
|
||||
|
||||
public EmailSenderService(IConfiguration configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region " Methods "
|
||||
|
||||
public void SendMail(string subject, string body, string receiver)
|
||||
{
|
||||
try
|
||||
{
|
||||
var server = _configuration["Mail:Server"];
|
||||
var user = _configuration["Mail:User"];
|
||||
var password = _configuration["Mail:Password"];
|
||||
var port = _configuration["Mail:Port"];
|
||||
var from = _configuration["Mail:MailFrom"];
|
||||
|
||||
var client = new SmtpClient(server, port.ToInteger());
|
||||
client.UseDefaultCredentials = false;
|
||||
client.Credentials = new NetworkCredential(user, password);
|
||||
client.EnableSsl = true;
|
||||
client.DeliveryMethod = SmtpDeliveryMethod.Network;
|
||||
|
||||
|
||||
var mail = new MailMessage();
|
||||
mail.From = new MailAddress(from, "eHR Bangkok Automation System");
|
||||
mail.To.Add(receiver);
|
||||
mail.Subject = subject;
|
||||
mail.Body = body;
|
||||
mail.IsBodyHtml = true;
|
||||
client.Send(mail);
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
using BMA.EHR.Application.Common.Interfaces;
|
||||
using BMA.EHR.Application.Messaging;
|
||||
using BMA.EHR.Application.Requests.Commands;
|
||||
using BMA.EHR.Application.Responses;
|
||||
using BMA.EHR.Domain.Extensions;
|
||||
|
|
@ -30,6 +31,7 @@ namespace BMA.EHR.Application.Repositories.Commands
|
|||
private readonly OrganizationCommonRepository _organizationCommonRepository;
|
||||
private readonly UserProfileRepository _userProfileRepository;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly EmailSenderService _emailSenderService;
|
||||
|
||||
#endregion
|
||||
|
||||
|
|
@ -39,13 +41,15 @@ namespace BMA.EHR.Application.Repositories.Commands
|
|||
IHttpContextAccessor httpContextAccessor,
|
||||
OrganizationCommonRepository organizationCommonRepository,
|
||||
UserProfileRepository userProfileRepository,
|
||||
IConfiguration configuration) : base(dbContext, httpContextAccessor)
|
||||
IConfiguration configuration,
|
||||
EmailSenderService emailSenderService) : base(dbContext, httpContextAccessor)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
_organizationCommonRepository = organizationCommonRepository;
|
||||
_userProfileRepository = userProfileRepository;
|
||||
_configuration = configuration;
|
||||
_emailSenderService = emailSenderService;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
|
@ -1758,7 +1762,13 @@ namespace BMA.EHR.Application.Repositories.Commands
|
|||
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
// Send noti inbox and email
|
||||
// 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 inbox = new Inbox
|
||||
{
|
||||
Subject = $"คุณได้รับคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}",
|
||||
|
|
@ -1777,6 +1787,8 @@ namespace BMA.EHR.Application.Repositories.Commands
|
|||
};
|
||||
_dbContext.Set<Notification>().Add(noti);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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);
|
||||
if (pf != null)
|
||||
{
|
||||
var inbox = new Inbox
|
||||
if (cc.IsSendInbox)
|
||||
{
|
||||
Subject = $"คุณได้รับสำเนาคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}",
|
||||
Body = $"คำสั่งบบรรจุเป็นข้าราชการกรุงเทพมหานครสามัญ คำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}",
|
||||
ReceiverUserId = pf.Id,
|
||||
Payload = "",
|
||||
};
|
||||
_dbContext.Set<Inbox>().Add(inbox);
|
||||
var inbox = new Inbox
|
||||
{
|
||||
Subject = $"คุณได้รับสำเนาคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}",
|
||||
Body = $"คำสั่งบบรรจุเป็นข้าราชการกรุงเทพมหานครสามัญ คำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}",
|
||||
ReceiverUserId = pf.Id,
|
||||
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
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue