-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consumer API: Terminate a Relationship (#588)
* chore: move entities to Aggregates folder * feat: creation of Relationship and remove Changes related stuff * test: split into two tests * feat: acceptance of creation * feat: reject creation * feat: revoke creation * test: split relationship tests into multiple files * feat: allow multiple relationships as long as there is only one active * chore: remove redundant parameter * refactor: make RelationshipTemplatesRepository.Find return null instead of throwing * feat: add Handler * feat: add and use expressions * chore: don't use AutoMapper and add more tests * feat: reject relationship * feat: AcceptRelationshipCommand * feat: RevokeRelationshipCommand * feat: add AuditLog to DTOs * feat: add CreationContent property to RelationshipDTO * feat: add additional properties to RelationshipCreatedIntegrationEvent and RelationshipStatusChangedIntegrationEvent * feat: handle new integration events in Synchronization module * chore: formatting * test: fix tests * feat: replace integration events in quotas module with new ones * feat: add migration * feat: add controller methods * chore: fix/ignore compiler warnings * refactor: cleanup error codes * feat: add insomnia workspace * feat: add openapi.yml * fix: add RelationshipMetadataDTO type and add creationContent property to RelationshipDTO * refactor: rename Content to CreationContent in request to create a relationship * chore: update InsomniaWorkspace and openapi.yml * feat: implement domain part of relationship termination * feat: implement application part of relationship termination * feat: implement relationship termination controller * feat: disable creating new relationship while terminated one exists * feat: disable sending messages when relationship is terminated * test: add relationship termination domain and handler tests * chore: rename RelationshipStatus "Accepted" to "Active" * feat: trigger external event * chore: fix formatting * chore: fix merge conflicts * chore: remove redundant whitespace * fix: add missing directive * fix: update Content to CreationContent * chore: update files prior to making PR ready for review * chore: fix formatting * feat: add AcceptanceContent * fix: avoid error on creation of RelationshipsOverview view when RelationshipChanges table does not exist * feat: (WIP!!): update Admin API RelationshipOverviews view * feat: add AcceptanceContent to DTO * fix: pass AcceptanceContent to AcceptRelationshipCommand * chore: use postgres in Admin CLI launchSettings.json * feat: implement PR change requests * feat: implement PR change requests * chore: fix formatting * feat: implement PR change requests * fix: relationships overview migration * feat: update revamped relationships overview view with audit log * test: add relationship tests * chore: fix formatting and remove redundant overload * chore: fix formatting * feat: implement PR change requests * feat: implement PR change requests * chore: remove redundant Relationship statuses * fix: update condition * fix: re-introduce Terminated status * chore: remove unused method * refactor: combine checks for other relationships --------- Co-authored-by: Timo Notheisen <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Timo Notheisen <[email protected]> Co-authored-by: Daniel Almeida <[email protected]>
- Loading branch information
1 parent
8c4f151
commit bb3ccbb
Showing
23 changed files
with
450 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...rc/Messages.Application/Infrastructure/Persistence/Repository/IRelationshipsRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
using Backbone.DevelopmentKit.Identity.ValueObjects; | ||
using Backbone.Modules.Messages.Domain.Ids; | ||
using Backbone.Modules.Messages.Domain.Entities; | ||
|
||
namespace Backbone.Modules.Messages.Application.Infrastructure.Persistence.Repository; | ||
|
||
public interface IRelationshipsRepository | ||
{ | ||
Task<RelationshipId?> GetIdOfRelationshipBetweenSenderAndRecipient(IdentityAddress identityA, IdentityAddress identityB); | ||
Task<Relationship?> FindRelationship(IdentityAddress identityA, IdentityAddress identityB, CancellationToken cancellationToken); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Backbone.BuildingBlocks.Domain.Errors; | ||
|
||
namespace Backbone.Modules.Messages.Domain; | ||
public static class DomainErrors | ||
{ | ||
public static DomainError RelationshipToRecipientNotActive(string recipient = "") | ||
{ | ||
var recipientText = string.IsNullOrEmpty(recipient) ? "one of the recipients" : recipient; | ||
|
||
return new DomainError( | ||
"error.platform.validation.message.relationshipToRecipientNotActive", | ||
$"Cannot send message to {recipientText} because the relationship to it is not active. In order to be able to send messages again, you have to reactivate the relationship."); | ||
} | ||
|
||
public static DomainError MaxNumberOfUnreceivedMessagesReached(string recipient = "") | ||
{ | ||
var recipientText = string.IsNullOrEmpty(recipient) ? "one of the recipients" : recipient; | ||
|
||
return new DomainError( | ||
"error.platform.validation.message.maxNumberOfUnreceivedMessagesReached", | ||
$"The message could not be sent because {recipientText} already has the maximum number of unread messages from you."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
Modules/Messages/test/Messages.Domain.Tests/Relationships/RelationshipTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using Backbone.BuildingBlocks.Domain; | ||
using Backbone.DevelopmentKit.Identity.ValueObjects; | ||
using Backbone.Modules.Messages.Domain.Entities; | ||
using Backbone.Modules.Messages.Domain.Ids; | ||
using Backbone.UnitTestTools.Data; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace Backbone.Modules.Messages.Domain.Tests.Relationships; | ||
public class RelationshipTests | ||
{ | ||
[Fact] | ||
public void Relationship_must_be_active_to_allow_sending_messages() | ||
{ | ||
// Arrange | ||
var relationship = CreateRelationship(RelationshipStatus.Pending); | ||
|
||
// Act | ||
var acting = () => relationship.EnsureSendingMessagesIsAllowed(0, 5); | ||
|
||
// Assert | ||
acting.Should().Throw<DomainException>().Which.Code.Should().Be("error.platform.validation.message.relationshipToRecipientNotActive"); | ||
} | ||
|
||
[Fact] | ||
public void Max_number_of_unreceived_messages_must_not_be_reached() | ||
{ | ||
// Arrange | ||
var relationship = CreateRelationship(); | ||
|
||
// Act | ||
var acting = () => relationship.EnsureSendingMessagesIsAllowed(5, 5); | ||
|
||
// Assert | ||
acting.Should().Throw<DomainException>().Which.Code.Should().Be("error.platform.validation.message.maxNumberOfUnreceivedMessagesReached"); | ||
} | ||
|
||
[Fact] | ||
public void Relationship_cannot_be_terminated_to_allow_sending_messages() | ||
{ | ||
// Arrange | ||
var relationship = CreateRelationship(RelationshipStatus.Terminated); | ||
|
||
// Act | ||
var acting = () => relationship.EnsureSendingMessagesIsAllowed(0, 5); | ||
|
||
// Assert | ||
acting.Should().Throw<DomainException>().Which.Code.Should().Be("error.platform.validation.message.relationshipToRecipientNotActive"); | ||
} | ||
|
||
#region helpers | ||
|
||
private static Relationship CreateRelationship(RelationshipStatus status) | ||
{ | ||
return CreateRelationship(null, null, null, null, status); | ||
} | ||
|
||
private static Relationship CreateRelationship(string? relationshipId = null, IdentityAddress? from = null, IdentityAddress? to = null, DateTime? createdAt = null, RelationshipStatus? status = null) | ||
{ | ||
relationshipId ??= "REL00000000000000000"; | ||
from ??= TestDataGenerator.CreateRandomIdentityAddress(); | ||
to ??= TestDataGenerator.CreateRandomIdentityAddress(); | ||
createdAt ??= DateTime.UtcNow; | ||
status ??= RelationshipStatus.Active; | ||
|
||
return Relationship.LoadForTesting(RelationshipId.Parse(relationshipId), from, to, createdAt.Value, status.Value); | ||
} | ||
#endregion | ||
} |
5 changes: 4 additions & 1 deletion
5
Modules/Relationships/src/Relationships.Application/ApplicationErrors.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
namespace Backbone.Modules.Relationships.Application; | ||
|
||
public static class ApplicationErrors; | ||
public static class ApplicationErrors | ||
{ | ||
public static class Relationship { } | ||
} |
38 changes: 38 additions & 0 deletions
38
...ips/src/Relationships.Application/Relationships/Commands/TerminateRelationship/Handler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Backbone.BuildingBlocks.Application.Abstractions.Infrastructure.EventBus; | ||
using Backbone.BuildingBlocks.Application.Abstractions.Infrastructure.UserContext; | ||
using Backbone.DevelopmentKit.Identity.ValueObjects; | ||
using Backbone.Modules.Relationships.Application.Infrastructure.Persistence.Repository; | ||
using Backbone.Modules.Relationships.Application.IntegrationEvents.Outgoing; | ||
using Backbone.Modules.Relationships.Domain.Aggregates.Relationships; | ||
using MediatR; | ||
|
||
namespace Backbone.Modules.Relationships.Application.Relationships.Commands.TerminateRelationship; | ||
public class Handler : IRequestHandler<TerminateRelationshipCommand, TerminateRelationshipResponse> | ||
{ | ||
private readonly IRelationshipsRepository _relationshipsRepository; | ||
private readonly IEventBus _eventBus; | ||
private readonly IdentityAddress _activeIdentity; | ||
private readonly DeviceId _activeDevice; | ||
|
||
public Handler(IRelationshipsRepository relationshipsRepository, IUserContext userContext, IEventBus eventBus) | ||
{ | ||
_relationshipsRepository = relationshipsRepository; | ||
_eventBus = eventBus; | ||
_activeIdentity = userContext.GetAddress(); | ||
_activeDevice = userContext.GetDeviceId(); | ||
} | ||
|
||
public async Task<TerminateRelationshipResponse> Handle(TerminateRelationshipCommand request, CancellationToken cancellationToken) | ||
{ | ||
var relationshipId = RelationshipId.Parse(request.RelationshipId); | ||
var relationship = await _relationshipsRepository.FindRelationship(relationshipId, _activeIdentity, cancellationToken, track: true); | ||
|
||
relationship.Terminate(_activeIdentity, _activeDevice); | ||
|
||
await _relationshipsRepository.Update(relationship); | ||
|
||
_eventBus.Publish(new RelationshipStatusChangedIntegrationEvent(relationship)); | ||
|
||
return new TerminateRelationshipResponse(relationship); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
....Application/Relationships/Commands/TerminateRelationship/TerminateRelationshipCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using Backbone.Modules.Relationships.Application.Relationships.Commands.CreateRelationship; | ||
using MediatR; | ||
|
||
namespace Backbone.Modules.Relationships.Application.Relationships.Commands.TerminateRelationship; | ||
public class TerminateRelationshipCommand : IRequest<TerminateRelationshipResponse> | ||
{ | ||
public required string RelationshipId { get; set; } | ||
} |
8 changes: 8 additions & 0 deletions
8
...Application/Relationships/Commands/TerminateRelationship/TerminateRelationshipResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using Backbone.Modules.Relationships.Application.Relationships.DTOs; | ||
using Backbone.Modules.Relationships.Domain.Aggregates.Relationships; | ||
|
||
namespace Backbone.Modules.Relationships.Application.Relationships.Commands.TerminateRelationship; | ||
public class TerminateRelationshipResponse : RelationshipMetadataDTO | ||
{ | ||
public TerminateRelationshipResponse(Relationship relationship) : base(relationship) { } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ public enum RelationshipStatus | |
Active = 20, | ||
Rejected = 30, | ||
Revoked = 40, | ||
Terminated = 50 | ||
} |
Oops, something went wrong.