Skip to content

Commit

Permalink
fix: use FirstOrDefault instead of Single when reading the identity
Browse files Browse the repository at this point in the history
  • Loading branch information
tnotheis committed Dec 6, 2024
1 parent a60956b commit 21bf46b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,29 @@ public Handler(IIdentitiesRepository identitiesRepository)

public async Task<IsIdentityOfUserDeletedResponse> Handle(IsIdentityOfUserDeletedQuery request, CancellationToken cancellationToken)
{
var identity = await _identitiesRepository.FindSingle(Identity.HasUser(request.Username), cancellationToken);
var identity = await _identitiesRepository.FindFirst(Identity.HasUser(request.Username), cancellationToken);

if (identity.IsGracePeriodOver)
return new IsIdentityOfUserDeletedResponse(true, identity.DeletionGracePeriodEndsAt);
bool isDeleted;
DateTime? deletionGracePeriodEndsAt;

var auditLogEntries = await _identitiesRepository.GetIdentityDeletionProcessAuditLogs(
IdentityDeletionProcessAuditLogEntry.IsAssociatedToUser(Username.Parse(request.Username)),
cancellationToken);
if (identity != null)
{
isDeleted = identity.IsGracePeriodOver;
deletionGracePeriodEndsAt = identity.IsGracePeriodOver ? identity.DeletionGracePeriodEndsAt : null;
}
else
{
var auditLogEntries = await _identitiesRepository.GetIdentityDeletionProcessAuditLogs(
IdentityDeletionProcessAuditLogEntry.IsAssociatedToUser(Username.Parse(request.Username)),
cancellationToken);

var deletionCompletedAuditLogEntry = auditLogEntries.FirstOrDefault(l => l.MessageKey == MessageKey.DeletionCompleted);
var deletionCompletedAuditLogEntry = auditLogEntries.FirstOrDefault(l => l.MessageKey == MessageKey.DeletionCompleted);

isDeleted = deletionCompletedAuditLogEntry != null;
deletionGracePeriodEndsAt = deletionCompletedAuditLogEntry?.CreatedAt;
}

return new IsIdentityOfUserDeletedResponse(isDeleted, deletionGracePeriodEndsAt);

return new IsIdentityOfUserDeletedResponse(deletionCompletedAuditLogEntry != null, deletionCompletedAuditLogEntry?.CreatedAt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface IIdentitiesRepository
Task<IEnumerable<Identity>> FindAllWithDeletionProcessInStatus(DeletionProcessStatus status, CancellationToken cancellationToken, bool track = false);
Task<int> CountByClientId(string clientId, CancellationToken cancellationToken);
Task<IEnumerable<Identity>> Find(Expression<Func<Identity, bool>> filter, CancellationToken cancellationToken, bool track = false);
Task<Identity> FindSingle(Expression<Func<Identity, bool>> filter, CancellationToken cancellationToken, bool track = false);
Task<Identity?> FindFirst(Expression<Func<Identity, bool>> filter, CancellationToken cancellationToken, bool track = false);
Task Delete(Expression<Func<Identity, bool>> filter, CancellationToken cancellationToken);

Task Add(Identity identity, string password);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@ public async Task<IEnumerable<Identity>> Find(Expression<Func<Identity, bool>> f
.ToListAsync(cancellationToken);
}

public async Task<Identity> FindSingle(Expression<Func<Identity, bool>> filter, CancellationToken cancellationToken, bool track = false)
public async Task<Identity?> FindFirst(Expression<Func<Identity, bool>> filter, CancellationToken cancellationToken, bool track = false)
{
return await (track ? _identities : _readonlyIdentities)
.IncludeAll(_dbContext)
.Where(filter)
.SingleAsync(cancellationToken);
.FirstOrDefaultAsync(cancellationToken);
}

public async Task Delete(Expression<Func<Identity, bool>> filter, CancellationToken cancellationToken)
Expand Down

0 comments on commit 21bf46b

Please sign in to comment.