using Microsoft.EntityFrameworkCore;
using Titan.API.Gateway.AccessManagement.UseCases.BasicAuth.Contracts.DeleteBulkBasicAuth;
using Titan.API.Gateway.Common.Audit;
using Titan.API.Gateway.Common.ServiceResponseModelling;
using Titan.API.Gateway.Storage;
using Titan.API.Gateway.Storage.Entities.Authentication.Esendex;
using Titan.API.Gateway.Storage.Entities.Authentication.Skebby;
namespace Titan.API.Gateway.AccessManagement.UseCases.BasicAuth.DeleteBulkBasicAuth;
public class DeleteBulkBasicAuthService : AccessManagementService, IDeleteBulkBasicAuthService
private readonly ApiGatewayDbContext _dbContext;
private readonly IAuditLogger _auditLogger;
private readonly IValidator<DeleteBulkBasicAuthInputModel> _deleteBulkBasicAuthValidator;
private List<EsendexUserEntity> _esendexUsers;
private List<SkebbyUserEntity> _skebbyUsers;
public DeleteBulkBasicAuthService(
IAuditLogger auditLogger,
ApiGatewayDbContext dbContext,
IValidator<DeleteBulkBasicAuthInputModel> deleteBulkBasicAuthValidator)
_auditLogger = auditLogger;
_deleteBulkBasicAuthValidator = deleteBulkBasicAuthValidator;
public async Task<ApiNoSuccessBodyResponse<AccessManagementErrorDto>> DeleteBulkAsync(DeleteBulkBasicAuthInputModel input, CancellationToken cancellationToken)
var validationResult = _deleteBulkBasicAuthValidator.Validate(input);
if (!validationResult.IsValid)
return (HttpStatusCode.BadRequest, ParseValidationResults(validationResult));
var basicAuthListToDelete = new List<string>();
var internalErrors = new List<AccessManagementInternalDataDto>();
var internalSuccess = new List<AccessManagementInternalDataDto>();
if (input.Data.Any(b => string.IsNullOrWhiteSpace(b.UserId)))
internalErrors.Add(new AccessManagementInternalDataDto("There is a least one element in the input with empty userId"));
await LoadTablesBasicAuthCredentials(input.Brand, cancellationToken);
var theWholeInputList = input.Data.ToList();
input.Data.Where(b => !string.IsNullOrEmpty(b.UserId)).ToList().ForEach(basicCred =>
var (hasErrors, errorDetails) = CheckIfElementHasErrors(basicCred, input.Brand);
internalErrors.Add(new AccessManagementInternalDataDto(errorDetails!,
new Dictionary<string, string> { { "index", theWholeInputList.IndexOf(basicCred).ToString() } }));
basicAuthListToDelete.Add(basicCred.UserId!);
await DeleteDataAsync(basicAuthListToDelete, input.Brand, cancellationToken);
var statusCode = internalErrors.Any() ? HttpStatusCode.MultiStatus : HttpStatusCode.NoContent;
internalSuccess.Add(new AccessManagementInternalDataDto("Successfully deleted",
new Dictionary<string, string> { { "deleted", basicAuthListToDelete.Count().ToString() } }));
return statusCode == HttpStatusCode.NoContent ? HttpStatusCode.NoContent : (statusCode, AccessManagementErrorDto.WithInternalData("Bulk delete basic authentication credentials", statusCode, internalErrors, internalSuccess));
private async Task LoadTablesBasicAuthCredentials(string brand, CancellationToken cancellationToken)
if (brand.ToLower().Equals("esendex"))
_esendexUsers = await _dbContext.EsendexUsers.ToListAsync(cancellationToken);
_skebbyUsers = await _dbContext.SkebbyUsers.ToListAsync(cancellationToken);
private (bool, string?) CheckIfElementHasErrors(DeleteBulkBasicAuthBodyDto inputBasicAuthCredential, string brand)
return CheckIfUserIdExists(inputBasicAuthCredential, brand);
private (bool, string?) CheckIfUserIdExists(DeleteBulkBasicAuthBodyDto inputBasicAuthCredential, string brand)
if (brand.ToLower().Equals("esendex"))
userIdFound = _esendexUsers.FirstOrDefault(u => u.UserId.ToString() == inputBasicAuthCredential.UserId) is not null;
if (brand.ToLower().Equals("skebby"))
userIdFound = _skebbyUsers.FirstOrDefault(u => u.UserId.ToString() == inputBasicAuthCredential.UserId) is not null;
return userIdFound ? (false, null) : (true, "User Id not found.");
private async Task DeleteDataAsync(List<string> basicAuthListToInsert, string brand, CancellationToken cancellationToken)
if (brand.ToLower().Equals("esendex"))
_dbContext.RemoveRange(_esendexUsers.Where(u => basicAuthListToInsert.Contains(u.UserId.ToString())));
_dbContext.RemoveRange(_skebbyUsers.Where(u => basicAuthListToInsert.Contains(u.UserId.ToString())));
await _dbContext.SaveChangesAsync(cancellationToken);