cronjob พ้นราชการ

This commit is contained in:
Kittapath 2023-09-13 13:11:05 +07:00
parent dd6471ddf1
commit cfaf90cb01
6 changed files with 171 additions and 20 deletions

View file

@ -1842,14 +1842,14 @@ namespace BMA.EHR.Application.Repositories.Commands
// TODO: ต้องเปลี่ยนเป็น Email จริงนะ ตอนนี้ Hardcode อยู่ // TODO: ต้องเปลี่ยนเป็น Email จริงนะ ตอนนี้ Hardcode อยู่
// Send noti inbox and email // Send noti inbox and email
var subject = $"คุณได้รับคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}"; 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"); _emailSenderService.SendMail(subject, body, "dev@frappet.com");
var inbox = new Inbox var inbox = new Inbox
{ {
Subject = $"คุณได้รับคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", 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, ReceiverUserId = profile.Id,
Payload = payload_str, Payload = payload_str,
}; };
@ -1857,7 +1857,7 @@ namespace BMA.EHR.Application.Repositories.Commands
var noti = new Notification 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, ReceiverUserId = profile.Id,
Type = "LINK", Type = "LINK",
Payload = payload_str, Payload = payload_str,
@ -1879,7 +1879,7 @@ namespace BMA.EHR.Application.Repositories.Commands
var inbox = new Inbox var inbox = new Inbox
{ {
Subject = $"คุณได้รับสำเนาคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}", 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, ReceiverUserId = pf.Id,
Payload = payload_str, Payload = payload_str,
}; };
@ -1891,14 +1891,14 @@ namespace BMA.EHR.Application.Repositories.Commands
// TODO: ต้องเปลี่ยนเป็น Email จริงนะ ตอนนี้ Hardcode อยู่ // TODO: ต้องเปลี่ยนเป็น Email จริงนะ ตอนนี้ Hardcode อยู่
// Send noti inbox and email // Send noti inbox and email
var subject = $"คุณได้รับสำเนาคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear.ToInteger().ToThaiYear()} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}"; 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"); _emailSenderService.SendMail(subject, body, "dev@frappet.com");
} }
var noti = new Notification 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, ReceiverUserId = pf.Id,
Type = "LINK", Type = "LINK",
Payload = payload_str, Payload = payload_str,
@ -3200,7 +3200,7 @@ namespace BMA.EHR.Application.Repositories.Commands
if (lastSarary.Order != null) if (lastSarary.Order != null)
order = lastSarary.Order.Value + 1; order = lastSarary.Order.Value + 1;
var salary = new ProfileSalary var salary = new ProfileSalary
{ {
@ -6495,12 +6495,24 @@ namespace BMA.EHR.Application.Repositories.Commands
PositionSalaryAmount = 0, PositionSalaryAmount = 0,
MonthSalaryAmount = 0 MonthSalaryAmount = 0
}; };
var profile = await _dbContext.Set<Profile>()
.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 return new PlacementSalaryResponse
{ {
SalaryAmount = cmdReceiver.Amount ?? 0, SalaryAmount = cmdReceiver.Amount != null ? cmdReceiver.Amount.Value : SalaryAmount,
PositionSalaryAmount = cmdReceiver.PositionSalaryAmount ?? 0, PositionSalaryAmount = cmdReceiver.PositionSalaryAmount != null ? cmdReceiver.PositionSalaryAmount.Value : PositionSalaryAmount,
MonthSalaryAmount = cmdReceiver.MouthSalaryAmount ?? 0 MonthSalaryAmount = cmdReceiver.MouthSalaryAmount != null ? cmdReceiver.MouthSalaryAmount.Value : MonthSalaryAmount,
}; };
} }
catch catch
@ -6519,12 +6531,24 @@ namespace BMA.EHR.Application.Repositories.Commands
if (placementProfile == null) if (placementProfile == null)
throw new Exception($"Invalid placement profile: {placementProfileId}"); throw new Exception($"Invalid placement profile: {placementProfileId}");
var profile = await _dbContext.Set<Profile>()
.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 return new PlacementSalaryResponse
{ {
SalaryAmount = placementProfile.Amount ?? 0, SalaryAmount = placementProfile.Amount != null ? placementProfile.Amount.Value : SalaryAmount,
PositionSalaryAmount = placementProfile.PositionSalaryAmount ?? 0, PositionSalaryAmount = placementProfile.PositionSalaryAmount != null ? placementProfile.PositionSalaryAmount.Value : PositionSalaryAmount,
MonthSalaryAmount = placementProfile.MouthSalaryAmount ?? 0 MonthSalaryAmount = placementProfile.MouthSalaryAmount != null ? placementProfile.MouthSalaryAmount.Value : MonthSalaryAmount,
}; };
} }

View file

@ -1,4 +1,5 @@
using BMA.EHR.Application.Common.Interfaces; using BMA.EHR.Application.Common.Interfaces;
using BMA.EHR.Application.Repositories.MessageQueue;
using BMA.EHR.Domain.Models.Retirement; using BMA.EHR.Domain.Models.Retirement;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -9,17 +10,83 @@ namespace BMA.EHR.Application.Repositories
{ {
private readonly IApplicationDBContext _dbContext; private readonly IApplicationDBContext _dbContext;
private readonly IHttpContextAccessor _httpContextAccessor; 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; _dbContext = dbContext;
_httpContextAccessor = httpContextAccessor; _httpContextAccessor = httpContextAccessor;
_repositoryNoti = repositoryNoti;
} }
// public async Task<IEnumerable<Retirement>> FindByNameAsync(string name) //ปลดออก
// { public async Task NotifyDischarge()
// var data = await _dbContext.Set<Retirement>().Where(x => x.Name == name).ToListAsync(); {
var cronjobNotis = await _dbContext.Set<RetirementDischarge>()
.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<RetirementExpulsion>()
.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<RetirementOut>()
.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();
}
} }
} }

View file

@ -13,6 +13,9 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="AWSSDK.S3" Version="3.7.201.7" /> <PackageReference Include="AWSSDK.S3" Version="3.7.201.7" />
<PackageReference Include="Hangfire" Version="1.8.5" />
<PackageReference Include="Hangfire.AspNetCore" Version="1.8.5" />
<PackageReference Include="Hangfire.MySqlStorage" Version="2.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.9" /> <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.NewtonsoftJson" Version="7.0.9" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.1.0" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.1.0" />

View file

@ -561,7 +561,7 @@ namespace BMA.EHR.Retirement.Service.Controllers
var data = new RetirementProfile var data = new RetirementProfile
{ {
Order = num, Order = num,
Remove = "PENDING", Remove = "ADD",
RetirementPeriod = retire, RetirementPeriod = retire,
Profile = profile, Profile = profile,
CreatedUserId = UserId ?? "System Administrator", CreatedUserId = UserId ?? "System Administrator",

View file

@ -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;
}
}
}

View file

@ -10,11 +10,17 @@ using Microsoft.AspNetCore.Mvc.Versioning;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Logging; using Microsoft.IdentityModel.Logging;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
using Hangfire;
using Hangfire.Common;
using Hangfire.MySql;
using Serilog; using Serilog;
using Serilog.Exceptions; using Serilog.Exceptions;
using Serilog.Sinks.Elasticsearch; using Serilog.Sinks.Elasticsearch;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Transactions;
using BMA.EHR.Retirement.Service.Filters;
using BMA.EHR.Application.Repositories;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
{ {
@ -89,6 +95,30 @@ var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
builder.Services.ConfigureOptions<ConfigureSwaggerOptions>(); builder.Services.ConfigureOptions<ConfigureSwaggerOptions>();
// Register DbContext
var defaultConnection = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDBContext>(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(); builder.Services.AddHealthChecks();
} }
@ -120,6 +150,17 @@ var app = builder.Build();
app.UseStaticFiles(); app.UseStaticFiles();
app.MapControllers(); app.MapControllers();
app.UseMiddleware<ErrorHandlerMiddleware>(); app.UseMiddleware<ErrorHandlerMiddleware>();
app.UseHangfireDashboard("/hangfire", new DashboardOptions()
{
Authorization = new[] { new CustomAuthorizeFilter() }
});
var manager = new RecurringJobManager();
if (manager != null)
{
manager.AddOrUpdate("แจ้งเตือนระบบปลดออก", Job.FromExpression<RetirementRepository>(x => x.NotifyDischarge()), Cron.Daily(Int32.Parse(builder.Configuration["KeycloakCron:Hour"]), Int32.Parse(builder.Configuration["KeycloakCron:Minute"])), TimeZoneInfo.Local);
manager.AddOrUpdate("แจ้งเตือนระบบไล่ออก", Job.FromExpression<RetirementRepository>(x => x.NotifyExpulsion()), Cron.Daily(Int32.Parse(builder.Configuration["KeycloakCron:Hour"]), Int32.Parse(builder.Configuration["KeycloakCron:Minute"])), TimeZoneInfo.Local);
manager.AddOrUpdate("แจ้งเตือนระบบให้ออก", Job.FromExpression<RetirementRepository>(x => x.NotifyOut()), Cron.Daily(Int32.Parse(builder.Configuration["KeycloakCron:Hour"]), Int32.Parse(builder.Configuration["KeycloakCron:Minute"])), TimeZoneInfo.Local);
}
// apply migrations // apply migrations
await using var scope = app.Services.CreateAsyncScope(); await using var scope = app.Services.CreateAsyncScope();