diff --git a/BMA.EHR.Application/Repositories/Leaves/GenericLeaveRepository.cs b/BMA.EHR.Application/Repositories/Leaves/GenericLeaveRepository.cs index d2263e74..e180fc83 100644 --- a/BMA.EHR.Application/Repositories/Leaves/GenericLeaveRepository.cs +++ b/BMA.EHR.Application/Repositories/Leaves/GenericLeaveRepository.cs @@ -70,8 +70,8 @@ namespace BMA.EHR.Application.Repositories.Leaves { if (entity is EntityBase) { - (entity as EntityBase).LastUpdateUserId = UserId!; - (entity as EntityBase).LastUpdateFullName = FullName!; + (entity as EntityBase).LastUpdateUserId = UserId ?? ""; + (entity as EntityBase).LastUpdateFullName = FullName ?? "System Administrator"; (entity as EntityBase).LastUpdatedAt = DateTime.Now; } diff --git a/BMA.EHR.Application/Repositories/Leaves/TimeAttendants/UserDutyTimeRepository.cs b/BMA.EHR.Application/Repositories/Leaves/TimeAttendants/UserDutyTimeRepository.cs index 327362e9..9c7a1053 100644 --- a/BMA.EHR.Application/Repositories/Leaves/TimeAttendants/UserDutyTimeRepository.cs +++ b/BMA.EHR.Application/Repositories/Leaves/TimeAttendants/UserDutyTimeRepository.cs @@ -67,7 +67,12 @@ namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants foreach (var d in data) { - var result = _userProfileRepository.UpdateDutyTimeAsync(d.ProfileId, d.DutyTimeId, d.EffectiveDate.Value.Date).Result; + d.IsProcess = true; + d.LastUpdateFullName = "Automation System"; + d.LastUpdateUserId = UserId ?? ""; + d.LastUpdatedAt = DateTime.Now; + var result = UpdateAsync(d).Result; + //var result = _userProfileRepository.UpdateDutyTimeAsync(d.ProfileId, d.DutyTimeId, d.EffectiveDate.Value.Date).Result; } } catch @@ -86,7 +91,7 @@ namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants return data; } - public async Task GetExist(Guid profileId,DateTime effectiveDate) + public async Task GetExist(Guid profileId, DateTime effectiveDate) { var data = await _dbContext.Set() .Where(x => x.ProfileId == profileId) @@ -96,6 +101,17 @@ namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants return data; } + public async Task GetLastEffectRound(Guid profileId) + { + var data = await _dbContext.Set() + .Where(x => x.ProfileId == profileId) + .Where(x => x.IsProcess) + .OrderByDescending(x => x.EffectiveDate) + .FirstOrDefaultAsync(); + + return data; + } + #endregion } diff --git a/BMA.EHR.Application/Repositories/UserProfileRepository.cs b/BMA.EHR.Application/Repositories/UserProfileRepository.cs index f250f80a..a130d13f 100644 --- a/BMA.EHR.Application/Repositories/UserProfileRepository.cs +++ b/BMA.EHR.Application/Repositories/UserProfileRepository.cs @@ -51,13 +51,20 @@ namespace BMA.EHR.Application.Repositories { try { - var profile = await _dbContext.Set().FirstOrDefaultAsync(x => x.Id == profileId); + var profile = await _dbContext.Set() + .AsQueryable() + .Include(x => x.Prefix) + .FirstOrDefaultAsync(x => x.Id == profileId); + if (profile == null) { throw new Exception(GlobalMessages.DataNotFound); } else { + var fullName = $"{profile.Prefix.Name}{profile.FirstName} {profile.LastName}"; + Console.WriteLine(fullName); + profile.DutyTimeId = roundId; profile.DutyTimeEffectiveDate = effectiveDate; diff --git a/BMA.EHR.Leave.Service/Controllers/LeaveController.cs b/BMA.EHR.Leave.Service/Controllers/LeaveController.cs index 0ca716f2..6ac14c11 100644 --- a/BMA.EHR.Leave.Service/Controllers/LeaveController.cs +++ b/BMA.EHR.Leave.Service/Controllers/LeaveController.cs @@ -525,7 +525,9 @@ namespace BMA.EHR.Leave.Service.Controllers return Error("ไม่พบรอบการลงเวลาทำงาน Default", StatusCodes.Status404NotFound); } - var userRound = await _dutyTimeRepository.GetByIdAsync(profile.DutyTimeId ?? Guid.Empty); + var effectiveDate = await _userDutyTimeRepository.GetLastEffectRound(profile.Id); + var roundId = effectiveDate != null ? effectiveDate.DutyTimeId : Guid.Empty; + var userRound = await _dutyTimeRepository.GetByIdAsync(roundId); // TODO : รอดุึงรอบที่ผูกกับ user var duty = userRound ?? defaultRound; @@ -684,7 +686,10 @@ namespace BMA.EHR.Leave.Service.Controllers return Error("ไม่พบรอบการลงเวลา Default", StatusCodes.Status404NotFound); } - var userRound = await _dutyTimeRepository.GetByIdAsync(profile.DutyTimeId ?? Guid.Empty); + //var userRound = await _dutyTimeRepository.GetByIdAsync(profile.DutyTimeId ?? Guid.Empty); + var effectiveDate = await _userDutyTimeRepository.GetLastEffectRound(profile.Id); + var roundId = effectiveDate != null ? effectiveDate.DutyTimeId : Guid.Empty; + var userRound = await _dutyTimeRepository.GetByIdAsync(roundId); var duty = userRound ?? defaultRound; @@ -759,7 +764,10 @@ namespace BMA.EHR.Leave.Service.Controllers return Error("ไม่พบรอบการลงเวลา Default", StatusCodes.Status404NotFound); } - var userRound = await _dutyTimeRepository.GetByIdAsync(profile.DutyTimeId ?? Guid.Empty); + //var userRound = await _dutyTimeRepository.GetByIdAsync(profile.DutyTimeId ?? Guid.Empty); + var effectiveDate = await _userDutyTimeRepository.GetLastEffectRound(profile.Id); + var roundId = effectiveDate != null ? effectiveDate.DutyTimeId : Guid.Empty; + var userRound = await _dutyTimeRepository.GetByIdAsync(roundId); var duty = userRound ?? defaultRound; @@ -850,6 +858,8 @@ namespace BMA.EHR.Leave.Service.Controllers var roundId = p.DutyTimeId ?? Guid.Empty; var round = await _dutyTimeRepository.GetByIdAsync(roundId); + var effectiveDate = await _userDutyTimeRepository.GetLastEffectRound(p.Id); + var res = new SearchProfileResultDto { ProfileId = p.Id, @@ -857,7 +867,7 @@ namespace BMA.EHR.Leave.Service.Controllers FullName = $"{p.Prefix.Name}{p.FirstName} {p.LastName}", StartTimeMorning = round != null ? round.StartTimeMorning : defaultRound.StartTimeMorning, LeaveTimeAfterNoon = round != null ? round.EndTimeAfternoon : defaultRound.EndTimeAfternoon, - EffectiveDate = p.DutyTimeEffectiveDate + EffectiveDate = effectiveDate == null ? null : effectiveDate.EffectiveDate.Value.Date }; resultSet.Add(res); } @@ -1014,7 +1024,12 @@ namespace BMA.EHR.Leave.Service.Controllers { return Error(GlobalMessages.DataNotFound, StatusCodes.Status404NotFound); } - var userRound = await _dutyTimeRepository.GetByIdAsync(profile.DutyTimeId ?? Guid.Empty); + //var userRound = await _dutyTimeRepository.GetByIdAsync(profile.DutyTimeId ?? Guid.Empty); + + var effectiveDate = await _userDutyTimeRepository.GetLastEffectRound(profile.Id); + var roundId = effectiveDate != null ? effectiveDate.DutyTimeId : Guid.Empty; + var userRound = await _dutyTimeRepository.GetByIdAsync(roundId); + var checkInData = await _userTimeStampRepository.GetTimestampByDateAsync(data.KeycloakUserId, data.CheckDate); var duty = userRound ?? getDefaultRound; @@ -1240,7 +1255,10 @@ namespace BMA.EHR.Leave.Service.Controllers return Error("ไม่พบรอบการลงเวลา Default", StatusCodes.Status404NotFound); } - var userRound = await _dutyTimeRepository.GetByIdAsync(profile.DutyTimeId ?? Guid.Empty); + //var userRound = await _dutyTimeRepository.GetByIdAsync(profile.DutyTimeId ?? Guid.Empty); + var effectiveDate = await _userDutyTimeRepository.GetLastEffectRound(profile.Id); + var roundId = effectiveDate != null ? effectiveDate.DutyTimeId : Guid.Empty; + var userRound = await _dutyTimeRepository.GetByIdAsync(roundId); var duty = userRound ?? defaultRound; @@ -1314,7 +1332,11 @@ namespace BMA.EHR.Leave.Service.Controllers { return Error(GlobalMessages.DataNotFound, StatusCodes.Status404NotFound); } - var userRound = await _dutyTimeRepository.GetByIdAsync(profile.DutyTimeId ?? Guid.Empty); + //var userRound = await _dutyTimeRepository.GetByIdAsync(profile.DutyTimeId ?? Guid.Empty); + var effectiveDate = await _userDutyTimeRepository.GetLastEffectRound(profile.Id); + var roundId = effectiveDate != null ? effectiveDate.DutyTimeId : Guid.Empty; + var userRound = await _dutyTimeRepository.GetByIdAsync(roundId); + var checkInData = await _userTimeStampRepository.GetTimestampByDateAsync(data.KeycloakUserId, data.CheckDate); var duty = userRound ?? getDefaultRound;