using CPRNIMS.Domain.UIContracts.Account; using CPRNIMS.Domain.UIContracts.Common; using CPRNIMS.Infrastructure.Helper; using CPRNIMS.Infrastructure.Models.Account; using CPRNIMS.Infrastructure.Models.Common; using CPRNIMS.Infrastructure.ViewModel.Account; using CPRNIMS.Infrastructure.ViewModel.Common; 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.Account { public class Account : IAccount { private readonly IConfiguration _configuration; private readonly TokenHelper _tokenHelper; private readonly IApiConfigurationService _apiConfigurationService; public string BaseUrl => _configuration["CommonEndpoints:ApiDefaultHeaders:BaseUrl"]; public Account(IConfiguration configuration, TokenHelper tokenHelper, IApiConfigurationService apiConfigurationService) { _configuration = configuration; _tokenHelper = tokenHelper; _apiConfigurationService = apiConfigurationService; } #region SendRequest service public async Task SendPostApiRequest(User user, UserRightsVM viewModel, string apiEndpoint) { var token = await _tokenHelper.GetValidTokenAsync(); try { if (string.IsNullOrEmpty(token)) { // Handle token retrieval failure return null; } viewModel.UserId = viewModel.UserId; viewModel.AdminUserId = user.UserId; var jsonContent = JsonSerializer.Serialize(viewModel); var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); using (var httpClient = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token)) { HttpResponseMessage response; response = await httpClient.PostAsync(apiEndpoint, content); var responseObject = JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync()); if (response.IsSuccessStatusCode) { viewModel.messCode = 1; viewModel.StatusResponse = responseObject.statusResponse; return viewModel; } else { viewModel.errMessage = responseObject.message; viewModel.messCode = responseObject.messCode; return viewModel; } } } catch (Exception ex) { ex.ToString(); throw; } } public async Task> SendGetApiRequest(User user, UserRightsVM viewModel, string apiEndpoint) { var token = await _tokenHelper.GetValidTokenAsync(); try { if (string.IsNullOrEmpty(token)) { // Handle token retrieval failure return null; } viewModel.UserId = viewModel.UserId; viewModel.AdminUserId = user.UserId; var jsonContent = JsonSerializer.Serialize(viewModel); var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); using (var httpClient = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token)) { HttpResponseMessage response; response = await httpClient.PostAsync(apiEndpoint, content); if (response.IsSuccessStatusCode) { var jsonResponse = await response.Content.ReadAsStringAsync(); var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; var myArtWork = JsonSerializer.Deserialize>(jsonResponse, options); return myArtWork; } else { // Handle API request failure return null; } } } catch (Exception ex) { throw; } } public async Task PutPostUserAccess(User user, UserRightsVM viewModel) { return await SendPostApiRequest(user, viewModel, _configuration["Account:PutPostUserAccess"]); } public async Task> GetDepartment(User user, UserRightsVM viewModel) { return await SendGetApiRequest(user, viewModel, _configuration["Account:GetDepartment"]); } public async Task> GetUserRights(User user, UserRightsVM viewModel) { return await SendGetApiRequest(user, viewModel, _configuration["Account:GetUserRights"]); } #endregion public async Task ChangePassword(EmailMessageDetailsVM viewModel) { try { viewModel.PasswordHash = viewModel.NewPassword; var jsonContent = JsonSerializer.Serialize(viewModel); var httpClient = new HttpClient(new HttpClientHandler { ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true }) { BaseAddress = new Uri(BaseUrl) }; var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); var response = await httpClient.PutAsync(_configuration["Account:ChangePassword"], content); var responseObject = JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync()); if (response.IsSuccessStatusCode) { viewModel.messCode = 1; viewModel.Message = responseObject.message; viewModel.status = responseObject.statusResponse; return viewModel; } else { viewModel.messCode = 0; viewModel.Message = responseObject.message; viewModel.status = responseObject.statusResponse; return viewModel; } } catch (Exception) { throw; } } public async Task GetUserByEmail(string email, EmailMessageDetailsVM forgotPassword) { forgotPassword.Email = email; var httpClient = new HttpClient(new HttpClientHandler { ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true }) { BaseAddress = new Uri(BaseUrl) }; var jsonContent = JsonSerializer.Serialize(forgotPassword); var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); var response = await httpClient.PostAsync(_configuration["Account:GetUserByEmail"], content); var responseObject = JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync()); if (response.IsSuccessStatusCode) { forgotPassword.messCode = 1; forgotPassword.status = responseObject.statusResponse; forgotPassword.Message = responseObject.message; return forgotPassword; } else { forgotPassword.messCode = 0; forgotPassword.status = responseObject.statusResponse; forgotPassword.Message = responseObject.message; return forgotPassword; } } public async Task> GetUserProfileById(User user) { var token = await _tokenHelper.GetValidTokenAsync(); if (!string.IsNullOrEmpty(token)) { using (var httpClient = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token)) { var response = await httpClient.GetAsync(_configuration["Account:GetUserById"] + user.UserId); if (response.IsSuccessStatusCode) { var jsonResponse = await response.Content.ReadAsStringAsync(); var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; var register = JsonSerializer.Deserialize>(jsonResponse, options); return register; } else { // Handle API request failure } } } // Handle token retrieval failure return null; } async Task IAccount.CreateUserAsync(RegisterVM registerModel, User user) { var token = await _tokenHelper.GetValidTokenAsync(); if (!string.IsNullOrEmpty(token)) { registerModel.Id = registerModel.NewUserId; registerModel.CreatedBy = user.UserName; registerModel.UpdatedBy = user.UserName; registerModel.ClaimType = registerModel.Company; registerModel.ClaimValue = registerModel.Role; registerModel.CreatedDate = DateTime.Now; registerModel.UpdatedDate = DateTime.Now; // Serialize the RegisterVM to JSON var jsonContent = JsonSerializer.Serialize(registerModel); var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); using (var httpClient = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token)) { var response = await httpClient.PostAsync(_configuration["Account:RegisterUser"], content); if (!response.IsSuccessStatusCode) { // Deserialize the JSON response var responseObject = JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync()); // Access the message property registerModel.message = responseObject.message; registerModel.statusResponse = responseObject.statusResponse; return registerModel; } return registerModel; } } return null; } Task IAccount.DisableUserAsync(RegisterVM registerModel) { throw new NotImplementedException(); } async Task> IAccount.GetAllUserAsync(User user) { var token = await _tokenHelper.GetValidTokenAsync(); if (!string.IsNullOrEmpty(token)) { using (var httpClient = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token)) { var response = await httpClient.GetAsync(_configuration["Account:GetAllUsers"]); if (response.IsSuccessStatusCode) { var jsonResponse = await response.Content.ReadAsStringAsync(); var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; var users = JsonSerializer.Deserialize>(jsonResponse, options); return users; } else { // Handle API request failure } } } // Handle token retrieval failure return null; } public async Task ValidateOTP(EmailMessageDetailsVM forgotPassword) { try { var httpClient = new HttpClient(new HttpClientHandler { ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true }) { BaseAddress = new Uri(BaseUrl) }; var jsonContent = JsonSerializer.Serialize(forgotPassword); var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); var response = await httpClient.PostAsync(_configuration["Account:ValidateOTP"], content); var responseObject = JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync()); if (response.IsSuccessStatusCode) { forgotPassword.messCode = 1; forgotPassword.status = responseObject.statusResponse; forgotPassword.Message = responseObject.message; return forgotPassword; } else { forgotPassword.messCode = 0; forgotPassword.status = responseObject.statusResponse; forgotPassword.Message = responseObject.message; return forgotPassword; } } catch (Exception ex) { ex.InnerException.ToString(); throw; } } public async Task> GetRoles(User user) { var token = await _tokenHelper.GetValidTokenAsync(); if (!string.IsNullOrEmpty(token)) { using (var httpClient = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token)) { var response = await httpClient.GetAsync(_configuration["Account:GetRoles"]); if (response.IsSuccessStatusCode) { var jsonResponse = await response.Content.ReadAsStringAsync(); Console.WriteLine($"Received JSON response: {jsonResponse}"); var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; var userRoles = JsonSerializer.Deserialize>(jsonResponse, options); return userRoles; } else { // Handle API request failure } } } // Handle token retrieval failure return null; } public async Task CreateUpdateRole(UserRoleVM UserRoleVM, User user) { var token = await _tokenHelper.GetValidTokenAsync(); if (!string.IsNullOrEmpty(token)) { // Serialize the RegisterVM to JSON var jsonContent = JsonSerializer.Serialize(UserRoleVM); var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); using (var httpClient = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token)) { var response = await httpClient.PostAsync(_configuration["Account:CreateUpdateRole"], content); if (!response.IsSuccessStatusCode) { // Deserialize the JSON response var responseObject = JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync()); // Access the message property UserRoleVM.message = responseObject.message; UserRoleVM.statusResponse = responseObject.statusResponse; return UserRoleVM; } return UserRoleVM; } } return null; } public async Task> GetAllRoleAsync(User user) { var token = await _tokenHelper.GetValidTokenAsync(); if (!string.IsNullOrEmpty(token)) { using (var httpClient = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token)) { var response = await httpClient.GetAsync(_configuration["Account:GetAllRoles"]); if (response.IsSuccessStatusCode) { var jsonResponse = await response.Content.ReadAsStringAsync(); var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; var userRoles = JsonSerializer.Deserialize>(jsonResponse, options); return userRoles; } else { // Handle API request failure } } } // Handle token retrieval failure return null; } public async Task> GetLandingPageByUserId(User user, string token) { try { if (string.IsNullOrEmpty(token)) return null; var jsonContent = JsonSerializer.Serialize(user); var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); using (var httpClient = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token)) { var response = await httpClient.PostAsync(_configuration["Account:GetLandingPageByUserId"], content); if (response.IsSuccessStatusCode) { var jsonResponse = await response.Content.ReadAsStringAsync(); var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; var myAccess = JsonSerializer.Deserialize>(jsonResponse, options); return myAccess; } } return null; } catch (Exception ex) { ex.ToString(); throw; } } public async Task> GetLandingPageByUserId(User user) { var token = await _tokenHelper.GetValidTokenAsync(); if (!string.IsNullOrEmpty(token)) { // Call the overload return await GetLandingPageByUserId(user, token); } return null; } public async Task> GetDepartment(User user) { var token = await _tokenHelper.GetValidTokenAsync(); if (string.IsNullOrEmpty(token)) { return null; } try { var jsonContent = JsonSerializer.Serialize(user); var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); using (var httpClient = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token)) { var response = await httpClient.GetAsync(_configuration["Account:GetDepartment"]); if (response.IsSuccessStatusCode) { var jsonResponse = await response.Content.ReadAsStringAsync(); var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; var departments = JsonSerializer.Deserialize>(jsonResponse, options); return departments; } else { // Handle API request failure return null; } } } catch (Exception ex) { // Log or handle the exception return null; } } public async Task UpdateUserProfile(UpdateUserVM viewModel, User user) { var token = await _tokenHelper.GetValidTokenAsync(); if (!string.IsNullOrEmpty(token)) { viewModel.UpdatedBy = user.UserName; viewModel.Id = viewModel.UserId; viewModel.Password = viewModel.PasswordHash; var jsonContent = JsonSerializer.Serialize(viewModel); var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); using (var httpClient = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token)) { var response = await httpClient.PutAsync(_configuration["Account:UpdateUser"], content); if (!response.IsSuccessStatusCode) { // Deserialize the JSON response var responseObject = JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync()); // Access the message property viewModel.message = responseObject.message; viewModel.statusResponse = responseObject.statusResponse; return viewModel; } return viewModel; } } return null; } } }