Compare commits

...

2 commits

Author SHA1 Message Date
Suphonchai Phoonsawat
7775ea85c3 Refactor error handling in LeaveController to return appropriate error responses instead of throwing exceptions
All checks were successful
Build & Deploy Leave Service / build (push) Successful in 1m18s
2026-02-04 10:32:44 +07:00
Suphonchai Phoonsawat
19000b2e42 Refactor check-out status logic to improve clarity and handle edge cases for same-day and next-day check-outs 2026-02-04 10:18:44 +07:00

View file

@ -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
{
@ -1158,35 +1160,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)