Skip to content

Commit

Permalink
feat: or-2316 return contactgegeven id on adding contactegegeven
Browse files Browse the repository at this point in the history
  • Loading branch information
emalfroy committed Oct 2, 2024
1 parent 55da57a commit a3ff720
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@ public static void AddETagHeader(this HttpResponse source, long? version)
responseHeaders.ETag = new EntityTagHeaderValue(new StringSegment($"\"{version}\""), isWeak: true);
}

public static void AddLocationHeader(this HttpResponse source, string vCode, string location, string id, string baseUrl)
public static void AddLocationHeader(this HttpResponse source, string vCode, string entityName, int id, string baseUrl)
{
if (string.IsNullOrEmpty(id))
return;

var responseHeaders = source.GetTypedHeaders();
responseHeaders.Location = new Uri($"{baseUrl}/v1/verenigingen/{vCode}/{location}/{id}");
responseHeaders.Location = new Uri($"{baseUrl}/v1/verenigingen/{vCode}/{entityName}/{id}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ public class WellKnownHeaderLocations
{
public const string Vertegenwoordigers = "vertegenwoordigers";
public const string Locaties = "locaties";
public const string Contactgegevens = "contactgegevens";
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Examples;
using FluentValidation;
using Framework;
using Hosts.Configuration.ConfigurationBindings;
using Infrastructure;
using Infrastructure.Extensions;
using Infrastructure.Middleware;
Expand All @@ -28,11 +29,13 @@ public class VoegContactgegevenToeController : ApiController
{
private readonly IMessageBus _messageBus;
private readonly IValidator<VoegContactgegevenToeRequest> _validator;
private readonly AppSettings _appSettings;

public VoegContactgegevenToeController(IMessageBus messageBus, IValidator<VoegContactgegevenToeRequest> validator)
public VoegContactgegevenToeController(IMessageBus messageBus, IValidator<VoegContactgegevenToeRequest> validator, AppSettings appSettings)
{
_messageBus = messageBus;
_validator = validator;
_appSettings = appSettings;
}

/// <summary>
Expand Down Expand Up @@ -76,10 +79,11 @@ public async Task<IActionResult> Post(

var metaData = metadataProvider.GetMetadata(IfMatchParser.ParseIfMatch(ifMatch));
var envelope = new CommandEnvelope<VoegContactgegevenToeCommand>(request.ToCommand(vCode), metaData);
var commandResult = await _messageBus.InvokeAsync<CommandResult>(envelope);
var commandResult = await _messageBus.InvokeAsync<EntityCommandResult>(envelope);

Response.AddSequenceHeader(commandResult.Sequence);
Response.AddETagHeader(commandResult.Version);
Response.AddLocationHeader(vCode, WellKnownHeaderLocations.Contactgegevens, commandResult.EntityId, _appSettings.BaseUrl);

return Accepted();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ public VoegContactgegevenToeCommandHandler(IVerenigingsRepository verenigingRepo
_verenigingRepository = verenigingRepository;
}

public async Task<CommandResult> Handle(
public async Task<EntityCommandResult> Handle(
CommandEnvelope<VoegContactgegevenToeCommand> envelope,
CancellationToken cancellationToken = default)
{
var vereniging =
await _verenigingRepository.Load<VerenigingOfAnyKind>(VCode.Create(envelope.Command.VCode), envelope.Metadata.ExpectedVersion);

vereniging.VoegContactgegevenToe(envelope.Command.Contactgegeven);
var contactgegeven = vereniging.VoegContactgegevenToe(envelope.Command.Contactgegeven);

var result = await _verenigingRepository.Save(vereniging, envelope.Metadata, cancellationToken);

return CommandResult.Create(VCode.Create(envelope.Command.VCode), result);
return EntityCommandResult.Create(VCode.Create(envelope.Command.VCode), contactgegeven.ContactgegevenId, result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ await _verenigingRepository.Load<VerenigingOfAnyKind>(

var result = await _verenigingRepository.Save(vereniging, _session, envelope.Metadata, cancellationToken);

return EntityCommandResult.Create(VCode.Create(envelope.Command.VCode), toegevoegdeLocatie.LocatieId.ToString(), result);
return EntityCommandResult.Create(VCode.Create(envelope.Command.VCode), toegevoegdeLocatie.LocatieId, result);
}

private async Task SynchroniseerLocatie(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public async Task<EntityCommandResult> Handle(

var result = await _repository.Save(vereniging, envelope.Metadata, cancellationToken);

return EntityCommandResult.Create(VCode.Create(envelope.Command.VCode), vertegenwoordigerId.ToString(), result);
return EntityCommandResult.Create(VCode.Create(envelope.Command.VCode), vertegenwoordigerId.VertegenwoordigerId, result);
}
}
6 changes: 3 additions & 3 deletions src/AssociationRegistry/Vereniging/EntityCommandResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace AssociationRegistry.Vereniging;

public class EntityCommandResult
{
private EntityCommandResult(VCode vcode, string entityId, long? sequence, long? version)
private EntityCommandResult(VCode vcode, int entityId, long? sequence, long? version)
{
Vcode = vcode;
EntityId = entityId;
Expand All @@ -13,13 +13,13 @@ private EntityCommandResult(VCode vcode, string entityId, long? sequence, long?
}

public VCode Vcode { get; }
public string EntityId { get; set; }
public int EntityId { get; set; }
public long? Sequence { get; }
public long? Version { get; }

public bool HasChanges()
=> Sequence is not null;

public static EntityCommandResult Create(VCode vCode, string entityId, StreamActionResult streamActionResult)
public static EntityCommandResult Create(VCode vCode, int entityId, StreamActionResult streamActionResult)
=> new(vCode, entityId, streamActionResult.Sequence, streamActionResult.Version);
}
4 changes: 2 additions & 2 deletions src/AssociationRegistry/Vereniging/Vereniging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ public void WijzigHoofdactiviteitenVerenigingsloket(HoofdactiviteitVerenigingslo
AddEvent(HoofdactiviteitenVerenigingsloketWerdenGewijzigd.With(hoofdactiviteiten.ToArray()));
}

public int VoegVertegenwoordigerToe(Vertegenwoordiger vertegenwoordiger)
public Vertegenwoordiger VoegVertegenwoordigerToe(Vertegenwoordiger vertegenwoordiger)
{
var toegevoegdeVertegenwoordiger = State.Vertegenwoordigers.VoegToe(vertegenwoordiger);

AddEvent(VertegenwoordigerWerdToegevoegd.With(toegevoegdeVertegenwoordiger));

return toegevoegdeVertegenwoordiger.VertegenwoordigerId;
return toegevoegdeVertegenwoordiger;
}

public void WijzigVertegenwoordiger(
Expand Down
4 changes: 3 additions & 1 deletion src/AssociationRegistry/Vereniging/VerenigingOfAnyKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ private static Registratiedata.Vertegenwoordiger[] ToVertegenwoordigersLijst(Ver
private static Registratiedata.Locatie[] ToLocatieLijst(Locatie[] locatieLijst)
=> locatieLijst.Select(Registratiedata.Locatie.With).ToArray();

public void VoegContactgegevenToe(Contactgegeven contactgegeven)
public Contactgegeven VoegContactgegevenToe(Contactgegeven contactgegeven)
{
var toegevoegdContactgegeven = State.Contactgegevens.VoegToe(contactgegeven);

AddEvent(ContactgegevenWerdToegevoegd.With(toegevoegdContactgegeven));

return toegevoegdContactgegeven;
}

public void WijzigContactgegeven(int contactgegevenId, string? waarde, string? beschrijving, bool? isPrimair)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ public async Task Then_A_EntityId_Is_Returned()
var vertegenwoordigerId = _verenigingRepositoryMock.SaveInvocations[0].Vereniging.UncommittedEvents.ToArray()[0]
.As<VertegenwoordigerWerdToegevoegd>().VertegenwoordigerId;

result.EntityId.Should().Be(vertegenwoordigerId.ToString());
result.EntityId.Should().Be(vertegenwoordigerId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Common.Framework;
using Common.Scenarios.CommandHandling;
using Events;
using FluentAssertions;
using Framework;
using Vereniging;
using Xunit;
Expand Down Expand Up @@ -51,4 +52,25 @@ public async Task Then_A_ContactgegevenWerdToegevoegd_Event_Is_Saved(string type
command.Contactgegeven.Beschrijving, IsPrimair: false)
);
}

[Fact]
public async Task Then_A_ContactgegevenWerdToegevoegd_Event_Is_Saved()
{
var command = new VoegContactgegevenToeCommand(
_scenario.VCode,
Contactgegeven.CreateFromInitiator(
Contactgegeventype.Parse("SocialMedia"),
"https://www.example.org",
_fixture.Create<string?>(),
isPrimair: false));

var result = await _commandHandler.Handle(
new CommandEnvelope<VoegContactgegevenToeCommand>(command, _fixture.Create<CommandMetadata>()));

var contactgegevenId = _verenigingRepositoryMock.SaveInvocations[0].Vereniging.UncommittedEvents.ToArray()[0]
.As<ContactgegevenWerdToegevoegd>()
.ContactgegevenId;

result.EntityId.Should().Be(contactgegevenId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using AutoFixture;
using FluentAssertions;
using Framework;
using Hosts.Configuration.ConfigurationBindings;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Moq;
Expand All @@ -24,7 +25,13 @@ public With_Invalid_ETag()
_fixture = new Fixture().CustomizeAdminApi();
var messageBusMock = new Mock<IMessageBus>();

_controller = new VoegContactgegevenToeController(messageBusMock.Object, new VoegContactgegevenToeValidator())
_controller = new VoegContactgegevenToeController(
messageBusMock.Object,
new VoegContactgegevenToeValidator(),
new AppSettings()
{
BaseUrl = "https://beheer.verenigingen.vlaanderen.be",
})
{ ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() } };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using AssociationRegistry.Admin.Api.Infrastructure.ExceptionHandlers;
using AssociationRegistry.Admin.Api.Verenigingen.Contactgegevens.FeitelijkeVereniging.VoegContactGegevenToe;
using Framework;
using Hosts.Configuration.ConfigurationBindings;
using Moq;
using Wolverine;
using Xunit;
Expand All @@ -16,7 +17,14 @@ public class With_Null_Request
public With_Null_Request()
{
var messageBus = Mock.Of<IMessageBus>();
_controller = new VoegContactgegevenToeController(messageBus, new VoegContactgegevenToeValidator());

_controller = new VoegContactgegevenToeController(
messageBus,
new VoegContactgegevenToeValidator(),
new AppSettings()
{
BaseUrl = "https://beheer.verenigingen.vlaanderen.be",
});
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using AssociationRegistry.Framework;
using AutoFixture;
using Framework;
using Hosts.Configuration.ConfigurationBindings;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Moq;
Expand All @@ -28,10 +29,16 @@ public With_Valid_ETag()
_messageBusMock = new Mock<IMessageBus>();

_messageBusMock
.Setup(x => x.InvokeAsync<CommandResult>(It.IsAny<CommandEnvelope<VoegContactgegevenToeCommand>>(), default, null))
.ReturnsAsync(new Fixture().CustomizeAdminApi().Create<CommandResult>());
.Setup(x => x.InvokeAsync<EntityCommandResult>(It.IsAny<CommandEnvelope<VoegContactgegevenToeCommand>>(), default, null))
.ReturnsAsync(new Fixture().CustomizeAdminApi().Create<EntityCommandResult>());

_toeController = new VoegContactgegevenToeController(_messageBusMock.Object, new VoegContactgegevenToeValidator())
_toeController = new VoegContactgegevenToeController(
_messageBusMock.Object,
new VoegContactgegevenToeValidator(),
new AppSettings()
{
BaseUrl = "https://beheer.verenigingen.vlaanderen.be",
})
{ ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() } };
}

Expand All @@ -49,7 +56,7 @@ public void Then_it_invokes_with_a_correct_version_number()
{
_messageBusMock.Verify(
expression: messageBus =>
messageBus.InvokeAsync<CommandResult>(
messageBus.InvokeAsync<EntityCommandResult>(
It.Is<CommandEnvelope<VoegContactgegevenToeCommand>>(
env =>
env.Metadata.ExpectedVersion == ETagNumber),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using FluentAssertions;
using FluentAssertions.Execution;
using Framework;
using Hosts.Configuration.ConfigurationBindings;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
Expand All @@ -25,20 +26,26 @@ public class With_Valid_Request
{
private readonly VoegContactgegevenToeController _controller;
private readonly Fixture _fixture;
private readonly CommandResult _commandResult;
private readonly EntityCommandResult _entityCommandResult;

public With_Valid_Request()
{
_fixture = new Fixture().CustomizeAdminApi();

var messageBusMock = new Mock<IMessageBus>();
_commandResult = new Fixture().CustomizeAdminApi().Create<CommandResult>();
_entityCommandResult = new Fixture().CustomizeAdminApi().Create<EntityCommandResult>();

messageBusMock
.Setup(mb => mb.InvokeAsync<CommandResult>(It.IsAny<CommandEnvelope<VoegContactgegevenToeCommand>>(), default, null))
.ReturnsAsync(_commandResult);
.Setup(mb => mb.InvokeAsync<EntityCommandResult>(It.IsAny<CommandEnvelope<VoegContactgegevenToeCommand>>(), default, null))
.ReturnsAsync(_entityCommandResult);

_controller = new VoegContactgegevenToeController(messageBusMock.Object, new VoegContactgegevenToeValidator())
_controller = new VoegContactgegevenToeController(
messageBusMock.Object,
new VoegContactgegevenToeValidator(),
new AppSettings()
{
BaseUrl = "https://beheer.verenigingen.vlaanderen.be",
})
{ ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() } };
}

Expand Down Expand Up @@ -67,7 +74,7 @@ await _controller.Post(

using (new AssertionScope())
{
_controller.Response.Headers[WellknownHeaderNames.Sequence].Should().BeEquivalentTo(_commandResult.Sequence.ToString());
_controller.Response.Headers[WellknownHeaderNames.Sequence].Should().BeEquivalentTo(_entityCommandResult.Sequence.ToString());
}
}

Expand All @@ -82,7 +89,23 @@ await _controller.Post(
using (new AssertionScope())
{
_controller.Response.GetTypedHeaders().ETag.Should()
.BeEquivalentTo(new EntityTagHeaderValue(new StringSegment($@"""{_commandResult.Version}"""), isWeak: true));
.BeEquivalentTo(new EntityTagHeaderValue(new StringSegment($@"""{_entityCommandResult.Version}"""), isWeak: true));
}
}

[Fact]
public async Task Then_it_returns_a_location_header()
{
var vCode = _fixture.Create<VCode>();
await _controller.Post(
vCode,
_fixture.Create<VoegContactgegevenToeRequest>(),
_fixture.Create<CommandMetadataProviderStub>());

using (new AssertionScope())
{
_controller.Response.GetTypedHeaders().Location.Should()
.BeEquivalentTo(new Uri($"https://beheer.verenigingen.vlaanderen.be/v1/verenigingen/{vCode}/contactgegevens/{_entityCommandResult.EntityId}"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public async Task Then_An_EntityId_Is_Returned(CommandhandlerScenarioBase scenar

var result = await commandHandler.Handle(new CommandEnvelope<VoegLocatieToeCommand>(command, _fixture.Create<CommandMetadata>()));

result.EntityId.Should().Be(expectedLocatieId.ToString());
result.EntityId.Should().Be(expectedLocatieId);
}

public static IEnumerable<object[]> Data
Expand Down

0 comments on commit a3ff720

Please sign in to comment.