From df43de9e7ea7ba18fd2c248ddc5633faea8bac0a Mon Sep 17 00:00:00 2001 From: Richard Mohammed Date: Tue, 12 May 2026 13:08:21 +0100 Subject: [PATCH 1/4] Add gapId to ExportedSubmissionsDto and implement mapping in CustomGrantExportMapper - Added a new field `gapId` to the `ExportedSubmissionsDto` class. - Updated the `CustomGrantExportMapperImpl` to map the `gapId` from `GrantExportEntity`. - Enhanced the `GrantExportMapper` interface to include the new mapping method for `gapId`. --- .../dtos/grantExport/ExportedSubmissionsDto.java | 1 + .../mappers/CustomGrantExportMapperImpl.java | 14 ++++++++++++++ .../adminbackend/mappers/GrantExportMapper.java | 5 +++++ 3 files changed, 20 insertions(+) diff --git a/src/main/java/gov/cabinetoffice/gap/adminbackend/dtos/grantExport/ExportedSubmissionsDto.java b/src/main/java/gov/cabinetoffice/gap/adminbackend/dtos/grantExport/ExportedSubmissionsDto.java index 9f082540..9a5a26e2 100644 --- a/src/main/java/gov/cabinetoffice/gap/adminbackend/dtos/grantExport/ExportedSubmissionsDto.java +++ b/src/main/java/gov/cabinetoffice/gap/adminbackend/dtos/grantExport/ExportedSubmissionsDto.java @@ -13,6 +13,7 @@ @Builder public class ExportedSubmissionsDto { + private String gapId; private String name; private String zipFileLocation; private UUID submissionId; diff --git a/src/main/java/gov/cabinetoffice/gap/adminbackend/mappers/CustomGrantExportMapperImpl.java b/src/main/java/gov/cabinetoffice/gap/adminbackend/mappers/CustomGrantExportMapperImpl.java index d3d04e2f..076f9d53 100644 --- a/src/main/java/gov/cabinetoffice/gap/adminbackend/mappers/CustomGrantExportMapperImpl.java +++ b/src/main/java/gov/cabinetoffice/gap/adminbackend/mappers/CustomGrantExportMapperImpl.java @@ -61,6 +61,7 @@ public ExportedSubmissionsDto grantExportEntityToExportedSubmissions(GrantExport exportedSubmissionsDto.name(mapExportedSubmissionName(grantExportEntity)); exportedSubmissionsDto.submittedDate(mapExportedSubmissionSubmittedDate(grantExportEntity)); exportedSubmissionsDto.submissionName(mapExportedSubmissionSubmissionName(grantExportEntity)); + exportedSubmissionsDto.gapId(mapExportedSubmissionGapId(grantExportEntity)); return exportedSubmissionsDto.build(); } @@ -111,6 +112,19 @@ public String mapExportedSubmissionSubmissionName(GrantExportEntity grantExportE return submission.get().getSubmissionName(); } + @Override + public String mapExportedSubmissionGapId(GrantExportEntity grantExportEntity) { + log.info("Getting gap ID from grant export {} and submission {}", grantExportEntity.getId(), + grantExportEntity.getId().getSubmissionId()); + final UUID submissionId = grantExportEntity.getId().getSubmissionId(); + final Optional submission = submissionRepository.findById(submissionId); + if (submission.isEmpty()) { + log.error("Submission not found for id: {}", submissionId); + return null; + } + return submission.get().getGapId(); + } + private UUID grantExportEntityIdSubmissionId(GrantExportEntity grantExportEntity) { if (grantExportEntity == null) { return null; diff --git a/src/main/java/gov/cabinetoffice/gap/adminbackend/mappers/GrantExportMapper.java b/src/main/java/gov/cabinetoffice/gap/adminbackend/mappers/GrantExportMapper.java index 94dc4194..e62eb728 100644 --- a/src/main/java/gov/cabinetoffice/gap/adminbackend/mappers/GrantExportMapper.java +++ b/src/main/java/gov/cabinetoffice/gap/adminbackend/mappers/GrantExportMapper.java @@ -22,6 +22,7 @@ public interface GrantExportMapper { @Mapping(target ="status", source = "status") @Mapping(target = "submittedDate", expression = "java(mapExportedSubmissionSubmittedDate(grantExportEntity))") @Mapping(target = "submissionName", expression = "java(mapExportedSubmissionSubmissionName(grantExportEntity))") + @Mapping(target = "gapId", expression = "java(mapExportedSubmissionGapId(grantExportEntity))") ExportedSubmissionsDto grantExportEntityToExportedSubmissions(GrantExportEntity grantExportEntity); default String mapExportedSubmissionName(GrantExportEntity grantExportEntity) { @@ -36,4 +37,8 @@ default String mapExportedSubmissionSubmissionName(GrantExportEntity grantExport return ""; } + default String mapExportedSubmissionGapId(GrantExportEntity grantExportEntity) { + return ""; + } + } From 5b0a1f59997513142b666663c3e52117295dfc81 Mon Sep 17 00:00:00 2001 From: Richard Mohammed Date: Mon, 1 Jun 2026 10:56:15 +0100 Subject: [PATCH 2/4] Enhance GrantExport functionality with sorting and pagination support --- .../controllers/GrantExportController.java | 17 +++++++++++++++-- .../repositories/GrantExportRepository.java | 5 +++++ .../services/GrantExportService.java | 5 +---- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/main/java/gov/cabinetoffice/gap/adminbackend/controllers/GrantExportController.java b/src/main/java/gov/cabinetoffice/gap/adminbackend/controllers/GrantExportController.java index 3353909f..a9064472 100644 --- a/src/main/java/gov/cabinetoffice/gap/adminbackend/controllers/GrantExportController.java +++ b/src/main/java/gov/cabinetoffice/gap/adminbackend/controllers/GrantExportController.java @@ -24,11 +24,15 @@ import lombok.RequiredArgsConstructor; import lombok.extern.log4j.Log4j2; import org.springdoc.core.converters.models.PageableAsQueryParam; +import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import java.util.Map; + import java.util.UUID; @Log4j2 @@ -120,13 +124,22 @@ public ResponseEntity getRemainingExportsCount(@PathV @PageableAsQueryParam public ResponseEntity getSubmissions(@PathVariable final UUID exportId, @RequestParam(name = "grabOnlyFailed", required = false, - defaultValue = "false") final boolean grabOnlyFailed, final Pageable pagination) { + defaultValue = "false") final boolean grabOnlyFailed, + @RequestParam(name = "sortField", required = false, defaultValue = "submittedDate") final String sortField, + @RequestParam(name = "sortDir", required = false, defaultValue = "asc") final String sortDir, + final Pageable pagination) { log.info("Getting submissions for exportId: {}, grabOnlyFailed : {}", exportId, grabOnlyFailed); + final Map allowedSortFields = Map.of("gapId", "s.gapId", "submittedDate", "s.submittedDate"); + final String jpqlSortField = allowedSortFields.getOrDefault(sortField, "s.submittedDate"); + final Sort.Direction direction = Sort.Direction.fromOptionalString(sortDir).orElse(Sort.Direction.ASC); + final Pageable sortedPagination = PageRequest.of(pagination.getPageNumber(), pagination.getPageSize(), + Sort.by(direction, jpqlSortField)); + final GrantExportStatus status = grabOnlyFailed ? GrantExportStatus.FAILED : GrantExportStatus.COMPLETE; final String superZipLocation = grantExportBatchService.getSuperZipLocation(exportId); final ExportedSubmissionsListDto exportedSubmissionsListDto = exportService - .generateExportedSubmissionsListDto(exportId, status, pagination, superZipLocation); + .generateExportedSubmissionsListDto(exportId, status, sortedPagination, superZipLocation); return ResponseEntity.ok() .header("cache-control", "private, no-cache, max-age=0, must-revalidate") diff --git a/src/main/java/gov/cabinetoffice/gap/adminbackend/repositories/GrantExportRepository.java b/src/main/java/gov/cabinetoffice/gap/adminbackend/repositories/GrantExportRepository.java index c280a9e4..45b36f34 100644 --- a/src/main/java/gov/cabinetoffice/gap/adminbackend/repositories/GrantExportRepository.java +++ b/src/main/java/gov/cabinetoffice/gap/adminbackend/repositories/GrantExportRepository.java @@ -49,4 +49,9 @@ void updateExportRecordLocation(@Param("submissionId") UUID submissionId, List findByCreatedByAndId_ExportBatchIdAndStatus(Integer createdBy, UUID exportBatchId, GrantExportStatus status, Pageable pageable); + @Query("SELECT ge FROM GrantExportEntity ge JOIN Submission s ON s.id = ge.id.submissionId " + + "WHERE ge.createdBy = :createdBy AND ge.id.exportBatchId = :exportBatchId AND ge.status = :status") + List findByCreatedByAndExportBatchIdAndStatusSorted(@Param("createdBy") Integer createdBy, + @Param("exportBatchId") UUID exportBatchId, @Param("status") GrantExportStatus status, Pageable pageable); + } diff --git a/src/main/java/gov/cabinetoffice/gap/adminbackend/services/GrantExportService.java b/src/main/java/gov/cabinetoffice/gap/adminbackend/services/GrantExportService.java index ba7fb1ad..4157bcc2 100644 --- a/src/main/java/gov/cabinetoffice/gap/adminbackend/services/GrantExportService.java +++ b/src/main/java/gov/cabinetoffice/gap/adminbackend/services/GrantExportService.java @@ -1,6 +1,5 @@ package gov.cabinetoffice.gap.adminbackend.services; -import gov.cabinetoffice.gap.adminbackend.dtos.grantExport.ExportedSubmissionsDto; import gov.cabinetoffice.gap.adminbackend.dtos.grantExport.ExportedSubmissionsListDto; import gov.cabinetoffice.gap.adminbackend.dtos.grantExport.GrantExportDTO; import gov.cabinetoffice.gap.adminbackend.dtos.grantExport.GrantExportListDTO; @@ -20,7 +19,6 @@ import java.util.List; import java.util.UUID; -import static java.util.Comparator.comparing; @Service @RequiredArgsConstructor @@ -62,7 +60,7 @@ public ExportedSubmissionsListDto generateExportedSubmissionsListDto(UUID export final AdminSession adminSession = HelperUtils.getAdminSessionForAuthenticatedUser(); log.info("Grabbing list of grant-export with id {} and status {}", exportId,status.toString()); - final List grantExports = exportRepository.findByCreatedByAndId_ExportBatchIdAndStatus(adminSession.getGrantAdminId(), + final List grantExports = exportRepository.findByCreatedByAndExportBatchIdAndStatusSorted(adminSession.getGrantAdminId(), exportId, status, pagination); log.info("Found {} grant-exports", grantExports.size()); @@ -74,7 +72,6 @@ public ExportedSubmissionsListDto generateExportedSubmissionsListDto(UUID export .superZipFileLocation(superZipLocation) .exportedSubmissions(grantExports.stream() .map(customGrantExportMapper::grantExportEntityToExportedSubmissions) - .sorted(comparing(ExportedSubmissionsDto::getSubmittedDate)) .toList()) .build(); From 4be475b2eadc832e351655bbe650c5557effe66e Mon Sep 17 00:00:00 2001 From: Richard Mohammed Date: Mon, 1 Jun 2026 18:13:58 +0100 Subject: [PATCH 3/4] Refactor GrantExportServiceTest to update repository method calls for improved clarity and consistency. Changed method names to reflect accurate parameters and enhance test readability. --- .../gap/adminbackend/services/GrantExportServiceTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/gov/cabinetoffice/gap/adminbackend/services/GrantExportServiceTest.java b/src/test/java/gov/cabinetoffice/gap/adminbackend/services/GrantExportServiceTest.java index 55e31be1..3ef5af44 100644 --- a/src/test/java/gov/cabinetoffice/gap/adminbackend/services/GrantExportServiceTest.java +++ b/src/test/java/gov/cabinetoffice/gap/adminbackend/services/GrantExportServiceTest.java @@ -227,7 +227,7 @@ void generateExportedSubmissionsListDto() { .submissionName("Application Name 1") .build(); - when(exportRepository.findByCreatedByAndId_ExportBatchIdAndStatus(SEC_CONTEXT_ADMIN_ID,mockExportId, GrantExportStatus.COMPLETE, pagination)) + when(exportRepository.findByCreatedByAndExportBatchIdAndStatusSorted(SEC_CONTEXT_ADMIN_ID,mockExportId, GrantExportStatus.COMPLETE, pagination)) .thenReturn(List.of(grantExport, grantExport2)); when(exportRepository.countByIdExportBatchIdAndStatus(mockExportId, GrantExportStatus.COMPLETE)) .thenReturn(2L); @@ -242,7 +242,7 @@ void generateExportedSubmissionsListDto() { final ExportedSubmissionsListDto response = grantExportService .generateExportedSubmissionsListDto(mockExportId, GrantExportStatus.COMPLETE, pagination, "superZip"); - verify(exportRepository).findByCreatedByAndId_ExportBatchIdAndStatus(SEC_CONTEXT_ADMIN_ID,mockExportId, GrantExportStatus.COMPLETE, pagination); + verify(exportRepository).findByCreatedByAndExportBatchIdAndStatusSorted(SEC_CONTEXT_ADMIN_ID,mockExportId, GrantExportStatus.COMPLETE, pagination); assertThat(response.getGrantExportId()).isEqualTo(mockExportId); assertThat(response.getExportedSubmissions().get(0)) .isEqualTo(exportedSubmissionsDto2); From 4ce77101f6e5a84befe9d283c8e4bf5ee08447e2 Mon Sep 17 00:00:00 2001 From: Richard Mohammed Date: Mon, 1 Jun 2026 18:17:02 +0100 Subject: [PATCH 4/4] Fix assertions in GrantExportServiceTest to correctly validate exported submissions. Updated expected values to ensure accurate test results and maintain consistency in test logic. --- .../gap/adminbackend/services/GrantExportServiceTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/gov/cabinetoffice/gap/adminbackend/services/GrantExportServiceTest.java b/src/test/java/gov/cabinetoffice/gap/adminbackend/services/GrantExportServiceTest.java index 3ef5af44..43480dc3 100644 --- a/src/test/java/gov/cabinetoffice/gap/adminbackend/services/GrantExportServiceTest.java +++ b/src/test/java/gov/cabinetoffice/gap/adminbackend/services/GrantExportServiceTest.java @@ -245,9 +245,9 @@ void generateExportedSubmissionsListDto() { verify(exportRepository).findByCreatedByAndExportBatchIdAndStatusSorted(SEC_CONTEXT_ADMIN_ID,mockExportId, GrantExportStatus.COMPLETE, pagination); assertThat(response.getGrantExportId()).isEqualTo(mockExportId); assertThat(response.getExportedSubmissions().get(0)) - .isEqualTo(exportedSubmissionsDto2); + .isEqualTo(exportedSubmissionsDto); assertThat(response.getExportedSubmissions().get(1)) - .isEqualTo(exportedSubmissionsDto); + .isEqualTo(exportedSubmissionsDto2); assertThat(response.getSuperZipFileLocation()).isEqualTo("superZip"); assertThat(response.getSuccessCount()).isEqualTo(2); assertThat(response.getFailedCount()).isEqualTo(0);