Pull Request 33520 suggestions Injected AppSettings instead of using GetEnvironmentVariable at Services level When debugging only app.Services.GetRequiredService<IPCRBService>(); Get ready to use VSCode IDE Align .editorconfig files
420 lines
18 KiB
C#
420 lines
18 KiB
C#
using System.Text;
|
|
|
|
using FabApprovalWorkerService.Models;
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
namespace FabApprovalWorkerService.Services;
|
|
|
|
public interface IUserService {
|
|
Task<List<OOOTemp>> GetAllPendingOOOUsersAsync();
|
|
Task<bool> IsUserAlreadyOOO(int userId);
|
|
Task<bool> IsDelegatorAlreadyDelegatedTo(int userId);
|
|
Task<bool> InsertDelegatedRoles(int userId);
|
|
Task<bool> RemoveDelegatedRoles(int userId);
|
|
Task<bool> DelegateUserSubRoles(int userId, int delegatedUserId);
|
|
Task<bool> RemoveDelegatedUserSubRoles(int userId, int delegatedUserId);
|
|
Task<bool> DelegateApprovalsForUser(int userId, int delegatedUserId);
|
|
Task<bool> RemoveDelegatedApprovalsForUser(int userId, int delegatedUserId);
|
|
Task<bool> FlagUserAsOOO(OOOTemp oooTemp);
|
|
Task<bool> RemoveOOOFlagForUser(int userId);
|
|
Task<bool> SetOOOTempProcessed(OOOTemp oOOTemp);
|
|
Task<List<User>> GetAllExpiredOOOUsersAsync();
|
|
Task<string> GetUserEmail(int userId);
|
|
Task<User> GetUserById(int userId);
|
|
Task<User> GetUserByEmail(string email);
|
|
}
|
|
|
|
public class UserService : IUserService {
|
|
private static readonly int PENDING_ITEM_STATUS = 0;
|
|
private static readonly int DENITED_ITEM_STATUS = 2;
|
|
|
|
private readonly ILogger<UserService> _logger;
|
|
private readonly IDalService _dalService;
|
|
|
|
public UserService(ILogger<UserService> logger, IDalService dalService) {
|
|
_logger = logger;
|
|
if (_logger is null)
|
|
throw new ArgumentNullException("ILogger not injected");
|
|
|
|
_dalService = dalService;
|
|
if (_dalService is null)
|
|
throw new ArgumentNullException("IDalService not injected");
|
|
}
|
|
|
|
public async Task<List<User>> GetAllExpiredOOOUsersAsync() {
|
|
try {
|
|
_logger.LogInformation("Attempting to get all active OOO users.");
|
|
|
|
StringBuilder queryBuilder = new();
|
|
queryBuilder.Append("select * from Users where OOO = 1 and IsActive = 1 and ");
|
|
queryBuilder.Append($"OOOExpirationDate <= '{DateTime.Now.Date.ToString("yyyy-MM-dd HH:mm:ss")}';");
|
|
|
|
return (await _dalService.QueryAsync<User>(queryBuilder.ToString())).ToList();
|
|
} catch (Exception ex) {
|
|
string errMsg = $"An exception occurred when attempting to get all active OOO users. Exception: {ex.Message}";
|
|
_logger.LogError(errMsg);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<List<OOOTemp>> GetAllPendingOOOUsersAsync() {
|
|
try {
|
|
_logger.LogInformation("Attempting to get all pending OOO users.");
|
|
|
|
StringBuilder queryBuilder = new();
|
|
queryBuilder.Append("select * from OOOTemp where Processed = 0 and ");
|
|
queryBuilder.Append($"OOOStartDate <= '{DateTime.Now.Date.ToString("yyyy-MM-dd HH:mm:ss")}';");
|
|
|
|
return (await _dalService.QueryAsync<OOOTemp>(queryBuilder.ToString())).ToList();
|
|
} catch (Exception ex) {
|
|
string errMsg = $"An exception occurred when attempting to get all pending OOO users. Exception: {ex.Message}";
|
|
_logger.LogError(errMsg);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> IsUserAlreadyOOO(int userId) {
|
|
try {
|
|
_logger.LogInformation($"Attempting to determine if user {userId} is already OOO");
|
|
|
|
if (userId <= 0) {
|
|
throw new ArgumentException($"User Id {userId} is not a valid user Id");
|
|
} else {
|
|
string sql = $"select * from Users where OOO = 1 and UserID = {userId}";
|
|
|
|
return (await _dalService.QueryAsync<User>(sql)).Count() > 0;
|
|
}
|
|
} catch (Exception ex) {
|
|
StringBuilder errMsgBuilder = new();
|
|
errMsgBuilder.Append("An exception occurred when attempting to determine if user ");
|
|
errMsgBuilder.Append($"{userId} is already OOO. Exception: {ex.Message}");
|
|
_logger.LogError(errMsgBuilder.ToString());
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> IsDelegatorAlreadyDelegatedTo(int userId) {
|
|
try {
|
|
_logger.LogInformation($"Attempting to determine if user {userId} is already a delegate.");
|
|
|
|
if (userId <= 0) {
|
|
_logger.LogInformation($"DelegatedTo is {userId}, which is not an active user Id");
|
|
return false;
|
|
} else {
|
|
string sql = $"select * from Users where DelegatedTo = {userId}";
|
|
|
|
return (await _dalService.QueryAsync<User>(sql)).Count() > 0;
|
|
}
|
|
} catch (Exception ex) {
|
|
StringBuilder errMsgBuilder = new();
|
|
errMsgBuilder.Append("An exception occurred when attempting to determine if user ");
|
|
errMsgBuilder.Append($"{userId} is already delegated to. Exception: {ex.Message}");
|
|
_logger.LogError(errMsgBuilder.ToString());
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> InsertDelegatedRoles(int userId) {
|
|
try {
|
|
_logger.LogInformation($"Attempting to add delegated roles for OOO user {userId}");
|
|
|
|
if (userId <= 0) {
|
|
throw new ArgumentException($"User Id {userId} is not a valid user Id");
|
|
} else {
|
|
StringBuilder queryBuilder = new StringBuilder();
|
|
|
|
string sql = $"select * from UserSubRole where UserID = {userId}";
|
|
|
|
List<UserSubRole> userSubRoles = (await _dalService.QueryAsync<UserSubRole>(sql)).ToList();
|
|
|
|
foreach (UserSubRole role in userSubRoles) {
|
|
queryBuilder.Clear();
|
|
queryBuilder.Append("insert into OOODelegatedRoles (UserID, DelegatedSubRoleID, InsertTimeStamp, Active) ");
|
|
queryBuilder.Append($"values ({userId}, {role.SubRoleID}, '{DateTime.Now.Date.ToString("yyyy-MM-dd HH:mm:ss")}', 1);");
|
|
|
|
await _dalService.ExecuteAsync(queryBuilder.ToString());
|
|
}
|
|
|
|
return true;
|
|
}
|
|
} catch (Exception ex) {
|
|
StringBuilder errMsgBuilder = new();
|
|
errMsgBuilder.Append($"An exception occurred when attempting to add delegated roles for OOO user {userId}. ");
|
|
errMsgBuilder.Append($"Exception: {ex.Message}");
|
|
_logger.LogError(errMsgBuilder.ToString());
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> RemoveDelegatedRoles(int userId) {
|
|
try {
|
|
_logger.LogInformation($"Attempting to remove delegated roles for OOO user {userId}");
|
|
|
|
if (userId <= 0) {
|
|
throw new ArgumentException($"User Id {userId} is not a valid user Id");
|
|
} else {
|
|
string sql = $"update OOODelegatedRoles set Active = 0 where UserID = {userId} and Active = 1;";
|
|
|
|
await _dalService.ExecuteAsync(sql);
|
|
|
|
return true;
|
|
}
|
|
} catch (Exception ex) {
|
|
StringBuilder errMsgBuilder = new();
|
|
errMsgBuilder.Append($"An exception occurred when attempting to remove delegated roles for OOO user {userId}. ");
|
|
errMsgBuilder.Append($"Exception: {ex.Message}");
|
|
_logger.LogError(errMsgBuilder.ToString());
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> DelegateUserSubRoles(int userId, int delegatedUserId) {
|
|
try {
|
|
StringBuilder logMsgBuilder = new();
|
|
logMsgBuilder.Append($"Attempting to update sub roles for user {userId} ");
|
|
logMsgBuilder.Append($"to delegated user {delegatedUserId}");
|
|
_logger.LogInformation(logMsgBuilder.ToString());
|
|
if (userId <= 0)
|
|
throw new ArgumentException($"User Id {userId} is not a valid user Id");
|
|
if (delegatedUserId <= 0)
|
|
throw new ArgumentException($"Delegated user Id {delegatedUserId} is not a valid user Id");
|
|
|
|
string sql = $"update UserSubRole set UserID = {delegatedUserId}, Delegated = 1 where UserID = {userId}";
|
|
|
|
await _dalService.ExecuteAsync(sql);
|
|
|
|
return true;
|
|
} catch (Exception ex) {
|
|
StringBuilder errMsgBuilder = new();
|
|
errMsgBuilder.Append($"An exception occurred when attempting to update sub roles for user {userId} ");
|
|
errMsgBuilder.Append($"to delegated user {delegatedUserId}. Exception: {ex.Message}");
|
|
_logger.LogError(errMsgBuilder.ToString());
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> RemoveDelegatedUserSubRoles(int userId, int delegatedUserId) {
|
|
try {
|
|
_logger.LogInformation($"Attempting to remove delegated roles for OOO user {userId}");
|
|
|
|
if (userId <= 0)
|
|
throw new ArgumentException($"User Id {userId} is not a valid user Id");
|
|
if (delegatedUserId > 0) {
|
|
_logger.LogInformation($"User {userId} delegated sub roles to {delegatedUserId}");
|
|
|
|
StringBuilder queryBuilder = new StringBuilder();
|
|
queryBuilder.Append("select DelegatedSubRoleID from OOODelegatedRoles ");
|
|
queryBuilder.Append($"where UserID = {userId} and Active = 1");
|
|
|
|
List<int> userSubRoleIds = (await _dalService.QueryAsync<int>(queryBuilder.ToString())).ToList();
|
|
|
|
foreach (int id in userSubRoleIds) {
|
|
queryBuilder.Clear();
|
|
queryBuilder.Append($"update UserSubRole set UserID = {userId}, Delegated = 0 ");
|
|
queryBuilder.Append($"where UserID = {delegatedUserId} and Delegated = 1 ");
|
|
queryBuilder.Append($"and SubRoleID in ({string.Join(',', userSubRoleIds)})");
|
|
|
|
await _dalService.ExecuteAsync(queryBuilder.ToString());
|
|
}
|
|
} else {
|
|
_logger.LogInformation($"User {userId} had no delegated sub roles");
|
|
}
|
|
|
|
return true;
|
|
} catch (Exception ex) {
|
|
StringBuilder errMsgBuilder = new();
|
|
errMsgBuilder.Append("An exception occurred when attempting to remove delegated roles for ");
|
|
errMsgBuilder.Append($"OOO user {userId}. Exception: {ex.Message}");
|
|
_logger.LogError(errMsgBuilder.ToString());
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> DelegateApprovalsForUser(int userId, int delegatedUserId) {
|
|
try {
|
|
_logger.LogInformation($"Attempting to delegate approvals for user {userId} to delegated user {delegatedUserId}");
|
|
if (userId <= 0)
|
|
throw new ArgumentException($"User Id {userId} is not a valid user Id");
|
|
if (delegatedUserId <= 0)
|
|
throw new ArgumentException($"Delegated user Id {delegatedUserId} is not a valid user Id");
|
|
|
|
StringBuilder queryBuilder = new();
|
|
queryBuilder.Append($"update Approval set UserID = {delegatedUserId}, ");
|
|
queryBuilder.Append($"Delegated = 1 where UserID = {userId} ");
|
|
queryBuilder.Append($"and (ItemStatus = {PENDING_ITEM_STATUS} or ItemStatus = {DENITED_ITEM_STATUS})");
|
|
|
|
await _dalService.ExecuteAsync(queryBuilder.ToString());
|
|
|
|
return true;
|
|
} catch (Exception ex) {
|
|
StringBuilder errMsgBuilder = new();
|
|
errMsgBuilder.Append($"An exception occurred when attempting to delegate approvals for user {userId} ");
|
|
errMsgBuilder.Append($"to delegated user {delegatedUserId}. Exception: {ex.Message}");
|
|
_logger.LogError(errMsgBuilder.ToString());
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> RemoveDelegatedApprovalsForUser(int userId, int delegatedUserId) {
|
|
try {
|
|
_logger.LogInformation($"Attempting to remove delegated roles for OOO user {userId}");
|
|
|
|
if (userId <= 0)
|
|
throw new ArgumentException($"User Id {userId} is not a valid user Id");
|
|
if (delegatedUserId > 0) {
|
|
_logger.LogInformation($"User {userId} delegated approvals to {delegatedUserId}");
|
|
|
|
StringBuilder queryBuilder = new StringBuilder();
|
|
|
|
queryBuilder.Append("select DelegatedSubRoleID from OOODelegatedRoles ");
|
|
queryBuilder.Append($"where UserID = {userId} and Active = 1");
|
|
|
|
List<int> userSubRoleIds = (await _dalService.QueryAsync<int>(queryBuilder.ToString())).ToList();
|
|
|
|
foreach (int id in userSubRoleIds) {
|
|
queryBuilder.Clear();
|
|
queryBuilder.Append($"update Approval set UserID = {userId}, Delegated = 0 ");
|
|
queryBuilder.Append($"where UserID = {delegatedUserId} and Delegated = 1 and ");
|
|
queryBuilder.Append($"(ItemStatus = {PENDING_ITEM_STATUS} or ItemStatus = {DENITED_ITEM_STATUS}) ");
|
|
queryBuilder.Append($"and SubRoleID in ({string.Join(',', userSubRoleIds)})");
|
|
|
|
await _dalService.ExecuteAsync(queryBuilder.ToString());
|
|
}
|
|
} else {
|
|
_logger.LogInformation($"User {userId} had not delegated approvals");
|
|
}
|
|
|
|
return true;
|
|
} catch (Exception ex) {
|
|
StringBuilder errMsgBuilder = new();
|
|
errMsgBuilder.Append("An exception occurred when attempting to remove delegated roles ");
|
|
errMsgBuilder.Append($"for OOO user {userId}. Exception: {ex.Message}");
|
|
_logger.LogError(errMsgBuilder.ToString());
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> FlagUserAsOOO(OOOTemp oooTemp) {
|
|
try {
|
|
if (oooTemp is null) throw new ArgumentNullException("oooTemp cannot be null");
|
|
|
|
_logger.LogInformation($"Attempting to flag user {oooTemp.OOOUserID} as OOO");
|
|
|
|
StringBuilder queryBuilder = new StringBuilder();
|
|
queryBuilder.Append("update Users set OOO = 1, ");
|
|
queryBuilder.Append($"OOOStartDate = '{oooTemp.OOOStartDate}', ");
|
|
queryBuilder.Append($"OOOExpirationDate = '{oooTemp.OOOExpirationDate}', ");
|
|
queryBuilder.Append($"DelegatedTo = {oooTemp.DelegatedTo} ");
|
|
queryBuilder.Append($"where UserID = {oooTemp.OOOUserID}");
|
|
|
|
return (await _dalService.ExecuteAsync(queryBuilder.ToString())) > 0;
|
|
} catch (Exception ex) {
|
|
_logger.LogError($"An exception occurred when attempting to flag user as OOO. Exception: {ex.Message}");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> RemoveOOOFlagForUser(int userId) {
|
|
try {
|
|
if (userId <= 0) throw new ArgumentException($"User Id {userId} is not a valid user Id");
|
|
|
|
_logger.LogInformation($"Attempting to remove OOO flag for user {userId}");
|
|
|
|
StringBuilder queryBuilder = new StringBuilder();
|
|
queryBuilder.Append("update Users set OOO = 0, ");
|
|
queryBuilder.Append("OOOStartDate = NULL, OOOExpirationDate = NULL, ");
|
|
queryBuilder.Append("DelegatedTo = NULL ");
|
|
queryBuilder.Append($"where UserID = {userId}");
|
|
|
|
return (await _dalService.ExecuteAsync(queryBuilder.ToString())) > 0;
|
|
} catch (Exception ex) {
|
|
_logger.LogError($"An exception occurred when attempting to flag user as OOO. Exception: {ex.Message}");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> SetOOOTempProcessed(OOOTemp oooTemp) {
|
|
try {
|
|
if (oooTemp is null) throw new ArgumentNullException("oooTemp cannot be null");
|
|
|
|
_logger.LogInformation($"Attempting to set OOOTemp {oooTemp.ID} Processed to {oooTemp.Processed}");
|
|
|
|
string sql = $"update OOOTemp set Processed = {Convert.ToInt32(oooTemp.Processed)} where ID = {oooTemp.ID}";
|
|
|
|
return (await _dalService.ExecuteAsync(sql)) > 0;
|
|
} catch (Exception ex) {
|
|
_logger.LogError($"An exception occurred when attempting to flag user as OOO. Exception: {ex.Message}");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<string> GetUserEmail(int userId) {
|
|
try {
|
|
if (userId <= 0) throw new ArgumentException($"User Id {userId} is not a valid user Id");
|
|
|
|
_logger.LogInformation($"Attempting to get email for user {userId}");
|
|
|
|
string sql = $"select Email from Users where UserID = {userId}";
|
|
|
|
string? userEmail = (await _dalService.QueryAsync<string>(sql)).FirstOrDefault();
|
|
|
|
if (userEmail is null)
|
|
throw new Exception($"No email found for user {userId}");
|
|
|
|
return userEmail;
|
|
} catch (Exception ex) {
|
|
StringBuilder errMsgBuilder = new();
|
|
errMsgBuilder.Append($"An exception occurred when attempting to get email for user {userId}. ");
|
|
errMsgBuilder.Append($"Exception: {ex.Message}");
|
|
_logger.LogError(errMsgBuilder.ToString());
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<User> GetUserById(int userId) {
|
|
if (userId <= 0) throw new ArgumentException($"{userId} not a valid UserID");
|
|
|
|
try {
|
|
_logger.LogInformation($"Attempting to get user {userId}");
|
|
|
|
string sql = $"select * from Users where UserID = {userId}";
|
|
|
|
User? user = (await _dalService.QueryAsync<User>(sql)).FirstOrDefault();
|
|
|
|
if (user is null)
|
|
throw new Exception($"No user found for id {userId}");
|
|
|
|
return user;
|
|
} catch (Exception ex) {
|
|
StringBuilder errMsgBuilder = new();
|
|
errMsgBuilder.Append($"An exception occurred when attempting to get user {userId}. ");
|
|
errMsgBuilder.Append($"Exception: {ex.Message}");
|
|
_logger.LogError(errMsgBuilder.ToString());
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<User> GetUserByEmail(string email) {
|
|
try {
|
|
_logger.LogInformation($"Attempting to get user by email");
|
|
|
|
if (email.IsNullOrEmpty()) throw new ArgumentException("Email cannot be null or empty");
|
|
|
|
string sql = $"select * from Users where Email = '{email}' collate SQL_Latin1_General_CP1_CI_AS;";
|
|
|
|
User? user = (await _dalService.QueryAsync<User>(sql)).FirstOrDefault();
|
|
|
|
if (user is null)
|
|
throw new Exception($"No user found for email {email}");
|
|
|
|
return user;
|
|
} catch (Exception ex) {
|
|
StringBuilder errMsgBuilder = new();
|
|
errMsgBuilder.Append("An exception occurred when attempting to get user by email. ");
|
|
errMsgBuilder.Append($"Exception: {ex.Message}");
|
|
_logger.LogError(errMsgBuilder.ToString());
|
|
throw;
|
|
}
|
|
}
|
|
} |