Skip to content

Commit

Permalink
PP-11205 Detach users from service
Browse files Browse the repository at this point in the history
- Removes users from service when service is archived.
  • Loading branch information
kbottla committed Nov 7, 2023
1 parent 6354cb2 commit 06446d5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {

Expand All @@ -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;
Expand All @@ -47,13 +50,15 @@ public class ExpungeAndArchiveHistoricalDataService {
public ExpungeAndArchiveHistoricalDataService(UserDao userDao, InviteDao inviteDao,
ForgottenPasswordDao forgottenPasswordDao,
ServiceDao serviceDao,
ServiceRoleDao serviceRoleDao,
LedgerService ledgerService,
AdminUsersConfig adminUsersConfig,
Clock clock) {
this.userDao = userDao;
this.inviteDao = inviteDao;
this.forgottenPasswordDao = forgottenPasswordDao;
this.serviceDao = serviceDao;
this.serviceRoleDao = serviceRoleDao;
this.ledgerService = ledgerService;
expungeAndArchiveDataConfig = adminUsersConfig.getExpungeAndArchiveDataConfig();
this.clock = clock;
Expand Down Expand Up @@ -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<ZonedDateTime> mayBeLastTransactionDateForService = getLastTransactionDateForService(serviceEntity);
ZonedDateTime archiveServicesBeforeDate = getArchiveServicesBeforeDate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ public class ServiceRoleDao extends JpaDao<ServiceRoleEntity> {
/* default */ ServiceRoleDao(Provider<EntityManager> entityManager) {
super(entityManager, ServiceRoleEntity.class);
}

public void removeUsersFromService(Integer serviceId) {
entityManager.get()
.createQuery("DELETE from ServiceRoleEntity where service.id = :serviceId")
.setParameter("serviceId", serviceId)
.executeUpdate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,6 +61,9 @@ class ExpungeAndArchiveHistoricalDataServiceTest {
@Mock
ServiceDao mockServiceDao;

@Mock
ServiceRoleDao mockServiceRoleDao;

@Mock
LedgerService mockLedgerService;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -198,6 +202,7 @@ void shouldArchiveService_WhenTheLastTransactionDateIsBeforeTheServicesEligibleF

assertTrue(serviceEntity.isArchived());
verify(mockServiceDao).merge(serviceEntity);
verify(mockServiceRoleDao).removeUsersFromService(serviceEntity.getId());
}

@Test
Expand All @@ -221,6 +226,7 @@ void shouldArchiveServiceWithoutTransactionsButCreatedBeforeTheServicesEligibleF

assertTrue(serviceEntity.isArchived());
verify(mockServiceDao).merge(serviceEntity);
verify(mockServiceRoleDao).removeUsersFromService(serviceEntity.getId());
}

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

Expand All @@ -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();
Expand All @@ -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<Map<String, Object>> 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));
}
}

0 comments on commit 06446d5

Please sign in to comment.