Skip to content

Commit

Permalink
fix(registration): disable duplicate bpn check (#406)
Browse files Browse the repository at this point in the history
disable the duplicate bpn check for endpoint /api/registration/application/{applicationId}/companyDetailsWithAddress

Refs: CPLP-3665
  • Loading branch information
Phil91 authored Jan 12, 2024
1 parent 4405f94 commit 20ae54f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ public Task RetriggerProcessStep(string externalId, ProcessStepTypeId processSte
await data.ValidateDatabaseData(
bpn => _portalRepositories.GetInstance<ICompanyRepository>().CheckBpnExists(bpn),
alpha2Code => countryRepository.CheckCountryExistsByAlpha2CodeAsync(alpha2Code),
(countryAlpha2Code, uniqueIdentifierIds) => countryRepository.GetCountryAssignedIdentifiers(countryAlpha2Code, uniqueIdentifierIds)).ConfigureAwait(false);
(countryAlpha2Code, uniqueIdentifierIds) => countryRepository.GetCountryAssignedIdentifiers(countryAlpha2Code, uniqueIdentifierIds),
true).ConfigureAwait(false);

if (!data.CompanyRoles.Any())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ public static void ValidateData(this RegistrationData data)
}
}

public static async Task ValidateDatabaseData(this RegistrationData data, Func<string, Task<bool>> checkBpn, Func<string, Task<bool>> checkCountryExistByAlpha2Code, Func<string, IEnumerable<UniqueIdentifierId>, Task<(bool IsValidCountry, IEnumerable<UniqueIdentifierId> UniqueIdentifierIds)>> getCountryAssignedIdentifiers)
public static async Task ValidateDatabaseData(this RegistrationData data, Func<string, Task<bool>> checkBpn, Func<string, Task<bool>> checkCountryExistByAlpha2Code, Func<string, IEnumerable<UniqueIdentifierId>, Task<(bool IsValidCountry, IEnumerable<UniqueIdentifierId> UniqueIdentifierIds)>> getCountryAssignedIdentifiers, bool checkBpnAlreadyExists)
{
if (data.BusinessPartnerNumber != null && await checkBpn(data.BusinessPartnerNumber.ToUpper()).ConfigureAwait(false))
if (data.BusinessPartnerNumber != null && checkBpnAlreadyExists && await checkBpn(data.BusinessPartnerNumber.ToUpper()).ConfigureAwait(false))
{
throw new ControllerArgumentException($"The Bpn {data.BusinessPartnerNumber} already exists", nameof(data.BusinessPartnerNumber));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ await companyDetails.ValidateDatabaseData(
_portalRepositories.GetInstance<ICountryRepository>()
.GetCountryAssignedIdentifiers(
countryAlpha2Code,
uniqueIdentifierIds)).ConfigureAwait(false);
uniqueIdentifierIds),
false).ConfigureAwait(false);

var applicationRepository = _portalRepositories.GetInstance<IApplicationRepository>();
var companyRepository = _portalRepositories.GetInstance<ICompanyRepository>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,12 +559,13 @@ public async Task SetCompanyWithAddressAsync__WithInvalidBpn_ThrowsControllerArg
}

[Fact]
public async Task SetCompanyWithAddressAsync__WithExistingBpn_ThrowsControllerArgumentException()
public async Task SetCompanyWithAddressAsync__WithExistingBpn_ModifiesCompany()
{
//Arrange
var applicationId = Guid.NewGuid();
var companyId = Guid.NewGuid();
var identityData = A.Fake<IIdentityData>();
Company? company = null;
A.CallTo(() => identityData.IdentityId).Returns(Guid.NewGuid());
A.CallTo(() => identityData.IdentityTypeId).Returns(IdentityTypeId.COMPANY_USER);
A.CallTo(() => identityData.CompanyId).Returns(companyId);
Expand All @@ -587,12 +588,35 @@ public async Task SetCompanyWithAddressAsync__WithExistingBpn_ThrowsControllerAr
_identityService,
_dateTimeProvider);

var existingData = _fixture.Build<CompanyApplicationDetailData>()
.With(x => x.IsUserOfCompany, true)
.Create();

A.CallTo(() => _applicationRepository.GetCompanyApplicationDetailDataAsync(applicationId, A<Guid>._, companyId))
.Returns(existingData);

A.CallTo(() => _companyRepository.AttachAndModifyCompany(A<Guid>._, A<Action<Company>>._, A<Action<Company>>._))
.Invokes((Guid companyId, Action<Company>? initialize, Action<Company> modify) =>
{
company = new Company(companyId, null!, default, default);
initialize?.Invoke(company);
modify(company);
});

// Act
async Task Act() => await sut.SetCompanyDetailDataAsync(applicationId, companyData).ConfigureAwait(false);
await sut.SetCompanyDetailDataAsync(applicationId, companyData).ConfigureAwait(false);

// Assert
var ex = await Assert.ThrowsAsync<ControllerArgumentException>(Act);
ex.Message.Should().Be($"The Bpn {companyData.BusinessPartnerNumber} already exists (Parameter 'BusinessPartnerNumber')");
A.CallTo(() => _companyRepository.AttachAndModifyCompany(A<Guid>._, A<Action<Company>>._, A<Action<Company>>._))
.MustHaveHappenedOnceExactly();
A.CallTo(() => _portalRepositories.SaveAsync()).MustHaveHappenedOnceExactly();

company.Should().NotBeNull();
company.Should().Match<Company>(c =>
c.Id == companyId &&
c.Name == companyData.Name &&
c.Shortname == companyData.ShortName &&
c.BusinessPartnerNumber == companyData.BusinessPartnerNumber);
}

[Fact]
Expand Down

0 comments on commit 20ae54f

Please sign in to comment.