diff --git a/Program.cs b/Program.cs index e654d4b..5cf91a7 100644 --- a/Program.cs +++ b/Program.cs @@ -84,6 +84,7 @@ builder.Services.AddCors(options => options.AddDefaultPolicy(builder => builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddTransient(); builder.Services.AddTransient(); // Add services to the container. diff --git a/Services/CandidateService.cs b/Services/CandidateService.cs index 01b9ed2..acf13ca 100644 --- a/Services/CandidateService.cs +++ b/Services/CandidateService.cs @@ -18,6 +18,7 @@ namespace BMA.EHR.Recurit.Exam.Service.Services private readonly MetadataDbContext _contextMetadata; private readonly IHttpContextAccessor _httpContextAccessor; private readonly MinIOService _minioService; + private readonly MailService _mailService; #endregion @@ -26,12 +27,14 @@ namespace BMA.EHR.Recurit.Exam.Service.Services public CandidateService(ApplicationDbContext context, MetadataDbContext contextMetadata, IHttpContextAccessor httpContextAccessor, - MinIOService minioService) + MinIOService minioService, + MailService mailService) { _context = context; _contextMetadata = contextMetadata; _httpContextAccessor = httpContextAccessor; _minioService = minioService; + _mailService = mailService; } #endregion @@ -1707,6 +1710,18 @@ namespace BMA.EHR.Recurit.Exam.Service.Services if (candidate == null) throw new Exception(GlobalMessages.CandidateNotFound); + if (status == "checkRegister") + { + var subject = "แจ้งผลการสมัครสอบคัดเลือก " + exam.Name; + var body = candidate.FirstName + " " + candidate.LastName + " สถานะการสมัครสอบ: รอเจ้าหน้าที่ตรวจสอบข้อมูล"; + _mailService.SendMailToUser(subject, body, "kittapath@frappet.com"); + } + if (status == "checkPayment") + { + var subject = "แจ้งผลการสมัครสอบคัดเลือก " + exam.Name; + var body = candidate.FirstName + " " + candidate.LastName + " สถานะการสมัครสอบ: รอเจ้าหน้าที่ตรวจสอบหลักฐานชำระเงิน"; + _mailService.SendMailToUser(subject, body, "kittapath@frappet.com"); + } candidate.Status = status; await _context.SaveChangesAsync(); @@ -1735,24 +1750,49 @@ namespace BMA.EHR.Recurit.Exam.Service.Services if (status != "rejectDelete") { candidate.Status = status; - if (status == "rejectRegister" || status == "rejectPayment") + if (status == "rejectRegister") { + var subject = "แจ้งผลการสมัครสอบคัดเลือก " + candidate.PeriodExam.Name; + var body = candidate.FirstName + " " + candidate.LastName + " มีคุณสมบัติสมัครสอบไม่ผ่านกรุณาตรวจสอบข้อมูล"; + if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); + candidate.RejectDetail = item.Reason; + } + if (status == "rejectPayment") + { + var subject = "แจ้งผลการสมัครสอบคัดเลือก " + candidate.PeriodExam.Name; + var body = candidate.FirstName + " " + candidate.LastName + " มีหลักฐานชำระเงินไม่ถูกต้องกรุณาตรวจสอบข้อมูล"; + if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); candidate.RejectDetail = item.Reason; } else if (status == "payment" && candidate.PeriodExam.Fee == 0) { + var subject = "แจ้งผลการสมัครสอบคัดเลือก " + candidate.PeriodExam.Name; + var body = candidate.FirstName + " " + candidate.LastName + " สถานะการสมัครสอบ: รอเจ้าหน้าที่จัดที่นั่งสอบ"; + if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); candidate.Status = "checkSeat"; var num = periodExam.Count() + 1; candidate.ExamIdenNumber = "CDC-" + num; } + else if (status == "payment" && candidate.PeriodExam.Fee != 0) + { + var subject = "แจ้งผลการสมัครสอบคัดเลือก " + candidate.PeriodExam.Name; + var body = candidate.FirstName + " " + candidate.LastName + " สถานะการสมัครสอบ: รอชำระค่าสมัครสอบ"; + if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); + } if (candidate.Status == "checkSeat") { + var subject = "แจ้งผลการสมัครสอบคัดเลือก " + candidate.PeriodExam.Name; + var body = candidate.FirstName + " " + candidate.LastName + " สถานะการสมัครสอบ: รอเจ้าหน้าที่จัดที่นั่งสอบ"; + if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); var num = periodExam.Count() + 1; candidate.ExamIdenNumber = "CDC-" + num; } } else { + var subject = "แจ้งผลการสมัครสอบคัดเลือก " + candidate.PeriodExam.Name; + var body = candidate.FirstName + " " + candidate.LastName + " มีคุณสมบัติไม่ผ่านตามเงื่อนไข"; + if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); _context.Candidates.Remove(candidate); } @@ -1762,11 +1802,27 @@ namespace BMA.EHR.Recurit.Exam.Service.Services public async Task AdminPassCandidateService(string candidateId, string status) { var candidate = await _context.Candidates.AsQueryable() + .Include(x => x.PeriodExam) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(candidateId)); if (candidate == null) throw new Exception(GlobalMessages.CandidateNotFound); + if (candidate.PeriodExam == null) + throw new Exception(GlobalMessages.ExamNotFound); + + if (status == "done") + { + var subject = "แจ้งผลการสมัครสอบคัดเลือก " + candidate.PeriodExam.Name; + var body = candidate.FirstName + " " + candidate.LastName + " สถานะการสมัครสอบ: สอบคัดเลือกสำเร็จ"; + if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); + } + if (status == "checkPoint") + { + var subject = "แจ้งผลการสมัครสอบคัดเลือก " + candidate.PeriodExam.Name; + var body = candidate.FirstName + " " + candidate.LastName + " สถานะการสมัครสอบ: รอเจ้าหน้าที่สรุปคะแนนสอบ"; + if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); + } candidate.Status = status; await _context.SaveChangesAsync(); diff --git a/Services/MailService.cs b/Services/MailService.cs new file mode 100644 index 0000000..2de3002 --- /dev/null +++ b/Services/MailService.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using BMA.EHR.Recurit.Exam.Service.Data; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using OfficeOpenXml; +using System.Data; +using System.Net; +using System.Net.Mail; + +namespace BMA.EHR.Recurit.Exam.Service.Services +{ + public class MailService + { + #region " Fields " + + private readonly ApplicationDbContext _context; + private readonly IConfiguration _configuration; + private readonly IWebHostEnvironment _webHostEnvironment; + + #endregion + + #region " Constructors " + + public MailService(ApplicationDbContext context, + IConfiguration configuration, + IWebHostEnvironment webHostEnvironment) + { + _context = context; + _configuration = configuration; + _webHostEnvironment = webHostEnvironment; + } + + public static IConfigurationRoot Configuration + { + get + { + return new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json") + .Build(); + } + } + + #endregion + + #region " Methods " + + public void SendMailToUser(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, Convert.ToInt32(port)); + 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, "ทรัพยากรบุคคลกรุงเทพมหานคร"); + mail.To.Add(receiver); + mail.Subject = subject; + mail.Body = body; + mail.IsBodyHtml = true; + client.Send(mail); + } + catch + { + throw; + } + } + #endregion + } +} \ No newline at end of file diff --git a/Services/PeriodExamService.cs b/Services/PeriodExamService.cs index 72fa073..b84c6de 100644 --- a/Services/PeriodExamService.cs +++ b/Services/PeriodExamService.cs @@ -21,6 +21,7 @@ namespace BMA.EHR.Recurit.Exam.Service.Services private readonly MetadataDbContext _contextMetadata; private readonly IHttpContextAccessor _httpContextAccessor; private readonly MinIOService _minioService; + private readonly MailService _mailService; #endregion @@ -29,12 +30,14 @@ namespace BMA.EHR.Recurit.Exam.Service.Services public PeriodExamService(ApplicationDbContext context, MetadataDbContext contextMetadata, IHttpContextAccessor httpContextAccessor, - MinIOService minioService) + MinIOService minioService, + MailService mailService) { _context = context; _contextMetadata = contextMetadata; _httpContextAccessor = httpContextAccessor; _minioService = minioService; + _mailService = mailService; } #endregion @@ -812,15 +815,30 @@ namespace BMA.EHR.Recurit.Exam.Service.Services if (candidate.Status == "checkSeat") { candidate.SeatNumber = item.SeatNumber; + var subject = "แจ้งผลการสมัครสอบคัดเลือก " + periodExam.Name; + var body = candidate.FirstName + " " + candidate.LastName + " สถานะการสมัครสอบ: รอเจ้าหน้าที่สรุปคะแนนสอบ"; + if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); candidate.Status = "checkPoint"; } else { + if (candidate.Status != "waiver") + { + var subject = "แจ้งผลการสมัครสอบคัดเลือก " + periodExam.Name; + var body = candidate.FirstName + " " + candidate.LastName + " สถานะการสมัครสอบ: สละสิทธิ์สอบ"; + if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); + } candidate.Status = "waiver"; } } else { + if (candidate.Status != "waiver") + { + var subject = "แจ้งผลการสมัครสอบคัดเลือก " + periodExam.Name; + var body = candidate.FirstName + " " + candidate.LastName + " สถานะการสมัครสอบ: สละสิทธิ์สอบ"; + if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); + } candidate.Status = "waiver"; } periodExam.SetSeat = true; @@ -858,15 +876,30 @@ namespace BMA.EHR.Recurit.Exam.Service.Services candidate.PointC = item.PointC; candidate.ResultC = item.ResultC; candidate.Pass = item.Pass; + var subject = "แจ้งผลการสมัครสอบคัดเลือก " + periodExam.Name; + var body = candidate.FirstName + " " + candidate.LastName + " สถานะการสมัครสอบ: สอบคัดเลือกสำเร็จ"; + if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); candidate.Status = "done"; } else { + if (candidate.Status != "waiver") + { + var subject = "แจ้งผลการสมัครสอบคัดเลือก " + periodExam.Name; + var body = candidate.FirstName + " " + candidate.LastName + " สถานะการสมัครสอบ: สละสิทธิ์สอบ"; + if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); + } candidate.Status = "waiver"; } } else { + if (candidate.Status != "waiver") + { + var subject = "แจ้งผลการสมัครสอบคัดเลือก " + periodExam.Name; + var body = candidate.FirstName + " " + candidate.LastName + " สถานะการสมัครสอบ: สละสิทธิ์สอบ"; + if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); + } candidate.Status = "waiver"; } periodExam.SetSeat = true; diff --git a/appsettings.Development.json b/appsettings.Development.json index 6d26054..8185702 100644 --- a/appsettings.Development.json +++ b/appsettings.Development.json @@ -15,8 +15,8 @@ "ConnectionStrings": { "MongoConnection": "mongodb://127.0.0.1:27017", "DefaultConnection": "server=127.0.0.1;user=root;password=P@ssw0rd;port=3308;database=bma_ehr_exam;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;", - // "MetadataConnection": "server=127.0.0.1;user=root;password=P@ssw0rd;port=3308;database=bma_ehr;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;" - "MetadataConnection": "server=192.168.1.9;user=root;password=adminVM123;database=bma_ehr;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;" + "MetadataConnection": "server=127.0.0.1;user=root;password=P@ssw0rd;port=3308;database=bma_ehr;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;" + // "MetadataConnection": "server=192.168.1.9;user=root;password=adminVM123;database=bma_ehr;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;" }, "Jwt": { "Key": "HP-FnQMUj9msHMSD3T9HtdEnphAKoCJLEl85CIqROFI", @@ -33,5 +33,12 @@ "SecretKey": "P@ssw0rd", "BucketName": "bma-recruit" }, + "Mail": { + "Server": "smtp.gmail.com", + "User": "suphonchai.ph@gmail.com", + "Password": "nnjazjcnwhepkxdm", + "MailFrom": "suphonchai.ph@gmail.com", + "Port": "587" + }, "Domain": "http://localhost:5173" } diff --git a/appsettings.json b/appsettings.json index 22db589..9617304 100644 --- a/appsettings.json +++ b/appsettings.json @@ -33,5 +33,12 @@ "SecretKey": "P@ssw0rd", "BucketName": "bma-recruit" }, + "Mail": { + "Server": "smtp.gmail.com", + "User": "suphonchai.ph@gmail.com", + "Password": "nnjazjcnwhepkxdm", + "MailFrom": "suphonchai.ph@gmail.com", + "Port": "587" + }, "Domain": "http://localhost:5173" } \ No newline at end of file diff --git a/bin/Debug/net7.0/appsettings.Development.json b/bin/Debug/net7.0/appsettings.Development.json index 2c39a43..8185702 100644 --- a/bin/Debug/net7.0/appsettings.Development.json +++ b/bin/Debug/net7.0/appsettings.Development.json @@ -33,5 +33,12 @@ "SecretKey": "P@ssw0rd", "BucketName": "bma-recruit" }, + "Mail": { + "Server": "smtp.gmail.com", + "User": "suphonchai.ph@gmail.com", + "Password": "nnjazjcnwhepkxdm", + "MailFrom": "suphonchai.ph@gmail.com", + "Port": "587" + }, "Domain": "http://localhost:5173" } diff --git a/bin/Debug/net7.0/appsettings.json b/bin/Debug/net7.0/appsettings.json index 22db589..9617304 100644 --- a/bin/Debug/net7.0/appsettings.json +++ b/bin/Debug/net7.0/appsettings.json @@ -33,5 +33,12 @@ "SecretKey": "P@ssw0rd", "BucketName": "bma-recruit" }, + "Mail": { + "Server": "smtp.gmail.com", + "User": "suphonchai.ph@gmail.com", + "Password": "nnjazjcnwhepkxdm", + "MailFrom": "suphonchai.ph@gmail.com", + "Port": "587" + }, "Domain": "http://localhost:5173" } \ No newline at end of file