225 lines
9.7 KiB
C#
225 lines
9.7 KiB
C#
using FabApprovalWorkerService.Models;
|
|
|
|
using System.Text;
|
|
|
|
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> UpdateUserSubRoles(int userId, int delegatedUserId);
|
|
Task<bool> DelegateApprovalsForUser(int userId, int delegatedUserId);
|
|
Task<bool> FlagUserAsOOO(OOOTemp oooTemp);
|
|
Task<bool> SetOOOTempProcessed(OOOTemp oOOTemp);
|
|
Task<List<User>> GetAllActiveOOOUsersAsync();
|
|
}
|
|
|
|
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>> GetAllActiveOOOUsersAsync() {
|
|
try {
|
|
_logger.LogInformation("Attempting to get all active OOO users.");
|
|
|
|
string sql = "select * from Users where OOO = 1;";
|
|
|
|
return (await _dalService.QueryAsync<User>(sql)).ToList();
|
|
} catch (Exception ex) {
|
|
_logger.LogError("An exception occurred when attempting to get all active OOO users. Exception: {0}",
|
|
ex.Message);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<List<OOOTemp>> GetAllPendingOOOUsersAsync() {
|
|
try {
|
|
_logger.LogInformation("Attempting to get all pending OOO users.");
|
|
|
|
string sql = string.Format("select * from OOOTemp where Processed = 0 and OOOStartDate <= '{0}';", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
|
|
|
|
return (await _dalService.QueryAsync<OOOTemp>(sql)).ToList();
|
|
} catch (Exception ex) {
|
|
_logger.LogError("An exception occurred when attempting to get all pending OOO users. Exception: {0}",
|
|
ex.Message);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> IsUserAlreadyOOO(int userId) {
|
|
try {
|
|
_logger.LogInformation("Attempting to determine if user {userId} is already OOO", userId);
|
|
|
|
if (userId <= 0) {
|
|
throw new ArgumentException(string.Format("User Id {0} is not a valid user Id", userId));
|
|
} else {
|
|
string sql = string.Format("select * from Users where OOO = 1 and UserID = {0}", userId);
|
|
|
|
return (await _dalService.QueryAsync<User>(sql)).Count() > 0;
|
|
}
|
|
} catch (Exception ex) {
|
|
_logger.LogError("An exception occurred when attempting to determine if user {userId} is already OOO. Exception: {ex}",
|
|
userId,
|
|
ex.Message);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> IsDelegatorAlreadyDelegatedTo(int userId) {
|
|
try {
|
|
_logger.LogInformation("Attempting to determine if user {userId} is already OOO.", userId);
|
|
|
|
if (userId <= 0) {
|
|
_logger.LogInformation("DelegatedTo is {id}, which is not an active user Id", userId);
|
|
return false;
|
|
} else {
|
|
string sql = string.Format("select * from Users where DelegatedTo = {0}", userId);
|
|
|
|
return (await _dalService.QueryAsync<User>(sql)).Count() > 0;
|
|
}
|
|
} catch (Exception ex) {
|
|
_logger.LogError("An exception occurred when attempting to determine if user {userId} is already delegated to. Exception: {ex}",
|
|
userId,
|
|
ex.Message);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> InsertDelegatedRoles(int userId) {
|
|
try {
|
|
_logger.LogInformation("Attempting to add delegated roles for OOO user {id}", userId);
|
|
|
|
if (userId <= 0) {
|
|
throw new ArgumentException(string.Format("User Id {0} is not a valid user Id", userId));
|
|
} else {
|
|
StringBuilder queryBuilder = new StringBuilder();
|
|
|
|
string sql = string.Format("select * from UserSubRole where UserID = {0}", 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.AppendFormat("values ({0}, {1}, '{2}', 1);", userId, role.SubRoleID, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
|
|
|
|
await _dalService.ExecuteAsync(queryBuilder.ToString());
|
|
}
|
|
|
|
return true;
|
|
}
|
|
} catch (Exception ex) {
|
|
_logger.LogError("An exception occurred when attempting to add delegated roles for OOO user {userId}. Exception: {ex}",
|
|
userId,
|
|
ex.Message);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> UpdateUserSubRoles(int userId, int delegatedUserId) {
|
|
try {
|
|
_logger.LogInformation("Attempting to update sub roles for user {userId} to delegated user {delegatedUserId}",
|
|
userId,
|
|
delegatedUserId);
|
|
if (userId <= 0) {
|
|
throw new ArgumentException(string.Format("User Id {0} is not a valid user Id", userId));
|
|
} else if (delegatedUserId <= 0) {
|
|
throw new ArgumentException(string.Format("Delegated user Id {0} is not a valid user Id", delegatedUserId));
|
|
} else {
|
|
string sql = String.Format("update UserSubRole set UserID = {0}, Delegated = 1 where UserID = {1}", delegatedUserId, userId);
|
|
|
|
await _dalService.ExecuteAsync(sql);
|
|
|
|
return true;
|
|
}
|
|
} catch (Exception ex) {
|
|
_logger.LogError("An exception occurred when attempting to update sub roles for user {userId} to delegated user {delegatedUserId}. Exception: {ex}",
|
|
userId,
|
|
delegatedUserId,
|
|
ex.Message);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> DelegateApprovalsForUser(int userId, int delegatedUserId) {
|
|
try {
|
|
_logger.LogInformation("Attempting to delegate approvals for user {userId} to delegated user {delegatedUserId}",
|
|
userId,
|
|
delegatedUserId);
|
|
if (userId <= 0) {
|
|
throw new ArgumentException(string.Format("User Id {0} is not a valid user Id", userId));
|
|
} else if (delegatedUserId <= 0) {
|
|
throw new ArgumentException(string.Format("Delegated user Id {0} is not a valid user Id", delegatedUserId));
|
|
} else {
|
|
string sql = String.Format("update Approval set UserID = {0}, Delegated = 1 where UserID = {1} and (ItemStatus = {2} or ItemStatus = {3})",
|
|
delegatedUserId,
|
|
userId,
|
|
PENDING_ITEM_STATUS,
|
|
DENITED_ITEM_STATUS);
|
|
|
|
await _dalService.ExecuteAsync(sql);
|
|
|
|
return true;
|
|
}
|
|
} catch (Exception ex) {
|
|
_logger.LogError("An exception occurred when attempting to delegate approvals for user {userId} to delegated user {delegatedUserId}. Exception: {ex}",
|
|
userId,
|
|
delegatedUserId,
|
|
ex.Message);
|
|
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 {id} as OOO", oooTemp.OOOUserID);
|
|
|
|
StringBuilder queryBuilder = new StringBuilder();
|
|
queryBuilder.Append("update Users set OOO = 1, ");
|
|
queryBuilder.AppendFormat("OOOStartDate = '{0}', ", oooTemp.OOOStartDate);
|
|
queryBuilder.AppendFormat("OOOExpirationDate = '{0}', ", oooTemp.OOOExpirationDate);
|
|
queryBuilder.AppendFormat("DelegatedTo = {0} ", oooTemp.DelegatedTo);
|
|
queryBuilder.AppendFormat("where UserID = {0}", 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}",
|
|
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 {id} Processed to {processed}", oooTemp.ID, oooTemp.Processed);
|
|
|
|
string sql = string.Format("update OOOTemp set Processed = {0} where ID = {1}", oooTemp.Processed, 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}",
|
|
ex.Message);
|
|
throw;
|
|
}
|
|
}
|
|
}
|