From 06446d5824f3c0f972b131392270a10a63af4e1f Mon Sep 17 00:00:00 2001 From: Krishna Bottla <40598480+kbottla@users.noreply.github.com> Date: Tue, 7 Nov 2023 14:15:48 +0000 Subject: [PATCH] PP-11205 Detach users from service - Removes users from service when service is archived. --- ...xpungeAndArchiveHistoricalDataService.java | 12 +++++++ .../persistence/dao/ServiceRoleDao.java | 7 ++++ ...geAndArchiveHistoricalDataServiceTest.java | 8 ++++- .../persistence/dao/ServiceRoleDaoIT.java | 32 +++++++++++++++++-- 4 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/main/java/uk/gov/pay/adminusers/expungeandarchive/service/ExpungeAndArchiveHistoricalDataService.java b/src/main/java/uk/gov/pay/adminusers/expungeandarchive/service/ExpungeAndArchiveHistoricalDataService.java index 074861e66..a822536ca 100644 --- a/src/main/java/uk/gov/pay/adminusers/expungeandarchive/service/ExpungeAndArchiveHistoricalDataService.java +++ b/src/main/java/uk/gov/pay/adminusers/expungeandarchive/service/ExpungeAndArchiveHistoricalDataService.java @@ -11,6 +11,7 @@ import uk.gov.pay.adminusers.persistence.dao.ForgottenPasswordDao; import uk.gov.pay.adminusers.persistence.dao.InviteDao; import uk.gov.pay.adminusers.persistence.dao.ServiceDao; +import uk.gov.pay.adminusers.persistence.dao.ServiceRoleDao; import uk.gov.pay.adminusers.persistence.dao.UserDao; import uk.gov.pay.adminusers.persistence.entity.ServiceEntity; @@ -25,6 +26,7 @@ import static java.time.ZoneOffset.UTC; import static java.time.temporal.ChronoUnit.DAYS; import static net.logstash.logback.argument.StructuredArguments.kv; +import static uk.gov.service.payments.logging.LoggingKeys.SERVICE_EXTERNAL_ID; public class ExpungeAndArchiveHistoricalDataService { @@ -33,6 +35,7 @@ public class ExpungeAndArchiveHistoricalDataService { private final InviteDao inviteDao; private final ForgottenPasswordDao forgottenPasswordDao; private final ServiceDao serviceDao; + private final ServiceRoleDao serviceRoleDao; private final LedgerService ledgerService; private final ExpungeAndArchiveDataConfig expungeAndArchiveDataConfig; private final Clock clock; @@ -47,6 +50,7 @@ public class ExpungeAndArchiveHistoricalDataService { public ExpungeAndArchiveHistoricalDataService(UserDao userDao, InviteDao inviteDao, ForgottenPasswordDao forgottenPasswordDao, ServiceDao serviceDao, + ServiceRoleDao serviceRoleDao, LedgerService ledgerService, AdminUsersConfig adminUsersConfig, Clock clock) { @@ -54,6 +58,7 @@ public ExpungeAndArchiveHistoricalDataService(UserDao userDao, InviteDao inviteD this.inviteDao = inviteDao; this.forgottenPasswordDao = forgottenPasswordDao; this.serviceDao = serviceDao; + this.serviceRoleDao = serviceRoleDao; this.ledgerService = ledgerService; expungeAndArchiveDataConfig = adminUsersConfig.getExpungeAndArchiveDataConfig(); this.clock = clock; @@ -98,12 +103,19 @@ private int archiveServices() { serviceEntity.setArchived(true); serviceDao.merge(serviceEntity); + detachUsers(serviceEntity); + + LOGGER.info("Archived service", kv(SERVICE_EXTERNAL_ID, serviceEntity.getExternalId())); } }); return numberOfServicesArchived.get(); } + private void detachUsers(ServiceEntity serviceEntity) { + serviceRoleDao.removeUsersFromService(serviceEntity.getId()); + } + private boolean canArchiveService(ServiceEntity serviceEntity) { Optional mayBeLastTransactionDateForService = getLastTransactionDateForService(serviceEntity); ZonedDateTime archiveServicesBeforeDate = getArchiveServicesBeforeDate(); diff --git a/src/main/java/uk/gov/pay/adminusers/persistence/dao/ServiceRoleDao.java b/src/main/java/uk/gov/pay/adminusers/persistence/dao/ServiceRoleDao.java index 6a4210dd0..a71e0fc37 100644 --- a/src/main/java/uk/gov/pay/adminusers/persistence/dao/ServiceRoleDao.java +++ b/src/main/java/uk/gov/pay/adminusers/persistence/dao/ServiceRoleDao.java @@ -14,4 +14,11 @@ public class ServiceRoleDao extends JpaDao { /* default */ ServiceRoleDao(Provider entityManager) { super(entityManager, ServiceRoleEntity.class); } + + public void removeUsersFromService(Integer serviceId) { + entityManager.get() + .createQuery("DELETE from ServiceRoleEntity where service.id = :serviceId") + .setParameter("serviceId", serviceId) + .executeUpdate(); + } } diff --git a/src/test/java/uk/gov/pay/adminusers/expungeandarchive/service/ExpungeAndArchiveHistoricalDataServiceTest.java b/src/test/java/uk/gov/pay/adminusers/expungeandarchive/service/ExpungeAndArchiveHistoricalDataServiceTest.java index 5a6080ce2..763dfdabe 100644 --- a/src/test/java/uk/gov/pay/adminusers/expungeandarchive/service/ExpungeAndArchiveHistoricalDataServiceTest.java +++ b/src/test/java/uk/gov/pay/adminusers/expungeandarchive/service/ExpungeAndArchiveHistoricalDataServiceTest.java @@ -23,6 +23,7 @@ import uk.gov.pay.adminusers.persistence.dao.ForgottenPasswordDao; import uk.gov.pay.adminusers.persistence.dao.InviteDao; import uk.gov.pay.adminusers.persistence.dao.ServiceDao; +import uk.gov.pay.adminusers.persistence.dao.ServiceRoleDao; import uk.gov.pay.adminusers.persistence.dao.UserDao; import uk.gov.pay.adminusers.persistence.entity.GatewayAccountIdEntity; import uk.gov.pay.adminusers.persistence.entity.ServiceEntity; @@ -60,6 +61,9 @@ class ExpungeAndArchiveHistoricalDataServiceTest { @Mock ServiceDao mockServiceDao; + @Mock + ServiceRoleDao mockServiceRoleDao; + @Mock LedgerService mockLedgerService; @@ -90,7 +94,7 @@ void setUp() { clock = Clock.fixed(Instant.parse(SYSTEM_INSTANT), UTC); when(mockAdminUsersConfig.getExpungeAndArchiveDataConfig()).thenReturn(mockExpungeAndArchiveConfig); expungeAndArchiveHistoricalDataService = new ExpungeAndArchiveHistoricalDataService(mockUserDao, - mockInviteDao, mockForgottenPasswordDao, mockServiceDao, mockLedgerService, mockAdminUsersConfig, clock); + mockInviteDao, mockForgottenPasswordDao, mockServiceDao, mockServiceRoleDao, mockLedgerService, mockAdminUsersConfig, clock); } @Test @@ -198,6 +202,7 @@ void shouldArchiveService_WhenTheLastTransactionDateIsBeforeTheServicesEligibleF assertTrue(serviceEntity.isArchived()); verify(mockServiceDao).merge(serviceEntity); + verify(mockServiceRoleDao).removeUsersFromService(serviceEntity.getId()); } @Test @@ -221,6 +226,7 @@ void shouldArchiveServiceWithoutTransactionsButCreatedBeforeTheServicesEligibleF assertTrue(serviceEntity.isArchived()); verify(mockServiceDao).merge(serviceEntity); + verify(mockServiceRoleDao).removeUsersFromService(serviceEntity.getId()); } @Test diff --git a/src/test/java/uk/gov/pay/adminusers/persistence/dao/ServiceRoleDaoIT.java b/src/test/java/uk/gov/pay/adminusers/persistence/dao/ServiceRoleDaoIT.java index 58f098bf4..e39ede501 100644 --- a/src/test/java/uk/gov/pay/adminusers/persistence/dao/ServiceRoleDaoIT.java +++ b/src/test/java/uk/gov/pay/adminusers/persistence/dao/ServiceRoleDaoIT.java @@ -17,7 +17,7 @@ import static org.hamcrest.core.Is.is; import static uk.gov.pay.adminusers.app.util.RandomIdGenerator.randomUuid; -public class ServiceRoleDaoIT extends DaoTestBase { +class ServiceRoleDaoIT extends DaoTestBase { private ServiceRoleDao serviceRoleDao; @@ -27,7 +27,7 @@ public void before() { } @Test - public void shouldRemoveAServiceRoleOfAUserSuccessfully() { + void shouldRemoveAServiceRoleOfAUserSuccessfully() { Service service = ServiceDbFixture.serviceDbFixture(databaseHelper).insertService(); int roleId = RoleDbFixture.roleDbFixture(databaseHelper).insertRole().getId(); @@ -53,4 +53,32 @@ public void shouldRemoveAServiceRoleOfAUserSuccessfully() { assertThat(serviceRolesAfterRemove.size(), is(0)); } + + @Test + void removeUsersFromService_shouldRemoveUsersForAGivenServiceId() { + Service service1 = ServiceDbFixture.serviceDbFixture(databaseHelper).insertService(); + int roleId = RoleDbFixture.roleDbFixture(databaseHelper).insertRole().getId(); + + User userThatShouldBeDeletedFromServiceRoles = UserDbFixture.userDbFixture(databaseHelper) + .withServiceRole(service1, roleId) + .insertUser(); + + Service service2 = ServiceDbFixture.serviceDbFixture(databaseHelper).insertService(); + User userThatShouldNotBeDeleted = UserDbFixture.userDbFixture(databaseHelper) + .withServiceRole(service2, roleId) + .insertUser(); + + List> serviceRoles = databaseHelper.findServiceRoleForUser(userThatShouldBeDeletedFromServiceRoles.getId()); + assertThat(serviceRoles.size(), is(1)); + serviceRoles = databaseHelper.findServiceRoleForUser(userThatShouldNotBeDeleted.getId()); + assertThat(serviceRoles.size(), is(1)); + + serviceRoleDao.removeUsersFromService(service1.getId()); + + serviceRoles = databaseHelper.findServiceRoleForUser(userThatShouldBeDeletedFromServiceRoles.getId()); + assertThat(serviceRoles.size(), is(0)); + + serviceRoles = databaseHelper.findServiceRoleForUser(userThatShouldNotBeDeleted.getId()); + assertThat(serviceRoles.size(), is(1)); + } }