Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes admin reference clean up #290

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ Optional<String> determineQuestionResponseType(@Param("appId") Integer applicati
List<ApplicationFormsFoundView> 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<ApplicationFormEntity> findByCreatedByOrLastUpdateBy(Integer createdBy);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -33,4 +35,8 @@ public interface GrantAdvertRepository extends JpaRepository<GrantAdvert, UUID>
@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<GrantAdvert> findByCreatedByOrLastUpdatedBy(GrantAdmin admin);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});


}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Loading