diff --git a/src/main/java/uk/gov/hmcts/reform/pip/account/management/database/UserRepository.java b/src/main/java/uk/gov/hmcts/reform/pip/account/management/database/UserRepository.java index 16c2119c..22e3705b 100644 --- a/src/main/java/uk/gov/hmcts/reform/pip/account/management/database/UserRepository.java +++ b/src/main/java/uk/gov/hmcts/reform/pip/account/management/database/UserRepository.java @@ -29,17 +29,20 @@ List findExistingByProvenanceId(@Param("provUserId") String provenanceUs nativeQuery = true) List getAccManDataForMI(); - @Query(value = "SELECT * FROM pi_user WHERE CAST(last_verified_date AS DATE) = CURRENT_DATE - (interval '1' day)" + " * :daysAgo AND user_provenance = 'PI_AAD' AND roles = 'VERIFIED'", nativeQuery = true) List findVerifiedUsersByLastVerifiedDate(@Param("daysAgo") int daysSinceLastVerified); + @Query(value = "SELECT * FROM pi_user WHERE user_provenance = 'PI_AAD' AND roles <> 'VERIFIED' AND " + + "CAST(last_signed_in_date AS DATE) = CURRENT_DATE - (interval '1' day) * :aadDays", nativeQuery = true) + List findAdminUsersFortNotificationByLastSignedInDate(@Param("aadDays") int aadNumberOfDays); + @Query(value = "SELECT * FROM pi_user WHERE (user_provenance = 'PI_AAD' AND roles <> 'VERIFIED' AND " + "CAST(last_signed_in_date AS DATE) <= CURRENT_DATE - (interval '1' day) * :aadDays) OR " - + "(user_provenance = 'SSO' AND :ssoDays > 0 AND " + + "(user_provenance = 'SSO' AND " + "CAST(last_signed_in_date AS DATE) <= CURRENT_DATE - (interval '1' day) * :ssoDays)", nativeQuery = true) - List findAdminUsersByLastSignedInDate(@Param("aadDays") int aadNumberOfDays, - @Param("ssoDays") int ssoNumberOfDays); + List findAdminUsersForDeletionByLastSignedInDate(@Param("aadDays") int aadNumberOfDays, + @Param("ssoDays") int ssoNumberOfDays); @Query(value = "SELECT * FROM pi_user WHERE (CAST(last_signed_in_date AS DATE) = CURRENT_DATE - (interval '1' day)" + " * :cftDaysAgo AND user_provenance = 'CFT_IDAM') " diff --git a/src/main/java/uk/gov/hmcts/reform/pip/account/management/service/InactiveAccountManagementService.java b/src/main/java/uk/gov/hmcts/reform/pip/account/management/service/InactiveAccountManagementService.java index 6778afd4..852a49e5 100644 --- a/src/main/java/uk/gov/hmcts/reform/pip/account/management/service/InactiveAccountManagementService.java +++ b/src/main/java/uk/gov/hmcts/reform/pip/account/management/service/InactiveAccountManagementService.java @@ -13,9 +13,6 @@ @Slf4j @Service public class InactiveAccountManagementService { - - private static final int SSO_ADMIN_ACCOUNT_SIGN_IN_NOTIFICATION_DAYS = 0; - private final UserRepository userRepository; private final AzureUserService azureUserService; private final PublicationService publicationService; @@ -79,9 +76,7 @@ public void sendMediaUsersForVerification() { * Then send their details on to publication services to send them a notification email. */ public void notifyAdminUsersToSignIn() { - userRepository.findAdminUsersByLastSignedInDate(aadAdminAccountSignInNotificationDays, - SSO_ADMIN_ACCOUNT_SIGN_IN_NOTIFICATION_DAYS - ) + userRepository.findAdminUsersFortNotificationByLastSignedInDate(aadAdminAccountSignInNotificationDays) .forEach(user -> { try { publicationService.sendInactiveAccountSignInNotificationEmail( @@ -126,7 +121,8 @@ public void findMediaAccountsForDeletion() { * Account service handles the deletion of their AAD, P&I user and subscriptions. */ public void findAdminAccountsForDeletion() { - userRepository.findAdminUsersByLastSignedInDate(aadAdminAccountDeletionDays, ssoAdminAccountDeletionDays) + userRepository.findAdminUsersForDeletionByLastSignedInDate(aadAdminAccountDeletionDays, + ssoAdminAccountDeletionDays) .forEach(user -> accountService.deleteAccount(user.getUserId())); } diff --git a/src/test/java/uk/gov/hmcts/reform/pip/account/management/service/InactiveAccountManagementServiceTest.java b/src/test/java/uk/gov/hmcts/reform/pip/account/management/service/InactiveAccountManagementServiceTest.java index 368d7eb6..ce81e6cb 100644 --- a/src/test/java/uk/gov/hmcts/reform/pip/account/management/service/InactiveAccountManagementServiceTest.java +++ b/src/test/java/uk/gov/hmcts/reform/pip/account/management/service/InactiveAccountManagementServiceTest.java @@ -106,7 +106,7 @@ void testNoMediaUsersForVerification() { @Test void testNotifyAdminUsersToSignIn() throws AzureCustomException { - when(userRepository.findAdminUsersByLastSignedInDate(anyInt(), eq(0))) + when(userRepository.findAdminUsersFortNotificationByLastSignedInDate(anyInt())) .thenReturn(Collections.singletonList(AAD_ADMIN_USER)); when(azureUserService.getUser(AAD_ADMIN_USER_EMAIL)).thenReturn(azureAdminUser); @@ -118,7 +118,7 @@ void testNotifyAdminUsersToSignIn() throws AzureCustomException { @Test void testNoNotificationOfAdminUsersToSignIn() { - when(userRepository.findAdminUsersByLastSignedInDate(anyInt(), anyInt())) + when(userRepository.findAdminUsersFortNotificationByLastSignedInDate(anyInt())) .thenReturn(Collections.emptyList()); inactiveAccountManagementService.notifyAdminUsersToSignIn(); @@ -169,7 +169,7 @@ void testNoMediaAccountDeletion() { @Test void testAdminAccountDeletion() { - when(userRepository.findAdminUsersByLastSignedInDate(anyInt(), anyInt())) + when(userRepository.findAdminUsersForDeletionByLastSignedInDate(anyInt(), anyInt())) .thenReturn(List.of(AAD_ADMIN_USER, SSO_ADMIN_USER)); inactiveAccountManagementService.findAdminAccountsForDeletion(); @@ -179,7 +179,7 @@ void testAdminAccountDeletion() { @Test void testNoAdminAccountDeletion() { - when(userRepository.findAdminUsersByLastSignedInDate(anyInt(), anyInt())) + when(userRepository.findAdminUsersForDeletionByLastSignedInDate(anyInt(), anyInt())) .thenReturn(Collections.emptyList()); inactiveAccountManagementService.findAdminAccountsForDeletion();