Refactor GenericRepository and GenericLeaveRepository to expose PostExternalAPIAsync method and enhance LeaveProcessJobStatusRepository with API integration for processing employee records
This commit is contained in:
parent
2cd7798dd9
commit
82c31a0f57
3 changed files with 71 additions and 20 deletions
|
|
@ -12,6 +12,7 @@ using BMA.EHR.Domain.Extensions;
|
|||
using BMA.EHR.Domain.Models.Leave.TimeAttendants;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants
|
||||
{
|
||||
|
|
@ -27,6 +28,7 @@ namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants
|
|||
private readonly UserDutyTimeRepository _userDutyTimeRepository;
|
||||
private readonly ProcessUserTimeStampRepository _processUserTimeStampRepository;
|
||||
private readonly LeaveRequestRepository _leaveRequestRepository;
|
||||
private readonly IConfiguration _configuration;
|
||||
|
||||
#endregion
|
||||
|
||||
|
|
@ -39,12 +41,14 @@ namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants
|
|||
DutyTimeRepository dutyTimeRepository,
|
||||
UserDutyTimeRepository userDutyTimeRepository,
|
||||
ProcessUserTimeStampRepository processUserTimeStampRepository,
|
||||
LeaveRequestRepository leaveRequestRepository) : base(dbContext, httpContextAccessor)
|
||||
LeaveRequestRepository leaveRequestRepository,
|
||||
IConfiguration configuration) : base(dbContext, httpContextAccessor)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
_userProfileRepository = userProfileRepository;
|
||||
_holidayRepository = holidayRepository;
|
||||
_configuration = configuration;
|
||||
_leaveRequestRepository = leaveRequestRepository;
|
||||
_dutyTimeRepository = dutyTimeRepository;
|
||||
_userDutyTimeRepository = userDutyTimeRepository;
|
||||
|
|
@ -364,38 +368,50 @@ namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants
|
|||
stampAmount = stampAmount,
|
||||
remark = remarkStr,
|
||||
status = status
|
||||
|
||||
};
|
||||
|
||||
employees.Add(emp);
|
||||
count++;
|
||||
}
|
||||
|
||||
// Write employees to JSON file
|
||||
var fileName = $"employees_{DateTime.Now:yyyyMMdd_HHmmss}.txt";
|
||||
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Exports", fileName);
|
||||
// // Write employees to JSON file
|
||||
// var fileName = $"employees_{DateTime.Now:yyyyMMdd_HHmmss}.txt";
|
||||
// var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Exports", fileName);
|
||||
|
||||
// Ensure directory exists
|
||||
var directory = Path.GetDirectoryName(filePath);
|
||||
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
|
||||
{
|
||||
Directory.CreateDirectory(directory);
|
||||
}
|
||||
// // Ensure directory exists
|
||||
// var directory = Path.GetDirectoryName(filePath);
|
||||
// if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
|
||||
// {
|
||||
// Directory.CreateDirectory(directory);
|
||||
// }
|
||||
|
||||
var jsonOptions = new JsonSerializerOptions
|
||||
{
|
||||
WriteIndented = true,
|
||||
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
|
||||
};
|
||||
// var jsonOptions = new JsonSerializerOptions
|
||||
// {
|
||||
// WriteIndented = true,
|
||||
// Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
|
||||
// };
|
||||
|
||||
var jsonContent = JsonSerializer.Serialize(employees, jsonOptions);
|
||||
await File.WriteAllTextAsync(filePath, jsonContent);
|
||||
// var jsonContent = JsonSerializer.Serialize(employees, jsonOptions);
|
||||
// await File.WriteAllTextAsync(filePath, jsonContent);
|
||||
}
|
||||
|
||||
//call api
|
||||
var apiPath = $"{_configuration["API"]}/org/profile/absent-late/batch";
|
||||
var apiKey = _configuration["API_KEY"];
|
||||
var body = new
|
||||
{
|
||||
records = employees
|
||||
};
|
||||
|
||||
var apiResult = await PostExternalAPIAsync(apiPath, AccessToken ?? "", body, apiKey);
|
||||
if(apiResult == "")
|
||||
{
|
||||
throw new Exception($"เรียก API {apiPath} ไม่สำเร็จ");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ProcessPendingJobsAsync()
|
||||
{
|
||||
|
||||
var pendingJobs = await GetPendingJobsAsync();
|
||||
Console.WriteLine($"พบงานที่ค้างอยู่ในสถานะ PENDING จำนวน {pendingJobs.Count} งาน");
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue