diff --git a/Modules/Devices/src/Devices.Application/Identities/Commands/CancelDeletionProcessAsOwner/Handler.cs b/Modules/Devices/src/Devices.Application/Identities/Commands/CancelDeletionProcessAsOwner/Handler.cs index 15427d761e..89bc8b28c7 100644 --- a/Modules/Devices/src/Devices.Application/Identities/Commands/CancelDeletionProcessAsOwner/Handler.cs +++ b/Modules/Devices/src/Devices.Application/Identities/Commands/CancelDeletionProcessAsOwner/Handler.cs @@ -41,7 +41,7 @@ public async Task Handle(CancelDeletionPro var newTierId = identity.TierId; _eventBus.Publish(new TierOfIdentityChangedDomainEvent(identity, oldTierId, newTierId)); - _eventBus.Publish(new IdentityDeletionProcessStatusChangedDomainEvent(identity.Address, deletionProcess.Id)); + _eventBus.Publish(new IdentityDeletionProcessStatusChangedDomainEvent(identity.Address, deletionProcess.Id, _userContext.GetAddress())); return new CancelDeletionProcessAsOwnerResponse(deletionProcess); } diff --git a/Modules/Devices/src/Devices.Application/Identities/Commands/CancelDeletionProcessAsSupport/Handler.cs b/Modules/Devices/src/Devices.Application/Identities/Commands/CancelDeletionProcessAsSupport/Handler.cs index fed088842a..80ef53d673 100644 --- a/Modules/Devices/src/Devices.Application/Identities/Commands/CancelDeletionProcessAsSupport/Handler.cs +++ b/Modules/Devices/src/Devices.Application/Identities/Commands/CancelDeletionProcessAsSupport/Handler.cs @@ -37,7 +37,7 @@ public async Task Handle(CancelDeletionAsSuppor var newTierId = identity.TierId; _eventBus.Publish(new TierOfIdentityChangedDomainEvent(identity, oldTierId, newTierId)); - _eventBus.Publish(new IdentityDeletionProcessStatusChangedDomainEvent(identity.Address, deletionProcess.Id)); + _eventBus.Publish(new IdentityDeletionProcessStatusChangedDomainEvent(identity.Address, deletionProcess.Id, null)); return new CancelDeletionAsSupportResponse(deletionProcess); } diff --git a/Modules/Devices/src/Devices.Application/Identities/Commands/CancelStaleIdentityDeletionProcesses/Handler.cs b/Modules/Devices/src/Devices.Application/Identities/Commands/CancelStaleIdentityDeletionProcesses/Handler.cs index a835059b0d..37970841a4 100644 --- a/Modules/Devices/src/Devices.Application/Identities/Commands/CancelStaleIdentityDeletionProcesses/Handler.cs +++ b/Modules/Devices/src/Devices.Application/Identities/Commands/CancelStaleIdentityDeletionProcesses/Handler.cs @@ -34,7 +34,7 @@ public async Task Handle(CancelSta await _identityRepository.Update(identity, cancellationToken); - _eventBus.Publish(new IdentityDeletionProcessStatusChangedDomainEvent(identity.Address, deletionProcess.Value.Id)); + _eventBus.Publish(new IdentityDeletionProcessStatusChangedDomainEvent(identity.Address, deletionProcess.Value.Id, null)); } return new CancelStaleIdentityDeletionProcessesResponse(idsOfCancelledDeletionProcesses); diff --git a/Modules/Devices/src/Devices.Domain/DomainEvents/Outgoing/IdentityDeletionProcessStatusChangedDomainEvent.cs b/Modules/Devices/src/Devices.Domain/DomainEvents/Outgoing/IdentityDeletionProcessStatusChangedDomainEvent.cs index f08ce41eb2..f67f1ecb58 100644 --- a/Modules/Devices/src/Devices.Domain/DomainEvents/Outgoing/IdentityDeletionProcessStatusChangedDomainEvent.cs +++ b/Modules/Devices/src/Devices.Domain/DomainEvents/Outgoing/IdentityDeletionProcessStatusChangedDomainEvent.cs @@ -4,13 +4,15 @@ namespace Backbone.Modules.Devices.Domain.DomainEvents.Outgoing; public class IdentityDeletionProcessStatusChangedDomainEvent : DomainEvent { - public IdentityDeletionProcessStatusChangedDomainEvent(string identityAddress, string deletionProcessId) + public IdentityDeletionProcessStatusChangedDomainEvent(string identityAddress, string deletionProcessId, string? initiator) : base($"{identityAddress}/IdentityDeletionProcessStatusChanged/{deletionProcessId}") { Address = identityAddress; DeletionProcessId = deletionProcessId; + Initiator = initiator; } public string Address { get; } public string DeletionProcessId { get; } + public string? Initiator { get; } } diff --git a/Modules/Synchronization/src/Synchronization.Application/DomainEvents/Incoming/IdentityDeletionProcessStatusChanged/IdentityDeletionProcessStatusChangedDomainEventHandler.cs b/Modules/Synchronization/src/Synchronization.Application/DomainEvents/Incoming/IdentityDeletionProcessStatusChanged/IdentityDeletionProcessStatusChangedDomainEventHandler.cs index 5914699d82..2c97609b5d 100644 --- a/Modules/Synchronization/src/Synchronization.Application/DomainEvents/Incoming/IdentityDeletionProcessStatusChanged/IdentityDeletionProcessStatusChangedDomainEventHandler.cs +++ b/Modules/Synchronization/src/Synchronization.Application/DomainEvents/Incoming/IdentityDeletionProcessStatusChanged/IdentityDeletionProcessStatusChangedDomainEventHandler.cs @@ -25,6 +25,10 @@ public IdentityDeletionProcessStatusChangedDomainEventHandler(ISynchronizationDb public async Task Handle(IdentityDeletionProcessStatusChangedDomainEvent @event) { + // No need to create an external event if the action that triggered the event was initiated by the owner of the deletion process (in that case it's not "external"). + if (@event.Initiator == @event.Address) + return; + #pragma warning disable IDE0037 var payload = new { DeletionProcessId = @event.DeletionProcessId }; #pragma warning restore IDE0037 diff --git a/Modules/Synchronization/src/Synchronization.Domain/DomainEvents/Incoming/IdentityDeletionProcessStatusChanged/IdentityDeletionProcessStatusChangedDomainEvent.cs b/Modules/Synchronization/src/Synchronization.Domain/DomainEvents/Incoming/IdentityDeletionProcessStatusChanged/IdentityDeletionProcessStatusChangedDomainEvent.cs index 4ea152df23..cbaf51fc92 100644 --- a/Modules/Synchronization/src/Synchronization.Domain/DomainEvents/Incoming/IdentityDeletionProcessStatusChanged/IdentityDeletionProcessStatusChangedDomainEvent.cs +++ b/Modules/Synchronization/src/Synchronization.Domain/DomainEvents/Incoming/IdentityDeletionProcessStatusChanged/IdentityDeletionProcessStatusChangedDomainEvent.cs @@ -4,12 +4,14 @@ namespace Backbone.Modules.Synchronization.Domain.DomainEvents.Incoming.Identity public class IdentityDeletionProcessStatusChangedDomainEvent : DomainEvent { - public IdentityDeletionProcessStatusChangedDomainEvent(string address, string deletionProcessId) + public IdentityDeletionProcessStatusChangedDomainEvent(string address, string deletionProcessId, string? initiator) { Address = address; DeletionProcessId = deletionProcessId; + Initiator = initiator; } public string Address { get; } public string DeletionProcessId { get; } + public string? Initiator { get; } } diff --git a/Modules/Synchronization/test/Synchronization.Application.Tests/Tests/DomainEvents/IdentityDeletionProcessStatusChangedDomainEventHandlerTests.cs b/Modules/Synchronization/test/Synchronization.Application.Tests/Tests/DomainEvents/IdentityDeletionProcessStatusChangedDomainEventHandlerTests.cs index 82272696e0..c02740f7c1 100644 --- a/Modules/Synchronization/test/Synchronization.Application.Tests/Tests/DomainEvents/IdentityDeletionProcessStatusChangedDomainEventHandlerTests.cs +++ b/Modules/Synchronization/test/Synchronization.Application.Tests/Tests/DomainEvents/IdentityDeletionProcessStatusChangedDomainEventHandlerTests.cs @@ -14,19 +14,19 @@ namespace Backbone.Modules.Synchronization.Application.Tests.Tests.DomainEvents; public class IdentityDeletionProcessStatusChangedDomainEventHandlerTests { [Fact] - public async Task Creates_an_external_event() + public async Task Creates_an_external_event_if_initiator_is_someone_else() { // Arrange - var identityAddress = TestDataGenerator.CreateRandomIdentityAddress(); - var identityDeletionProcessStatusChangedDomainEvent = new IdentityDeletionProcessStatusChangedDomainEvent(identityAddress, "someDeletionProcessId"); + var deletionProcessOwner = TestDataGenerator.CreateRandomIdentityAddress(); + var identityDeletionProcessStatusChangedDomainEvent = new IdentityDeletionProcessStatusChangedDomainEvent(deletionProcessOwner, "someDeletionProcessId", null); var mockDbContext = A.Fake(); - var externalEvent = new ExternalEvent(ExternalEventType.IdentityDeletionProcessStatusChanged, IdentityAddress.Parse(identityAddress), 1, + var externalEvent = new ExternalEvent(ExternalEventType.IdentityDeletionProcessStatusChanged, IdentityAddress.Parse(deletionProcessOwner), 1, new { identityDeletionProcessStatusChangedDomainEvent.DeletionProcessId }); A.CallTo(() => mockDbContext.CreateExternalEvent( - identityAddress, + deletionProcessOwner, ExternalEventType.IdentityDeletionProcessStatusChanged, A._) ).Returns(externalEvent); @@ -38,26 +38,26 @@ public async Task Creates_an_external_event() // Act await handler.Handle(identityDeletionProcessStatusChangedDomainEvent); - // Handle - A.CallTo(() => mockDbContext.CreateExternalEvent(identityAddress, ExternalEventType.IdentityDeletionProcessStatusChanged, A._)) + // Assert + A.CallTo(() => mockDbContext.CreateExternalEvent(deletionProcessOwner, ExternalEventType.IdentityDeletionProcessStatusChanged, A._)) .MustHaveHappenedOnceExactly(); } [Fact] - public async Task Publishes_an_external_event() + public async Task Publishes_a_domain_event_after_creating_an_external_event() { // Arrange - var identityAddress = TestDataGenerator.CreateRandomIdentityAddress(); - var identityDeletionProcessStatusChangedDomainEvent = new IdentityDeletionProcessStatusChangedDomainEvent(identityAddress, "someDeletionProcessId"); + var deletionProcessOwner = TestDataGenerator.CreateRandomIdentityAddress(); + var identityDeletionProcessStatusChangedDomainEvent = new IdentityDeletionProcessStatusChangedDomainEvent(deletionProcessOwner, "someDeletionProcessId", null); var fakeDbContext = A.Fake(); var mockEventBus = A.Fake(); - var externalEvent = new ExternalEvent(ExternalEventType.IdentityDeletionProcessStatusChanged, IdentityAddress.Parse(identityAddress), 1, + var externalEvent = new ExternalEvent(ExternalEventType.IdentityDeletionProcessStatusChanged, IdentityAddress.Parse(deletionProcessOwner), 1, new { identityDeletionProcessStatusChangedDomainEvent.DeletionProcessId }); A.CallTo(() => fakeDbContext.CreateExternalEvent( - identityAddress, + deletionProcessOwner, ExternalEventType.IdentityDeletionProcessStatusChanged, A._) ).Returns(externalEvent); @@ -69,9 +69,40 @@ public async Task Publishes_an_external_event() // Act await handler.Handle(identityDeletionProcessStatusChangedDomainEvent); - // Handle + // Assert A.CallTo(() => mockEventBus.Publish( A.That.Matches(e => e.Owner == externalEvent.Owner && e.EventId == externalEvent.Id)) ).MustHaveHappenedOnceExactly(); } + + [Fact] + public async Task Does_nothing_if_initiator_is_deletion_process_owner() + { + // Arrange + var deletionProcessOwner = TestDataGenerator.CreateRandomIdentityAddress(); + var identityDeletionProcessStatusChangedDomainEvent = new IdentityDeletionProcessStatusChangedDomainEvent(deletionProcessOwner, "someDeletionProcessId", deletionProcessOwner); + + var mockDbContext = A.Fake(); + var mockEventBus = A.Fake(); + + var externalEvent = new ExternalEvent(ExternalEventType.IdentityDeletionProcessStatusChanged, IdentityAddress.Parse(deletionProcessOwner), 1, + new { identityDeletionProcessStatusChangedDomainEvent.DeletionProcessId }); + + A.CallTo(() => mockDbContext.CreateExternalEvent( + deletionProcessOwner, + ExternalEventType.IdentityDeletionProcessStatusChanged, + A._) + ).Returns(externalEvent); + + var handler = new IdentityDeletionProcessStatusChangedDomainEventHandler(mockDbContext, + mockEventBus, + A.Fake>()); + + // Act + await handler.Handle(identityDeletionProcessStatusChangedDomainEvent); + + // Assert + A.CallTo(() => mockDbContext.CreateExternalEvent(A._, A._, A._)).MustNotHaveHappened(); + A.CallTo(() => mockEventBus.Publish(A._)).MustNotHaveHappened(); + } }