Skip to content

Commit

Permalink
fix(dim): fix callback logic for dim requests (#863)
Browse files Browse the repository at this point in the history
Refs: #862
Co-authored-by: Norbert Truchsess <[email protected]>
Reviewed-By: Evelyn Gurschler <[email protected]>
Reviewed-By: Norbert Truchsess <[email protected]>
  • Loading branch information
Phil91 and ntruchsess authored Jul 24, 2024
1 parent 0abdd08 commit fd1bb04
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,22 @@ private async Task CreateWalletInternal(Guid applicationId, CancellationToken ca

public async Task ProcessDimResponse(string bpn, DimWalletData data, CancellationToken cancellationToken)
{
var (exists, companyId, companyApplicationStatusIds) = await _portalRepositories.GetInstance<ICompanyRepository>().GetCompanyIdByBpn(bpn).ConfigureAwait(ConfigureAwaitOptions.None);
if (!exists)
var companySubmittedApplicationIds = await _portalRepositories.GetInstance<ICompanyRepository>().GetCompanySubmittedApplicationIdsByBpn(bpn).ToListAsync(cancellationToken).ConfigureAwait(false);
if (companySubmittedApplicationIds.Count == 0)
{
throw new NotFoundException($"No company found for bpn {bpn}");
}

if (companyApplicationStatusIds.Count() != 1)
var companyApplicationIds = companySubmittedApplicationIds.SelectMany(x => x.SubmittedApplicationIds.Select(applicationId => (x.CompanyId, ApplicationId: applicationId)));
if (companyApplicationIds.Count() != 1)
{
throw new ConflictException($"There must be exactly one company application in state {CompanyApplicationStatusId.SUBMITTED}");
}

var (companyId, applicationId) = companyApplicationIds.First();
var context = await _checklistService
.VerifyChecklistEntryAndProcessSteps(
companyApplicationStatusIds.Single(),
applicationId,
ApplicationChecklistEntryTypeId.IDENTITY_WALLET,
[ApplicationChecklistEntryStatusId.IN_PROGRESS],
ProcessStepTypeId.AWAIT_DIM_RESPONSE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,16 +367,15 @@ public void CreateWalletData(Guid companyId, string did, JsonDocument didDocumen
.Select(x => new ValueTuple<bool, JsonDocument>(true, x.DidDocument))
.SingleOrDefaultAsync();

public Task<(bool Exists, Guid CompanyId, IEnumerable<Guid> SubmittedCompanyApplicationId)> GetCompanyIdByBpn(string bpn) =>
public IAsyncEnumerable<(Guid CompanyId, IEnumerable<Guid> SubmittedApplicationIds)> GetCompanySubmittedApplicationIdsByBpn(string bpn) =>
context.Companies
.Where(x => x.BusinessPartnerNumber == bpn)
.Select(x => new ValueTuple<bool, Guid, IEnumerable<Guid>>(
true,
.Select(x => new ValueTuple<Guid, IEnumerable<Guid>>(
x.Id,
x.CompanyApplications
.Where(ca => ca.ApplicationStatusId == CompanyApplicationStatusId.SUBMITTED)
.Select(ca => ca.Id)))
.SingleOrDefaultAsync();
.ToAsyncEnumerable();

public Task<(string? Bpn, string? Did, string? WalletUrl)> GetDimServiceUrls(Guid companyId) =>
context.Companies.Where(x => x.Id == companyId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public interface ICompanyRepository
Task<bool> CheckBpnExists(string bpn);
void CreateWalletData(Guid companyId, string did, JsonDocument didDocument, string clientId, byte[] clientSecret, byte[]? initializationVector, int encryptionMode, string authenticationServiceUrl);
Task<(bool Exists, JsonDocument DidDocument)> GetDidDocumentById(string bpn);
Task<(bool Exists, Guid CompanyId, IEnumerable<Guid> SubmittedCompanyApplicationId)> GetCompanyIdByBpn(string bpn);
IAsyncEnumerable<(Guid CompanyId, IEnumerable<Guid> SubmittedApplicationIds)> GetCompanySubmittedApplicationIdsByBpn(string bpn);
Task<(string? Bpn, string? Did, string? WalletUrl)> GetDimServiceUrls(Guid companyId);
Task<(string? Holder, string? BusinessPartnerNumber, WalletInformation? WalletInformation)> GetWalletData(Guid identityId);
void RemoveProviderCompanyDetails(Guid providerCompanyDetailId);
Expand Down
20 changes: 10 additions & 10 deletions tests/externalsystems/Dim.Library.Tests/DimBusinessLogicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ public async Task ProcessDimResponse_NoCompanyForBpn_ThrowsNotFoundException()
{
// Arrange
var data = _fixture.Create<DimWalletData>();
A.CallTo(() => _companyRepository.GetCompanyIdByBpn(BPN))
.Returns(new ValueTuple<bool, Guid, IEnumerable<Guid>>(false, default, Enumerable.Empty<Guid>()));
A.CallTo(() => _companyRepository.GetCompanySubmittedApplicationIdsByBpn(BPN))
.Returns(Enumerable.Empty<ValueTuple<Guid, IEnumerable<Guid>>>().ToAsyncEnumerable());
async Task Act() => await _logic.ProcessDimResponse(BPN, data, CancellationToken.None);

// Act
Expand All @@ -277,8 +277,8 @@ public async Task ProcessDimResponse_WithMultipleSubmittedApplications_ThrowsCon
{
// Arrange
var data = _fixture.Create<DimWalletData>();
A.CallTo(() => _companyRepository.GetCompanyIdByBpn(BPN))
.Returns(new ValueTuple<bool, Guid, IEnumerable<Guid>>(true, default, _fixture.CreateMany<Guid>(2)));
A.CallTo(() => _companyRepository.GetCompanySubmittedApplicationIdsByBpn(BPN))
.Returns(Enumerable.Repeat(new ValueTuple<Guid, IEnumerable<Guid>>(default, _fixture.CreateMany<Guid>(2)), 1).ToAsyncEnumerable());
async Task Act() => await _logic.ProcessDimResponse(BPN, data, CancellationToken.None);

// Act
Expand All @@ -304,8 +304,8 @@ public async Task ProcessDimResponse_WithDidSchemaInvalid_CallsExpected()
.With(x => x.Did, "did:web:test.com:BPNL0000000000XX")
.Create();
var companyId = Guid.NewGuid();
A.CallTo(() => _companyRepository.GetCompanyIdByBpn(BPN))
.Returns(new ValueTuple<bool, Guid, IEnumerable<Guid>>(true, companyId, Enumerable.Repeat(ApplicationId, 1)));
A.CallTo(() => _companyRepository.GetCompanySubmittedApplicationIdsByBpn(BPN))
.Returns(Enumerable.Repeat(new ValueTuple<Guid, IEnumerable<Guid>>(companyId, Enumerable.Repeat(ApplicationId, 1)), 1).ToAsyncEnumerable());
A.CallTo(() => _checklistService.VerifyChecklistEntryAndProcessSteps(ApplicationId, ApplicationChecklistEntryTypeId.IDENTITY_WALLET, A<IEnumerable<ApplicationChecklistEntryStatusId>>._, ProcessStepTypeId.AWAIT_DIM_RESPONSE, A<IEnumerable<ApplicationChecklistEntryTypeId>?>._, A<IEnumerable<ProcessStepTypeId>?>._))
.Returns(context);

Expand All @@ -330,8 +330,8 @@ public async Task ProcessDimResponse_WithFailingSchemaValidation_CallsExpected()
var didDocument = JsonDocument.Parse("{\n \"@context\": [\n \"abc\" ],\n \"id\": \"did:web:example.org:did:BPNL0000000000XX\",\n \"verificationMethod\": [\n {\n \"id\": [\"did:web:example.com:did:BPNL0000000000XX#key-0\"],\n \"publicKeyJwk\": {\n \"kty\": \"JsonWebKey2020\",\n \"crv\": \"Ed25519\",\n \"x\": \"3534354354353\"\n }\n }\n ],\n \"services\": [\n {\n \"id\": [\"did:web:example.com:did:BPNL0000000000XX#key-0\"],\n \"serviceEndpoint\": \"test.org:123\"\n }\n ]\n}");
var data = _fixture.Build<DimWalletData>().With(x => x.DidDocument, didDocument).With(x => x.Did, "did:web:example.org:did:BPNL0000000000XX").Create();
var companyId = Guid.NewGuid();
A.CallTo(() => _companyRepository.GetCompanyIdByBpn(BPN))
.Returns(new ValueTuple<bool, Guid, IEnumerable<Guid>>(true, companyId, Enumerable.Repeat(ApplicationId, 1)));
A.CallTo(() => _companyRepository.GetCompanySubmittedApplicationIdsByBpn(BPN))
.Returns(Enumerable.Repeat(new ValueTuple<Guid, IEnumerable<Guid>>(companyId, Enumerable.Repeat(ApplicationId, 1)), 1).ToAsyncEnumerable());
A.CallTo(() => _checklistService.VerifyChecklistEntryAndProcessSteps(ApplicationId, ApplicationChecklistEntryTypeId.IDENTITY_WALLET, A<IEnumerable<ApplicationChecklistEntryStatusId>>._, ProcessStepTypeId.AWAIT_DIM_RESPONSE, A<IEnumerable<ApplicationChecklistEntryTypeId>?>._, A<IEnumerable<ProcessStepTypeId>?>._))
.Returns(context);

Expand Down Expand Up @@ -390,8 +390,8 @@ public async Task ProcessDimResponse_WithValid_CallsExpected()
.With(x => x.Did, "did:web:example.org:did:BPNL0000000000XX")
.Create();
var companyId = Guid.NewGuid();
A.CallTo(() => _companyRepository.GetCompanyIdByBpn(BPN))
.Returns(new ValueTuple<bool, Guid, IEnumerable<Guid>>(true, companyId, Enumerable.Repeat(ApplicationId, 1)));
A.CallTo(() => _companyRepository.GetCompanySubmittedApplicationIdsByBpn(BPN))
.Returns(Enumerable.Repeat(new ValueTuple<Guid, IEnumerable<Guid>>(companyId, Enumerable.Repeat(ApplicationId, 1)), 1).ToAsyncEnumerable());
A.CallTo(() => _checklistService.VerifyChecklistEntryAndProcessSteps(ApplicationId, ApplicationChecklistEntryTypeId.IDENTITY_WALLET, A<IEnumerable<ApplicationChecklistEntryStatusId>>._, ProcessStepTypeId.AWAIT_DIM_RESPONSE, A<IEnumerable<ApplicationChecklistEntryTypeId>?>._, A<IEnumerable<ProcessStepTypeId>?>._))
.Returns(context);
byte[]? encrypted = null;
Expand Down

0 comments on commit fd1bb04

Please sign in to comment.