Check in and Check Out

This commit is contained in:
Suphonchai Phoonsawat 2023-11-13 15:26:14 +07:00
parent 065314fd6c
commit 8bf646230a
7 changed files with 769 additions and 50 deletions

View file

@ -3,7 +3,6 @@ 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;
@ -31,6 +30,9 @@ namespace BMA.EHR.Command.Service.Controllers
private readonly IConfiguration _configuration;
private readonly UserProfileRepository _userProfileRepository;
private readonly UserTimeStampRepository _userTimeStampRepository;
private readonly MinIOService _minIOService;
private readonly string _bucketName = "check-in";
#endregion
@ -42,7 +44,8 @@ namespace BMA.EHR.Command.Service.Controllers
IWebHostEnvironment hostingEnvironment,
IConfiguration configuration,
UserProfileRepository userProfileRepository,
UserTimeStampRepository userTimeStampRepository)
UserTimeStampRepository userTimeStampRepository,
MinIOService minIOService)
{
_dutyTimeRepository = dutyTimeRepository;
_context = context;
@ -51,6 +54,7 @@ namespace BMA.EHR.Command.Service.Controllers
_configuration = configuration;
_userProfileRepository = userProfileRepository;
_userTimeStampRepository = userTimeStampRepository;
_minIOService = minIOService;
}
#endregion
@ -345,6 +349,72 @@ namespace BMA.EHR.Command.Service.Controllers
return Success(ret);
}
/// <summary>
/// LV1_005 - ลงเวลาเข้า-ออกงาน (USER)
/// </summary>
/// <returns>
/// </returns>
/// <response code="200">เมื่อทำรายการสำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpPost("check-in"), DisableRequestSizeLimit]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> CheckInAsync([FromForm] CheckTimeDto data)
{
if (data.Img == null) throw new Exception(GlobalMessages.NoFileToUpload);
var currentDate = DateTime.Now;
var fileName = $"{_bucketName}/{UserId}/{DateTime.Now.ToString("dd-MM-yyyy")}/{data.Img.FileName}";
using (var ms = new MemoryStream())
{
data.Img.CopyTo(ms);
await _minIOService.UploadFileAsync(fileName, ms);
}
// create check in object
if (data.CheckInId == null)
{
var checkin = new UserTimeStamp
{
KeycloakUserId = UserId != null ? Guid.Parse(UserId) : Guid.Empty,
CheckInLat = data.Lat,
CheckInLon = data.Lon,
IsLocationCheckIn = data.IsLocation,
CheckInLocationName = data.LocationName,
CheckInPOI = data.POI,
CheckInRemark = data.Remark,
CheckInImageUrl = fileName,
CheckIn = currentDate
};
await _userTimeStampRepository.AddAsync(checkin);
}
else
{
var checkout = await _userTimeStampRepository.GetByIdAsync(data.CheckInId.Value);
if (checkout != null)
{
checkout.CheckOutLat = data.Lat;
checkout.CheckOutLon = data.Lon;
checkout.IsLocationCheckOut = data.IsLocation;
checkout.CheckOutLocationName = data.LocationName;
checkout.CheckOutPOI = data.POI;
checkout.CheckOutRemark = data.Remark;
checkout.CheckOutImageUrl = fileName;
checkout.CheckOut = currentDate;
}
else
{
throw new Exception(GlobalMessages.DataNotFound);
}
}
return Success(new { date = currentDate });
}
#endregion
#endregion