198 lines
9.5 KiB
C#
198 lines
9.5 KiB
C#
using Azure.Core;
|
|
using CPRNIMS.Domain.Services;
|
|
using CPRNIMS.Domain.UIContracts.Common;
|
|
using CPRNIMS.Domain.UIContracts.Inventory;
|
|
using CPRNIMS.Infrastructure.Dto.Common;
|
|
using CPRNIMS.Infrastructure.Dto.Inventory;
|
|
using CPRNIMS.Infrastructure.Dto.Inventory.Request;
|
|
using CPRNIMS.Infrastructure.Dto.Inventory.Response;
|
|
using CPRNIMS.Infrastructure.Helper;
|
|
using Microsoft.Extensions.Configuration;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace CPRNIMS.Domain.UIServices.Inventory
|
|
{
|
|
public class MRS : IMRS
|
|
{
|
|
private readonly IConfiguration _configuration;
|
|
private readonly TokenHelper _tokenHelper;
|
|
private readonly IApiConfigurationService _apiConfigurationService;
|
|
public MRS(IConfiguration configuration, TokenHelper tokenHelper,IApiConfigurationService apiConfigurationService)
|
|
{
|
|
_configuration = configuration;
|
|
_tokenHelper = tokenHelper;
|
|
_apiConfigurationService = apiConfigurationService;
|
|
}
|
|
private static readonly JsonSerializerOptions _mrsJsonOpts = new()
|
|
{
|
|
PropertyNameCaseInsensitive = true
|
|
};
|
|
|
|
public async Task<MRSPagedResponse> GetMRSPaged(MRSPagedRequest request, CancellationToken ct)
|
|
{
|
|
var token = await _tokenHelper.GetValidTokenAsync();
|
|
if (string.IsNullOrEmpty(token))
|
|
throw new InvalidOperationException("Token has been expired.");
|
|
|
|
var baseEndpoint = _configuration["LLI:NonInvent:InventoryMgmt:GetMRS"]
|
|
?? throw new InvalidOperationException("GetMRS endpoint is not configured.");
|
|
|
|
var qs = new StringBuilder(baseEndpoint).Append('?');
|
|
qs.Append($"pageNumber={request.PageNumber}&pageSize={request.PageSize}");
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.SearchMRSNo))
|
|
qs.Append($"&searchMRSNo={Uri.EscapeDataString(request.SearchMRSNo)}");
|
|
if (!string.IsNullOrWhiteSpace(request.SearchRISNo))
|
|
qs.Append($"&searchRISNo={Uri.EscapeDataString(request.SearchRISNo)}");
|
|
if (!string.IsNullOrWhiteSpace(request.SearchItemName))
|
|
qs.Append($"&searchItemName={Uri.EscapeDataString(request.SearchItemName)}");
|
|
if (!string.IsNullOrWhiteSpace(request.SearchReturnedBy))
|
|
qs.Append($"&searchReturnedBy={Uri.EscapeDataString(request.SearchReturnedBy)}");
|
|
if (request.Status.HasValue)
|
|
qs.Append($"&status={request.Status.Value}");
|
|
if (!string.IsNullOrWhiteSpace(request.Condition))
|
|
qs.Append($"&condition={Uri.EscapeDataString(request.Condition)}");
|
|
|
|
using var http = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token);
|
|
var response = await http.GetAsync(qs.ToString(), ct);
|
|
var json = await response.Content.ReadAsStringAsync(ct);
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
return new MRSPagedResponse { Data = [], RecordsTotal = 0 };
|
|
|
|
var result = JsonSerializer.Deserialize<MRSPagedResponse>(json, _mrsJsonOpts);
|
|
return result ?? new MRSPagedResponse();
|
|
}
|
|
|
|
public async Task<ApiResponse<object>> CreateMRS(CreateMRSRequest request, CancellationToken ct)
|
|
{
|
|
var token = await _tokenHelper.GetValidTokenAsync();
|
|
if (string.IsNullOrEmpty(token))
|
|
throw new InvalidOperationException("Token has been expired.");
|
|
|
|
var endpoint = _configuration["LLI:NonInvent:InventoryMgmt:CreateMRS"]
|
|
?? throw new InvalidOperationException("CreateMRS endpoint is not configured.");
|
|
|
|
using var http = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token);
|
|
using var content = new StringContent(JsonSerializer.Serialize(request), Encoding.UTF8, "application/json");
|
|
|
|
var response = await http.PostAsync(endpoint, content, ct);
|
|
var json = await response.Content.ReadAsStringAsync(ct);
|
|
|
|
var result = JsonSerializer.Deserialize<ApiResponse<object>>(json, _mrsJsonOpts)
|
|
?? new ApiResponse<object> { success = false, message = $"HTTP {(int)response.StatusCode}" };
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
result.success = false;
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<ApiResponse<object>> ApproveMRS(ApproveMRSRequest request, CancellationToken ct)
|
|
{
|
|
var token = await _tokenHelper.GetValidTokenAsync();
|
|
if (string.IsNullOrEmpty(token))
|
|
throw new InvalidOperationException("Token has been expired.");
|
|
|
|
var endpoint = _configuration["LLI:NonInvent:InventoryMgmt:ApproveMRS"]
|
|
?? throw new InvalidOperationException("ApproveMRS endpoint is not configured.");
|
|
|
|
using var http = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token);
|
|
using var content = new StringContent(
|
|
JsonSerializer.Serialize(request),
|
|
Encoding.UTF8, "application/json");
|
|
|
|
var response = await http.PostAsync(endpoint, content, ct);
|
|
var json = await response.Content.ReadAsStringAsync(ct);
|
|
var result = JsonSerializer.Deserialize<ApiResponse<object>>(json, _mrsJsonOpts)
|
|
?? new ApiResponse<object> { success = false, message = $"HTTP {(int)response.StatusCode}" };
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
result.success = false;
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<ApiResponse<object>> CancelMRS(CancelMRSRequest request, CancellationToken ct)
|
|
{
|
|
var token = await _tokenHelper.GetValidTokenAsync();
|
|
if (string.IsNullOrEmpty(token))
|
|
throw new InvalidOperationException("Token has been expired.");
|
|
|
|
var endpoint = _configuration["LLI:NonInvent:InventoryMgmt:CancelMRS"]
|
|
?? throw new InvalidOperationException("CancelMRS endpoint is not configured.");
|
|
|
|
using var http = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token);
|
|
using var content = new StringContent(
|
|
JsonSerializer.Serialize(request),
|
|
Encoding.UTF8, "application/json");
|
|
|
|
var response = await http.PostAsync(endpoint, content, ct);
|
|
var json = await response.Content.ReadAsStringAsync(ct);
|
|
var result = JsonSerializer.Deserialize<ApiResponse<object>>(json, _mrsJsonOpts)
|
|
?? new ApiResponse<object> { success = false, message = $"HTTP {(int)response.StatusCode}" };
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
result.success = false;
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<ApiResponse<List<SearchRISProjectCodeResponse>>> SearchRIS(SearchRISProjectCodeRequest request, CancellationToken ct)
|
|
{
|
|
var token = await _tokenHelper.GetValidTokenAsync();
|
|
if (string.IsNullOrEmpty(token))
|
|
throw new InvalidOperationException("Token has been expired.");
|
|
|
|
var baseEndpoint = _configuration["LLI:NonInvent:InventoryMgmt:SearchRIS"]
|
|
?? throw new InvalidOperationException("GetMRS endpoint is not configured.");
|
|
|
|
var qs = new StringBuilder(baseEndpoint).Append('?');
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.SearchRISNo))
|
|
qs.Append($"&searchRISNo={Uri.EscapeDataString(request.SearchRISNo)}");
|
|
if (!string.IsNullOrWhiteSpace(request.SearchRISNo))
|
|
qs.Append($"&projectCodeId= {request.SearchProjectCodeId}");
|
|
|
|
using var http = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token);
|
|
var response = await http.GetAsync(qs.ToString(), ct);
|
|
var json = await response.Content.ReadAsStringAsync(ct);
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
return new ApiResponse<List<SearchRISProjectCodeResponse>>{ };
|
|
|
|
var result = JsonSerializer.Deserialize<ApiResponse<List<SearchRISProjectCodeResponse>>>(json, _mrsJsonOpts);
|
|
return result ?? new ApiResponse<List<SearchRISProjectCodeResponse>>();
|
|
}
|
|
|
|
public async Task<ApiResponse<List<ProjectCodeOptionResponse>>> SearchProjects(SearchRISProjectCodeRequest request, CancellationToken ct)
|
|
{
|
|
var token = await _tokenHelper.GetValidTokenAsync();
|
|
if (string.IsNullOrEmpty(token))
|
|
throw new InvalidOperationException("Token has expired.");
|
|
|
|
var baseEndpoint = _configuration["LLI:NonInvent:InventoryMgmt:SearchProjects"]
|
|
?? throw new InvalidOperationException("SearchProjects endpoint is not configured.");
|
|
|
|
var qs = new StringBuilder(baseEndpoint);
|
|
if (!string.IsNullOrWhiteSpace(request.SearchProjectCode))
|
|
qs.Append('?').Append("searchProjectCode=").Append(Uri.EscapeDataString(request.SearchProjectCode));
|
|
|
|
using var http = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token);
|
|
var response = await http.GetAsync(qs.ToString(), ct);
|
|
var json = await response.Content.ReadAsStringAsync(ct);
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
return new ApiResponse<List<ProjectCodeOptionResponse>>();
|
|
|
|
var result = JsonSerializer.Deserialize<ApiResponse<List<ProjectCodeOptionResponse>>>(json, _mrsJsonOpts);
|
|
return result ?? new ApiResponse<List<ProjectCodeOptionResponse>>();
|
|
}
|
|
}
|
|
}
|