Add job status check for pending or processing check-in/check-out requests
All checks were successful
Build & Deploy Leave Service / build (push) Successful in 1m19s
All checks were successful
Build & Deploy Leave Service / build (push) Successful in 1m19s
This commit is contained in:
parent
2f366374fa
commit
4e4eec3d84
1 changed files with 40 additions and 1 deletions
|
|
@ -530,6 +530,26 @@ namespace BMA.EHR.Leave.Service.Controllers
|
|||
// prepare data and convert request body and send to queue
|
||||
var userId = UserId == null ? Guid.Empty : Guid.Parse(UserId);
|
||||
var currentDate = DateTime.Now;
|
||||
|
||||
// ตรวจสอบว่ามีงานที่กำลัง pending หรือ processing อยู่หรือไม่
|
||||
var existingJobs = await _checkInJobStatusRepository.GetPendingOrProcessingJobsAsync(userId);
|
||||
if (existingJobs != null && existingJobs.Count > 0)
|
||||
{
|
||||
// กรองเฉพาะงานที่เป็นประเภทเดียวกัน (CHECK_IN หรือ CHECK_OUT)
|
||||
var checkType = data.CheckInId == null ? "CHECK_IN" : "CHECK_OUT";
|
||||
var sameTypeJob = existingJobs.FirstOrDefault(j => j.CheckType == checkType);
|
||||
|
||||
if (sameTypeJob != null)
|
||||
{
|
||||
// ตรวจสอบว่างานที่มีอยู่ถูกสร้างเมื่อไหร่ ถ้าเกิน 2 นาทีให้สร้างใหม่ได้
|
||||
var timeDiff = (currentDate - sameTypeJob.CreatedDate).TotalMinutes;
|
||||
if (timeDiff < 2)
|
||||
{
|
||||
return Error($"มีงาน {checkType} กำลังดำเนินการอยู่ กรุณารอสักครู่", StatusCodes.Status409Conflict);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var checkFileBytes = new byte[0];
|
||||
|
||||
// fix issue : ระบบลงเวลาปฏิบัติงาน>>รูปภาพไม่แสดงในฝั่งของ Admin #804
|
||||
|
|
@ -564,6 +584,8 @@ namespace BMA.EHR.Leave.Service.Controllers
|
|||
};
|
||||
|
||||
var channel = _objectPool.Get();
|
||||
CheckInJobStatus? jobStatus = null;
|
||||
|
||||
try
|
||||
{
|
||||
var queue = _configuration["Rabbit:Queue"] ?? "basic-queue";
|
||||
|
|
@ -576,7 +598,7 @@ namespace BMA.EHR.Leave.Service.Controllers
|
|||
properties.MessageId = taskId;
|
||||
|
||||
// บันทึกสถานะงานก่อนส่งไป RabbitMQ
|
||||
var jobStatus = new CheckInJobStatus
|
||||
jobStatus = new CheckInJobStatus
|
||||
{
|
||||
TaskId = Guid.Parse(taskId),
|
||||
KeycloakUserId = userId,
|
||||
|
|
@ -593,6 +615,7 @@ namespace BMA.EHR.Leave.Service.Controllers
|
|||
};
|
||||
await _checkInJobStatusRepository.AddAsync(jobStatus);
|
||||
|
||||
// ส่งไป RabbitMQ
|
||||
channel.BasicPublish(exchange: "",
|
||||
routingKey: queue,
|
||||
basicProperties: properties,
|
||||
|
|
@ -600,6 +623,22 @@ namespace BMA.EHR.Leave.Service.Controllers
|
|||
|
||||
return Success(new { date = currentDate, taskId = taskId, keycloakId = userId });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// ถ้าส่งไป queue ไม่สำเร็จ ให้ลบ job status ที่สร้างไว้ออก
|
||||
if (jobStatus != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _checkInJobStatusRepository.DeleteAsync(jobStatus);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Ignore delete error
|
||||
}
|
||||
}
|
||||
throw new Exception($"ไม่สามารถส่งงานไปยัง Queue ได้: {ex.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
_objectPool.Return(channel);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue