diff --git a/pom.xml b/pom.xml index ecec557a..314587c4 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ Modules for queen back-office - 4.4.0 + 4.5.0 21 21 diff --git a/queen-application/src/main/java/fr/insee/queen/application/surveyunit/controller/SurveyUnitController.java b/queen-application/src/main/java/fr/insee/queen/application/surveyunit/controller/SurveyUnitController.java index bc413b91..9e890b11 100644 --- a/queen-application/src/main/java/fr/insee/queen/application/surveyunit/controller/SurveyUnitController.java +++ b/queen-application/src/main/java/fr/insee/queen/application/surveyunit/controller/SurveyUnitController.java @@ -14,6 +14,7 @@ import fr.insee.queen.application.web.authentication.AuthenticationHelper; import fr.insee.queen.application.web.validation.IdValid; import fr.insee.queen.domain.campaign.model.CampaignSensitivity; +import fr.insee.queen.domain.common.paging.PagingResult; import fr.insee.queen.domain.pilotage.service.PilotageRole; import fr.insee.queen.domain.surveyunit.model.*; import fr.insee.queen.domain.surveyunit.service.StateDataService; @@ -62,6 +63,22 @@ public List getSurveyUnitIds() { return surveyUnitService.findAllSurveyUnitIds(); } + /** + * Retrieve survey units filtered by state + * + * @param stateDataType state + * @param pageNumber page to retrieve + * @return all ids of survey units + */ + @Operation(summary = "Retrieve survey-units by state") + @PostMapping(path = "/survey-units") + @PreAuthorize(AuthorityPrivileges.HAS_ADMIN_PRIVILEGES) + public PagingResult getSurveyUnitsByState( + @RequestParam(name="state") StateDataType stateDataType, + @RequestParam(defaultValue = "0") Integer pageNumber) { + return surveyUnitService.getSurveyUnits(stateDataType, pageNumber); + } + /** * Retrieve the survey unit * diff --git a/queen-application/src/test/java/fr/insee/queen/application/surveyunit/integration/SurveyUnitTests.java b/queen-application/src/test/java/fr/insee/queen/application/surveyunit/integration/SurveyUnitTests.java index 4e64e09b..1d7578ce 100644 --- a/queen-application/src/test/java/fr/insee/queen/application/surveyunit/integration/SurveyUnitTests.java +++ b/queen-application/src/test/java/fr/insee/queen/application/surveyunit/integration/SurveyUnitTests.java @@ -1,8 +1,12 @@ package fr.insee.queen.application.surveyunit.integration; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; import fr.insee.queen.application.configuration.ContainerConfiguration; +import fr.insee.queen.application.surveyunit.dto.input.StateDataTypeInput; import fr.insee.queen.application.utils.AuthenticatedUserTestHelper; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.skyscreamer.jsonassert.JSONAssert; import org.skyscreamer.jsonassert.JSONCompareMode; @@ -13,9 +17,11 @@ import java.util.List; +import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.Matchers.*; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -35,6 +41,39 @@ void on_get_survey_unit_ids_return_ids() throws Exception { .andExpect(jsonPath("$.size()", is(16))); } + @Test + @DisplayName("Should return survey units with states") + void on_get_survey_units_return_survey_units() throws Exception { + ObjectMapper mapper = new ObjectMapper(); + String expectedFirstResult = """ + { + "id":"12", + "questionnaireId":"simpsons", + "campaignId":"SIMPSONS2020X00", + "stateData":{ + "state":"INIT", + "date":1111111111, + "currentPage":"2.3#5" + } + }"""; + mockMvc.perform(post("/api/survey-units") + .accept(MediaType.APPLICATION_JSON) + .param("state", StateDataTypeInput.INIT.name()) + .with(authentication(authenticatedUserTestHelper.getAdminUser())) + ) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.contents.size()", is(13))) + // extract and check first element + .andExpect(result -> { + String responseContent = result.getResponse().getContentAsString(); + JsonNode rootNode = mapper.readTree(responseContent); + JsonNode firstContent = rootNode.path("contents").get(0); + JsonNode expectedNode = mapper.readTree(expectedFirstResult); + assertThat(firstContent).isEqualTo(expectedNode); + }); + } + @Test void on_get_survey_units_by_campaign_when_campaign_not_exist_return_404() throws Exception { mockMvc.perform(get("/api/campaign/not-exist/survey-units") diff --git a/queen-application/src/test/java/fr/insee/queen/application/surveyunit/service/dummy/SurveyUnitFakeService.java b/queen-application/src/test/java/fr/insee/queen/application/surveyunit/service/dummy/SurveyUnitFakeService.java index 636066dd..9feb0a72 100644 --- a/queen-application/src/test/java/fr/insee/queen/application/surveyunit/service/dummy/SurveyUnitFakeService.java +++ b/queen-application/src/test/java/fr/insee/queen/application/surveyunit/service/dummy/SurveyUnitFakeService.java @@ -5,6 +5,7 @@ import fr.insee.queen.domain.campaign.model.CampaignSensitivity; import fr.insee.queen.domain.campaign.model.CampaignSummary; import fr.insee.queen.domain.common.exception.EntityNotFoundException; +import fr.insee.queen.domain.common.paging.PagingResult; import fr.insee.queen.domain.surveyunit.model.*; import fr.insee.queen.domain.surveyunit.service.SurveyUnitService; import lombok.Getter; @@ -189,4 +190,9 @@ public List findAllSurveyUnits() { new SurveyUnit(SURVEY_UNIT2_ID, "campaign-id", "questionnaire-id", null, null, null, null) ); } + + @Override + public PagingResult getSurveyUnits(StateDataType stateDataType, Integer pageNumber) { + return null; + } } diff --git a/queen-domain-pilotage/src/test/java/fr/insee/queen/domain/surveyunit/service/dummy/SurveyUnitFakeService.java b/queen-domain-pilotage/src/test/java/fr/insee/queen/domain/surveyunit/service/dummy/SurveyUnitFakeService.java index aa0cfe02..df696965 100644 --- a/queen-domain-pilotage/src/test/java/fr/insee/queen/domain/surveyunit/service/dummy/SurveyUnitFakeService.java +++ b/queen-domain-pilotage/src/test/java/fr/insee/queen/domain/surveyunit/service/dummy/SurveyUnitFakeService.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import fr.insee.queen.domain.campaign.model.CampaignSensitivity; import fr.insee.queen.domain.campaign.model.CampaignSummary; +import fr.insee.queen.domain.common.paging.PagingResult; import fr.insee.queen.domain.surveyunit.model.*; import fr.insee.queen.domain.surveyunit.service.SurveyUnitService; import lombok.Getter; @@ -129,4 +130,9 @@ public List findAllSurveyUnits() { new SurveyUnit(SURVEY_UNIT2_ID, "campaign-id", "questionnaire-id", null, null, null, null) ); } + + @Override + public PagingResult getSurveyUnits(StateDataType stateDataType, Integer pageNumber) { + return null; + } } diff --git a/queen-domain/src/main/java/fr/insee/queen/domain/common/paging/PagingResult.java b/queen-domain/src/main/java/fr/insee/queen/domain/common/paging/PagingResult.java new file mode 100644 index 00000000..0f68512a --- /dev/null +++ b/queen-domain/src/main/java/fr/insee/queen/domain/common/paging/PagingResult.java @@ -0,0 +1,11 @@ +package fr.insee.queen.domain.common.paging; + +import java.util.List; + +public record PagingResult( + List contents, + int pageNumber, + int pageSize, + long totalElements, + int totalPages) { +} diff --git a/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/gateway/SurveyUnitRepository.java b/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/gateway/SurveyUnitRepository.java index 0ec1a95c..7ee0be8c 100644 --- a/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/gateway/SurveyUnitRepository.java +++ b/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/gateway/SurveyUnitRepository.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; +import fr.insee.queen.domain.common.paging.PagingResult; import fr.insee.queen.domain.surveyunit.model.*; import java.util.List; @@ -58,6 +59,14 @@ public interface SurveyUnitRepository { */ Optional> findAllIds(); + /** + * + * @param stateDataType state data type to filter + * @param pageNumber page number + * @return pages of survey units with states + */ + PagingResult findAllByState(StateDataType stateDataType, Integer pageNumber); + /** * Find survey units with state linked by ids * diff --git a/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/service/SurveyUnitApiService.java b/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/service/SurveyUnitApiService.java index b2883dd7..57b6543d 100644 --- a/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/service/SurveyUnitApiService.java +++ b/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/service/SurveyUnitApiService.java @@ -6,6 +6,7 @@ import fr.insee.queen.domain.common.cache.CacheName; import fr.insee.queen.domain.common.exception.EntityAlreadyExistException; import fr.insee.queen.domain.common.exception.EntityNotFoundException; +import fr.insee.queen.domain.common.paging.PagingResult; import fr.insee.queen.domain.surveyunit.model.*; import fr.insee.queen.domain.surveyunit.service.exception.StateDataInvalidDateException; import fr.insee.queen.domain.surveyunit.gateway.SurveyUnitRepository; @@ -89,6 +90,11 @@ public List findAllSurveyUnitIds() { .orElseThrow(() -> new EntityNotFoundException("List of survey unit ids not found")); } + @Override + public PagingResult getSurveyUnits(StateDataType stateDataType, Integer pageNumber) { + return surveyUnitRepository.findAllByState(stateDataType, pageNumber); + } + @Transactional @Override public void updateSurveyUnit(SurveyUnit surveyUnit) { diff --git a/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/service/SurveyUnitService.java b/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/service/SurveyUnitService.java index f8adb761..e11db7cb 100644 --- a/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/service/SurveyUnitService.java +++ b/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/service/SurveyUnitService.java @@ -1,6 +1,7 @@ package fr.insee.queen.domain.surveyunit.service; import com.fasterxml.jackson.databind.node.ObjectNode; +import fr.insee.queen.domain.common.paging.PagingResult; import fr.insee.queen.domain.surveyunit.model.*; import fr.insee.queen.domain.surveyunit.service.exception.StateDataInvalidDateException; @@ -43,4 +44,6 @@ public interface SurveyUnitService { List findByIds(List surveyUnitIds); List findAllSurveyUnits(); + + PagingResult getSurveyUnits(StateDataType stateDataType, Integer pageNumber); } diff --git a/queen-domain/src/test/java/fr/insee/queen/domain/surveyunit/infrastructure/dummy/SurveyUnitFakeDao.java b/queen-domain/src/test/java/fr/insee/queen/domain/surveyunit/infrastructure/dummy/SurveyUnitFakeDao.java index f716d251..323ecad7 100644 --- a/queen-domain/src/test/java/fr/insee/queen/domain/surveyunit/infrastructure/dummy/SurveyUnitFakeDao.java +++ b/queen-domain/src/test/java/fr/insee/queen/domain/surveyunit/infrastructure/dummy/SurveyUnitFakeDao.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; +import fr.insee.queen.domain.common.paging.PagingResult; import fr.insee.queen.domain.surveyunit.gateway.SurveyUnitRepository; import fr.insee.queen.domain.surveyunit.model.*; import lombok.Getter; @@ -54,6 +55,11 @@ public Optional> findAllIds() { return Optional.empty(); } + @Override + public PagingResult findAllByState(StateDataType stateDataType, Integer pageNumber) { + return null; + } + @Override public List findAllWithStateByIdIn(List surveyUnitIds) { return null; diff --git a/queen-domain/src/test/java/fr/insee/queen/domain/surveyunit/service/dummy/SurveyUnitFakeService.java b/queen-domain/src/test/java/fr/insee/queen/domain/surveyunit/service/dummy/SurveyUnitFakeService.java index c8ec5a10..5ee981c2 100644 --- a/queen-domain/src/test/java/fr/insee/queen/domain/surveyunit/service/dummy/SurveyUnitFakeService.java +++ b/queen-domain/src/test/java/fr/insee/queen/domain/surveyunit/service/dummy/SurveyUnitFakeService.java @@ -4,6 +4,7 @@ import fr.insee.queen.domain.campaign.model.CampaignSensitivity; import fr.insee.queen.domain.campaign.model.CampaignSummary; import fr.insee.queen.domain.common.exception.EntityNotFoundException; +import fr.insee.queen.domain.common.paging.PagingResult; import fr.insee.queen.domain.surveyunit.model.*; import fr.insee.queen.domain.surveyunit.service.SurveyUnitService; import lombok.Getter; @@ -131,4 +132,9 @@ public List findAllSurveyUnits() { new SurveyUnit(SURVEY_UNIT2_ID, "campaign-id", "questionnaire-id", null, null, null, null) ); } + + @Override + public PagingResult getSurveyUnits(StateDataType stateDataType, Integer pageNumber) { + return null; + } } diff --git a/queen-infra-db/src/main/java/fr/insee/queen/infrastructure/db/surveyunit/repository/SurveyUnitDao.java b/queen-infra-db/src/main/java/fr/insee/queen/infrastructure/db/surveyunit/repository/SurveyUnitDao.java index 074c713a..b052fbd8 100644 --- a/queen-infra-db/src/main/java/fr/insee/queen/infrastructure/db/surveyunit/repository/SurveyUnitDao.java +++ b/queen-infra-db/src/main/java/fr/insee/queen/infrastructure/db/surveyunit/repository/SurveyUnitDao.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; +import fr.insee.queen.domain.common.paging.PagingResult; import fr.insee.queen.domain.surveyunit.gateway.SurveyUnitRepository; import fr.insee.queen.domain.surveyunit.model.*; import fr.insee.queen.infrastructure.db.campaign.entity.CampaignDB; @@ -18,6 +19,10 @@ import fr.insee.queen.infrastructure.db.paradata.repository.jpa.ParadataEventJpaRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; import org.springframework.stereotype.Repository; import java.util.List; @@ -73,6 +78,21 @@ public Optional> findAllIds() { return crudRepository.findAllIds(); } + @Override + public PagingResult findAllByState(StateDataType state, Integer pageNumber) { + + Pageable pageable = PageRequest.of(pageNumber, 1000, Sort.by("id")); + Page surveyUnitPages; + + surveyUnitPages = crudRepository.findAllByState(state, pageable); + + return new PagingResult<>(surveyUnitPages.toList(), + surveyUnitPages.getNumber(), + surveyUnitPages.getSize(), + surveyUnitPages.getTotalElements(), + surveyUnitPages.getTotalPages()); + } + @Override public List findAllWithStateByIdIn(List surveyUnitIds) { return crudRepository.findAllWithStateByIdIn(surveyUnitIds); diff --git a/queen-infra-db/src/main/java/fr/insee/queen/infrastructure/db/surveyunit/repository/jpa/SurveyUnitJpaRepository.java b/queen-infra-db/src/main/java/fr/insee/queen/infrastructure/db/surveyunit/repository/jpa/SurveyUnitJpaRepository.java index db315ab5..ce0b23e0 100644 --- a/queen-infra-db/src/main/java/fr/insee/queen/infrastructure/db/surveyunit/repository/jpa/SurveyUnitJpaRepository.java +++ b/queen-infra-db/src/main/java/fr/insee/queen/infrastructure/db/surveyunit/repository/jpa/SurveyUnitJpaRepository.java @@ -3,6 +3,8 @@ import fr.insee.queen.domain.surveyunit.model.*; import fr.insee.queen.infrastructure.db.surveyunit.entity.SurveyUnitDB; import fr.insee.queen.infrastructure.db.surveyunit.projection.SurveyUnitProjection; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; @@ -159,6 +161,26 @@ public interface SurveyUnitJpaRepository extends JpaRepository> findAllIds(); + /** + * Find all survey units by state + * + * @param stateDataType state data used for filtering + * @return List of survey units by state + */ + @Query(""" + select new fr.insee.queen.domain.surveyunit.model.SurveyUnitState( + s.id, + s.questionnaireModel.id, + s.campaign.id, + new fr.insee.queen.domain.surveyunit.model.StateData( + st.state, + st.date, + st.currentPage + ) + ) + from SurveyUnitDB s left join s.stateData st where st.state = :stateDataType""") + Page findAllByState(StateDataType stateDataType, Pageable pageable); + /** * Search survey units by ids * @param surveyUnitIds ids to search