Skip to content

Commit

Permalink
feat(document): get company certificate by document id (#519)
Browse files Browse the repository at this point in the history
Refs: #462 
Reviewed-By: Phil Schneider <[email protected]>
  • Loading branch information
AnuragNagpure authored Mar 4, 2024
1 parent 24b926e commit 07cafc2
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -645,4 +645,23 @@ public async Task<int> DeleteCompanyCertificateAsync(Guid documentId)

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

/// <inheritdoc />
public async Task<(string FileName, byte[] Content, string MediaType)> GetCompanyCertificateDocumentAsync(Guid documentId)
{
var documentDetails = await _portalRepositories.GetInstance<ICompanyCertificateRepository>()
.GetCompanyCertificateDocumentDataAsync(documentId, DocumentTypeId.COMPANY_CERTIFICATE)
.ConfigureAwait(false);

if (!documentDetails.Exists)
{
throw new NotFoundException($"Company certificate document {documentId} does not exist");
}
if (!documentDetails.IsStatusLocked)
{
throw new ForbiddenException($"Document {documentId} status is not locked");
}

return (documentDetails.FileName, documentDetails.Content, documentDetails.MediaTypeId.MapToMediaType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public interface ICompanyDataBusinessLogic

Task CreateCompanyCertificate(CompanyCertificateCreationData data, CancellationToken cancellationToken);

Task<(string FileName, byte[] Content, string MediaType)> GetCompanyCertificateDocumentAsync(Guid documentId);

Task<int> DeleteCompanyCertificateAsync(Guid documentId);

Task<Pagination.Response<CompanyCertificateData>> GetAllCompanyCertificatesAsync(int page, int size, CertificateSorting? sorting, CompanyCertificateStatusId? certificateStatus, CompanyCertificateTypeId? certificateType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,31 @@ public IAsyncEnumerable<CompanyCertificateBpnData> GetCompanyCertificatesByBpn(s
public Task<Pagination.Response<CompanyCertificateData>> GetAllCompanyCertificatesAsync([FromQuery] int page = 0, [FromQuery] int size = 15, [FromQuery] CertificateSorting? sorting = null, [FromQuery] CompanyCertificateStatusId? certificateStatus = null, [FromQuery] CompanyCertificateTypeId? certificateType = null) =>
_logic.GetAllCompanyCertificatesAsync(page, size, sorting, certificateStatus, certificateType);

/// <summary>
/// Retrieves a specific company certificate document for the given id.
/// </summary>
/// <param name="documentId" example="4ad087bb-80a1-49d3-9ba9-da0b175cd4e3">Id of the document to get.</param>
/// <returns>Returns the file.</returns>
/// <remarks>Example: GET /api/administration/companydata/companyCertificates/documents/4ad087bb-80a1-49d3-9ba9-da0b175cd4e3</remarks>
/// <response code="200">Returns the file.</response>
/// <response code="403">The document which is not in status "ACTIVE".</response>
/// <response code="404">The document was not found.</response>
/// <response code="503">document Content is null.</response>
[HttpGet]
[Route("companyCertificates/documents/{documentId}")]
[Authorize(Roles = "view_certificates")]
[Authorize(Policy = PolicyTypes.ValidCompany)]
[Authorize(Policy = PolicyTypes.CompanyUser)]
[ProducesResponseType(typeof(FileContentResult), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status403Forbidden)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status503ServiceUnavailable)]
public async Task<ActionResult> GetCompanyCertificateDocumentContentFileAsync([FromRoute] Guid documentId)
{
var (fileName, content, mediaType) = await _logic.GetCompanyCertificateDocumentAsync(documentId).ConfigureAwait(false);
return File(content, mediaType, fileName);
}

/// <summary>
/// Deletes the company certificate with the given id
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ public IAsyncEnumerable<CompanyCertificateBpnData> GetCompanyCertificateData(Gui
))
.SingleOrDefaultAsync();

public Task<(byte[] Content, string FileName, MediaTypeId MediaTypeId, bool Exists, bool IsStatusLocked)> GetCompanyCertificateDocumentDataAsync(Guid documentId, DocumentTypeId documentTypeId) =>
_context.Documents
.Where(x => x.Id == documentId &&
x.DocumentTypeId == documentTypeId)
.Select(x => new ValueTuple<byte[], string, MediaTypeId, bool, bool>(x.DocumentContent, x.DocumentName, x.MediaTypeId, true, x.DocumentStatusId == DocumentStatusId.LOCKED))
.SingleOrDefaultAsync();

public Task<(Guid DocumentId, DocumentStatusId DocumentStatusId, IEnumerable<Guid> CompanyCertificateId, bool IsSameCompany)> GetCompanyCertificateDocumentDetailsForIdUntrackedAsync(Guid documentId, Guid companyId) =>
_context.Documents
.AsNoTracking()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

using Org.Eclipse.TractusX.Portal.Backend.Framework.Models;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Entities;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Enums;
Expand Down Expand Up @@ -65,6 +64,13 @@ public interface ICompanyCertificateRepository
/// <returns>Returns an Pagination</returns>
Func<int, int, Task<Pagination.Source<CompanyCertificateData>?>> GetActiveCompanyCertificatePaginationSource(CertificateSorting? sorting, CompanyCertificateStatusId? certificateStatus, CompanyCertificateTypeId? certificateType, Guid companyId);

/// <summary>
/// Get the company certificate document data
/// </summary>
/// <param name="documentId">id of the document</param>
/// <returns>Returns the document data</returns>
Task<(byte[] Content, string FileName, MediaTypeId MediaTypeId, bool Exists, bool IsStatusLocked)> GetCompanyCertificateDocumentDataAsync(Guid documentId, DocumentTypeId documentTypeId);

Task<(Guid DocumentId, DocumentStatusId DocumentStatusId, IEnumerable<Guid> CompanyCertificateId, bool IsSameCompany)> GetCompanyCertificateDocumentDetailsForIdUntrackedAsync(Guid documentId, Guid companyId);

void AttachAndModifyCompanyCertificateDetails(Guid id, Action<CompanyCertificate>? initialize, Action<CompanyCertificate> updateFields);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class CompanyDataBusinessLogicTests
private readonly IIdentityData _identity;
private readonly Guid _traceabilityExternalTypeDetailId = Guid.NewGuid();
private readonly Guid _validCredentialId = Guid.NewGuid();
private static readonly Guid _validDocumentId = Guid.NewGuid();
private readonly IFixture _fixture;
private readonly IPortalRepositories _portalRepositories;
private readonly IConsentRepository _consentRepository;
Expand All @@ -55,7 +56,6 @@ public class CompanyDataBusinessLogicTests
private readonly IMailingService _mailingService;
private readonly ICustodianService _custodianService;
private readonly IDateTimeProvider _dateTimeProvider;

private readonly CompanyDataBusinessLogic _sut;
private readonly IIdentityService _identityService;

Expand Down Expand Up @@ -1670,6 +1670,55 @@ public async Task GetAllCompanyCertificatesAsync_WithSmallSize_GetsExpectedEntri

#endregion

#region GetCompanyCertificateDocuments

[Fact]
public async Task GetCompanyCertificateDocumentAsync_WithValidData_ReturnsExpected()
{
// Arrange
SetupFakesForGetDocument();

// Act
var result = await _sut.GetCompanyCertificateDocumentAsync(_validDocumentId).ConfigureAwait(false);

// Assert
result.Should().NotBeNull();
result.FileName.Should().Be("test.pdf");
result.MediaType.Should().Be("application/pdf");
}

[Fact]
public async Task GetCompanyCertificateDocumentAsync_WithNotExistingDocument_ThrowsNotFoundException()
{
// Arrange
var documentId = Guid.NewGuid();
SetupFakesForGetDocument();

// Act
async Task Act() => await _sut.GetCompanyCertificateDocumentAsync(documentId).ConfigureAwait(false);

// Assert
var ex = await Assert.ThrowsAsync<NotFoundException>(Act);
ex.Message.Should().Be($"Company certificate document {documentId} does not exist");
}

[Fact]
public async Task GetCompanyCertificateDocumentAsync_WithDocumentStatusIsNotLocked_ThrowsNotFoundException()
{
// Arrange
var documentId = new Guid("aaf53459-c36b-408e-a805-0b406ce9751d");
SetupFakesForGetDocument();

// Act
async Task Act() => await _sut.GetCompanyCertificateDocumentAsync(documentId).ConfigureAwait(false);

// Assert
var ex = await Assert.ThrowsAsync<ForbiddenException>(Act);
ex.Message.Should().Be($"Document {documentId} status is not locked");
}

#endregion

#region DeleteCompanyCertificates

[Fact]
Expand Down Expand Up @@ -1777,5 +1826,13 @@ private void SetupPagination(int count = 5)
A.CallTo(() => _portalRepositories.GetInstance<ICompanyCertificateRepository>()).Returns(_companyCertificateRepository);
}

private void SetupFakesForGetDocument()
{
var content = new byte[7];
A.CallTo(() => _companyCertificateRepository.GetCompanyCertificateDocumentDataAsync(_validDocumentId, DocumentTypeId.COMPANY_CERTIFICATE))
.ReturnsLazily(() => new ValueTuple<byte[], string, MediaTypeId, bool, bool>(content, "test.pdf", MediaTypeId.PDF, true, true));
A.CallTo(() => _companyCertificateRepository.GetCompanyCertificateDocumentDataAsync(new Guid("aaf53459-c36b-408e-a805-0b406ce9751d"), DocumentTypeId.COMPANY_CERTIFICATE))
.ReturnsLazily(() => new ValueTuple<byte[], string, MediaTypeId, bool, bool>(content, "test1.pdf", MediaTypeId.PDF, true, false));
}
#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,38 @@ public async Task GetCompanyCertificateData_NoResults_ReturnsExpected()

#endregion

#region GetCompanyCertificateDocumentContentFile

[Fact]
public async Task GetCompanyCertificateDocumentContentFile_WithValidData_ReturnsExpectedDocument()
{
// Arrange
var sut = await CreateSut().ConfigureAwait(false);

// Act
var result = await sut.GetCompanyCertificateDocumentDataAsync(new Guid("aaf53459-c36b-408e-a805-0b406ce9751f"), DocumentTypeId.COMPANY_CERTIFICATE).ConfigureAwait(false);

// Assert
result.Should().NotBe(default);
result.FileName.Should().Be("AdditionalServiceDetails2.pdf");
result.MediaTypeId.Should().Be(MediaTypeId.PDF);
}

[Fact]
public async Task GetCompanyCertificateDocumentContentFile_WithNotExistingDocument_ReturnsDefault()
{
// Arrange
var sut = await CreateSut().ConfigureAwait(false);

// Act
var result = await sut.GetCompanyCertificateDocumentDataAsync(Guid.NewGuid(), DocumentTypeId.COMPANY_CERTIFICATE).ConfigureAwait(false);

// Assert
result.Should().Be(default);
}

#endregion

#region DeleteCertificate

[Fact]
Expand Down

0 comments on commit 07cafc2

Please sign in to comment.