Skip to content

Commit

Permalink
Merge pull request #2274 from alphagov/pp_11812_remove_users
Browse files Browse the repository at this point in the history
PP-11812 Remove users when service is archived
  • Loading branch information
kbottla authored Nov 24, 2023
2 parents b783057 + 87957ca commit 7cc43c6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
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.MDC_REQUEST_ID_KEY;
import static uk.gov.service.payments.logging.LoggingKeys.SERVICE_EXTERNAL_ID;

public class ExpungeAndArchiveHistoricalDataService {
Expand Down Expand Up @@ -131,7 +130,7 @@ private int archiveServices() {
return numberOfServicesArchived.get();
}

private void archiveService(ServiceEntity serviceEntity) {
public void archiveService(ServiceEntity serviceEntity) {
MDC.put(SERVICE_EXTERNAL_ID, serviceEntity.getExternalId());
serviceEntity.setArchived(true);
serviceEntity.setArchivedDate(clock.instant().atZone(UTC));
Expand Down
29 changes: 20 additions & 9 deletions src/main/java/uk/gov/pay/adminusers/service/ServiceUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;
import uk.gov.pay.adminusers.exception.ServiceNotFoundException;
import uk.gov.pay.adminusers.expungeandarchive.service.ExpungeAndArchiveHistoricalDataService;
import uk.gov.pay.adminusers.model.GoLiveStage;
import uk.gov.pay.adminusers.model.PspTestAccountStage;
import uk.gov.pay.adminusers.model.Service;
Expand Down Expand Up @@ -54,8 +55,10 @@ public class ServiceUpdater {
private final ServiceDao serviceDao;
private final Map<String, BiConsumer<ServiceUpdateRequest, ServiceEntity>> attributeUpdaters;

ExpungeAndArchiveHistoricalDataService expungeAndArchiveHistoricalDataService;

@Inject
public ServiceUpdater(ServiceDao serviceDao) {
public ServiceUpdater(ServiceDao serviceDao, ExpungeAndArchiveHistoricalDataService expungeAndArchiveHistoricalDataService) {
Map<String, BiConsumer<ServiceUpdateRequest, ServiceEntity>> attributeUpdaters = new HashMap<>(Map.ofEntries(
entry(FIELD_GATEWAY_ACCOUNT_IDS, assignGatewayAccounts()),
entry(FIELD_CUSTOM_BRANDING, updateCustomBranding()),
Expand Down Expand Up @@ -86,6 +89,7 @@ public ServiceUpdater(ServiceDao serviceDao) {
.forEach(language -> attributeUpdaters.put(FIELD_SERVICE_NAME_PREFIX + '/' + language.toString(), updateServiceName()));
this.attributeUpdaters = Map.copyOf(attributeUpdaters);
this.serviceDao = serviceDao;
this.expungeAndArchiveHistoricalDataService = expungeAndArchiveHistoricalDataService;
}

@Transactional
Expand Down Expand Up @@ -146,14 +150,15 @@ private BiConsumer<ServiceUpdateRequest, ServiceEntity> updateRedirectImmediatel
return (serviceUpdateRequest, serviceEntity) ->
serviceEntity.setRedirectToServiceImmediatelyOnTerminalState(serviceUpdateRequest.valueAsBoolean());
}

private BiConsumer<ServiceUpdateRequest, ServiceEntity> updateExperimentalFeaturesEnabled() {
return ((serviceUpdateRequest, serviceEntity) -> serviceEntity.setExperimentalFeaturesEnabled(serviceUpdateRequest.valueAsBoolean()));
}

private BiConsumer<ServiceUpdateRequest, ServiceEntity> updateTakesPaymentsOverPhone() {
return ((serviceUpdateRequest, serviceEntity) -> serviceEntity.setTakesPaymentsOverPhone(serviceUpdateRequest.valueAsBoolean()));
}

private BiConsumer<ServiceUpdateRequest, ServiceEntity> updateAgentInitiatedMotoEnabled() {
return ((serviceUpdateRequest, serviceEntity) -> serviceEntity.setAgentInitiatedMotoEnabled(serviceUpdateRequest.valueAsBoolean()));
}
Expand All @@ -162,10 +167,10 @@ private BiConsumer<ServiceUpdateRequest, ServiceEntity> updateCollectBillingAddr
return (serviceUpdateRequest, serviceEntity) ->
serviceEntity.setCollectBillingAddress(serviceUpdateRequest.valueAsBoolean());
}

private BiConsumer<ServiceUpdateRequest, ServiceEntity> updateDefaultBillingAddressCountry() {
return (serviceUpdateRequest, serviceEntity) ->
serviceEntity.setDefaultBillingAddressCountry(serviceUpdateRequest.valueAsString());
serviceEntity.setDefaultBillingAddressCountry(serviceUpdateRequest.valueAsString());
}

private BiConsumer<ServiceUpdateRequest, ServiceEntity> updateCurrentGoLiveStage() {
Expand All @@ -182,15 +187,21 @@ private BiConsumer<ServiceUpdateRequest, ServiceEntity> updateSector() {
return (serviceUpdateRequest, serviceEntity) ->
serviceEntity.setSector(serviceUpdateRequest.valueAsString());
}

private BiConsumer<ServiceUpdateRequest, ServiceEntity> updateInternal() {
return ((serviceUpdateRequest, serviceEntity) ->
return ((serviceUpdateRequest, serviceEntity) ->
serviceEntity.setInternal(serviceUpdateRequest.valueAsBoolean()));
}

private BiConsumer<ServiceUpdateRequest, ServiceEntity> updateArchived() {
return ((serviceUpdateRequest, serviceEntity) ->
serviceEntity.setArchived(serviceUpdateRequest.valueAsBoolean()));
return ((serviceUpdateRequest, serviceEntity) -> {
if (serviceUpdateRequest.valueAsBoolean()) {
expungeAndArchiveHistoricalDataService.archiveService(serviceEntity);
} else {
serviceEntity.setArchived(serviceUpdateRequest.valueAsBoolean());
serviceEntity.setArchivedDate(null);
}
});
}

private BiConsumer<ServiceUpdateRequest, ServiceEntity> updateWentLiveDate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.junit.jupiter.api.Test;
import org.mockito.InOrder;
import uk.gov.pay.adminusers.exception.ServiceNotFoundException;
import uk.gov.pay.adminusers.expungeandarchive.service.ExpungeAndArchiveHistoricalDataService;
import uk.gov.pay.adminusers.model.GoLiveStage;
import uk.gov.pay.adminusers.model.PspTestAccountStage;
import uk.gov.pay.adminusers.model.Service;
Expand Down Expand Up @@ -46,12 +47,13 @@ public class ServiceUpdaterTest {
private static final String NON_EXISTENT_SERVICE_EXTERNAL_ID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
public static final String SERVICE_ID = randomUuid();
private ServiceDao serviceDao = mock(ServiceDao.class);
private ExpungeAndArchiveHistoricalDataService expungeAndArchiveHistoricalDataService = mock(ExpungeAndArchiveHistoricalDataService.class);
private ServiceUpdater updater;
private final ObjectMapper mapper = new ObjectMapper();

@BeforeEach
public void before() {
updater = new ServiceUpdater(serviceDao);
updater = new ServiceUpdater(serviceDao, expungeAndArchiveHistoricalDataService);
}

@Test
Expand Down Expand Up @@ -290,7 +292,7 @@ public void shouldUpdateInternalSuccessfully() {
}

@Test
public void shouldUpdateArchivedSuccessfully() {
void shouldArchiveServiceForTrueValue() {
ServiceUpdateRequest request = serviceUpdateRequest("replace", "archived", true);
ServiceEntity serviceEntity = mock(ServiceEntity.class);

Expand All @@ -300,8 +302,25 @@ public void shouldUpdateArchivedSuccessfully() {
Optional<Service> maybeService = updater.doUpdate(SERVICE_ID, request);

assertThat(maybeService.isPresent(), is(true));

verify(expungeAndArchiveHistoricalDataService).archiveService(serviceEntity);
}

@Test
void shouldUpdateArchivedForFalseValue() {
ServiceUpdateRequest request = serviceUpdateRequest("replace", "archived", false);
ServiceEntity serviceEntity = mock(ServiceEntity.class);

when(serviceDao.findByExternalId(SERVICE_ID)).thenReturn(of(serviceEntity));
when(serviceEntity.toService()).thenReturn(Service.from());

Optional<Service> maybeService = updater.doUpdate(SERVICE_ID, request);

assertThat(maybeService.isPresent(), is(true));

InOrder inOrder = inOrder(serviceEntity, serviceDao);
inOrder.verify(serviceEntity).setArchived(true);
inOrder.verify(serviceEntity).setArchived(false);
inOrder.verify(serviceEntity).setArchivedDate(null);
inOrder.verify(serviceDao).merge(serviceEntity);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import uk.gov.pay.adminusers.expungeandarchive.service.ExpungeAndArchiveHistoricalDataService;
import uk.gov.pay.adminusers.fixtures.ServiceEntityFixture;
import uk.gov.pay.adminusers.persistence.dao.UserDao;
import uk.gov.pay.adminusers.persistence.entity.GatewayAccountIdEntity;
Expand All @@ -14,7 +15,11 @@
import uk.gov.pay.adminusers.resources.ServiceRequestValidator;
import uk.gov.pay.adminusers.resources.ServiceResource;
import uk.gov.pay.adminusers.resources.ServiceUpdateOperationValidator;
import uk.gov.pay.adminusers.service.*;
import uk.gov.pay.adminusers.service.GovUkPayAgreementService;
import uk.gov.pay.adminusers.service.SendLiveAccountCreatedEmailService;
import uk.gov.pay.adminusers.service.ServiceServicesFactory;
import uk.gov.pay.adminusers.service.ServiceUpdater;
import uk.gov.pay.adminusers.service.StripeAgreementService;
import uk.gov.pay.adminusers.validations.RequestValidations;
import uk.gov.service.payments.commons.model.SupportedLanguage;

Expand All @@ -26,7 +31,11 @@
import static java.lang.String.format;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.core.Is.is;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand All @@ -40,7 +49,8 @@ public class ServiceResourceUpdateTest extends ServiceResourceBaseTest {
private static UserDao mockedUserDao = mock(UserDao.class);
private static ServiceServicesFactory mockedServicesFactory = mock(ServiceServicesFactory.class);

private static ServiceUpdater serviceUpdater = new ServiceUpdater(mockedServiceDao);
private static ExpungeAndArchiveHistoricalDataService expungeAndArchiveHistoricalDataService = mock(ExpungeAndArchiveHistoricalDataService.class);
private static ServiceUpdater serviceUpdater = new ServiceUpdater(mockedServiceDao, expungeAndArchiveHistoricalDataService);
private static RequestValidations requestValidations = new RequestValidations();
private static ServiceRequestValidator requestValidator = new ServiceRequestValidator(requestValidations, new ServiceUpdateOperationValidator(requestValidations));
private static StripeAgreementService stripeAgreementService = mock(StripeAgreementService.class);
Expand Down

0 comments on commit 7cc43c6

Please sign in to comment.