diff --git a/src/main/java/gov/cabinetoffice/gap/adminbackend/repositories/ApplicationFormRepository.java b/src/main/java/gov/cabinetoffice/gap/adminbackend/repositories/ApplicationFormRepository.java index 6e9232d3..7b0e5386 100644 --- a/src/main/java/gov/cabinetoffice/gap/adminbackend/repositories/ApplicationFormRepository.java +++ b/src/main/java/gov/cabinetoffice/gap/adminbackend/repositories/ApplicationFormRepository.java @@ -36,4 +36,6 @@ Optional determineQuestionResponseType(@Param("appId") Integer applicati List findMatchingApplicationForm(@Param("applicationId") Integer applicationId, @Param("applicationName") String applicationName, @Param("grantSchemeId") Integer grantSchemeId); + @Query("select a from ApplicationFormEntity a where a.createdBy = ?1 or a.lastUpdateBy = ?1") + List findByCreatedByOrLastUpdateBy(Integer createdBy); } diff --git a/src/main/java/gov/cabinetoffice/gap/adminbackend/repositories/GrantAdvertRepository.java b/src/main/java/gov/cabinetoffice/gap/adminbackend/repositories/GrantAdvertRepository.java index 3e70a6ce..fd5952dd 100644 --- a/src/main/java/gov/cabinetoffice/gap/adminbackend/repositories/GrantAdvertRepository.java +++ b/src/main/java/gov/cabinetoffice/gap/adminbackend/repositories/GrantAdvertRepository.java @@ -1,5 +1,6 @@ package gov.cabinetoffice.gap.adminbackend.repositories; +import gov.cabinetoffice.gap.adminbackend.entities.GrantAdmin; import gov.cabinetoffice.gap.adminbackend.entities.GrantAdvert; import org.springframework.data.jpa.repository.EntityGraph; import org.springframework.data.jpa.repository.JpaRepository; @@ -9,6 +10,7 @@ import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; +import java.util.List; import java.util.Optional; import java.util.UUID; @@ -33,4 +35,8 @@ public interface GrantAdvertRepository extends JpaRepository @Query("DELETE FROM GrantAdvert g WHERE g.id = :id " + "AND EXISTS (SELECT 1 FROM g.scheme.grantAdmins ga WHERE ga.id = :grantAdminId)") int deleteByIdAndSchemeEditor(UUID id, Integer grantAdminId); + + @Query("select g from GrantAdvert g where g.createdBy = ?1 or g.lastUpdatedBy = ?1") + List findByCreatedByOrLastUpdatedBy(GrantAdmin admin); + } diff --git a/src/main/java/gov/cabinetoffice/gap/adminbackend/services/ApplicationFormService.java b/src/main/java/gov/cabinetoffice/gap/adminbackend/services/ApplicationFormService.java index 0745d37d..4beddada 100644 --- a/src/main/java/gov/cabinetoffice/gap/adminbackend/services/ApplicationFormService.java +++ b/src/main/java/gov/cabinetoffice/gap/adminbackend/services/ApplicationFormService.java @@ -410,9 +410,9 @@ public ApplicationStatusEnum getApplicationStatus(Integer applicationId) { .getApplicationStatus(); } - public void removeAdminReferenceBySchemeId(GrantAdmin grantAdmin, Integer schemeId) { - applicationFormRepository.findByGrantSchemeId(schemeId) - .ifPresent(application -> { + public void removeAdminReferenceBySchemeId(GrantAdmin grantAdmin) { + applicationFormRepository.findByCreatedByOrLastUpdateBy(grantAdmin.getId()) + .forEach(application -> { if (Objects.equals(application.getLastUpdateBy(), grantAdmin.getId())) { application.setLastUpdateBy(null); } diff --git a/src/main/java/gov/cabinetoffice/gap/adminbackend/services/GrantAdvertService.java b/src/main/java/gov/cabinetoffice/gap/adminbackend/services/GrantAdvertService.java index ae29e75f..276be0a4 100644 --- a/src/main/java/gov/cabinetoffice/gap/adminbackend/services/GrantAdvertService.java +++ b/src/main/java/gov/cabinetoffice/gap/adminbackend/services/GrantAdvertService.java @@ -611,18 +611,18 @@ public void updateAdvertOwner(Integer adminId, Integer schemeId) { }); } - public void removeAdminReferenceBySchemeId(GrantAdmin grantAdmin, Integer schemeId) { - grantAdvertRepository.findBySchemeId(schemeId) - .ifPresent(advert -> { - if (advert.getLastUpdatedBy() != null && advert.getLastUpdatedBy() == grantAdmin) { - advert.setLastUpdatedBy(null); - } - if (advert.getCreatedBy() != null && advert.getCreatedBy() == grantAdmin) { - advert.setCreatedBy(null); - } + public void removeAdminReferenceBySchemeId(GrantAdmin grantAdmin) { - grantAdvertRepository.save(advert); - }); + grantAdvertRepository.findByCreatedByOrLastUpdatedBy(grantAdmin).forEach(advert -> { + if (advert.getLastUpdatedBy() != null && advert.getLastUpdatedBy() == grantAdmin) { + advert.setLastUpdatedBy(null); + } + if (advert.getCreatedBy() != null && advert.getCreatedBy() == grantAdmin) { + advert.setCreatedBy(null); + } + + grantAdvertRepository.save(advert); + }); } public static void validateAdvertStatus(GrantAdvert grantAdvert) { diff --git a/src/main/java/gov/cabinetoffice/gap/adminbackend/services/SchemeService.java b/src/main/java/gov/cabinetoffice/gap/adminbackend/services/SchemeService.java index 0257a4c9..509fbbaa 100644 --- a/src/main/java/gov/cabinetoffice/gap/adminbackend/services/SchemeService.java +++ b/src/main/java/gov/cabinetoffice/gap/adminbackend/services/SchemeService.java @@ -203,14 +203,13 @@ public void removeAdminReference(String userSub) { } scheme.removeAdmin(grantAdmin); - grantAdvertService.removeAdminReferenceBySchemeId(grantAdmin, scheme.getId()); - applicationFormService.removeAdminReferenceBySchemeId(grantAdmin, scheme.getId()); } + grantAdvertService.removeAdminReferenceBySchemeId(grantAdmin); + applicationFormService.removeAdminReferenceBySchemeId(grantAdmin); schemeRepo.saveAll(schemes); }); - } diff --git a/src/test/java/gov/cabinetoffice/gap/adminbackend/services/GrantAdvertServiceTest.java b/src/test/java/gov/cabinetoffice/gap/adminbackend/services/GrantAdvertServiceTest.java index ae2ad6c2..2ffe88ba 100644 --- a/src/test/java/gov/cabinetoffice/gap/adminbackend/services/GrantAdvertServiceTest.java +++ b/src/test/java/gov/cabinetoffice/gap/adminbackend/services/GrantAdvertServiceTest.java @@ -1362,9 +1362,10 @@ void testRemoveAdminReferenceBySchemeId() { GrantAdvert grantAdvert = RandomGrantAdvertGenerators.randomGrantAdvertEntity().build(); - when(grantAdvertRepository.findBySchemeId(anyInt())).thenReturn(Optional.of(grantAdvert)); + when(grantAdvertRepository.findByCreatedByOrLastUpdatedBy(grantAdmin)) + .thenReturn(Collections.singletonList(grantAdvert)); - grantAdvertService.removeAdminReferenceBySchemeId(grantAdmin, 1); + grantAdvertService.removeAdminReferenceBySchemeId(grantAdmin); verify(grantAdvertRepository).save(grantAdvert); } diff --git a/src/test/java/gov/cabinetoffice/gap/adminbackend/services/SchemeServiceTest.java b/src/test/java/gov/cabinetoffice/gap/adminbackend/services/SchemeServiceTest.java index d9b819ed..683fd34a 100644 --- a/src/test/java/gov/cabinetoffice/gap/adminbackend/services/SchemeServiceTest.java +++ b/src/test/java/gov/cabinetoffice/gap/adminbackend/services/SchemeServiceTest.java @@ -564,7 +564,7 @@ void testRemoveAdminReferenceWhenUserExists() { schemeService.removeAdminReference("userSub"); verify(schemeRepository).saveAll(schemes); - verify(grantAdvertService, times(1)).removeAdminReferenceBySchemeId(grantAdmin, 1); - verify(applicationFormService, times(1)).removeAdminReferenceBySchemeId(grantAdmin, 1); + verify(grantAdvertService, times(1)).removeAdminReferenceBySchemeId(grantAdmin); + verify(applicationFormService, times(1)).removeAdminReferenceBySchemeId(grantAdmin); } } \ No newline at end of file