Skip to content

Commit

Permalink
feat(apps): added endpoint to fetch app active documents
Browse files Browse the repository at this point in the history
Ref: CPLP-3087
  • Loading branch information
VPrasannaK94 committed Sep 13, 2023
1 parent 5ae0519 commit aff39b8
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,17 @@ private async Task UpdateTenantUrlAsyncInternal(Guid offerId, Guid subscriptionI

await _portalRepositories.SaveAsync().ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<ActiveAppDocumentData> GetActiveAppDocumentTypeDataAsync(Guid appId, Guid companyId)
{
var documentData = new Dictionary<DocumentTypeId, IEnumerable<DocumentData?>>();
var results = await _portalRepositories.GetInstance<IOfferRepository>().GetActiveOfferDocumentTypeDataAsync(appId, companyId, OfferTypeId.APP, _settings.ActiveAppDocumentTypeIds).ConfigureAwait(false);
var appDocTypeData = results!.GroupBy(d => d.DocumentTypeId).ToDictionary(g => g.Key, g => g.Select(d => new DocumentData(d.DocumentId, d.DocumentName)));
foreach (var doctype in _settings.ActiveAppDocumentTypeIds)
{
documentData.Add(doctype, appDocTypeData.Where(x => x.Key == doctype).SelectMany(x => x.Value));
}
return new ActiveAppDocumentData(documentData);
}
}
8 changes: 8 additions & 0 deletions src/marketplace/Apps.Service/BusinessLogic/AppsSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ public class AppsSettings
[Required]
[DistinctValues("x => x.ClientId")]
public IEnumerable<UserRoleConfig> SubscriptionManagerRoles { get; set; } = null!;

/// <summary>
/// Active Document Types
/// </summary>
[Required]
[EnumEnumeration]
[DistinctValues]
public IEnumerable<DocumentTypeId> ActiveAppDocumentTypeIds { get; set; } = null!;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,11 @@ public interface IAppChangeBusinessLogic
/// <param name="data">the data to update the url</param>
/// <param name="companyId"></param>
Task UpdateTenantUrlAsync(Guid offerId, Guid subscriptionId, UpdateTenantData data, Guid companyId);

/// <summary>
/// Gets the Active App Documents
/// </summary>
/// <param name="appId">Id of the offer</param>
/// <param name="companyId"></param>
Task<ActiveAppDocumentData> GetActiveAppDocumentTypeDataAsync(Guid appId, Guid companyId);
}
15 changes: 15 additions & 0 deletions src/marketplace/Apps.Service/Controllers/AppChangeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,19 @@ public async Task<NoContentResult> UpdateTenantUrl([FromRoute] Guid appId, [From
await this.WithCompanyId(companyId => _businessLogic.UpdateTenantUrlAsync(appId, subscriptionId, data, companyId)).ConfigureAwait(false);
return NoContent();
}

/// <summary>
/// Returns the Active App Documents
/// </summary>
/// <param name="appId" example="092bdae3-a044-4314-94f4-85c65a09e31b">Id of the app.</param>
/// <remarks>Example: GET /apps/appchange/{appId}/documents</remarks>
/// <response code="200">Gets the Active Apps documents</response>
/// <response code="404">If App does not exists</response>
[HttpGet]
[Route("{appId}/documents")]
[Authorize(Roles = "edit_apps")]
[ProducesResponseType(typeof(ActiveAppDocumentData), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status404NotFound)]
public async Task<ActiveAppDocumentData> GetActiveAppDocuments([FromRoute] Guid appId) =>
await this.WithCompanyId(companyId => _businessLogic.GetActiveAppDocumentTypeDataAsync(appId, companyId)).ConfigureAwait(false);
}
7 changes: 7 additions & 0 deletions src/marketplace/Apps.Service/ViewModels/AppData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Enums;

namespace Org.Eclipse.TractusX.Portal.Backend.Apps.Service.ViewModels;
Expand All @@ -43,3 +44,9 @@ public record AppData(
string Price,
Guid LeadPictureId,
IEnumerable<string> UseCases);

/// <summary>
/// View model of an application's base data.
/// </summary>
/// <param name="Documents">Id of the App.</param>
public record ActiveAppDocumentData(IDictionary<DocumentTypeId, IEnumerable<DocumentData?>> Documents);
Original file line number Diff line number Diff line change
Expand Up @@ -487,4 +487,13 @@ public interface IOfferRepository
/// <param name="subscriptionId"></param>
/// <returns></returns>
Task<(bool IsSingleInstance, IEnumerable<IEnumerable<UserRoleData>> ServiceAccountProfiles, string? OfferName)> GetServiceAccountProfileDataForSubscription(Guid subscriptionId);

/// <summary>
/// Gets the Active Offer DocumentType Data
/// </summary>
/// <param name="offerId"></param>
/// <param name="offerTypeId"></param>
/// <param name="documentTypeIds"></param>
/// <returns></returns>
Task<IEnumerable<DocumentTypeData>?> GetActiveOfferDocumentTypeDataAsync(Guid offerId, Guid userCompanyId, OfferTypeId offerTypeId, IEnumerable<DocumentTypeId> documentTypeIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -822,4 +822,19 @@ public void AttachAndModifyAppInstanceSetup(Guid appInstanceSetupId, Guid offerI
o.Offer.Name
))
.SingleOrDefaultAsync();

/// <inheritdoc />
public Task<IEnumerable<DocumentTypeData>?> GetActiveOfferDocumentTypeDataAsync(Guid offerId, Guid userCompanyId, OfferTypeId offerTypeId, IEnumerable<DocumentTypeId> documentTypeIds) =>
_context.Offers
.Where(o => o.Id == offerId &&
o.OfferStatusId == OfferStatusId.ACTIVE &&
o.OfferTypeId == offerTypeId &&
o.ProviderCompanyId == userCompanyId)
.Select(o => o.Documents.Where(doc => documentTypeIds.Contains(doc.DocumentTypeId))
.Select(doc => new DocumentTypeData(
doc.DocumentTypeId,
doc.Id,
doc.DocumentName)))
.SingleOrDefaultAsync();

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
using AutoFixture.AutoFakeItEasy;
using FakeItEasy;
using FluentAssertions;
using Flurl.Util;
using Microsoft.Extensions.Options;
using MimeKit.Encodings;
using Org.BouncyCastle.Utilities.Collections;
using Org.Eclipse.TractusX.Portal.Backend.Apps.Service.ViewModels;
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Models.Configuration;
Expand Down Expand Up @@ -92,6 +95,13 @@ public AppChangeBusinessLogicTest()
CompanyAdminRoles = new[]
{
new UserRoleConfig(ClientId, new [] { "Company Admin" })
},
ActiveAppDocumentTypeIds = new[]
{
DocumentTypeId.APP_IMAGE,
DocumentTypeId.APP_TECHNICAL_INFORMATION,
DocumentTypeId.APP_CONTRACT,
DocumentTypeId.ADDITIONAL_DETAILS
}
};
A.CallTo(() => _portalRepositories.GetInstance<INotificationRepository>()).Returns(_notificationRepository);
Expand Down Expand Up @@ -941,4 +951,35 @@ public async Task UpdateTenantUrlAsync_WithoutSubscriptionDetails_ThrowsConflict
}

#endregion

#region GetActiveAppDocumentTypeDataAsync

[Fact]
public async Task GetActiveAppDocumentTypeDataAsync_ReturnsExpected()
{
// Arrange
var appId = _fixture.Create<Guid>();
var documentId1 = _fixture.Create<Guid>();
var documenntData = new[] {
new DocumentTypeData(DocumentTypeId.APP_IMAGE, documentId1, "TestDoc1")
};

A.CallTo(() => _offerRepository.GetActiveOfferDocumentTypeDataAsync(A<Guid>._, A<Guid>._, OfferTypeId.APP, A<IEnumerable<DocumentTypeId>>._))
.Returns(documenntData);

// Act
var result = await _sut.GetActiveAppDocumentTypeDataAsync(appId, _identity.CompanyId).ConfigureAwait(false);

// Assert
A.CallTo(() => _offerRepository.GetActiveOfferDocumentTypeDataAsync(A<Guid>._, A<Guid>._, OfferTypeId.APP, A<IEnumerable<DocumentTypeId>>._)).MustHaveHappened();
result.Documents.Should().NotBeNull().And.HaveCount(4).And.Satisfy(
x => x.Key == DocumentTypeId.APP_IMAGE && x.Value.Any(y => y!.DocumentId == documentId1 && y.DocumentName == "TestDoc1"),
x => x.Key == DocumentTypeId.APP_TECHNICAL_INFORMATION && !x.Value.Any(),
x => x.Key == DocumentTypeId.APP_CONTRACT && !x.Value.Any(),
x => x.Key == DocumentTypeId.ADDITIONAL_DETAILS && !x.Value.Any()
);
}

#endregion

}
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,18 @@ public async Task UpdateTenantUrl_ReturnsExpected()
A.CallTo(() => _logic.UpdateTenantUrlAsync(appId, subscriptionId, data, _identity.CompanyId)).MustHaveHappened();
result.Should().BeOfType<NoContentResult>();
}

[Fact]
public async Task GetActiveAppDocuments_ReturnsExpected()
{
//Arrange
var appId = _fixture.Create<Guid>();

//Act
await this._controller.GetActiveAppDocuments(appId).ConfigureAwait(false);

//Assert
A.CallTo(() => _logic.GetActiveAppDocumentTypeDataAsync(appId, _identity.CompanyId)).MustHaveHappened();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@
"APP_TECHNICAL_INFORMATION",
"CONFORMITY_APPROVAL_BUSINESS_APPS"
],
"ActiveAppDocumentTypeIds": [
"APP_IMAGE",
"APP_TECHNICAL_INFORMATION",
"APP_CONTRACT",
"ADDITIONAL_DETAILS"
],
"ITAdminRoles": [
{
"ClientId": "Cl2-CX-Portal",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,31 @@ public async Task GetCompanyProvidedServiceStatusDataAsync_ReturnsExpectedResult

#endregion

#region GetActiveOfferDocumentTypeData

[Fact]
public async Task GetActiveOfferDocumentTypeDataAsync_ReturnsExpectedResult()
{
// Arrange
var activeDocumentType = new[]{
DocumentTypeId.APP_IMAGE,
DocumentTypeId.APP_TECHNICAL_INFORMATION,
DocumentTypeId.APP_CONTRACT,
DocumentTypeId.ADDITIONAL_DETAILS
};
var sut = await CreateSut().ConfigureAwait(false);

// Act
var result = await sut.GetActiveOfferDocumentTypeDataAsync(new("ac1cf001-7fbc-1f2f-817f-bce0572c0007"), new("2dc4249f-b5ca-4d42-bef1-7a7a950a4f87"), OfferTypeId.APP, activeDocumentType).ConfigureAwait(false);

// Assert
result.Should().NotBeNull().And.HaveCount(1).And.Satisfy(
x => x.DocumentId == new Guid("e020787d-1e04-4c0b-9c06-bd1cd44724b2") && x.DocumentName == "Default_App_Image.png" && x.DocumentTypeId == DocumentTypeId.APP_IMAGE
);
}

#endregion

#region Setup

private async Task<OfferRepository> CreateSut()
Expand Down

0 comments on commit aff39b8

Please sign in to comment.