Skip to content

Commit

Permalink
feat(companyid): get company certificate document by company userid (#…
Browse files Browse the repository at this point in the history
…522)

Refs: #463
Reviewed-By: Phil Schneider <[email protected]>
  • Loading branch information
AnuragNagpure authored Mar 4, 2024
1 parent 07cafc2 commit fa5073f
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,7 @@ public async IAsyncEnumerable<CompanyCertificateBpnData> GetCompanyCertificatesB
_settings.MaxPageSize,
_portalRepositories.GetInstance<ICompanyCertificateRepository>().GetActiveCompanyCertificatePaginationSource(sorting, certificateStatus, certificateType, _identityData.CompanyId));

/// <inheritdoc />
public async Task<int> DeleteCompanyCertificateAsync(Guid documentId)
{
var companyCertificateRepository = _portalRepositories.GetInstance<ICompanyCertificateRepository>();
Expand Down Expand Up @@ -646,6 +647,21 @@ public async Task<int> DeleteCompanyCertificateAsync(Guid documentId)
return await _portalRepositories.SaveAsync().ConfigureAwait(false);
}

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

if (!documentDetails.Exists)
{
throw new NotFoundException($"Company certificate document {documentId} does not exist");
}

return (documentDetails.FileName, documentDetails.Content, documentDetails.MediaTypeId.MapToMediaType());
}

/// <inheritdoc />
public async Task<(string FileName, byte[] Content, string MediaType)> GetCompanyCertificateDocumentAsync(Guid documentId)
{
Expand Down
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)> GetCompanyCertificateDocumentByCompanyIdAsync(Guid documentId);

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

Task<int> DeleteCompanyCertificateAsync(Guid documentId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,29 @@ 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 documentid and companyuserid.
/// </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/4ad087bb-80a1-49d3-9ba9-da0b175cd4e3</remarks>
/// <response code="200">Returns the file.</response>
/// <response code="404">The document was not found.</response>
[HttpGet]
[Route("companyCertificates/{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> GetCompanyCertificateSpecificDocumentContentFileAsync([FromRoute] Guid documentId)
{
var (fileName, content, mediaType) = await _logic.GetCompanyCertificateDocumentByCompanyIdAsync(documentId).ConfigureAwait(false);
return File(content, mediaType, fileName);
}

/// <summary>
/// Retrieves a specific company certificate document for the given id.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,12 @@ public void AttachAndModifyCompanyCertificateDocumentDetails(Guid id, Action<Doc
_context.Attach(entity);
updateFields.Invoke(entity);
}

public Task<(byte[] Content, string FileName, MediaTypeId MediaTypeId, bool Exists)> GetCompanyCertificateDocumentByCompanyIdDataAsync(Guid documentId, Guid companyId, DocumentTypeId documentTypeId) =>
_context.Documents
.Where(x => x.Id == documentId &&
x.DocumentTypeId == documentTypeId &&
x.CompanyUser!.Identity!.CompanyId == companyId)
.Select(x => new ValueTuple<byte[], string, MediaTypeId, bool>(x.DocumentContent, x.DocumentName, x.MediaTypeId, true))
.SingleOrDefaultAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,13 @@ public interface ICompanyCertificateRepository
void AttachAndModifyCompanyCertificateDetails(Guid id, Action<CompanyCertificate>? initialize, Action<CompanyCertificate> updateFields);

void AttachAndModifyCompanyCertificateDocumentDetails(Guid id, Action<Document>? initialize, Action<Document> updateFields);

/// <summary>
/// Get the company certificate document with own company id data
/// </summary>
/// <param name="documentId">id of the document</param>
/// <param name="companyId">id of the company user</param>
/// <param name="documentTypeId">document type id</param>
/// <returns>Returns the document data</returns>
Task<(byte[] Content, string FileName, MediaTypeId MediaTypeId, bool Exists)> GetCompanyCertificateDocumentByCompanyIdDataAsync(Guid documentId, Guid companyId, DocumentTypeId documentTypeId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -1670,6 +1670,40 @@ public async Task GetAllCompanyCertificatesAsync_WithSmallSize_GetsExpectedEntri

#endregion

#region GetCompanyCertificateDocumentByCompanyId

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

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

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

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

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

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

#endregion

#region GetCompanyCertificateDocuments

[Fact]
Expand Down Expand Up @@ -1829,10 +1863,13 @@ private void SetupPagination(int count = 5)
private void SetupFakesForGetDocument()
{
var content = new byte[7];
A.CallTo(() => _companyCertificateRepository.GetCompanyCertificateDocumentByCompanyIdDataAsync(_validDocumentId, _identity.CompanyId, DocumentTypeId.COMPANY_CERTIFICATE))
.ReturnsLazily(() => new ValueTuple<byte[], string, MediaTypeId, bool>(content, "test.pdf", MediaTypeId.PDF, true));
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 GetCompanyCertificateDocumentByCompanyUserIdContentFile

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

// Act
var result = await sut.GetCompanyCertificateDocumentByCompanyIdDataAsync(new Guid("aaf53459-c36b-408e-a805-0b406ce9752d"), new Guid("41fd2ab8-71cd-4546-9bef-a388d91b2542"), DocumentTypeId.COMPANY_CERTIFICATE).ConfigureAwait(false);

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

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

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

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

#endregion

#region GetCompanyCertificateDocumentContentFile

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,14 @@
"company_certificate_status_id": 2,
"company_id": "2dc4249f-b5ca-4d42-bef1-7a7a950a4f87",
"document_id": "88793f9f-c5a4-4621-847b-3d47cd839283"
},
{
"id": "9f5b9934-4014-4099-91e9-7b1aee696c13",
"valid_from": "2023-06-07 00:00:00.000000 +00:00",
"valid_till": "2024-04-26 00:00:00.000000 +00:00",
"company_certificate_type_id": 6,
"company_certificate_status_id": 1,
"company_id": "41fd2ab8-71cd-4546-9bef-a388d91b2542",
"document_id": "aaf53459-c36b-408e-a805-0b406ce9752d"
}
]
Loading

0 comments on commit fa5073f

Please sign in to comment.