Skip to content

Commit

Permalink
feat: or-1394 add legal entity type field
Browse files Browse the repository at this point in the history
  • Loading branch information
koenmetsu committed Aug 9, 2024
1 parent fad630f commit 000be9d
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace OrganisationRegistry.Api.Infrastructure.Magda;
using System.Linq;
using global::Magda.GeefOnderneming;
using Organisation;
using CodeSoortOndernemingType = global::Magda.ZoekOnderneming.CodeSoortOndernemingType;

public class MagdaOrganisationResponse : IMagdaOrganisationResponse
{
Expand All @@ -30,6 +31,9 @@ public class MagdaOrganisationResponse : IMagdaOrganisationResponse

public IMagdaTermination? Termination { get; }

public IMagdaLegalEntityType LegalEntityType { get; set; }


public MagdaOrganisationResponse(Onderneming2_0Type? onderneming, IDateTimeProvider dateTimeProvider)
{
FormalName = new Name(onderneming?.Namen?.MaatschappelijkeNamen);
Expand Down Expand Up @@ -65,6 +69,8 @@ public MagdaOrganisationResponse(Onderneming2_0Type? onderneming, IDateTimeProvi

if (onderneming?.Stopzetting != null)
Termination = new MagdaTermination(onderneming.Stopzetting);

LegalEntityType = new MagdaLegalEntityType(onderneming?.SoortOnderneming.Code.Value, onderneming?.SoortOnderneming.Code.Value);

Check warning on line 73 in src/OrganisationRegistry.Api/Infrastructure/Magda/MagdaOrganisationResponse.cs

View workflow job for this annotation

GitHub Actions / Run Tests (test/OrganisationRegistry.KboMutations.UnitTests)

Possible null reference argument for parameter 'description' in 'MagdaLegalEntityType.MagdaLegalEntityType(string code, string description)'.

Check warning on line 73 in src/OrganisationRegistry.Api/Infrastructure/Magda/MagdaOrganisationResponse.cs

View workflow job for this annotation

GitHub Actions / Run Tests (test/OrganisationRegistry.KboMutations.UnitTests)

Possible null reference argument for parameter 'code' in 'MagdaLegalEntityType.MagdaLegalEntityType(string code, string description)'.

Check warning on line 73 in src/OrganisationRegistry.Api/Infrastructure/Magda/MagdaOrganisationResponse.cs

View workflow job for this annotation

GitHub Actions / Run Tests (test/OrganisationRegistry.KboMutations.UnitTests)

Possible null reference argument for parameter 'description' in 'MagdaLegalEntityType.MagdaLegalEntityType(string code, string description)'.

Check warning on line 73 in src/OrganisationRegistry.Api/Infrastructure/Magda/MagdaOrganisationResponse.cs

View workflow job for this annotation

GitHub Actions / Run Tests (test/OrganisationRegistry.SqlServer.IntegrationTests)

Possible null reference argument for parameter 'description' in 'MagdaLegalEntityType.MagdaLegalEntityType(string code, string description)'.

Check warning on line 73 in src/OrganisationRegistry.Api/Infrastructure/Magda/MagdaOrganisationResponse.cs

View workflow job for this annotation

GitHub Actions / Run Tests (test/OrganisationRegistry.SqlServer.IntegrationTests)

Possible null reference argument for parameter 'code' in 'MagdaLegalEntityType.MagdaLegalEntityType(string code, string description)'.

Check warning on line 73 in src/OrganisationRegistry.Api/Infrastructure/Magda/MagdaOrganisationResponse.cs

View workflow job for this annotation

GitHub Actions / Run Tests (test/OrganisationRegistry.SqlServer.IntegrationTests)

Possible null reference argument for parameter 'description' in 'MagdaLegalEntityType.MagdaLegalEntityType(string code, string description)'.

Check warning on line 73 in src/OrganisationRegistry.Api/Infrastructure/Magda/MagdaOrganisationResponse.cs

View workflow job for this annotation

GitHub Actions / Run Tests (test/OrganisationRegistry.UnitTests)

Possible null reference argument for parameter 'description' in 'MagdaLegalEntityType.MagdaLegalEntityType(string code, string description)'.

Check warning on line 73 in src/OrganisationRegistry.Api/Infrastructure/Magda/MagdaOrganisationResponse.cs

View workflow job for this annotation

GitHub Actions / Run Tests (test/OrganisationRegistry.UnitTests)

Possible null reference argument for parameter 'code' in 'MagdaLegalEntityType.MagdaLegalEntityType(string code, string description)'.

Check warning on line 73 in src/OrganisationRegistry.Api/Infrastructure/Magda/MagdaOrganisationResponse.cs

View workflow job for this annotation

GitHub Actions / Run Tests (test/OrganisationRegistry.UnitTests)

Possible null reference argument for parameter 'description' in 'MagdaLegalEntityType.MagdaLegalEntityType(string code, string description)'.

Check warning on line 73 in src/OrganisationRegistry.Api/Infrastructure/Magda/MagdaOrganisationResponse.cs

View workflow job for this annotation

GitHub Actions / Run Tests (test/OrganisationRegistry.ElasticSearch.Tests)

Possible null reference argument for parameter 'code' in 'MagdaLegalEntityType.MagdaLegalEntityType(string code, string description)'.

Check warning on line 73 in src/OrganisationRegistry.Api/Infrastructure/Magda/MagdaOrganisationResponse.cs

View workflow job for this annotation

GitHub Actions / Run Tests (test/OrganisationRegistry.ElasticSearch.Tests)

Possible null reference argument for parameter 'description' in 'MagdaLegalEntityType.MagdaLegalEntityType(string code, string description)'.

Check warning on line 73 in src/OrganisationRegistry.Api/Infrastructure/Magda/MagdaOrganisationResponse.cs

View workflow job for this annotation

GitHub Actions / Run Tests (test/OrganisationRegistry.ElasticSearch.Tests)

Possible null reference argument for parameter 'description' in 'MagdaLegalEntityType.MagdaLegalEntityType(string code, string description)'.

Check warning on line 73 in src/OrganisationRegistry.Api/Infrastructure/Magda/MagdaOrganisationResponse.cs

View workflow job for this annotation

GitHub Actions / Run Tests (test/OrganisationRegistry.Api.IntegrationTests)

Possible null reference argument for parameter 'code' in 'MagdaLegalEntityType.MagdaLegalEntityType(string code, string description)'.

Check warning on line 73 in src/OrganisationRegistry.Api/Infrastructure/Magda/MagdaOrganisationResponse.cs

View workflow job for this annotation

GitHub Actions / Run Tests (test/OrganisationRegistry.Api.IntegrationTests)

Possible null reference argument for parameter 'description' in 'MagdaLegalEntityType.MagdaLegalEntityType(string code, string description)'.

Check warning on line 73 in src/OrganisationRegistry.Api/Infrastructure/Magda/MagdaOrganisationResponse.cs

View workflow job for this annotation

GitHub Actions / Run Tests (test/OrganisationRegistry.Api.IntegrationTests)

Possible null reference argument for parameter 'description' in 'MagdaLegalEntityType.MagdaLegalEntityType(string code, string description)'.
}

private static bool OverlapsWithToday(RechtsvormExtentieType type, DateTime today)
Expand Down Expand Up @@ -214,3 +220,15 @@ public MagdaTermination(StopzettingExtentieType stopzetting)
Reason = stopzetting.Code.Beschrijving ?? string.Empty;
}
}

public class MagdaLegalEntityType : IMagdaLegalEntityType
{
public string Code { get; }
public string Description { get; }

public MagdaLegalEntityType(string code, string description)
{
Code = code;
Description = description;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public class Organisation :
IElasticEventHandler<OrganisationTerminated>,
IElasticEventHandler<OrganisationTerminatedV2>,
IElasticEventHandler<OrganisationPlacedUnderVlimpersManagement>,
IElasticEventHandler<OrganisationReleasedFromVlimpersManagement>
IElasticEventHandler<OrganisationReleasedFromVlimpersManagement>,
IElasticEventHandler<KboLegalEntityTypeAdded>
{
private readonly Elastic _elastic;
private readonly ElasticSearchConfiguration _elasticSearchOptions;
Expand Down Expand Up @@ -120,6 +121,23 @@ public async Task<IElasticChange> Handle(
}
).ToAsyncResult();

public async Task<IElasticChange> Handle(
DbConnection dbConnection,
DbTransaction dbTransaction,
IEnvelope<KboLegalEntityTypeAdded> message)
=> await new ElasticPerDocumentChange<OrganisationDocument>(
message.Body.OrganisationId,
document =>
{
document.ChangeId = message.Number;
document.ChangeTime = message.Timestamp;
document.LegalEntityType = new LegalEntityType(
message.Body.LegalEntityTypeCode,
message.Body.LegalEntityTypeDescription);
}
).ToAsyncResult();

public async Task<IElasticChange> Handle(
DbConnection dbConnection,
DbTransaction dbTransaction,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace OrganisationRegistry.ElasticSearch.Organisations;

using Osc;

public class LegalEntityType
{
public LegalEntityType(string code, string description)
{
Code = code;
Description = description;
}

public LegalEntityType()
{
Code = string.Empty;
Description = string.Empty;
}

public string Code { get; set; }
public string Description { get; set; }

public static IPromise<IProperties> Mapping(PropertiesDescriptor<LegalEntityType> map)
=> map
.Keyword(
k => k
.Name(p => p.Code)).Keyword(
k => k
.Name(p => p.Description));
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ public OrganisationDocument()
BankAccounts = new List<OrganisationBankAccount>();
OpeningHours = new List<OrganisationOpeningHour>();
Regulations = new List<OrganisationRegulation>();
LegalEntityType = null;
Validity = Period.Infinite();
}

public LegalEntityType? LegalEntityType { get; set; }

public int ChangeId { get; set; }
public DateTimeOffset ChangeTime { get; set; }
public Guid Id { get; set; }
Expand Down Expand Up @@ -191,5 +194,10 @@ public static TypeMappingDescriptor<OrganisationDocument> Mapping(
.Name(p => p.Regulations)
.IncludeInRoot()
.Properties(OrganisationRegulation.Mapping))
.Nested<LegalEntityType>(
n => n
.Name(p => p.LegalEntityType)
.IncludeInRoot()
.Properties(LegalEntityType.Mapping))
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace OrganisationRegistry.Organisation.Events;

using System;
using Infrastructure;
using Newtonsoft.Json;

public class KboLegalEntityTypeAdded : BaseEvent<KboLegalEntityTypeAdded>
{
public KboLegalEntityTypeAdded(Guid organisationId, string legalEntityTypeCode, string legalEntityTypeDescription)
{
Id = organisationId;
LegalEntityTypeCode = legalEntityTypeCode;
LegalEntityTypeDescription = legalEntityTypeDescription;
}

public Guid OrganisationId => Id;

public string LegalEntityTypeCode { get; }
public string LegalEntityTypeDescription { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public interface IMagdaOrganisationResponse
IMagdaLegalForm? LegalForm { get; }
IMagdaAddress? Address { get; }
IMagdaTermination? Termination { get; }
IMagdaLegalEntityType LegalEntityType { get; }
}

public interface IMagdaName
Expand Down Expand Up @@ -51,3 +52,9 @@ public interface IMagdaTermination
string Code { get; }
string Reason { get; }
}

public interface IMagdaLegalEntityType
{
string Code { get; }
string Description { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ private async Task CreateFromKbo(CreateOrganisationFromKbo command, IUser user)

AddLabel(organisation, kboOrganisation);

organisation.AddKboLegalEntityType(kboOrganisation.LegalEntityType);

await Session.Commit(user);
}

Expand Down
15 changes: 15 additions & 0 deletions src/OrganisationRegistry/Organisation/Organisation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,16 @@ public void AddKboLegalFormOrganisationClassification(Guid organisationOrganisat
validity.End));
}

public void AddKboLegalEntityType(IMagdaLegalEntityType kboOrganisationLegalEntityType)
{
ApplyChange(
new KboLegalEntityTypeAdded(
Id,
kboOrganisationLegalEntityType.Code,
kboOrganisationLegalEntityType.Description
));
}

public void UpdateKboLegalFormOrganisationClassification(
IKboOrganisationClassificationRetriever organisationClassificationRetriever,
OrganisationClassificationType legalFormOrganisationClassificationType,
Expand Down Expand Up @@ -3114,4 +3124,9 @@ public void Apply(OrganisationLocationRemoved evnt)
{
State.OrganisationLocations.Remove(State.OrganisationLocations[evnt.OrganisationLocationId]);
}

public void Apply(KboLegalEntityTypeAdded evnt)
{
// no need to keep this in state at time of writing.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,8 @@ public async void LocationUpdated_UpdatesOrganisationLocation()
var organisationLocationUpdated = scenario.CreateOrganisationLocationUpdated(organisationLocationAdded);
var locationUpdated = scenario.CreateLocationUpdated(organisationLocationUpdated);

var kboLegalEntityTypeAdded = scenario.Create<KboLegalEntityTypeAdded>();

await _eventProcessor.Handle<OrganisationDocument>(
new List<IEnvelope>()
{
Expand All @@ -505,6 +507,7 @@ await _eventProcessor.Handle<OrganisationDocument>(
organisationLocationAdded.ToEnvelope(),
organisationLocationUpdated.ToEnvelope(),
locationUpdated.ToEnvelope(),
kboLegalEntityTypeAdded.ToEnvelope(),
}
);

Expand All @@ -524,5 +527,9 @@ await _eventProcessor.Handle<OrganisationDocument>(
locationUpdated.ZipCode,
locationUpdated.City,
locationUpdated.Country));

organisation.Source.LegalEntityType.Should().BeEquivalentTo(new LegalEntityType(
kboLegalEntityTypeAdded.LegalEntityTypeCode,
kboLegalEntityTypeAdded.LegalEntityTypeDescription));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,24 @@ await Given(Events)
organisationLabelAdded.ValidTo.Should().Be(new ValidTo());
}

[Fact]
public async Task AddsLegalEntityType()
{
await Given(Events)
.When(CreateOrganisationFromKboCommand, TestUser.AlgemeenBeheerder)
.Then();

var organisationLabelAdded = PublishedEvents[9].UnwrapBody<KboLegalEntityTypeAdded>();
organisationLabelAdded.Should().NotBeNull();

organisationLabelAdded.OrganisationId.Should().Be(_organisationId);
organisationLabelAdded.LegalEntityTypeCode.Should().Be("1");
organisationLabelAdded.LegalEntityTypeDescription.Should().Be("Natuurlijke Persoon");
}

[Fact]
public async Task PublishesTheCorrectNumberOfEvents()
=> await Given(Events)
.When(CreateOrganisationFromKboCommand, TestUser.AlgemeenBeheerder)
.ThenItPublishesTheCorrectNumberOfEvents(9);
.ThenItPublishesTheCorrectNumberOfEvents(10);
}
Original file line number Diff line number Diff line change
Expand Up @@ -262,5 +262,5 @@ await Given(Events)
public async Task PublishesTheCorrectNumberOfEvents()
=> await Given(Events)
.When(CreateOrganisationFromKboCommand, TestUser.AlgemeenBeheerder)
.ThenItPublishesTheCorrectNumberOfEvents(6);
.ThenItPublishesTheCorrectNumberOfEvents(7);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace OrganisationRegistry.UnitTests.Organisation.Kbo;

using System;
using System.Collections.Generic;
using Api.Infrastructure.Magda;
using OrganisationRegistry.Organisation;

public class MockMagdaOrganisationResponse : IMagdaOrganisationResponse
Expand All @@ -13,10 +14,12 @@ public class MockMagdaOrganisationResponse : IMagdaOrganisationResponse
public IMagdaLegalForm? LegalForm { get; set; }
public IMagdaAddress? Address { get; set; }
public IMagdaTermination? Termination { get; set; }
public IMagdaLegalEntityType LegalEntityType { get; }


public MockMagdaOrganisationResponse()
{
LegalEntityType = new MagdaLegalEntityType("1", "Natuurlijke Persoon");
BankAccounts = new List<IMagdaBankAccount>();
}
}

0 comments on commit 000be9d

Please sign in to comment.