api เกษียณ

This commit is contained in:
Kittapath 2023-07-22 22:14:50 +07:00
parent 25274562c8
commit 6d655f3f3b
29 changed files with 27484 additions and 46 deletions

View file

@ -0,0 +1,25 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md

View file

@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>7be0011e-a539-4e0e-a300-ed7f11163989</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<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.Versioning" Version="5.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.1.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.9">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.IdentityModel.Logging" Version="6.31.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.18.1" />
<PackageReference Include="runtime.osx.10.10-x64.CoreCompat.System.Drawing" Version="6.0.5.128" />
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Sentry.AspNetCore" Version="3.33.1" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.2.0" />
<PackageReference Include="Serilog.Exceptions" Version="8.4.0" />
<PackageReference Include="Serilog.Sinks.Debug" Version="2.0.0" />
<PackageReference Include="Serilog.Sinks.Elasticsearch" Version="9.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,42 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>7be0011e-a539-4e0e-a300-ed7f11163989</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<DockerfileContext>.</DockerfileContext>
<RootNamespace>BMA.EHR.Retirement.Service</RootNamespace>
</PropertyGroup>
<ItemGroup>
<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.Versioning" Version="5.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.1.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.9">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.IdentityModel.Logging" Version="6.31.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.18.1" />
<PackageReference Include="runtime.osx.10.10-x64.CoreCompat.System.Drawing" Version="6.0.5.128" />
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Sentry.AspNetCore" Version="3.33.1" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.2.0" />
<PackageReference Include="Serilog.Exceptions" Version="8.4.0" />
<PackageReference Include="Serilog.Sinks.Debug" Version="2.0.0" />
<PackageReference Include="Serilog.Sinks.Elasticsearch" Version="9.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BMA.EHR.Infrastructure\BMA.EHR.Infrastructure.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,84 @@
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Reflection;
namespace BMA.EHR.Retirement.Service
{
public class ConfigureSwaggerOptions : IConfigureNamedOptions<SwaggerGenOptions>
{
private readonly IApiVersionDescriptionProvider _provider;
public ConfigureSwaggerOptions(
IApiVersionDescriptionProvider provider)
{
_provider = provider;
}
public void Configure(SwaggerGenOptions options)
{
// add swagger document for every API version discovered
foreach (var description in _provider.ApiVersionDescriptions)
{
options.EnableAnnotations();
options.SwaggerDoc(
description.GroupName,
CreateVersionInfo(description));
}
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Description = "Please enter a valid token",
Name = "Authorization",
Type = SecuritySchemeType.Http,
BearerFormat = "JWT",
Scheme = "Bearer"
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
new string[]{}
}
});
// generate the XML docs that'll drive the swagger docs
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
options.IncludeXmlComments(xmlPath);
}
public void Configure(string name, SwaggerGenOptions options)
{
Configure(options);
}
private OpenApiInfo CreateVersionInfo(
ApiVersionDescription desc)
{
var info = new OpenApiInfo()
{
Title = "BMA EHR Retirement Service Document",
Version = desc.ApiVersion.ToString()
};
if (desc.IsDeprecated)
{
info.Description += " This API version has been deprecated. Please use one of the new APIs available from the explorer.";
}
return info;
}
}
}

View file

@ -0,0 +1,441 @@
using BMA.EHR.Application.Repositories;
using BMA.EHR.Domain.Common;
using BMA.EHR.Domain.Extensions;
using BMA.EHR.Domain.Models.MetaData;
using BMA.EHR.Domain.Models.Placement;
using BMA.EHR.Domain.Models.Retirement;
using BMA.EHR.Domain.Shared;
using BMA.EHR.Infrastructure.Persistence;
using BMA.EHR.Retirement.Service.Requests;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Swashbuckle.AspNetCore.Annotations;
using System.Drawing;
using System.Linq;
using System.Security.Claims;
using System.Security.Cryptography;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace BMA.EHR.Retirement.Service.Controllers
{
[Route("api/v{version:apiVersion}/retirement")]
[ApiVersion("1.0")]
[ApiController]
[Produces("application/json")]
[Authorize]
[SwaggerTag("ระบบพ้นราชการ")]
public class RetirementController : BaseController
{
private readonly RetirementRepository _repository;
private readonly ApplicationDBContext _context;
private readonly MinIOService _documentService;
private readonly IHttpContextAccessor _httpContextAccessor;
public RetirementController(RetirementRepository repository,
ApplicationDBContext context,
MinIOService documentService,
IHttpContextAccessor httpContextAccessor)
{
_repository = repository;
_context = context;
_documentService = documentService;
_httpContextAccessor = httpContextAccessor;
}
#region " Properties "
private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value;
#endregion
#region " จัดลำดับเกษียณ "
private async Task GenOrderByYear(string type, int year)
{
if (type.Trim().ToUpper().Contains("OFFICER"))
{
var profiles = await _context.RetirementProfiles
.Where(x => x.CreatedAt.Year == year)
.Where(x => x.RetirementPeriod.Type.Trim().ToUpper().Contains(type.Trim().ToUpper()))
.OrderBy(x => x.Profile.OrganizationOrganization)
.ThenBy(x => x.Profile.PositionType == null ? null : x.Profile.PositionType.Name)
.ThenBy(x => x.Profile.PositionLevel == null ? null : x.Profile.PositionLevel.Name)
.ToListAsync();
var order = 1;
foreach (var profile in profiles)
{
profile.Order = order;
profile.LastUpdateFullName = FullName ?? "System Administrator";
profile.LastUpdateUserId = UserId ?? "";
profile.LastUpdatedAt = DateTime.Now;
order++;
}
}
if (type.Trim().ToUpper().Contains("EMPLOYEE"))
{
var profiles = await _context.RetirementProfiles
.Where(x => x.CreatedAt.Year == year)
.Where(x => x.RetirementPeriod.Type.Trim().ToUpper().Contains(type.Trim().ToUpper()))
.OrderBy(x => x.Profile.OrganizationOrganization)
.ThenBy(x => x.Profile.EmployeeType)
.ThenBy(x => x.Profile.PositionEmployeeLevel)
.ToListAsync();
var order = 1;
foreach (var profile in profiles)
{
profile.Order = order;
profile.LastUpdateFullName = FullName ?? "System Administrator";
profile.LastUpdateUserId = UserId ?? "";
profile.LastUpdatedAt = DateTime.Now;
order++;
}
}
_context.SaveChanges();
}
#endregion
/// <summary>
/// list ประกาศเกษียณอายุราชการ
/// </summary>
/// <param name="type">ประเภทUser(officer,employee)(ตัวใหญ่หรือเล็กก็ได้)</param>
/// <param name="year">ปีงบประมาณ(ค.ศ.)</param>
/// <returns></returns>
/// <response code="200"></response>
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpGet("{type}/{year}")]
public async Task<ActionResult<ResponseObject>> GetRetirement(string type, int year)
{
if (type.Trim().ToUpper().Contains("OFFICER") || type.Trim().ToUpper().Contains("EMPLOYEE"))
{
var retire_old = await _context.RetirementPeriods
.Include(x => x.RetirementProfiles)
.Where(x => x.CreatedAt.Year == year)
.Where(x => x.Type.Trim().ToUpper().Contains(type.Trim().ToUpper()))
.OrderByDescending(x => x.Round)
.FirstOrDefaultAsync();
if (retire_old != null)
{
var data = await _context.RetirementPeriods
.Where(x => x.Type.Trim().ToUpper().Contains(type.Trim().ToUpper()))
.Where(x => year > 0 ? (x.CreatedAt.Year == year) : (x.CreatedAt.Year > 0))
.Select(x => new
{
Id = x.Id,
CreatedAt = x.CreatedAt,
Round = x.Round,
Total = retire_old.Id == x.Id ? retire_old.RetirementProfiles.Count() : retire_old.RetirementProfiles.Count() - x.RetirementProfiles.Where(x => x.Remove == true).Count(),
}).OrderByDescending(x => x.CreatedAt)
.ToListAsync();
return Success(data);
}
}
return Success();
}
/// <summary>
/// สร้างประกาศเกษียณใหม่
/// </summary>
/// <param name="type">ประเภทUser(officer,employee)(ตัวใหญ่หรือเล็กก็ได้)</param>
/// <param name="year">ปีงบประมาณ(ค.ศ.)</param>
/// <returns></returns>
/// <response code="200"></response>
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpGet("profile/{type}/{year}")]
public async Task<ActionResult<ResponseObject>> CreateProfileRetirement(string type, int year)
{
var round = 1;
var retire_old = await _context.RetirementPeriods
.Include(x => x.RetirementProfiles)
.Where(x => x.CreatedAt.Year == year)
.Where(x => x.Type.Trim().ToUpper().Contains(type.Trim().ToUpper()))
.OrderByDescending(x => x.Round)
.FirstOrDefaultAsync();
if (retire_old != null)
round = retire_old.Round + 1;
var retire = new RetirementPeriod
{
Round = round,
Type = type.Trim().ToUpper(),
CreatedUserId = FullName ?? "",
CreatedFullName = UserId ?? "System Administrator",
CreatedAt = DateTime.Now,
LastUpdateFullName = FullName ?? "System Administrator",
LastUpdateUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now,
};
await _context.RetirementPeriods.AddAsync(retire);
if (retire_old != null)
{
var profiles = await _context.RetirementProfiles
.Where(x => x.RetirementPeriod == retire_old)
.Where(x => x.Remove == false)
.Where(x => x.RetirementPeriod.Type.Trim().ToUpper().Contains(type.Trim().ToUpper()))
.ToListAsync();
foreach (var profile in profiles)
{
profile.RetirementPeriod = retire;
profile.LastUpdateFullName = FullName ?? "System Administrator";
profile.LastUpdateUserId = UserId ?? "";
profile.LastUpdatedAt = DateTime.Now;
}
}
else
{
var profiles = await _context.Profiles
// .Where(x => x.BirthDate.CalculateRetireDate().Year == year)
.Where(x => x.CitizenId == "0000000000001")
.ToListAsync();
var order = 1;
foreach (var profile in profiles)
{
var data = new RetirementProfile
{
Order = order,
Remove = false,
RetirementPeriod = retire,
Profile = profile,
CreatedUserId = FullName ?? "",
CreatedFullName = UserId ?? "System Administrator",
CreatedAt = DateTime.Now,
LastUpdateFullName = FullName ?? "System Administrator",
LastUpdateUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now,
};
await _context.RetirementProfiles.AddAsync(data);
order++;
}
}
_context.SaveChanges();
var retire_all_year = await _context.RetirementPeriods
.Where(x => x.CreatedAt.Year == year)
.Where(x => x.Type.Trim().ToUpper().Contains(type.Trim().ToUpper()))
.CountAsync();
if (retire_all_year <= 1)
{
await GenOrderByYear(type.Trim().ToUpper(), year);
}
_context.SaveChanges();
var profile_new = await _context.RetirementProfiles
.Where(x => x.RetirementPeriod == retire)
.Where(x => x.RetirementPeriod.Type.Trim().ToUpper().Contains(retire.Type.Trim().ToUpper()))
.Select(x => new
{
Order = x.Order,
Id = x.Id,
Reason = x.Reason,
Remove = x.Remove,
ProfileId = x.Profile.Id,
CitizenId = x.Profile.CitizenId,
Prefix = x.Profile.Prefix == null ? null : x.Profile.Prefix.Name,
FullName = $"{x.Profile.FirstName} {x.Profile.LastName}",
OrganizationOrganization = x.Profile.OrganizationOrganization,
Oc = x.Profile.Oc,
Position = x.Profile.Position == null ? null : x.Profile.Position.Name,
PositionType = x.Profile.PositionType == null ? null : x.Profile.PositionType.Name,
PositionExecutive = x.Profile.PositionExecutive,
PosNo = x.Profile.PosNo == null ? null : x.Profile.PosNo.Name,
PositionEmployeePosition = x.Profile.PositionEmployeePosition,
PositionEmployeeLevel = x.Profile.PositionEmployeeLevel,
PositionEmployeeGroup = x.Profile.PositionEmployeeGroup,
PosNoEmployee = x.Profile.PosNoEmployee,
})
.ToListAsync();
return Success(new { retire.Id, retire.CreatedAt, retire.Round, retire.Type, profile = profile_new });
}
/// <summary>
/// View รายชื่อผู้เกษียณอายุราชการในประกาศ
/// </summary>
/// <param name="retireId">Id ประกาศ</param>
/// <returns></returns>
/// <response code="200"></response>
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpGet("{retireId:length(36)}")]
public async Task<ActionResult<ResponseObject>> GetProfileRetirement(Guid retireId)
{
var retire = await _context.RetirementPeriods
.FirstOrDefaultAsync(x => x.Id == retireId);
if (retire == null)
return Error(GlobalMessages.InvalidRetirementRequest, 404);
var retire_old = await _context.RetirementPeriods
.Where(x => x.CreatedAt < retire.CreatedAt)
.Where(x => x.Type.Trim().ToUpper().Contains(retire.Type.Trim().ToUpper()))
.ToListAsync();
var profile = await _context.RetirementProfiles
.Where(x => !retire_old.Contains(x.RetirementPeriod))
.Where(x => x.RetirementPeriod.Type.Trim().ToUpper().Contains(retire.Type.Trim().ToUpper()))
.Select(x => new
{
Order = x.Order,
Id = x.Id,
Reason = x.Reason,
Remove = x.Remove,
ProfileId = x.Profile.Id,
CitizenId = x.Profile.CitizenId,
Prefix = x.Profile.Prefix == null ? null : x.Profile.Prefix.Name,
FullName = $"{x.Profile.FirstName} {x.Profile.LastName}",
OrganizationOrganization = x.Profile.OrganizationOrganization,
Oc = x.Profile.Oc,
Position = x.Profile.Position == null ? null : x.Profile.Position.Name,
PositionType = x.Profile.PositionType == null ? null : x.Profile.PositionType.Name,
PositionExecutive = x.Profile.PositionExecutive,
PosNo = x.Profile.PosNo == null ? null : x.Profile.PosNo.Name,
PositionEmployeePosition = x.Profile.PositionEmployeePosition,
PositionEmployeeLevel = x.Profile.PositionEmployeeLevel,
PositionEmployeeGroup = x.Profile.PositionEmployeeGroup,
PosNoEmployee = x.Profile.PosNoEmployee,
})
.ToListAsync();
return Success(profile);
}
/// <summary>
/// Delete รายชื่อผู้เกษียณอายุราชการในประกาศ
/// </summary>
/// <param name="retireProfileId">Id ผู้ใช้งานในประกาศ</param>
/// <returns></returns>
/// <response code="200"></response>
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpDelete("profile/{retireProfileId:length(36)}")]
public async Task<ActionResult<ResponseObject>> DeleteProfileRetirement(Guid retireProfileId)
{
var profile = await _context.RetirementProfiles
.FirstOrDefaultAsync(x => x.Id == retireProfileId);
if (profile == null)
return Error(GlobalMessages.DataNotFound, 404);
_context.RetirementProfiles.Remove(profile);
_context.SaveChanges();
return Success();
}
/// <summary>
/// Add รายชื่อผู้เกษียณอายุราชการในประกาศ
/// </summary>
/// <param name="retireId">Id ประกาศ</param>
/// <param name="profileId">Id ผู้ใช้งาน</param>
/// <returns></returns>
/// <response code="200"></response>
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpPut("profile/{retireId:length(36)}")]
public async Task<ActionResult<ResponseObject>> AddProfileRetirement([FromBody] ProfileRequest req, Guid retireId)
{
var profile = await _context.Profiles
.FirstOrDefaultAsync(x => x.Id == req.ProfileId);
if (profile == null)
return Error(GlobalMessages.DataNotFound, 404);
var retire = await _context.RetirementPeriods
.FirstOrDefaultAsync(x => x.Id == retireId);
if (retire == null)
return Error(GlobalMessages.InvalidRetirementRequest, 404);
var order = 1;
var retire_old = await _context.RetirementPeriods
.Where(x => x.CreatedAt.Year == retire.CreatedAt.Year)
.Where(x => x.Type.Trim().ToUpper().Contains(retire.Type.Trim().ToUpper()))
.ToListAsync();
var last_order = await _context.RetirementProfiles
.Where(x => retire_old.Contains(x.RetirementPeriod))
.Where(x => x.RetirementPeriod.Type.Trim().ToUpper().Contains(retire.Type.Trim().ToUpper()))
.OrderByDescending(x => x.Order)
.FirstOrDefaultAsync();
if (last_order != null)
order = last_order.Order + 1;
var data = new RetirementProfile
{
Order = order,
Remove = false,
RetirementPeriod = retire,
Profile = profile,
CreatedUserId = FullName ?? "",
CreatedFullName = UserId ?? "System Administrator",
CreatedAt = DateTime.Now,
LastUpdateFullName = FullName ?? "System Administrator",
LastUpdateUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now,
};
if (retire_old.Count() <= 1)
{
await GenOrderByYear(retire.Type.Trim().ToUpper(), retire.CreatedAt.Year);
}
_context.RetirementProfiles.Add(data);
_context.SaveChanges();
return Success();
}
/// <summary>
/// ใส่เหตุผลไม่เกษียณ
/// </summary>
/// <param name="retireProfileId">Id ผู้ใช้งานในประกาศ</param>
/// <param name="reason">เหตุผล</param>
/// <returns></returns>
/// <response code="200"></response>
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpPost("reason")]
public async Task<ActionResult<ResponseObject>> EditReasonProfileRetirement([FromBody] ProfileRetireRequest req)
{
var profile = await _context.RetirementProfiles
.FirstOrDefaultAsync(x => x.Id == req.RetireProfileId);
if (profile == null)
return Error(GlobalMessages.DataNotFound, 404);
profile.Remove = true;
profile.Reason = req.Reason;
profile.LastUpdateFullName = FullName ?? "System Administrator";
profile.LastUpdateUserId = UserId ?? "";
profile.LastUpdatedAt = DateTime.Now;
_context.SaveChanges();
return Success();
}
/// <summary>
/// View เหตุผลไม่เกษียณ
/// </summary>
/// <param name="retireProfileId">Id ผู้ใช้งานในประกาศ</param>
/// <returns></returns>
/// <response code="200"></response>
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpGet("reason/{retireProfileId:length(36)}")]
public async Task<ActionResult<ResponseObject>> ViewReasonProfileRetirement(Guid retireProfileId)
{
var profile = await _context.RetirementProfiles
.Select(x => new
{
Id = x.Id,
Reason = x.Reason,
})
.FirstOrDefaultAsync(x => x.Id == retireProfileId);
if (profile == null)
return Error(GlobalMessages.DataNotFound, 404);
return Success(profile);
}
}
}

View file

@ -0,0 +1,27 @@
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["BMA.EHR.Domain/BMA.EHR.Domain.csproj", "BMA.EHR.Domain/"]
COPY ["BMA.EHR.Application/BMA.EHR.Application.csproj", "BMA.EHR.Application/"]
COPY ["BMA.EHR.Infrastructure/BMA.EHR.Infrastructure.csproj", "BMA.EHR.Infrastructure/"]
COPY ["BMA.EHR.Retirement.Service/BMA.EHR.Retirement.Service.csproj", "BMA.EHR.Retirement.Service/"]
RUN dotnet restore "BMA.EHR.Retirement.Service/BMA.EHR.Retirement.Service.csproj"
COPY . .
WORKDIR "/src/BMA.EHR.Retirement.Service"
RUN dotnet build "BMA.EHR.Retirement.Service.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "BMA.EHR.Retirement.Service.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "BMA.EHR.Retirement.Service.dll"]

View file

@ -0,0 +1,161 @@
using BMA.EHR.Application;
using BMA.EHR.Domain.Middlewares;
using BMA.EHR.Infrastructure;
using BMA.EHR.Infrastructure.Persistence;
using BMA.EHR.Retirement.Service;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Versioning;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Logging;
using Microsoft.IdentityModel.Tokens;
using Serilog;
using Serilog.Exceptions;
using Serilog.Sinks.Elasticsearch;
using System.Reflection;
using System.Text;
var builder = WebApplication.CreateBuilder(args);
{
var issuer = builder.Configuration["Jwt:Issuer"];
var key = builder.Configuration["Jwt:Key"];
IdentityModelEventSource.ShowPII = true;
builder.Services.AddHttpContextAccessor();
builder.Services.AddApiVersioning(opt =>
{
opt.DefaultApiVersion = new ApiVersion(1, 0);
opt.AssumeDefaultVersionWhenUnspecified = true;
opt.ReportApiVersions = true;
opt.ApiVersionReader = ApiVersionReader.Combine(new UrlSegmentApiVersionReader(),
new HeaderApiVersionReader("x-api-version"),
new MediaTypeApiVersionReader("x-api-version"));
});
builder.Services.AddVersionedApiExplorer(setup =>
{
setup.GroupNameFormat = "'v'VVV";
setup.SubstituteApiVersionInUrl = true;
});
builder.Services.AddEndpointsApiExplorer();
// Authorization
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(opt =>
{
opt.RequireHttpsMetadata = false; //false for dev
opt.Authority = issuer;
opt.TokenValidationParameters = new()
{
ValidateIssuer = true,
ValidateAudience = false,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = issuer,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key))
};
});
builder.Services.AddAuthorization();
// use serilog
ConfigureLogs();
builder.Host.UseSerilog();
// Add config CORS
builder.Services.AddCors(options => options.AddDefaultPolicy(builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.SetIsOriginAllowedToAllowWildcardSubdomains();
}));
// Add services to the container.
builder.Services.AddApplication();
builder.Services.AddPersistence(builder.Configuration);
builder.Services.AddControllers(options =>
{
options.SuppressAsyncSuffixInActionNames = false;
})
.AddNewtonsoftJson(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
builder.Services.AddSwaggerGen();
builder.Services.ConfigureOptions<ConfigureSwaggerOptions>();
builder.Services.AddHealthChecks();
}
var app = builder.Build();
{
var apiVersionDescriptionProvider = app.Services.GetRequiredService<IApiVersionDescriptionProvider>();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(options =>
{
foreach (var description in apiVersionDescriptionProvider.ApiVersionDescriptions)
{
options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json",
description.GroupName.ToUpperInvariant());
}
});
}
app.MapHealthChecks("/health");
app.UseHttpsRedirection();
app.UseCors();
app.UseAuthentication();
app.UseAuthorization();
app.UseDefaultFiles();
app.UseStaticFiles();
app.MapControllers();
app.UseMiddleware<ErrorHandlerMiddleware>();
// apply migrations
await using var scope = app.Services.CreateAsyncScope();
await using var db = scope.ServiceProvider.GetRequiredService<ApplicationDBContext>();
await db.Database.MigrateAsync();
app.Run();
}
void ConfigureLogs()
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile(
$"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json",
optional: true)
.Build();
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.MinimumLevel.Error()
.WriteTo.Console()
.Enrich.WithExceptionDetails()
.WriteTo.Elasticsearch(ConfigureElasticSink(configuration, environment ?? ""))
.Enrich.WithProperty("Environment", environment)
.ReadFrom.Configuration(configuration)
.CreateLogger();
}
ElasticsearchSinkOptions ConfigureElasticSink(IConfigurationRoot configuration, string environment)
{
return new ElasticsearchSinkOptions(new Uri(configuration["ElasticConfiguration:Uri"] ?? ""))
{
AutoRegisterTemplate = true,
IndexFormat = $"{Assembly.GetExecutingAssembly()?.GetName()?.Name?.ToLower().Replace(".", "-")}-{environment?.ToLower().Replace(".", "-")}"
};
}

View file

@ -0,0 +1,48 @@
{
"profiles": {
"http": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:5039"
},
"https": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:7120;http://localhost:5039"
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Docker": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
"publishAllPorts": true,
"useSSL": true
}
},
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:35013",
"sslPort": 44374
}
}
}

View file

@ -0,0 +1,10 @@
using BMA.EHR.Domain.Models.MetaData;
using Microsoft.EntityFrameworkCore;
namespace BMA.EHR.Retirement.Service.Requests
{
public class ProfileRequest
{
public Guid ProfileId { get; set; }
}
}

View file

@ -0,0 +1,11 @@
using BMA.EHR.Domain.Models.MetaData;
using Microsoft.EntityFrameworkCore;
namespace BMA.EHR.Retirement.Service.Requests
{
public class ProfileRetireRequest
{
public Guid RetireProfileId { get; set; }
public string Reason { get; set; }
}
}

View file

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View file

@ -0,0 +1,36 @@
{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Information",
"System": "Warning"
}
}
},
"ElasticConfiguration": {
"Uri": "http://localhost:9200"
},
"AllowedHosts": "*",
"ConnectionStrings": {
//"DefaultConnection": "User Id=sys;Password=P@ssw0rd;DBA Privilege=SYSDBA;Data Source=localhost:1521/ORCLCDB",
"DefaultConnection": "server=127.0.0.1;user=root;password=P@ssw0rd;port=3308;database=bma_ehr_demo;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;"
// "DefaultConnection": "server=192.168.1.9;user=root;password=adminVM123;port=3306;database=bma_ehr_demo;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;"
},
"Jwt": {
"Key": "HP-FnQMUj9msHMSD3T9HtdEnphAKoCJLEl85CIqROFI",
"Issuer": "https://identity.frappet.com/realms/bma-ehr"
},
"EPPlus": {
"ExcelPackage": {
"LicenseContext": "NonCommercial"
}
},
"MinIO": {
"Endpoint": "https://s3.frappet.com/",
"AccessKey": "frappet",
"SecretKey": "P@ssw0rd",
"BucketName": "bma-recruit"
},
"Protocol": "HTTPS"
}

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<!--To inherit the global NuGet package sources remove the <clear/> line below -->
<clear />
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
<add key="private_nuget" value="https://nuget.frappet.synology.me/v3/index.json" />
</packageSources>
</configuration>

View file

@ -0,0 +1,184 @@
<!--
~ Copyright 2016 Red Hat, Inc. and/or its affiliates
~ and other contributors as indicated by the @author tags.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<html>
<head>
<script src="./keycloak.js"></script>
</head>
<body>
<div>
<button onclick="keycloak.login()">Login</button>
<button onclick="keycloak.login({ action: 'UPDATE_PASSWORD' })">Update Password</button>
<button onclick="keycloak.logout()">Logout</button>
<button onclick="keycloak.register()">Register</button>
<button onclick="keycloak.accountManagement()">Account</button>
<button onclick="refreshToken(9999)">Refresh Token</button>
<button onclick="refreshToken(30)">Refresh Token (if <30s validity)</button>
<button onclick="loadProfile()">Get Profile</button>
<button onclick="updateProfile()">Update profile</button>
<button onclick="loadUserInfo()">Get User Info</button>
<button onclick="output(keycloak.tokenParsed)">Show Token</button>
<button onclick="output(keycloak.refreshTokenParsed)">Show Refresh Token</button>
<button onclick="output(keycloak.idTokenParsed)">Show ID Token</button>
<button onclick="showExpires()">Show Expires</button>
<button onclick="output(keycloak)">Show Details</button>
<button onclick="output(keycloak.createLoginUrl())">Show Login URL</button>
<button onclick="output(keycloak.createLogoutUrl())">Show Logout URL</button>
<button onclick="output(keycloak.createRegisterUrl())">Show Register URL</button>
<button onclick="output(keycloak.createAccountUrl())">Show Account URL</button>
</div>
<h2>Result</h2>
<pre style="background-color: #ddd; border: 1px solid #ccc; padding: 10px; word-wrap: break-word; white-space: pre-wrap;" id="output"></pre>
<h2>Events</h2>
<pre style="background-color: #ddd; border: 1px solid #ccc; padding: 10px; word-wrap: break-word; white-space: pre-wrap;" id="events"></pre>
<script>
function loadProfile() {
keycloak.loadUserProfile().success(function(profile) {
output(profile);
}).error(function() {
output('Failed to load profile');
});
}
function updateProfile() {
var url = keycloak.createAccountUrl().split('?')[0];
var req = new XMLHttpRequest();
req.open('POST', url, true);
req.setRequestHeader('Accept', 'application/json');
req.setRequestHeader('Content-Type', 'application/json');
req.setRequestHeader('Authorization', 'bearer ' + keycloak.token);
req.onreadystatechange = function () {
if (req.readyState == 4) {
if (req.status == 200) {
output('Success');
} else {
output('Failed');
}
}
}
req.send('{"email":"myemail@foo.bar","firstName":"test","lastName":"bar"}');
}
function loadUserInfo() {
keycloak.loadUserInfo().success(function(userInfo) {
output(userInfo);
}).error(function() {
output('Failed to load user info');
});
}
function refreshToken(minValidity) {
keycloak.updateToken(minValidity).then(function(refreshed) {
if (refreshed) {
output(keycloak.tokenParsed);
} else {
output('Token not refreshed, valid for ' + Math.round(keycloak.tokenParsed.exp + keycloak.timeSkew - new Date().getTime() / 1000) + ' seconds');
}
}).catch(function() {
output('Failed to refresh token');
});
}
function showExpires() {
if (!keycloak.tokenParsed) {
output("Not authenticated");
return;
}
var o = 'Token Expires:\t\t' + new Date((keycloak.tokenParsed.exp + keycloak.timeSkew) * 1000).toLocaleString() + '\n';
o += 'Token Expires in:\t' + Math.round(keycloak.tokenParsed.exp + keycloak.timeSkew - new Date().getTime() / 1000) + ' seconds\n';
if (keycloak.refreshTokenParsed) {
o += 'Refresh Token Expires:\t' + new Date((keycloak.refreshTokenParsed.exp + keycloak.timeSkew) * 1000).toLocaleString() + '\n';
o += 'Refresh Expires in:\t' + Math.round(keycloak.refreshTokenParsed.exp + keycloak.timeSkew - new Date().getTime() / 1000) + ' seconds';
}
output(o);
}
function output(data) {
if (typeof data === 'object') {
data = JSON.stringify(data, null, ' ');
}
document.getElementById('output').innerHTML = data;
}
function event(event) {
var e = document.getElementById('events').innerHTML;
document.getElementById('events').innerHTML = new Date().toLocaleString() + "\t" + event + "\n" + e;
}
var keycloak = Keycloak();
keycloak.onAuthSuccess = function () {
event('Auth Success');
};
keycloak.onAuthError = function (errorData) {
event("Auth Error: " + JSON.stringify(errorData) );
};
keycloak.onAuthRefreshSuccess = function () {
event('Auth Refresh Success');
};
keycloak.onAuthRefreshError = function () {
event('Auth Refresh Error');
};
keycloak.onAuthLogout = function () {
event('Auth Logout');
};
keycloak.onTokenExpired = function () {
event('Access token expired.');
};
keycloak.onActionUpdate = function (status) {
switch (status) {
case 'success':
event('Action completed successfully'); break;
case 'cancelled':
event('Action cancelled by user'); break;
case 'error':
event('Action failed'); break;
}
};
// Flow can be changed to 'implicit' or 'hybrid', but then client must enable implicit flow in admin console too
var initOptions = {
responseMode: 'fragment',
flow: 'standard'
};
keycloak.init(initOptions).then(function(authenticated) {
output('Init Success (' + (authenticated ? 'Authenticated' : 'Not Authenticated') + ')');
}).catch(function() {
output('Init Error');
});
</script>
</body>
</html>

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,7 @@
{
"realm": "bma-ehr",
"auth-server-url": "https://identity.frappet.com",
"ssl-required": "external",
"resource": "bma-ehr",
"public-client": true
}