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

PP-11205 Add method to find services #2214

Merged
merged 1 commit into from
Nov 1, 2023
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 @@ -7,6 +7,7 @@

import javax.inject.Inject;
import javax.persistence.EntityManager;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Optional;

Expand All @@ -24,15 +25,15 @@ public List<ServiceEntity> listAll() {
.createQuery(query, ServiceEntity.class)
.getResultList();
}

@SuppressWarnings("unchecked")
public List<ServiceEntity> findByENServiceName(String searchString) {
String query = "SELECT * FROM services s WHERE s.id IN (SELECT service_id FROM service_names sn WHERE to_tsvector('english', sn.name) @@ plainto_tsquery('english', ?) AND sn.language = 'en')";
return entityManager.get().createNativeQuery(query, ServiceEntity.class)
.setParameter(1, searchString)
.getResultList();
}

@SuppressWarnings("unchecked")
public List<ServiceEntity> findByServiceMerchantName(String searchString) {
String query = "SELECT * FROM services s WHERE to_tsvector('english', s.merchant_name) @@ plainto_tsquery('english', ?)";
Expand Down Expand Up @@ -80,4 +81,14 @@ public Optional<ServiceEntity> findByExternalId(String serviceExternalId) {
.stream()
.findFirst();
}

public List<ServiceEntity> findServicesToCheckForArchiving(ZonedDateTime archiveServicesBeforeDate) {
String query = "SELECT s FROM ServiceEntity as s" +
" WHERE s.createdDate < :archiveServicesBeforeDate" +
" AND NOT s.archived";
return entityManager.get()
.createQuery(query, ServiceEntity.class)
.setParameter("archiveServicesBeforeDate", archiveServicesBeforeDate)
.getResultList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
import uk.gov.pay.adminusers.persistence.entity.service.ServiceNameEntity;
import uk.gov.service.payments.commons.model.SupportedLanguage;

import static org.apache.commons.lang3.RandomUtils.nextInt;

import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.apache.commons.lang3.RandomUtils.nextInt;

public final class ServiceEntityFixture {
private Integer id = nextInt();
private String externalId = RandomIdGenerator.randomUuid();
Expand All @@ -37,6 +37,7 @@ public final class ServiceEntityFixture {
private ZonedDateTime wentLiveDate;
private String sector;
private PspTestAccountStage pspTestAccountStage = PspTestAccountStage.NOT_STARTED;
private boolean archived = false;

private ServiceEntityFixture() {
}
Expand Down Expand Up @@ -127,6 +128,11 @@ public ServiceEntityFixture withPspTestAccountStage(PspTestAccountStage pspTestA
return this;
}

public ServiceEntityFixture withArchived(boolean archived) {
this.archived = archived;
return this;
}

public ServiceEntity build() {
ServiceEntity serviceEntity = new ServiceEntity();
serviceEntity.setId(id);
Expand All @@ -145,6 +151,7 @@ public ServiceEntity build() {
serviceEntity.setWentLiveDate(wentLiveDate);
serviceEntity.setSector(sector);
serviceEntity.setCurrentPspTestAccountStage(pspTestAccountStage);
serviceEntity.setArchived(archived);
return serviceEntity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.RandomUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.postgresql.util.PGobject;
import uk.gov.pay.adminusers.fixtures.ServiceEntityFixture;
import uk.gov.pay.adminusers.fixtures.UserDbFixture;
import uk.gov.pay.adminusers.model.GoLiveStage;
import uk.gov.pay.adminusers.model.Permission;
Expand All @@ -19,7 +21,6 @@
import uk.gov.pay.adminusers.persistence.entity.MerchantDetailsEntity;
import uk.gov.pay.adminusers.persistence.entity.MerchantDetailsEntityBuilder;
import uk.gov.pay.adminusers.persistence.entity.ServiceEntity;
import uk.gov.pay.adminusers.fixtures.ServiceEntityFixture;
import uk.gov.pay.adminusers.persistence.entity.service.ServiceNameEntity;
import uk.gov.service.payments.commons.model.SupportedLanguage;

Expand Down Expand Up @@ -374,6 +375,59 @@ void shouldMergePSPTestAccountStage() {
assertThat(optionalService.get().getCurrentPspTestAccountStage(), is(PspTestAccountStage.REQUEST_SUBMITTED));
}

@Nested
class TestFindServicesToCheckForArchiving {
@Test
void shouldReturnServicesOlderThanTheProvidedDate() {
ZonedDateTime archiveServicesBeforeDate = ZonedDateTime.parse("2022-01-01T00:00:00Z");
ServiceEntity insertedServiceEntity = ServiceEntityFixture
.aServiceEntity()
.withCreatedDate(archiveServicesBeforeDate.minusDays(1))
.build();

databaseHelper.insertServiceEntity(insertedServiceEntity);

List<ServiceEntity> services = serviceDao.findServicesToCheckForArchiving(archiveServicesBeforeDate);
assertThat(services.size(), is(1));
assertThat(services.get(0).getExternalId(), is(insertedServiceEntity.getExternalId()));
}

@Test
void shouldNotReturnServiceCreatedAfterTheProvidedDate() {
ZonedDateTime archiveServicesBeforeDate = ZonedDateTime.parse("2022-01-01T00:00:00Z");
ServiceEntity insertedServiceEntity1 = ServiceEntityFixture
.aServiceEntity()
.withCreatedDate(archiveServicesBeforeDate)
.build();
ServiceEntity insertedServiceEntity2 = ServiceEntityFixture
.aServiceEntity()
.withCreatedDate(archiveServicesBeforeDate.plusDays(1))
.build();

databaseHelper.insertServiceEntity(insertedServiceEntity1);
databaseHelper.insertServiceEntity(insertedServiceEntity2);

List<ServiceEntity> services = serviceDao.findServicesToCheckForArchiving(archiveServicesBeforeDate);
assertThat(services.size(), is(0));
}

@Test
void shouldNotReturnServiceArchived() {
ZonedDateTime archiveServicesBeforeDate = ZonedDateTime.parse("2022-01-01T00:00:00Z");

ServiceEntity insertedServiceEntity = ServiceEntityFixture
.aServiceEntity()
.withCreatedDate(archiveServicesBeforeDate.minusDays(1))
.withArchived(true)
.build();

databaseHelper.insertServiceEntity(insertedServiceEntity);

List<ServiceEntity> services = serviceDao.findServicesToCheckForArchiving(archiveServicesBeforeDate);
assertThat(services.size(), is(0));
}
}

private void setupUsersForServiceAndRole(String externalId, int roleId, int noOfUsers) {
Permission perm1 = aPermission();
Permission perm2 = aPermission();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,10 @@ public DatabaseTestHelper insertServiceEntity(ServiceEntity serviceEntity) {
"id, custom_branding, " +
"merchant_name, merchant_telephone_number, merchant_address_line1, merchant_address_line2, merchant_address_city, " +
"merchant_address_postcode, merchant_address_country, merchant_email, merchant_url, external_id, redirect_to_service_immediately_on_terminal_state, " +
"current_go_live_stage, experimental_features_enabled, current_psp_test_account_stage, created_date) " +
"current_go_live_stage, experimental_features_enabled, current_psp_test_account_stage, created_date, archived) " +
"VALUES (:id, :customBranding, :merchantName, :merchantTelephoneNumber, :merchantAddressLine1, :merchantAddressLine2, " +
":merchantAddressCity, :merchantAddressPostcode, :merchantAddressCountry, :merchantEmail, :merchantUrl, :externalId, :redirectToServiceImmediatelyOnTerminalState, " +
":currentGoLiveStage, :experimentalFeaturesEnabled, :pspTestAccountStage, :createdDate)")
":currentGoLiveStage, :experimentalFeaturesEnabled, :pspTestAccountStage, :createdDate, :archived)")
.bind("id", serviceEntity.getId())
.bindBySqlType("customBranding", customBranding, OTHER)
.bind("merchantName", merchantDetails.getName())
Expand All @@ -406,6 +406,7 @@ public DatabaseTestHelper insertServiceEntity(ServiceEntity serviceEntity) {
.bind("experimentalFeaturesEnabled", serviceEntity.isExperimentalFeaturesEnabled())
.bind("pspTestAccountStage", serviceEntity.getCurrentPspTestAccountStage())
.bind("createdDate", serviceEntity.getCreatedDate())
.bind("archived", serviceEntity.isArchived())
.execute();
});
serviceEntity.getGatewayAccountIds().forEach(gatewayAccount ->
Expand Down