Skip to content

Commit

Permalink
feat: notify peers about identity deletion in case of pending and ter…
Browse files Browse the repository at this point in the history
…minated relationships
  • Loading branch information
tnotheis committed Sep 18, 2024
1 parent 6ed9267 commit 3a5a814
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Backbone.BuildingBlocks.Application.Abstractions.Infrastructure.EventBus;
using Backbone.BuildingBlocks.Application.Extensions;
using Backbone.Modules.Relationships.Application.Infrastructure.Persistence.Repository;
using Backbone.Modules.Relationships.Domain.Aggregates.Relationships;
using Backbone.Modules.Relationships.Domain.DomainEvents.Incoming;
Expand Down Expand Up @@ -26,7 +27,8 @@ public async Task Handle(IdentityDeletedDomainEvent @event)
private async Task<List<Relationship>> GetRelationshipsOf(string identityAddress)
{
var relationships = (await _relationshipsRepository
.FindRelationships(r => (r.From == identityAddress || r.To == identityAddress) && r.Status == RelationshipStatus.Active,
.FindRelationships(
Relationship.HasParticipant(identityAddress).And(Relationship.HasStatusInWhichPeerShouldBeNotifiedAboutDeletion()),
CancellationToken.None)).ToList();
return relationships;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Backbone.BuildingBlocks.Application.Abstractions.Infrastructure.EventBus;
using Backbone.BuildingBlocks.Application.Extensions;
using Backbone.Modules.Relationships.Application.Infrastructure.Persistence.Repository;
using Backbone.Modules.Relationships.Domain.Aggregates.Relationships;
using Backbone.Modules.Relationships.Domain.DomainEvents.Incoming;
Expand Down Expand Up @@ -26,7 +27,8 @@ public async Task Handle(IdentityDeletionCancelledDomainEvent @event)
private async Task<List<Relationship>> GetRelationshipsOf(string identityAddress)
{
var relationships = (await _relationshipsRepository
.FindRelationships(r => (r.From == identityAddress || r.To == identityAddress) && r.Status == RelationshipStatus.Active,
.FindRelationships(
Relationship.HasParticipant(identityAddress).And(Relationship.HasStatusInWhichPeerShouldBeNotifiedAboutDeletion()),
CancellationToken.None)).ToList();
return relationships;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Backbone.BuildingBlocks.Application.Abstractions.Infrastructure.EventBus;
using Backbone.BuildingBlocks.Application.Extensions;
using Backbone.Modules.Relationships.Application.Infrastructure.Persistence.Repository;
using Backbone.Modules.Relationships.Domain.Aggregates.Relationships;
using Backbone.Modules.Relationships.Domain.DomainEvents.Incoming;
Expand Down Expand Up @@ -26,9 +27,9 @@ public async Task Handle(IdentityToBeDeletedDomainEvent @event)
private async Task<List<Relationship>> GetPendingAndActiveRelationshipsOf(string identityAddress)
{
var relationships = (await _relationshipsRepository
.FindRelationships(r => (r.From == identityAddress || r.To == identityAddress) && r.Status == RelationshipStatus.Active,
.FindRelationships(
Relationship.HasParticipant(identityAddress).And(Relationship.HasStatusInWhichPeerShouldBeNotifiedAboutDeletion()),
CancellationToken.None)).ToList();

return relationships;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,30 @@ public void HasParticipant_recognizes_foreign_addresses()
}

#endregion

#region HasStatusInWhichPeerShouldBeNotifiedAboutDeletion

[Theory]
[InlineData(RelationshipStatus.Pending, true)]
[InlineData(RelationshipStatus.Rejected, false)]
[InlineData(RelationshipStatus.Revoked, false)]
[InlineData(RelationshipStatus.Active, true)]
[InlineData(RelationshipStatus.Terminated, true)]
[InlineData(RelationshipStatus.DeletionProposed, false)]
[InlineData(RelationshipStatus.ReadyForDeletion, false)]
public void HasStatusInWhichPeerShouldBeNotifiedAboutDeletion_with_status_Pending(RelationshipStatus status, bool expected)
{
// Arrange
var relationship = TestData.CreateRelationshipInStatus(status);

// Act
var result = relationship.EvaluateHasStatusInWhichPeerShouldBeNotifiedAboutDeletion();

// Assert
result.Should().Be(expected);
}

#endregion
}

file static class RelationshipExtensions
Expand All @@ -120,4 +144,10 @@ public static bool EvaluateCountsAsActiveExpression(this Relationship relationsh
var expression = Relationship.CountsAsActive();
return expression.Compile()(relationship);
}

public static bool EvaluateHasStatusInWhichPeerShouldBeNotifiedAboutDeletion(this Relationship relationship)
{
var expression = Relationship.HasStatusInWhichPeerShouldBeNotifiedAboutDeletion();
return expression.Compile()(relationship);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -372,5 +372,12 @@ public static Expression<Func<Relationship, bool>> CountsAsActive()
r.Status != RelationshipStatus.ReadyForDeletion;
}

public static Expression<Func<Relationship, bool>> HasStatusInWhichPeerShouldBeNotifiedAboutDeletion()
{
return r => r.Status == RelationshipStatus.Pending ||
r.Status == RelationshipStatus.Active ||
r.Status == RelationshipStatus.Terminated;
}

#endregion
}

0 comments on commit 3a5a814

Please sign in to comment.