diff --git a/src/AssociationRegistry.Admin.Api/Verenigingen/Registreer/Afdeling/RegistreerAfdelingController.cs b/src/AssociationRegistry.Admin.Api/Verenigingen/Registreer/Afdeling/RegistreerAfdelingController.cs index 8ab5d2243..387de8efd 100644 --- a/src/AssociationRegistry.Admin.Api/Verenigingen/Registreer/Afdeling/RegistreerAfdelingController.cs +++ b/src/AssociationRegistry.Admin.Api/Verenigingen/Registreer/Afdeling/RegistreerAfdelingController.cs @@ -1,6 +1,7 @@ namespace AssociationRegistry.Admin.Api.Verenigingen.Registreer.Afdeling; using Acties.RegistreerAfdeling; +using Acties.RegistreerVerenigingUitKbo; using Be.Vlaanderen.Basisregisters.Api; using Be.Vlaanderen.Basisregisters.Api.Exceptions; using DuplicateVerenigingDetection; @@ -87,15 +88,11 @@ public async Task Post( var skipDuplicateDetection = _bevestigingsTokenHelper.IsValid(bevestigingsToken, request); Throw.If(!string.IsNullOrWhiteSpace(bevestigingsToken) && !skipDuplicateDetection); - var command = request.ToCommand() - with - { - SkipDuplicateDetection = skipDuplicateDetection, - }; + var registratieMoederResult = await RegistreerMoeder( + metadataProvider, + request.KboNummerMoedervereniging); - var metaData = metadataProvider.GetMetadata(); - var envelope = new CommandEnvelope(command, metaData); - var registratieResult = await _bus.InvokeAsync(envelope); + var registratieResult = await RegistreerAfdeling(metadataProvider, request, skipDuplicateDetection); return registratieResult switch { @@ -110,4 +107,35 @@ public async Task Post( _ => throw new ArgumentOutOfRangeException(), }; } + + private async Task RegistreerMoeder(ICommandMetadataProvider metadataProvider, + string kboNummerMoedervereniging + ) + { + var command = new RegistreerVerenigingUitKboCommand(KboNummer.Create(kboNummerMoedervereniging)); + var metaData = metadataProvider.GetMetadata(); + var envelope = new CommandEnvelope(command, metaData); + var registratieResult = await _bus.InvokeAsync(envelope); + + return registratieResult; + + } + + private async Task RegistreerAfdeling( + ICommandMetadataProvider metadataProvider, + RegistreerAfdelingRequest request, + bool skipDuplicateDetection) + { + var command = request.ToCommand() + with + { + SkipDuplicateDetection = skipDuplicateDetection, + }; + + var metaData = metadataProvider.GetMetadata(); + var envelope = new CommandEnvelope(command, metaData); + var registratieResult = await _bus.InvokeAsync(envelope); + + return registratieResult; + } } diff --git a/src/AssociationRegistry/DuplicateVerenigingDetection/DuplicateKboFound.cs b/src/AssociationRegistry/DuplicateVerenigingDetection/DuplicateKboFound.cs index 6c301b634..cde6ae12e 100644 --- a/src/AssociationRegistry/DuplicateVerenigingDetection/DuplicateKboFound.cs +++ b/src/AssociationRegistry/DuplicateVerenigingDetection/DuplicateKboFound.cs @@ -5,5 +5,5 @@ public record DuplicateKboFound(VCode VCode) { - public static Result WithVcode(VCode vCode)=>new Result(new DuplicateKboFound(vCode), ResultStatus.Failed); + public static Result WithVcode(VCode vCode) => new Result(new DuplicateKboFound(vCode), ResultStatus.Failed); } diff --git a/test/AssociationRegistry.Test.Admin.Api/Afdeling/When_RegistreerAfdeling/With_All_Fields.cs b/test/AssociationRegistry.Test.Admin.Api/Afdeling/When_RegistreerAfdeling/With_All_Fields.cs index a99219051..9529331f7 100644 --- a/test/AssociationRegistry.Test.Admin.Api/Afdeling/When_RegistreerAfdeling/With_All_Fields.cs +++ b/test/AssociationRegistry.Test.Admin.Api/Afdeling/When_RegistreerAfdeling/With_All_Fields.cs @@ -184,8 +184,8 @@ public void Then_it_saves_the_events() savedEvent.KorteNaam.Should().Be(Request.KorteNaam); savedEvent.Moedervereniging.KboNummer.Should().Be(Request.KboNummerMoedervereniging); - savedEvent.Moedervereniging.VCode.Should().Be(string.Empty); - savedEvent.Moedervereniging.Naam.Should().Be($"Moeder {Request.KboNummerMoedervereniging}"); + savedEvent.Moedervereniging.VCode.Should().NotBeEmpty(); + savedEvent.Moedervereniging.Naam.Should().NotBeEmpty(); savedEvent.KorteBeschrijving.Should().Be(Request.KorteBeschrijving); savedEvent.Startdatum.Should().Be(Request.Startdatum!.Value); savedEvent.Doelgroep.Should().BeEquivalentTo(new Registratiedata.Doelgroep( diff --git a/test/AssociationRegistry.Test.Admin.Api/Afdeling/When_RegistreerAfdeling/With_An_Existing_Moeder.cs b/test/AssociationRegistry.Test.Admin.Api/Afdeling/When_RegistreerAfdeling/With_An_Existing_Moeder.cs index fec21afc6..77fa78aab 100644 --- a/test/AssociationRegistry.Test.Admin.Api/Afdeling/When_RegistreerAfdeling/With_An_Existing_Moeder.cs +++ b/test/AssociationRegistry.Test.Admin.Api/Afdeling/When_RegistreerAfdeling/With_An_Existing_Moeder.cs @@ -17,7 +17,6 @@ public sealed class When_RegistreerAfdeling_With_An_Existing_Moeder { public readonly RegistreerAfdelingRequest Request; - public readonly HttpResponseMessage Response; public When_RegistreerAfdeling_With_An_Existing_Moeder(EventsInDbScenariosFixture fixture) @@ -28,8 +27,8 @@ public When_RegistreerAfdeling_With_An_Existing_Moeder(EventsInDbScenariosFixtur { Naam = autoFixture.Create(), KboNummerMoedervereniging = fixture - .V016VerenigingMetRechtspersoonlijkheidWerdGeregistreerdAlsMoederVoorRegistratieAfdeling - .VerenigingMetRechtspersoonlijkheidWerdGeregistreerd.KboNummer, + .V016VerenigingMetRechtspersoonlijkheidWerdGeregistreerdAlsMoederVoorRegistratieAfdeling + .VerenigingMetRechtspersoonlijkheidWerdGeregistreerd.KboNummer, }; Response ??= fixture.DefaultClient.RegistreerAfdeling(GetJsonBody(Request)).GetAwaiter().GetResult(); @@ -37,9 +36,9 @@ public When_RegistreerAfdeling_With_An_Existing_Moeder(EventsInDbScenariosFixtur private string GetJsonBody(RegistreerAfdelingRequest request) => GetType() - .GetAssociatedResourceJson("files.request.with_minimum_fields") - .Replace("{{vereniging.naam}}", request.Naam) - .Replace("{{vereniging.kboNummerMoedervereniging}}", request.KboNummerMoedervereniging); + .GetAssociatedResourceJson("files.request.with_minimum_fields") + .Replace("{{vereniging.naam}}", request.Naam) + .Replace("{{vereniging.kboNummerMoedervereniging}}", request.KboNummerMoedervereniging); } [Collection(nameof(AdminApiCollection))] @@ -66,17 +65,28 @@ public With_An_Existing_Moeder(When_RegistreerAfdeling_With_An_Existing_Moeder s public void Then_it_saves_the_events() { using var session = _fixture.DocumentStore - .LightweightSession(); + .LightweightSession(); var savedEvent = session.Events - .QueryRawEventDataOnly() - .Single(e => e.Naam == _request.Naam); + .QueryRawEventDataOnly() + .Single(e => e.Naam == _request.Naam); savedEvent.Moedervereniging.KboNummer.Should().Be(_request.KboNummerMoedervereniging); savedEvent.Moedervereniging.VCode.Should().Be(_vCodeMoeder); savedEvent.Moedervereniging.Naam.Should().Be(_naamMoeder); } + [Fact] + public void Then_it_does_not_save_an_extra_VerenigingMetRechtspersoonlijkheidWerdGeregistreerd_event() + { + using var session = _fixture.DocumentStore + .LightweightSession(); + + session.Events.QueryRawEventDataOnly() + .Should() + .ContainSingle(e => e.KboNummer == _request.KboNummerMoedervereniging); + } + [Fact] public void Then_it_returns_an_accepted_response() { @@ -87,8 +97,9 @@ public void Then_it_returns_an_accepted_response() public void Then_it_returns_a_location_header() { _response.Headers.Should().ContainKey(HeaderNames.Location); + _response.Headers.Location!.OriginalString.Should() - .StartWith($"{_fixture.ServiceProvider.GetRequiredService().BaseUrl}/v1/verenigingen/V"); + .StartWith($"{_fixture.ServiceProvider.GetRequiredService().BaseUrl}/v1/verenigingen/V"); } [Fact] diff --git a/test/AssociationRegistry.Test.Admin.Api/Afdeling/When_RegistreerAfdeling/With_An_Onbekend_MoederKboNummer_Volgens_KBO.cs b/test/AssociationRegistry.Test.Admin.Api/Afdeling/When_RegistreerAfdeling/With_An_Onbekend_MoederKboNummer_Volgens_KBO.cs new file mode 100644 index 000000000..6fc2843fd --- /dev/null +++ b/test/AssociationRegistry.Test.Admin.Api/Afdeling/When_RegistreerAfdeling/With_An_Onbekend_MoederKboNummer_Volgens_KBO.cs @@ -0,0 +1,61 @@ +namespace AssociationRegistry.Test.Admin.Api.Afdeling.When_RegistreerAfdeling; + +using AssociationRegistry.Admin.Api.Verenigingen.Registreer.Afdeling.RequestModels; +using AutoFixture; +using Fixtures; +using FluentAssertions; +using Framework; +using System.Net; +using Xunit; +using Xunit.Categories; + +public sealed class When_RegistreerAfdeling_With_An_Onbekend_MoederKboNummer_Volgens_KBO +{ + private const string KboNummerNietGekendInKBO = "0000000196"; + public readonly RegistreerAfdelingRequest Request; + public readonly HttpResponseMessage Response; + + public When_RegistreerAfdeling_With_An_Onbekend_MoederKboNummer_Volgens_KBO(EventsInDbScenariosFixture fixture) + { + var autoFixture = new Fixture().CustomizeAdminApi(); + + Request = new RegistreerAfdelingRequest + { + Naam = autoFixture.Create(), + KboNummerMoedervereniging = KboNummerNietGekendInKBO, + }; + + Response ??= fixture.DefaultClient.RegistreerAfdeling(GetJsonBody(Request)).GetAwaiter().GetResult(); + } + + private string GetJsonBody(RegistreerAfdelingRequest request) + => GetType() + .GetAssociatedResourceJson("files.request.with_minimum_fields") + .Replace("{{vereniging.naam}}", request.Naam) + .Replace("{{vereniging.kboNummerMoedervereniging}}", request.KboNummerMoedervereniging); +} + +[Collection(nameof(AdminApiCollection))] +[Category("AdminApi")] +[IntegrationTest] +public class With_An_Onbekend_MoederKboNummer_Volgens_KBO : IClassFixture +{ + private readonly EventsInDbScenariosFixture _fixture; + private readonly RegistreerAfdelingRequest _request; + private readonly HttpResponseMessage _response; + + public With_An_Onbekend_MoederKboNummer_Volgens_KBO( + When_RegistreerAfdeling_With_An_Onbekend_MoederKboNummer_Volgens_KBO setup, + EventsInDbScenariosFixture fixture) + { + _request = setup.Request; + _response = setup.Response; + _fixture = fixture; + } + + [Fact] + public void Then_it_returns_a_badRequest_response() + { + _response.StatusCode.Should().Be(HttpStatusCode.BadRequest); + } +} diff --git a/test/AssociationRegistry.Test.Admin.Api/Afdeling/When_RegistreerAfdeling/With_Moeder_Niet_Geregistreerd.cs b/test/AssociationRegistry.Test.Admin.Api/Afdeling/When_RegistreerAfdeling/With_Moeder_Niet_Geregistreerd.cs new file mode 100644 index 000000000..a82b3ba24 --- /dev/null +++ b/test/AssociationRegistry.Test.Admin.Api/Afdeling/When_RegistreerAfdeling/With_Moeder_Niet_Geregistreerd.cs @@ -0,0 +1,101 @@ +namespace AssociationRegistry.Test.Admin.Api.Afdeling.When_RegistreerAfdeling; + +using AssociationRegistry.Admin.Api.Infrastructure; +using AssociationRegistry.Admin.Api.Infrastructure.ConfigurationBindings; +using AssociationRegistry.Admin.Api.Verenigingen.Registreer.Afdeling.RequestModels; +using AutoFixture; +using Events; +using Fixtures; +using FluentAssertions; +using Framework; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Net.Http.Headers; +using System.Net; +using Xunit; +using Xunit.Categories; + +public sealed class When_RegistreerAfdeling_With_Moeder_Niet_Geregistreerd +{ + public readonly RegistreerAfdelingRequest Request; + public readonly HttpResponseMessage Response; + + public When_RegistreerAfdeling_With_Moeder_Niet_Geregistreerd(EventsInDbScenariosFixture fixture) + { + var autoFixture = new Fixture().CustomizeAdminApi(); + + Request = new RegistreerAfdelingRequest + { + Naam = autoFixture.Create(), + KboNummerMoedervereniging = "0407622110", + }; + + Response ??= fixture.DefaultClient.RegistreerAfdeling(GetJsonBody(Request)).GetAwaiter().GetResult(); + } + + private string GetJsonBody(RegistreerAfdelingRequest request) + => GetType() + .GetAssociatedResourceJson("files.request.with_minimum_fields") + .Replace("{{vereniging.naam}}", request.Naam) + .Replace("{{vereniging.kboNummerMoedervereniging}}", request.KboNummerMoedervereniging); +} + +[Collection(nameof(AdminApiCollection))] +[Category("AdminApi")] +[IntegrationTest] +public class With_Moeder_Niet_Geregistreerd : IClassFixture +{ + private readonly EventsInDbScenariosFixture _fixture; + private readonly RegistreerAfdelingRequest _request; + private readonly HttpResponseMessage _response; + + public With_Moeder_Niet_Geregistreerd(When_RegistreerAfdeling_With_Moeder_Niet_Geregistreerd setup, EventsInDbScenariosFixture fixture) + { + _request = setup.Request; + _response = setup.Response; + _fixture = fixture; + } + + [Fact] + public void Then_it_saves_the_events() + { + using var session = _fixture.DocumentStore + .LightweightSession(); + + var savedMoederEvent = session.Events + .QueryRawEventDataOnly() + .Single(e => e.KboNummer == _request.KboNummerMoedervereniging); + + var savedEvent = session.Events + .QueryRawEventDataOnly() + .Single(e => e.Naam == _request.Naam); + + savedEvent.Moedervereniging.KboNummer.Should().Be(_request.KboNummerMoedervereniging); + savedEvent.Moedervereniging.VCode.Should().Be(savedMoederEvent.VCode); + savedEvent.Moedervereniging.Naam.Should().Be(savedMoederEvent.Naam); + } + + [Fact] + public void Then_it_returns_an_accepted_response() + { + _response.StatusCode.Should().Be(HttpStatusCode.Accepted); + } + + [Fact] + public void Then_it_returns_a_location_header() + { + _response.Headers.Should().ContainKey(HeaderNames.Location); + + _response.Headers.Location!.OriginalString.Should() + .StartWith($"{_fixture.ServiceProvider.GetRequiredService().BaseUrl}/v1/verenigingen/V"); + } + + [Fact] + public void Then_it_returns_a_sequence_header() + { + _response.Headers.Should().ContainKey(WellknownHeaderNames.Sequence); + var sequenceValues = _response.Headers.GetValues(WellknownHeaderNames.Sequence).ToList(); + sequenceValues.Should().HaveCount(expected: 1); + var sequence = Convert.ToInt64(sequenceValues.Single()); + sequence.Should().BeGreaterThan(expected: 0); + } +} diff --git a/test/AssociationRegistry.Test.Admin.Api/VerenigingMetRechtspersoonlijkheid/When_RegistreerVerenigingMetRechtspersoonlijkheid/CommandHandling/With_A_Duplicate_KboNummer.cs b/test/AssociationRegistry.Test.Admin.Api/VerenigingMetRechtspersoonlijkheid/When_RegistreerVerenigingMetRechtspersoonlijkheid/CommandHandling/With_A_Duplicate_KboNummer.cs index 4953c2567..d4d1538d9 100644 --- a/test/AssociationRegistry.Test.Admin.Api/VerenigingMetRechtspersoonlijkheid/When_RegistreerVerenigingMetRechtspersoonlijkheid/CommandHandling/With_A_Duplicate_KboNummer.cs +++ b/test/AssociationRegistry.Test.Admin.Api/VerenigingMetRechtspersoonlijkheid/When_RegistreerVerenigingMetRechtspersoonlijkheid/CommandHandling/With_A_Duplicate_KboNummer.cs @@ -62,7 +62,6 @@ public void Then_The_MagdaService_Is_Not_Invoked() _magdaGeefVerenigingService.Invocations.Should().BeEmpty(); } - public Task DisposeAsync() => Task.CompletedTask; }