Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: or-1952 registratie Moedervereniging uit KBO when registratie a… #551

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -87,15 +88,11 @@ public async Task<IActionResult> Post(
var skipDuplicateDetection = _bevestigingsTokenHelper.IsValid(bevestigingsToken, request);
Throw<InvalidBevestigingstokenProvided>.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<RegistreerAfdelingCommand>(command, metaData);
var registratieResult = await _bus.InvokeAsync<Result>(envelope);
var registratieResult = await RegistreerAfdeling(metadataProvider, request, skipDuplicateDetection);

return registratieResult switch
{
Expand All @@ -110,4 +107,35 @@ public async Task<IActionResult> Post(
_ => throw new ArgumentOutOfRangeException(),
};
}

private async Task<object> RegistreerMoeder(ICommandMetadataProvider metadataProvider,
string kboNummerMoedervereniging
)
{
var command = new RegistreerVerenigingUitKboCommand(KboNummer.Create(kboNummerMoedervereniging));
var metaData = metadataProvider.GetMetadata();
var envelope = new CommandEnvelope<RegistreerVerenigingUitKboCommand>(command, metaData);
var registratieResult = await _bus.InvokeAsync<Result>(envelope);

return registratieResult;

}

private async Task<Result> RegistreerAfdeling(
ICommandMetadataProvider metadataProvider,
RegistreerAfdelingRequest request,
bool skipDuplicateDetection)
{
var command = request.ToCommand()
with
{
SkipDuplicateDetection = skipDuplicateDetection,
};

var metaData = metadataProvider.GetMetadata();
var envelope = new CommandEnvelope<RegistreerAfdelingCommand>(command, metaData);
var registratieResult = await _bus.InvokeAsync<Result>(envelope);

return registratieResult;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

public record DuplicateKboFound(VCode VCode)
{
public static Result WithVcode(VCode vCode)=>new Result<DuplicateKboFound>(new DuplicateKboFound(vCode), ResultStatus.Failed);
public static Result WithVcode(VCode vCode) => new Result<DuplicateKboFound>(new DuplicateKboFound(vCode), ResultStatus.Failed);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -28,18 +27,18 @@ public When_RegistreerAfdeling_With_An_Existing_Moeder(EventsInDbScenariosFixtur
{
Naam = autoFixture.Create<string>(),
KboNummerMoedervereniging = fixture
.V016VerenigingMetRechtspersoonlijkheidWerdGeregistreerdAlsMoederVoorRegistratieAfdeling
.VerenigingMetRechtspersoonlijkheidWerdGeregistreerd.KboNummer,
.V016VerenigingMetRechtspersoonlijkheidWerdGeregistreerdAlsMoederVoorRegistratieAfdeling
.VerenigingMetRechtspersoonlijkheidWerdGeregistreerd.KboNummer,
};

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);
.GetAssociatedResourceJson("files.request.with_minimum_fields")
.Replace("{{vereniging.naam}}", request.Naam)
.Replace("{{vereniging.kboNummerMoedervereniging}}", request.KboNummerMoedervereniging);
}

[Collection(nameof(AdminApiCollection))]
Expand All @@ -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<AfdelingWerdGeregistreerd>()
.Single(e => e.Naam == _request.Naam);
.QueryRawEventDataOnly<AfdelingWerdGeregistreerd>()
.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<VerenigingMetRechtspersoonlijkheidWerdGeregistreerd>()
.Should()
.ContainSingle(e => e.KboNummer == _request.KboNummerMoedervereniging);
}

[Fact]
public void Then_it_returns_an_accepted_response()
{
Expand All @@ -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<AppSettings>().BaseUrl}/v1/verenigingen/V");
.StartWith($"{_fixture.ServiceProvider.GetRequiredService<AppSettings>().BaseUrl}/v1/verenigingen/V");
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -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<string>(),
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<When_RegistreerAfdeling_With_An_Onbekend_MoederKboNummer_Volgens_KBO>
{
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);
}
}
Original file line number Diff line number Diff line change
@@ -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<string>(),
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<When_RegistreerAfdeling_With_Moeder_Niet_Geregistreerd>
{
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<VerenigingMetRechtspersoonlijkheidWerdGeregistreerd>()
.Single(e => e.KboNummer == _request.KboNummerMoedervereniging);

var savedEvent = session.Events
.QueryRawEventDataOnly<AfdelingWerdGeregistreerd>()
.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<AppSettings>().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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public void Then_The_MagdaService_Is_Not_Invoked()
_magdaGeefVerenigingService.Invocations.Should().BeEmpty();
}


public Task DisposeAsync()
=> Task.CompletedTask;
}
Loading