Skip to content

Commit

Permalink
feat: add endpoint to retrieve ssurvey units with states (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
davdarras authored Jan 24, 2025
1 parent d3ab67b commit ce02169
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<description>Modules for queen back-office</description>

<properties>
<revision>4.4.0</revision>
<revision>4.5.0</revision>
<changelist></changelist>
<java.version>21</java.version>
<maven.compiler.source>21</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,6 +63,22 @@ public List<String> 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<SurveyUnitState> getSurveyUnitsByState(
@RequestParam(name="state") StateDataType stateDataType,
@RequestParam(defaultValue = "0") Integer pageNumber) {
return surveyUnitService.getSurveyUnits(stateDataType, pageNumber);
}

/**
* Retrieve the survey unit
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -189,4 +190,9 @@ public List<SurveyUnit> findAllSurveyUnits() {
new SurveyUnit(SURVEY_UNIT2_ID, "campaign-id", "questionnaire-id", null, null, null, null)
);
}

@Override
public PagingResult<SurveyUnitState> getSurveyUnits(StateDataType stateDataType, Integer pageNumber) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -129,4 +130,9 @@ public List<SurveyUnit> findAllSurveyUnits() {
new SurveyUnit(SURVEY_UNIT2_ID, "campaign-id", "questionnaire-id", null, null, null, null)
);
}

@Override
public PagingResult<SurveyUnitState> getSurveyUnits(StateDataType stateDataType, Integer pageNumber) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package fr.insee.queen.domain.common.paging;

import java.util.List;

public record PagingResult<T>(
List<T> contents,
int pageNumber,
int pageSize,
long totalElements,
int totalPages) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,6 +59,14 @@ public interface SurveyUnitRepository {
*/
Optional<List<String>> findAllIds();

/**
*
* @param stateDataType state data type to filter
* @param pageNumber page number
* @return pages of survey units with states
*/
PagingResult<SurveyUnitState> findAllByState(StateDataType stateDataType, Integer pageNumber);

/**
* Find survey units with state linked by ids
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -89,6 +90,11 @@ public List<String> findAllSurveyUnitIds() {
.orElseThrow(() -> new EntityNotFoundException("List of survey unit ids not found"));
}

@Override
public PagingResult<SurveyUnitState> getSurveyUnits(StateDataType stateDataType, Integer pageNumber) {
return surveyUnitRepository.findAllByState(stateDataType, pageNumber);
}

@Transactional
@Override
public void updateSurveyUnit(SurveyUnit surveyUnit) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -43,4 +44,6 @@ public interface SurveyUnitService {
List<SurveyUnit> findByIds(List<String> surveyUnitIds);

List<SurveyUnit> findAllSurveyUnits();

PagingResult<SurveyUnitState> getSurveyUnits(StateDataType stateDataType, Integer pageNumber);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,6 +55,11 @@ public Optional<List<String>> findAllIds() {
return Optional.empty();
}

@Override
public PagingResult<SurveyUnitState> findAllByState(StateDataType stateDataType, Integer pageNumber) {
return null;
}

@Override
public List<SurveyUnitState> findAllWithStateByIdIn(List<String> surveyUnitIds) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -131,4 +132,9 @@ public List<SurveyUnit> findAllSurveyUnits() {
new SurveyUnit(SURVEY_UNIT2_ID, "campaign-id", "questionnaire-id", null, null, null, null)
);
}

@Override
public PagingResult<SurveyUnitState> getSurveyUnits(StateDataType stateDataType, Integer pageNumber) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -73,6 +78,21 @@ public Optional<List<String>> findAllIds() {
return crudRepository.findAllIds();
}

@Override
public PagingResult<SurveyUnitState> findAllByState(StateDataType state, Integer pageNumber) {

Pageable pageable = PageRequest.of(pageNumber, 1000, Sort.by("id"));
Page<SurveyUnitState> surveyUnitPages;

surveyUnitPages = crudRepository.findAllByState(state, pageable);

return new PagingResult<>(surveyUnitPages.toList(),
surveyUnitPages.getNumber(),
surveyUnitPages.getSize(),
surveyUnitPages.getTotalElements(),
surveyUnitPages.getTotalPages());
}

@Override
public List<SurveyUnitState> findAllWithStateByIdIn(List<String> surveyUnitIds) {
return crudRepository.findAllWithStateByIdIn(surveyUnitIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -159,6 +161,26 @@ public interface SurveyUnitJpaRepository extends JpaRepository<SurveyUnitDB, Str
@Query("select s.id from SurveyUnitDB s order by s.id asc")
Optional<List<String>> 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<SurveyUnitState> findAllByState(StateDataType stateDataType, Pageable pageable);

/**
* Search survey units by ids
* @param surveyUnitIds ids to search
Expand Down

0 comments on commit ce02169

Please sign in to comment.