From 19000b2e42dfe441c3f7c31c82d0166fe75ad032 Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Wed, 4 Feb 2026 10:18:44 +0700 Subject: [PATCH 1/2] Refactor check-out status logic to improve clarity and handle edge cases for same-day and next-day check-outs --- BMA.EHR.Leave/Controllers/LeaveController.cs | 102 ++++++++++++++----- 1 file changed, 78 insertions(+), 24 deletions(-) diff --git a/BMA.EHR.Leave/Controllers/LeaveController.cs b/BMA.EHR.Leave/Controllers/LeaveController.cs index 9819ecd0..3f8c9536 100644 --- a/BMA.EHR.Leave/Controllers/LeaveController.cs +++ b/BMA.EHR.Leave/Controllers/LeaveController.cs @@ -1158,35 +1158,89 @@ namespace BMA.EHR.Leave.Service.Controllers else { // fix issue : SIT ระบบบันทึกเวลาปฏิบัติงาน>>ลงเวลาเข้า-ออกงาน (กรณีลงเวลาออกอีกวัน) #921 - checkOutStatus = DateTime.Parse(currentDate.ToString("yyyy-MM-dd HH:mm")) < - DateTime.Parse($"{currentDate.ToString("yyyy-MM-dd")} {duty.EndTimeAfternoon}") ? - // "ABSENT" : - checkout.CheckIn.Date < currentDate.Date ? "NORMAL" : - DateTime.Parse(currentDate.ToString("yyyy-MM-dd HH:mm")) >= - DateTime.Parse($"{currentDate.ToString("yyyy-MM-dd")} {endTime}") ? - "NORMAL" : - "ABSENT" : - DateTime.Parse(currentDate.ToString("yyyy-MM-dd HH:mm")) < - DateTime.Parse($"{currentDate.ToString("yyyy-MM-dd")} {endTimeMorning}") ? - "ABSENT" : - "NORMAL"; + var currentDateTime = DateTime.Parse(currentDate.ToString("yyyy-MM-dd HH:mm")); + var dutyEndTimeAfternoon = DateTime.Parse($"{checkout.CheckIn.ToString("yyyy-MM-dd")} {endTime}"); + var dutyEndTimeMorning = DateTime.Parse($"{checkout.CheckIn.ToString("yyyy-MM-dd")} {endTimeMorning}"); + + + if(currentDateTime.Date > checkout.CheckIn.Date) + { + // ถ้า check-out เป็นวันถัดไป สถานะปกติเสมอ + checkOutStatus = "NORMAL"; + } + else + { + // ถ้า check-out เป็นวันเดียวกับ check-in + // ตรวจสอบเวลาว่าสิ้นสุดก่อนบ่ายหรือไม่ + if(currentDateTime < dutyEndTimeMorning) // ถ้าออกก่อนเวลาสิ้นสุดตอนเช้า ขาดราชการ + { + checkOutStatus = "ABSENT"; + } + else if(currentDateTime >= dutyEndTimeAfternoon) // ถ้าออกหลังเวลาสิ้นสุดตอนบ่าย ปกติ + { + checkOutStatus = "NORMAL"; + } + else + { + checkOutStatus = "ABSENT"; + } + } + // checkOutStatus = DateTime.Parse(currentDate.ToString("yyyy-MM-dd HH:mm")) < + // DateTime.Parse($"{checkout.CheckIn.ToString("yyyy-MM-dd")} {duty.EndTimeAfternoon}") ? + // // "ABSENT" : + // checkout.CheckIn.Date < currentDate.Date ? "NORMAL" : + // DateTime.Parse(currentDate.ToString("yyyy-MM-dd HH:mm")) >= + // DateTime.Parse($"{checkout.CheckIn.ToString("yyyy-MM-dd")} {endTime}") ? + // "NORMAL" : + // "ABSENT" : + // DateTime.Parse(currentDate.ToString("yyyy-MM-dd HH:mm")) < + // DateTime.Parse($"{currentDate.ToString("yyyy-MM-dd")} {endTimeMorning}") ? + // "ABSENT" : + // "NORMAL"; } } else { // fix issue : SIT ระบบบันทึกเวลาปฏิบัติงาน>>ลงเวลาเข้า-ออกงาน (กรณีลงเวลาออกอีกวัน) #921 - checkOutStatus = DateTime.Parse(currentDate.ToString("yyyy-MM-dd HH:mm")) < - DateTime.Parse($"{currentDate.ToString("yyyy-MM-dd")} {duty.EndTimeAfternoon}") ? - // "ABSENT" : - checkout.CheckIn.Date < currentDate.Date ? "NORMAL" : - DateTime.Parse(currentDate.ToString("yyyy-MM-dd HH:mm")) >= - DateTime.Parse($"{currentDate.ToString("yyyy-MM-dd")} {endTime}") ? - "NORMAL" : - "ABSENT" : - DateTime.Parse(currentDate.ToString("yyyy-MM-dd HH:mm")) < - DateTime.Parse($"{currentDate.ToString("yyyy-MM-dd")} {endTimeMorning}") ? - "ABSENT" : - "NORMAL"; + var currentDateTime = DateTime.Parse(currentDate.ToString("yyyy-MM-dd HH:mm")); + var dutyEndTimeAfternoon = DateTime.Parse($"{checkout.CheckIn.ToString("yyyy-MM-dd")} {endTime}"); + var dutyEndTimeMorning = DateTime.Parse($"{checkout.CheckIn.ToString("yyyy-MM-dd")} {endTimeMorning}"); + + + if(currentDateTime.Date > checkout.CheckIn.Date) + { + // ถ้า check-out เป็นวันถัดไป สถานะปกติเสมอ + checkOutStatus = "NORMAL"; + } + else + { + // ถ้า check-out เป็นวันเดียวกับ check-in + // ตรวจสอบเวลาว่าสิ้นสุดก่อนบ่ายหรือไม่ + if(currentDateTime < dutyEndTimeMorning) // ถ้าออกก่อนเวลาสิ้นสุดตอนเช้า ขาดราชการ + { + checkOutStatus = "ABSENT"; + } + else if(currentDateTime >= dutyEndTimeAfternoon) // ถ้าออกหลังเวลาสิ้นสุดตอนบ่าย ปกติ + { + checkOutStatus = "NORMAL"; + } + else + { + checkOutStatus = "ABSENT"; + } + } + // checkOutStatus = DateTime.Parse(currentDate.ToString("yyyy-MM-dd HH:mm")) < + // DateTime.Parse($"{currentDate.ToString("yyyy-MM-dd")} {duty.EndTimeAfternoon}") ? + // // "ABSENT" : + // checkout.CheckIn.Date < currentDate.Date ? "NORMAL" : + // DateTime.Parse(currentDate.ToString("yyyy-MM-dd HH:mm")) >= + // DateTime.Parse($"{currentDate.ToString("yyyy-MM-dd")} {endTime}") ? + // "NORMAL" : + // "ABSENT" : + // DateTime.Parse(currentDate.ToString("yyyy-MM-dd HH:mm")) < + // DateTime.Parse($"{currentDate.ToString("yyyy-MM-dd")} {endTimeMorning}") ? + // "ABSENT" : + // "NORMAL"; } if (checkout_process != null) From 7775ea85c3e483aa88e79250de6a4e3911ca3e37 Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Wed, 4 Feb 2026 10:32:44 +0700 Subject: [PATCH 2/2] Refactor error handling in LeaveController to return appropriate error responses instead of throwing exceptions --- BMA.EHR.Leave/Controllers/LeaveController.cs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/BMA.EHR.Leave/Controllers/LeaveController.cs b/BMA.EHR.Leave/Controllers/LeaveController.cs index 3f8c9536..d90321ba 100644 --- a/BMA.EHR.Leave/Controllers/LeaveController.cs +++ b/BMA.EHR.Leave/Controllers/LeaveController.cs @@ -527,12 +527,13 @@ namespace BMA.EHR.Leave.Service.Controllers if (sameTypeJob != null) { - // ตรวจสอบว่างานที่มีอยู่ถูกสร้างเมื่อไหร่ ถ้าเกิน 2 นาทีให้สร้างใหม่ได้ - var timeDiff = (currentDate - sameTypeJob.CreatedDate).TotalMinutes; - if (timeDiff < 2) - { - return Error($"มีงาน {checkType} กำลังดำเนินการอยู่ กรุณารอสักครู่", StatusCodes.Status409Conflict); - } + + return Error($"มีงาน {checkType} กำลังดำเนินการอยู่", StatusCodes.Status500InternalServerError); + // var timeDiff = (currentDate - sameTypeJob.CreatedDate).TotalMinutes; + // if (timeDiff < 2) + // { + // return Error($"มีงาน {checkType} กำลังดำเนินการอยู่ กรุณารอสักครู่", StatusCodes.Status409Conflict); + // } } } @@ -623,7 +624,8 @@ namespace BMA.EHR.Leave.Service.Controllers // Ignore delete error } } - throw new Exception($"ไม่สามารถส่งงานไปยัง Queue ได้: {ex.Message}"); + return Error($"ไม่สามารถส่งงานไปยัง Queue ได้: {ex.Message}"); + //throw new Exception($"ไม่สามารถส่งงานไปยัง Queue ได้: {ex.Message}"); } finally {