LV1_006 - เช็คเวลาต้องลงเวลาเข้าหรือออกงาน (USER)

This commit is contained in:
Suphonchai Phoonsawat 2023-11-10 14:40:53 +07:00
parent c9f68b045b
commit 065314fd6c
20 changed files with 967 additions and 258 deletions

View file

@ -1,114 +0,0 @@
using BMA.EHR.Application.Repositories;
using BMA.EHR.Application.Repositories.Leaves;
using BMA.EHR.Command.Service.DTOs.POI;
using BMA.EHR.Domain.Common;
using BMA.EHR.Infrastructure.Persistence;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Swashbuckle.AspNetCore.Annotations;
using System.Security.Claims;
namespace BMA.EHR.Command.Service.Controllers
{
[Route("api/v{version:apiVersion}/leave/check-in")]
[ApiVersion("1.0")]
[ApiController]
[Produces("application/json")]
[Authorize]
[SwaggerTag("API ระบบลงเวลาทำงาน")]
public class CheckInController : BaseController
{
#region " Fields "
private readonly DutyTimeRepository _repository;
private readonly LeaveDbContext _context;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IWebHostEnvironment _hostingEnvironment;
private readonly IConfiguration _configuration;
private readonly UserProfileRepository _userProfileRepository;
#endregion
#region " Constuctor and Destructor "
public CheckInController(DutyTimeRepository repository,
LeaveDbContext context,
IHttpContextAccessor httpContextAccessor,
IWebHostEnvironment hostingEnvironment,
IConfiguration configuration,
UserProfileRepository userProfileRepository)
{
_repository = repository;
_context = context;
_httpContextAccessor = httpContextAccessor;
_hostingEnvironment = hostingEnvironment;
_configuration = configuration;
_userProfileRepository = userProfileRepository;
}
#endregion
#region " Properties "
private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value;
private bool? PlacementAdmin => _httpContextAccessor?.HttpContext?.User?.IsInRole("placement1");
private Guid OcId
{
get
{
if (UserId != null || UserId != "")
return _userProfileRepository.GetUserOCId(Guid.Parse(UserId!));
else
return Guid.Empty;
}
}
#endregion
#region " Methods "
[HttpPost("locations")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[AllowAnonymous]
public async Task<ActionResult<ResponseObject>> ListPOIAsync([FromBody] GetPOIDto data)
{
var api_url = $"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={data.Lat},{data.Lon}&types=point_of_interest&radius=100&sensor=false&language=th&key=AIzaSyDXKvpU4hinlCKGOEJUgLDbx9yCSZe3woc";
using (var client = new HttpClient())
{
var req = new HttpRequestMessage(HttpMethod.Get, api_url);
var res = await client.SendAsync(req);
var result = await res.Content.ReadAsStringAsync();
var poi_result = JsonConvert.DeserializeObject<GetPOIResultDto>(result);
var poi_data = new List<POIResultDto>();
if (poi_result != null)
{
foreach (var r in poi_result.results.Take(5))
{
poi_data.Add(new POIResultDto
{
Id = r.place_id,
Name = r.name,
Latitude = r.geometry.location.lat,
Longitude = r.geometry.location.lng,
});
}
}
return Success(poi_data);
}
}
#endregion
}
}

View file

@ -1,8 +1,10 @@
using BMA.EHR.Application.Repositories;
using BMA.EHR.Application.Repositories.Leaves;
using BMA.EHR.Application.Repositories.Leaves.TimeAttendants;
using BMA.EHR.Command.Service.DTOs.CheckIn;
using BMA.EHR.Command.Service.DTOs.DutyTime;
using BMA.EHR.Domain.Common;
using BMA.EHR.Domain.Models.Leave;
using BMA.EHR.Domain.Models.Leave.TimeAttendants;
using BMA.EHR.Domain.Shared;
using BMA.EHR.Infrastructure.Persistence;
using Microsoft.AspNetCore.Authorization;
@ -12,40 +14,43 @@ using System.Security.Claims;
namespace BMA.EHR.Command.Service.Controllers
{
[Route("api/v{version:apiVersion}/leave/duty-time")]
[Route("api/v{version:apiVersion}/leave")]
[ApiVersion("1.0")]
[ApiController]
[Produces("application/json")]
[Authorize]
[SwaggerTag("API ระบบจัดการรอบการลงเวลาทำงาน")]
public class DutyTimeController : BaseController
[SwaggerTag("API ระบบลงเวลาและการลา")]
public class LeaveController : BaseController
{
#region " Fields "
private readonly DutyTimeRepository _repository;
private readonly DutyTimeRepository _dutyTimeRepository;
private readonly LeaveDbContext _context;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IWebHostEnvironment _hostingEnvironment;
private readonly IConfiguration _configuration;
private readonly UserProfileRepository _userProfileRepository;
private readonly UserTimeStampRepository _userTimeStampRepository;
#endregion
#region " Constuctor and Destructor "
public DutyTimeController(DutyTimeRepository repository,
public LeaveController(DutyTimeRepository dutyTimeRepository,
LeaveDbContext context,
IHttpContextAccessor httpContextAccessor,
IWebHostEnvironment hostingEnvironment,
IConfiguration configuration,
UserProfileRepository userProfileRepository)
UserProfileRepository userProfileRepository,
UserTimeStampRepository userTimeStampRepository)
{
_repository = repository;
_dutyTimeRepository = dutyTimeRepository;
_context = context;
_httpContextAccessor = httpContextAccessor;
_hostingEnvironment = hostingEnvironment;
_configuration = configuration;
_userProfileRepository = userProfileRepository;
_userTimeStampRepository = userTimeStampRepository;
}
#endregion
@ -73,6 +78,8 @@ namespace BMA.EHR.Command.Service.Controllers
#region " Methods "
#region " Duty Time รอบการทำงาน "
/// <summary>
/// LV1_004 - ข้อมูลทั้งหมดของรอบการปฏิบัติงาน (ADMIN)
/// </summary>
@ -81,13 +88,13 @@ namespace BMA.EHR.Command.Service.Controllers
/// <response code="200">เมื่อทำรายการสำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpGet]
[HttpGet("duty-time")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> GetAllAsync()
{
var data = await _repository.GetAllAsync();
var data = await _dutyTimeRepository.GetAllAsync();
return Success(data);
}
@ -100,13 +107,13 @@ namespace BMA.EHR.Command.Service.Controllers
/// <response code="200">เมื่อทำรายการสำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpGet("{id:guid}")]
[HttpGet("duty-time/{id:guid}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> GetByIdAsync(Guid id)
{
var data = await _repository.GetByIdAsync(id);
var data = await _dutyTimeRepository.GetByIdAsync(id);
return Success(data);
}
@ -119,7 +126,7 @@ namespace BMA.EHR.Command.Service.Controllers
/// <response code="200">เมื่อทำรายการสำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpPost]
[HttpPost("duty-time")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
@ -141,7 +148,7 @@ namespace BMA.EHR.Command.Service.Controllers
throw new Exception(GlobalMessages.StartTimeGreaterEnd);
}
var oldData = await _repository.GetAllAsync();
var oldData = await _dutyTimeRepository.GetAllAsync();
if (oldData == null || oldData.Count == 0)
{
var inserted = new DutyTime
@ -156,7 +163,7 @@ namespace BMA.EHR.Command.Service.Controllers
IsDefault = true,
};
var ret = await _repository.AddAsync(inserted);
var ret = await _dutyTimeRepository.AddAsync(inserted);
return Success(ret);
}
@ -167,7 +174,7 @@ namespace BMA.EHR.Command.Service.Controllers
foreach (var d in oldData)
{
d.IsDefault = false;
await _repository.UpdateAsync(d);
await _dutyTimeRepository.UpdateAsync(d);
}
}
@ -183,7 +190,7 @@ namespace BMA.EHR.Command.Service.Controllers
IsDefault = data.IsDefault,
};
var ret = await _repository.AddAsync(inserted);
var ret = await _dutyTimeRepository.AddAsync(inserted);
return Success(ret);
}
@ -198,26 +205,26 @@ namespace BMA.EHR.Command.Service.Controllers
/// <response code="200">เมื่อทำรายการสำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpPut("{id:guid}")]
[HttpPut("duty-time/{id:guid}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> PutAsync(Guid id, [FromBody] UpdateDutyTimeDto data)
{
var oldData = await _repository.GetByIdAsync(id);
var oldData = await _dutyTimeRepository.GetByIdAsync(id);
if (oldData == null)
{
throw new Exception(GlobalMessages.DataNotFound);
}
else
{
var oldDataList = await _repository.GetAllAsync();
{
var oldDataList = await _dutyTimeRepository.GetAllAsync();
if (data.IsDefault)
{
foreach (var d in oldDataList)
{
d.IsDefault = false;
await _repository.UpdateAsync(d);
await _dutyTimeRepository.UpdateAsync(d);
}
}
@ -225,7 +232,7 @@ namespace BMA.EHR.Command.Service.Controllers
oldData.IsDefault = data.IsDefault;
oldData.IsActive = data.IsActive;
await _repository.UpdateAsync(oldData);
await _dutyTimeRepository.UpdateAsync(oldData);
return Success(oldData);
@ -241,13 +248,13 @@ namespace BMA.EHR.Command.Service.Controllers
/// <response code="200">เมื่อทำรายการสำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpDelete("{id:guid}")]
[HttpDelete("duty-time/{id:guid}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> DeleteAsync(Guid id)
{
var oldData = await _repository.GetByIdAsync(id);
var oldData = await _dutyTimeRepository.GetByIdAsync(id);
if (oldData == null)
{
throw new Exception(GlobalMessages.DataNotFound);
@ -259,11 +266,87 @@ namespace BMA.EHR.Command.Service.Controllers
throw new Exception("ไม่สามารถลบรอบการปฏิบัติงานที่ยังใช้งานอยู่ได้");
}
await _repository.DeleteAsync(oldData);
await _dutyTimeRepository.DeleteAsync(oldData);
return Success();
}
}
/// <summary>
/// LV1_012 - ข้อมูลทั้งหมดของรอบการปฏิบัติงานที่ active (ADMIN)
/// </summary>
/// <returns>
/// </returns>
/// <response code="200">เมื่อทำรายการสำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpGet("round")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> GetAllActiveAsync()
{
var data = await _dutyTimeRepository.GetAllActiveAsync();
return Success(data);
}
#endregion
#region " Check-In Check-Out ลงเวลา "
/// <summary>
/// LV1_006 - เช็คเวลาต้องลงเวลาเข้าหรือออกงาน (USER)
/// </summary>
/// <returns>
/// </returns>
/// <response code="200">เมื่อทำรายการสำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpGet("check-time")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> CheckTimeAsync()
{
var userId = UserId == null ? Guid.Empty : Guid.Parse(UserId);
var data = await _userTimeStampRepository.GetLastRecord(userId);
// TODO : รอดุึงรอบที่ผูกกับ user
var duty = await _dutyTimeRepository.GetDefaultAsync();
CheckInResultDto ret;
if (data == null)
{
ret = new CheckInResultDto
{
StartTimeMorning = duty == null ? "00:00" : duty.StartTimeMorning,
EndTimeMorning = duty == null ? "00:00" : duty.EndTimeMorning,
StartTimeAfternoon = duty == null ? "00:00" : duty.StartTimeAfternoon,
EndTimeAfternoon = duty == null ? "00:00" : duty.EndTimeAfternoon,
Description = duty == null ? "-" : duty.Description,
CheckInTime = null,
CheckInId = null,
};
}
else
{
ret = new CheckInResultDto
{
StartTimeMorning = duty == null ? "00:00" : duty.StartTimeMorning,
EndTimeMorning = duty == null ? "00:00" : duty.EndTimeMorning,
StartTimeAfternoon = duty == null ? "00:00" : duty.StartTimeAfternoon,
EndTimeAfternoon = duty == null ? "00:00" : duty.EndTimeAfternoon,
Description = duty == null ? "-" : duty.Description,
CheckInTime = data.CheckIn,
CheckInId = data.CheckOut == null ? null : data.Id,
};
}
return Success(ret);
}
#endregion
#endregion
}
}

View file

@ -1,94 +0,0 @@
using BMA.EHR.Application.Repositories;
using BMA.EHR.Application.Repositories.Leaves;
using BMA.EHR.Domain.Common;
using BMA.EHR.Infrastructure.Persistence;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using System.Security.Claims;
namespace BMA.EHR.Command.Service.Controllers
{
[Route("api/v{version:apiVersion}/leave/round")]
[ApiVersion("1.0")]
[ApiController]
[Produces("application/json")]
[Authorize]
[SwaggerTag("API ระบบจัดการรอบการลงเวลาทำงาน")]
public class RoundController : BaseController
{
#region " Fields "
private readonly DutyTimeRepository _repository;
private readonly LeaveDbContext _context;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IWebHostEnvironment _hostingEnvironment;
private readonly IConfiguration _configuration;
private readonly UserProfileRepository _userProfileRepository;
#endregion
#region " Constuctor and Destructor "
public RoundController(DutyTimeRepository repository,
LeaveDbContext context,
IHttpContextAccessor httpContextAccessor,
IWebHostEnvironment hostingEnvironment,
IConfiguration configuration,
UserProfileRepository userProfileRepository)
{
_repository = repository;
_context = context;
_httpContextAccessor = httpContextAccessor;
_hostingEnvironment = hostingEnvironment;
_configuration = configuration;
_userProfileRepository = userProfileRepository;
}
#endregion
#region " Properties "
private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value;
private bool? PlacementAdmin => _httpContextAccessor?.HttpContext?.User?.IsInRole("placement1");
private Guid OcId
{
get
{
if (UserId != null || UserId != "")
return _userProfileRepository.GetUserOCId(Guid.Parse(UserId!));
else
return Guid.Empty;
}
}
#endregion
#region " Methods "
/// <summary>
/// LV1_012 - ข้อมูลทั้งหมดของรอบการปฏิบัติงานที่ active (ADMIN)
/// </summary>
/// <returns>
/// </returns>
/// <response code="200">เมื่อทำรายการสำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> GetAllActiveAsync()
{
var data = await _repository.GetAllActiveAsync();
return Success(data);
}
#endregion
}
}