diff --git a/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs b/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs index eeb4675e..fd7bd16c 100644 --- a/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs +++ b/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs @@ -1842,14 +1842,14 @@ namespace BMA.EHR.Application.Repositories.Commands // TODO: ต้องเปลี่ยนเป็น Email จริงนะ ตอนนี้ Hardcode อยู่ // Send noti inbox and email var subject = $"คุณได้รับคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}"; - var body = $"คุณได้รับบรรจุเป็นข้าราชการกรุงเทพมหานครสามัญ ตามคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}"; + var body = $"คุณได้รับรรจุเป็นข้าราชการกรุงเทพมหานครสามัญ ตามคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}"; _emailSenderService.SendMail(subject, body, "dev@frappet.com"); var inbox = new Inbox { Subject = $"คุณได้รับคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", - Body = $"คุณได้รับบรรจุเป็นข้าราชการกรุงเทพมหานครสามัญ ตามคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + Body = $"คุณได้รับรรจุเป็นข้าราชการกรุงเทพมหานครสามัญ ตามคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", ReceiverUserId = profile.Id, Payload = payload_str, }; @@ -1857,7 +1857,7 @@ namespace BMA.EHR.Application.Repositories.Commands var noti = new Notification { - Body = $"คุณได้รับบรรจุเป็นข้าราชการกรุงเทพมหานครสามัญ ตามคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + Body = $"คุณได้รับรรจุเป็นข้าราชการกรุงเทพมหานครสามัญ ตามคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", ReceiverUserId = profile.Id, Type = "LINK", Payload = payload_str, @@ -1879,7 +1879,7 @@ namespace BMA.EHR.Application.Repositories.Commands var inbox = new Inbox { Subject = $"คุณได้รับสำเนาคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", - Body = $"คำสั่งบบรรจุเป็นข้าราชการกรุงเทพมหานครสามัญ คำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + Body = $"คำสั่งบรรจุเป็นข้าราชการกรุงเทพมหานครสามัญ คำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", ReceiverUserId = pf.Id, Payload = payload_str, }; @@ -1891,14 +1891,14 @@ namespace BMA.EHR.Application.Repositories.Commands // TODO: ต้องเปลี่ยนเป็น Email จริงนะ ตอนนี้ Hardcode อยู่ // Send noti inbox and email var subject = $"คุณได้รับสำเนาคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}"; - var body = $"คำสั่งบบรรจุเป็นข้าราชการกรุงเทพมหานครสามัญ คำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}"; + var body = $"คำสั่งบรรจุเป็นข้าราชการกรุงเทพมหานครสามัญ คำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}"; _emailSenderService.SendMail(subject, body, "dev@frappet.com"); } var noti = new Notification { - Body = $"คำสั่งบบรรจุเป็นข้าราชการกรุงเทพมหานครสามัญ คำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", + Body = $"คำสั่งบรรจุเป็นข้าราชการกรุงเทพมหานครสามัญ คำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", ReceiverUserId = pf.Id, Type = "LINK", Payload = payload_str, @@ -3200,7 +3200,7 @@ namespace BMA.EHR.Application.Repositories.Commands if (lastSarary.Order != null) order = lastSarary.Order.Value + 1; - + var salary = new ProfileSalary { @@ -6495,12 +6495,24 @@ namespace BMA.EHR.Application.Repositories.Commands PositionSalaryAmount = 0, MonthSalaryAmount = 0 }; + var profile = await _dbContext.Set() + .Include(x => x.Salaries) + .FirstOrDefaultAsync(p => p.CitizenId == cmdReceiver.CitizenId); + Double SalaryAmount = 0; + Double PositionSalaryAmount = 0; + Double MonthSalaryAmount = 0; + if (profile != null && profile.Salaries.Count() > 0) + { + SalaryAmount = placementProfile.Amount == null ? profile.Salaries.OrderByDescending(x => x.Order).FirstOrDefault().Amount.Value : 0; + PositionSalaryAmount = placementProfile.PositionSalaryAmount == null ? profile.Salaries.OrderByDescending(x => x.Order).FirstOrDefault().PositionSalaryAmount.Value : 0; + MonthSalaryAmount = placementProfile.MouthSalaryAmount == null ? profile.Salaries.OrderByDescending(x => x.Order).FirstOrDefault().MouthSalaryAmount.Value : 0; + } return new PlacementSalaryResponse { - SalaryAmount = cmdReceiver.Amount ?? 0, - PositionSalaryAmount = cmdReceiver.PositionSalaryAmount ?? 0, - MonthSalaryAmount = cmdReceiver.MouthSalaryAmount ?? 0 + SalaryAmount = cmdReceiver.Amount != null ? cmdReceiver.Amount.Value : SalaryAmount, + PositionSalaryAmount = cmdReceiver.PositionSalaryAmount != null ? cmdReceiver.PositionSalaryAmount.Value : PositionSalaryAmount, + MonthSalaryAmount = cmdReceiver.MouthSalaryAmount != null ? cmdReceiver.MouthSalaryAmount.Value : MonthSalaryAmount, }; } catch @@ -6519,12 +6531,24 @@ namespace BMA.EHR.Application.Repositories.Commands if (placementProfile == null) throw new Exception($"Invalid placement profile: {placementProfileId}"); + var profile = await _dbContext.Set() + .Include(x => x.Salaries) + .FirstOrDefaultAsync(p => p.CitizenId == placementProfile.CitizenId); + Double SalaryAmount = 0; + Double PositionSalaryAmount = 0; + Double MonthSalaryAmount = 0; + if (profile != null && profile.Salaries.Count() > 0) + { + SalaryAmount = placementProfile.Amount == null ? profile.Salaries.OrderByDescending(x => x.Order).FirstOrDefault().Amount.Value : 0; + PositionSalaryAmount = placementProfile.PositionSalaryAmount == null ? profile.Salaries.OrderByDescending(x => x.Order).FirstOrDefault().PositionSalaryAmount.Value : 0; + MonthSalaryAmount = placementProfile.MouthSalaryAmount == null ? profile.Salaries.OrderByDescending(x => x.Order).FirstOrDefault().MouthSalaryAmount.Value : 0; + } return new PlacementSalaryResponse { - SalaryAmount = placementProfile.Amount ?? 0, - PositionSalaryAmount = placementProfile.PositionSalaryAmount ?? 0, - MonthSalaryAmount = placementProfile.MouthSalaryAmount ?? 0 + SalaryAmount = placementProfile.Amount != null ? placementProfile.Amount.Value : SalaryAmount, + PositionSalaryAmount = placementProfile.PositionSalaryAmount != null ? placementProfile.PositionSalaryAmount.Value : PositionSalaryAmount, + MonthSalaryAmount = placementProfile.MouthSalaryAmount != null ? placementProfile.MouthSalaryAmount.Value : MonthSalaryAmount, }; } diff --git a/BMA.EHR.Application/Repositories/RetirementRepository.cs b/BMA.EHR.Application/Repositories/RetirementRepository.cs index f9a1d819..ab4f300a 100644 --- a/BMA.EHR.Application/Repositories/RetirementRepository.cs +++ b/BMA.EHR.Application/Repositories/RetirementRepository.cs @@ -1,4 +1,5 @@ using BMA.EHR.Application.Common.Interfaces; +using BMA.EHR.Application.Repositories.MessageQueue; using BMA.EHR.Domain.Models.Retirement; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; @@ -9,17 +10,83 @@ namespace BMA.EHR.Application.Repositories { private readonly IApplicationDBContext _dbContext; private readonly IHttpContextAccessor _httpContextAccessor; - public RetirementRepository(IApplicationDBContext dbContext, IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor) + private readonly NotificationRepository _repositoryNoti; + public RetirementRepository(IApplicationDBContext dbContext, + NotificationRepository repositoryNoti, + IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor) { _dbContext = dbContext; _httpContextAccessor = httpContextAccessor; + _repositoryNoti = repositoryNoti; } - // public async Task> FindByNameAsync(string name) - // { - // var data = await _dbContext.Set().Where(x => x.Name == name).ToListAsync(); + //ปลดออก + public async Task NotifyDischarge() + { + var cronjobNotis = await _dbContext.Set() + .Include(x => x.Profile) + .ThenInclude(x => x.Prefix) + .Where(x => x.Date != null && x.Date.Value.Date == DateTime.Now.Date) + .AsQueryable() + .ToListAsync(); + foreach (var cronjobNoti in cronjobNotis) + { + cronjobNoti.Profile.IsLeave = true; + cronjobNoti.Profile.LeaveReason = "DISCHARGE"; + cronjobNoti.Profile.LeaveDate = DateTime.Now; + // await _repositoryNoti.PushNotificationAsync( + // Guid.Parse("08db721d-ada0-4e64-89d3-7584a893d8b8"), + // $"แจ้งเตือนการปลดออกของ {cronjobNoti.Profile.Prefix?.Name}{cronjobNoti.Profile.FirstName} {cronjobNoti.Profile.LastName}", + // $"แจ้งเตือนการปลดออกของ {cronjobNoti.Profile.Prefix?.Name}{cronjobNoti.Profile.FirstName} {cronjobNoti.Profile.LastName}" + // ); + } + await _dbContext.SaveChangesAsync(); + } - // return data; - // } + //ไล่ออก + public async Task NotifyExpulsion() + { + var cronjobNotis = await _dbContext.Set() + .Include(x => x.Profile) + .ThenInclude(x => x.Prefix) + .Where(x => x.Date != null && x.Date.Value.Date == DateTime.Now.Date) + .AsQueryable() + .ToListAsync(); + foreach (var cronjobNoti in cronjobNotis) + { + cronjobNoti.Profile.IsLeave = true; + cronjobNoti.Profile.LeaveReason = "DISMISS"; + cronjobNoti.Profile.LeaveDate = DateTime.Now; + // await _repositoryNoti.PushNotificationAsync( + // Guid.Parse("08db721d-ada0-4e64-89d3-7584a893d8b8"), + // $"แจ้งเตือนการปลดออกของ {cronjobNoti.Profile.Prefix?.Name}{cronjobNoti.Profile.FirstName} {cronjobNoti.Profile.LastName}", + // $"แจ้งเตือนการปลดออกของ {cronjobNoti.Profile.Prefix?.Name}{cronjobNoti.Profile.FirstName} {cronjobNoti.Profile.LastName}" + // ); + } + await _dbContext.SaveChangesAsync(); + } + + //ให้ออก + public async Task NotifyOut() + { + var cronjobNotis = await _dbContext.Set() + .Include(x => x.Profile) + .ThenInclude(x => x.Prefix) + .Where(x => x.Date != null && x.Date.Value.Date == DateTime.Now.Date) + .AsQueryable() + .ToListAsync(); + foreach (var cronjobNoti in cronjobNotis) + { + cronjobNoti.Profile.IsLeave = true; + cronjobNoti.Profile.LeaveReason = "LAYOFF"; + cronjobNoti.Profile.LeaveDate = DateTime.Now; + // await _repositoryNoti.PushNotificationAsync( + // Guid.Parse("08db721d-ada0-4e64-89d3-7584a893d8b8"), + // $"แจ้งเตือนการปลดออกของ {cronjobNoti.Profile.Prefix?.Name}{cronjobNoti.Profile.FirstName} {cronjobNoti.Profile.LastName}", + // $"แจ้งเตือนการปลดออกของ {cronjobNoti.Profile.Prefix?.Name}{cronjobNoti.Profile.FirstName} {cronjobNoti.Profile.LastName}" + // ); + } + await _dbContext.SaveChangesAsync(); + } } } diff --git a/BMA.EHR.Retirement.Service/BMA.EHR.Retirement.Service.csproj b/BMA.EHR.Retirement.Service/BMA.EHR.Retirement.Service.csproj index e73b2d88..8c1fd03f 100644 --- a/BMA.EHR.Retirement.Service/BMA.EHR.Retirement.Service.csproj +++ b/BMA.EHR.Retirement.Service/BMA.EHR.Retirement.Service.csproj @@ -13,6 +13,9 @@ + + + diff --git a/BMA.EHR.Retirement.Service/Controllers/RetirementController.cs b/BMA.EHR.Retirement.Service/Controllers/RetirementController.cs index d9ab938d..c9760189 100644 --- a/BMA.EHR.Retirement.Service/Controllers/RetirementController.cs +++ b/BMA.EHR.Retirement.Service/Controllers/RetirementController.cs @@ -561,7 +561,7 @@ namespace BMA.EHR.Retirement.Service.Controllers var data = new RetirementProfile { Order = num, - Remove = "PENDING", + Remove = "ADD", RetirementPeriod = retire, Profile = profile, CreatedUserId = UserId ?? "System Administrator", diff --git a/BMA.EHR.Retirement.Service/Filters/CustomAuthorizeFilter.cs b/BMA.EHR.Retirement.Service/Filters/CustomAuthorizeFilter.cs new file mode 100644 index 00000000..3ccc8e28 --- /dev/null +++ b/BMA.EHR.Retirement.Service/Filters/CustomAuthorizeFilter.cs @@ -0,0 +1,16 @@ +using System.Diagnostics.CodeAnalysis; +using Hangfire.Dashboard; + +namespace BMA.EHR.Retirement.Service.Filters +{ + public class CustomAuthorizeFilter : IDashboardAuthorizationFilter + { + public bool Authorize([NotNull] DashboardContext context) + { + //var httpcontext = context.GetHttpContext(); + //return httpcontext.User.Identity.IsAuthenticated; + return true; + } + + } +} \ No newline at end of file diff --git a/BMA.EHR.Retirement.Service/Program.cs b/BMA.EHR.Retirement.Service/Program.cs index 4ec77b3c..1256318f 100644 --- a/BMA.EHR.Retirement.Service/Program.cs +++ b/BMA.EHR.Retirement.Service/Program.cs @@ -10,11 +10,17 @@ using Microsoft.AspNetCore.Mvc.Versioning; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Logging; using Microsoft.IdentityModel.Tokens; +using Hangfire; +using Hangfire.Common; +using Hangfire.MySql; using Serilog; using Serilog.Exceptions; using Serilog.Sinks.Elasticsearch; using System.Reflection; using System.Text; +using System.Transactions; +using BMA.EHR.Retirement.Service.Filters; +using BMA.EHR.Application.Repositories; var builder = WebApplication.CreateBuilder(args); { @@ -89,6 +95,30 @@ var builder = WebApplication.CreateBuilder(args); builder.Services.AddSwaggerGen(); builder.Services.ConfigureOptions(); + // Register DbContext + var defaultConnection = builder.Configuration.GetConnectionString("DefaultConnection"); + builder.Services.AddDbContext(options => + options.UseMySql(defaultConnection, ServerVersion.AutoDetect(defaultConnection))); + builder.Services.AddHealthChecks(); + // Add Hangfire services. + builder.Services.AddHangfire(configuration => configuration + .SetDataCompatibilityLevel(CompatibilityLevel.Version_170) + .UseSimpleAssemblyNameTypeSerializer() + .UseRecommendedSerializerSettings() + .UseStorage( + new MySqlStorage( + defaultConnection, + new MySqlStorageOptions + { + TransactionIsolationLevel = IsolationLevel.ReadCommitted, + QueuePollInterval = TimeSpan.FromSeconds(15), + JobExpirationCheckInterval = TimeSpan.FromHours(1), + CountersAggregateInterval = TimeSpan.FromMinutes(5), + PrepareSchemaIfNecessary = true, + DashboardJobListLimit = 50000, + TransactionTimeout = TimeSpan.FromMinutes(1), + TablesPrefix = "Hangfire" + }))); builder.Services.AddHealthChecks(); } @@ -120,6 +150,17 @@ var app = builder.Build(); app.UseStaticFiles(); app.MapControllers(); app.UseMiddleware(); + app.UseHangfireDashboard("/hangfire", new DashboardOptions() + { + Authorization = new[] { new CustomAuthorizeFilter() } + }); + var manager = new RecurringJobManager(); + if (manager != null) + { + manager.AddOrUpdate("แจ้งเตือนระบบปลดออก", Job.FromExpression(x => x.NotifyDischarge()), Cron.Daily(Int32.Parse(builder.Configuration["KeycloakCron:Hour"]), Int32.Parse(builder.Configuration["KeycloakCron:Minute"])), TimeZoneInfo.Local); + manager.AddOrUpdate("แจ้งเตือนระบบไล่ออก", Job.FromExpression(x => x.NotifyExpulsion()), Cron.Daily(Int32.Parse(builder.Configuration["KeycloakCron:Hour"]), Int32.Parse(builder.Configuration["KeycloakCron:Minute"])), TimeZoneInfo.Local); + manager.AddOrUpdate("แจ้งเตือนระบบให้ออก", Job.FromExpression(x => x.NotifyOut()), Cron.Daily(Int32.Parse(builder.Configuration["KeycloakCron:Hour"]), Int32.Parse(builder.Configuration["KeycloakCron:Minute"])), TimeZoneInfo.Local); + } // apply migrations await using var scope = app.Services.CreateAsyncScope();