Refactor ScheduleUpdateDnaAsync to handle a list of ScheduleUpdateDnaDto and streamline profile updates #2341
All checks were successful
Build & Deploy Leave Service / build (push) Successful in 1m59s

This commit is contained in:
Suphonchai Phoonsawat 2026-02-26 20:36:48 +07:00
parent f866435897
commit 4650f7a2ab

View file

@ -580,32 +580,37 @@ namespace BMA.EHR.Leave.Service.Controllers
[HttpPut("schedule/update-dna")] [HttpPut("schedule/update-dna")]
[AllowAnonymous] [AllowAnonymous]
public async Task<ActionResult<ResponseObject>> ScheduleUpdateDnaAsync([FromBody] ScheduleUpdateDnaDto req) public async Task<ActionResult<ResponseObject>> ScheduleUpdateDnaAsync([FromBody] List<ScheduleUpdateDnaDto> req)
{ {
try try
{ {
var profile = await _userProfileRepository.GetProfileByProfileIdNoAuthAsync(req.ProfileId, AccessToken); foreach(var item in req)
if(profile == null)
{ {
return Error("ไม่พบข้อมูลข้าราชการหรือลูกจ้าง", StatusCodes.Status404NotFound); // var profile = await _userProfileRepository.GetProfileByProfileIdNoAuthAsync(item.ProfileId, AccessToken);
} // if(profile == null)
// check duplicate // {
var oldData = await _context.LeaveBeginnings.Where(x => x.ProfileId == req.ProfileId).ToListAsync(); // return Error("ไม่พบข้อมูลข้าราชการหรือลูกจ้าง", StatusCodes.Status404NotFound);
// }
foreach(var item in oldData) // check duplicate
{ var oldData = await _context.LeaveBeginnings.Where(x => x.ProfileId == item.ProfileId).ToListAsync();
item.RootDnaId = profile.RootDnaId;
item.Child1DnaId = profile.Child1DnaId; foreach(var o in oldData)
item.Child2DnaId = profile.Child2DnaId; {
item.Child3DnaId = profile.Child3DnaId; o.RootDnaId = item.RootDnaId;
item.Child4DnaId = profile.Child4DnaId; o.Child1DnaId = item.Child1DnaId;
o.Child2DnaId = item.Child2DnaId;
o.Child3DnaId = item.Child3DnaId;
o.Child4DnaId = item.Child4DnaId;
item.LastUpdateUserId = ""; o.LastUpdateUserId = "";
item.LastUpdateFullName = "System"; o.LastUpdateFullName = "System";
item.LastUpdatedAt = DateTime.Now; o.LastUpdatedAt = DateTime.Now;
await _leaveBeginningRepository.UpdateAsync(item); await _leaveBeginningRepository.UpdateAsync(o);
}
} }
return Success(); return Success();
} }
catch (Exception ex) catch (Exception ex)