From 3e335b8f6f2d89bdefd9e4ea5dec17fc14711dfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Demazi=C3=A8re?= Date: Fri, 7 Oct 2022 15:33:09 +0200 Subject: [PATCH] update contactOutcome counts (#86) expose new closing cause in coutings --- pom.xml | 2 +- .../pearljam/api/constants/Constants.java | 2 +- .../api/controller/SurveyUnitController.java | 3 + .../closingcause/ClosingCauseCountDto.java | 15 ++ .../ContactOutcomeTypeCountDto.java | 163 ++++++++++-------- .../pearljam/api/dto/state/StateCountDto.java | 9 + .../repository/ClosingCauseRepository.java | 8 + .../repository/ContactOutcomeRepository.java | 20 ++- .../api/repository/SurveyUnitRepository.java | 82 +++++++-- .../service/impl/SurveyUnitServiceImpl.java | 28 ++- .../resources/dataset/insert_test_data.sql | 3 +- .../api/authKeycloak/TestAuthKeyCloak.java | 22 ++- 12 files changed, 238 insertions(+), 119 deletions(-) diff --git a/pom.xml b/pom.xml index c5986875..b3a278e8 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 fr.insee.pearljam pearljam-back-office - 4.0.3 + 4.0.4 war Pearl-Jam-Back-Office Back-office services for for PearlJam diff --git a/src/main/java/fr/insee/pearljam/api/constants/Constants.java b/src/main/java/fr/insee/pearljam/api/constants/Constants.java index 704aa461..402e9b35 100644 --- a/src/main/java/fr/insee/pearljam/api/constants/Constants.java +++ b/src/main/java/fr/insee/pearljam/api/constants/Constants.java @@ -98,7 +98,7 @@ private Constants() { public static final String[] CLOSING_CAUSE_FIELDS = { "npaCount", "npiCount", "rowCount", "total" }; public static final String[] CONTACT_OUTCOME_FIELDS = { "inaCount", "refCount", "impCount", "ucdCount", "utrCount", - "alaCount", "acpCount", "dcdCount", "nuhCount", "nerCount" }; + "alaCount", "dcdCount", "nuhCount", "dukCount", "duuCount", "noaCount" }; public static final String UNAVAILABLE = "UNAVAILABLE"; diff --git a/src/main/java/fr/insee/pearljam/api/controller/SurveyUnitController.java b/src/main/java/fr/insee/pearljam/api/controller/SurveyUnitController.java index ea126cd7..548651f5 100644 --- a/src/main/java/fr/insee/pearljam/api/controller/SurveyUnitController.java +++ b/src/main/java/fr/insee/pearljam/api/controller/SurveyUnitController.java @@ -418,6 +418,7 @@ public ResponseEntity getStatesBySurveyUnitId(HttpServletRe @GetMapping(path = "/survey-units/closable") public ResponseEntity> getClosableSurveyUnits(HttpServletRequest request) { String userId = utilsService.getUserId(request); + LOGGER.info("{} try to GET closable units",userId); if (StringUtils.isBlank(userId)) { LOGGER.info("GET closable survey units resulting in 401"); return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); @@ -436,6 +437,8 @@ public ResponseEntity> getClosableSurveyUnits(HttpSe @ApiOperation(value = "Delete survey-unit") @DeleteMapping(path = "/survey-unit/{id}") public ResponseEntity deleteSurveyUnit(HttpServletRequest request, @PathVariable(value = "id") String id){ + String userId = utilsService.getUserId(request); + LOGGER.info("{} try to DELETE survey-unit {}",userId, id); Optional surveyUnitOptional = surveyUnitService.findById(id); if (!surveyUnitOptional.isPresent()) { LOGGER.error("DELETE survey-unit with id {} resulting in 404 because it does not exists", id); diff --git a/src/main/java/fr/insee/pearljam/api/dto/closingcause/ClosingCauseCountDto.java b/src/main/java/fr/insee/pearljam/api/dto/closingcause/ClosingCauseCountDto.java index f9b89f99..26deac3b 100644 --- a/src/main/java/fr/insee/pearljam/api/dto/closingcause/ClosingCauseCountDto.java +++ b/src/main/java/fr/insee/pearljam/api/dto/closingcause/ClosingCauseCountDto.java @@ -12,6 +12,7 @@ public class ClosingCauseCountDto { private Long npiCount; private Long npaCount; + private Long npxCount; private Long rowCount; private Long total; @@ -69,6 +70,20 @@ public void setNpaCount(Long npaCount) { this.npaCount = npaCount; } + /** + * @return the npxCount + */ + public Long getNpxCount() { + return npxCount; + } + + /** + * @param npxCount the npxCount to set + */ + public void setNpxCount(Long npxCount) { + this.npxCount = npxCount; + } + /** * @return the rowCount */ diff --git a/src/main/java/fr/insee/pearljam/api/dto/contactoutcome/ContactOutcomeTypeCountDto.java b/src/main/java/fr/insee/pearljam/api/dto/contactoutcome/ContactOutcomeTypeCountDto.java index 4ebdf729..b1f256bc 100644 --- a/src/main/java/fr/insee/pearljam/api/dto/contactoutcome/ContactOutcomeTypeCountDto.java +++ b/src/main/java/fr/insee/pearljam/api/dto/contactoutcome/ContactOutcomeTypeCountDto.java @@ -11,75 +11,74 @@ @JsonInclude(JsonInclude.Include.NON_NULL) public class ContactOutcomeTypeCountDto { - + private String idDem; - - private String labelDem; - + + private String labelDem; + private CampaignDto campaign; - + private Long inaCount; - - private Long refCount; - - private Long impCount; - - private Long ucdCount; - - private Long utrCount; - - private Long alaCount; - - private Long acpCount; - - private Long dcdCount; - - private Long nuhCount; - - private Long nerCount; - - private Long total; - - public ContactOutcomeTypeCountDto() { - super(); - } - - public ContactOutcomeTypeCountDto(Map obj) { - if (obj != null && !obj.isEmpty()) { - for(String str : Constants.CONTACT_OUTCOME_FIELDS) { + + private Long refCount; + + private Long impCount; + + private Long ucdCount; + + private Long utrCount; + + private Long alaCount; + + private Long dcdCount; + + private Long nuhCount; + + private Long dukCount; + + private Long duuCount; + + private Long noaCount; + + private Long total; + + public ContactOutcomeTypeCountDto() { + super(); + } + + public ContactOutcomeTypeCountDto(Map obj) { + if (obj != null && !obj.isEmpty()) { + for (String str : Constants.CONTACT_OUTCOME_FIELDS) { try { - setLongField(str, obj.get(str)!=null?obj.get(str).longValue():0L); + setLongField(str, obj.get(str) != null ? obj.get(str).longValue() : 0L); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); - } + } } } - - } - - public ContactOutcomeTypeCountDto(Map obj, CampaignDto campaign) { - this.campaign = campaign; - if (obj != null && !obj.isEmpty()) { - for(String str : Constants.CONTACT_OUTCOME_FIELDS) { + + } + + public ContactOutcomeTypeCountDto(Map obj, CampaignDto campaign) { + this.campaign = campaign; + if (obj != null && !obj.isEmpty()) { + for (String str : Constants.CONTACT_OUTCOME_FIELDS) { try { - setLongField(str, obj.get(str)!=null?obj.get(str).longValue():0L); + setLongField(str, obj.get(str) != null ? obj.get(str).longValue() : 0L); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); - } + } } } - - } - - public ContactOutcomeTypeCountDto(String idDem, String labelDem, Map obj) { + + } + + public ContactOutcomeTypeCountDto(String idDem, String labelDem, Map obj) { this(obj); this.idDem = idDem; this.setLabelDem(labelDem); } - - - - + /** * @return the idDem */ @@ -206,20 +205,6 @@ public void setAlaCount(Long alaCount) { this.alaCount = alaCount; } - /** - * @return the acpCount - */ - public Long getAcpCount() { - return acpCount; - } - - /** - * @param acpCount the acpCount to set - */ - public void setAcpCount(Long acpCount) { - this.acpCount = acpCount; - } - /** * @return the dcdCount */ @@ -249,17 +234,45 @@ public void setNuhCount(Long nuhCount) { } /** - * @return the nerCount + * @return the dukCount + */ + public Long getDukCount() { + return dukCount; + } + + /** + * @param dukCount the dukCount to set + */ + public void setDukCount(Long dukCount) { + this.dukCount = dukCount; + } + + /** + * @return the duuCount + */ + public Long getDuuCount() { + return duuCount; + } + + /** + * @param nuhCount the duuCount to set + */ + public void setDuuCount(Long duuCount) { + this.duuCount = duuCount; + } + + /** + * @return the noaCount */ - public Long getNerCount() { - return nerCount; + public Long getNoaCount() { + return noaCount; } /** - * @param nerCount the nerCount to set + * @param noaCount the noaCount to set */ - public void setNerCount(Long nerCount) { - this.nerCount = nerCount; + public void setNoaCount(Long noaCount) { + this.noaCount = noaCount; } /** @@ -277,9 +290,9 @@ public void setTotal(Long total) { } public void setLongField(String fieldName, Long value) - throws NoSuchFieldException, IllegalAccessException { - Field field = getClass().getDeclaredField(fieldName); - field.set(this, value); + throws NoSuchFieldException, IllegalAccessException { + Field field = getClass().getDeclaredField(fieldName); + field.set(this, value); } } diff --git a/src/main/java/fr/insee/pearljam/api/dto/state/StateCountDto.java b/src/main/java/fr/insee/pearljam/api/dto/state/StateCountDto.java index 0f540c6c..1248d4a5 100644 --- a/src/main/java/fr/insee/pearljam/api/dto/state/StateCountDto.java +++ b/src/main/java/fr/insee/pearljam/api/dto/state/StateCountDto.java @@ -34,6 +34,7 @@ public class StateCountDto { private Long nvaCount; private Long npaCount; private Long npiCount; + private Long npxCount; private Long rowCount; private Long total; @@ -316,6 +317,14 @@ public void setNpiCount(Long npiCount) { this.npiCount = npiCount; } + public Long getNpxCount() { + return npxCount; + } + + public void setNpxCount(Long npxCount) { + this.npxCount = npxCount; + } + public Long getRowCount() { return rowCount; } diff --git a/src/main/java/fr/insee/pearljam/api/repository/ClosingCauseRepository.java b/src/main/java/fr/insee/pearljam/api/repository/ClosingCauseRepository.java index a5d40a94..f83a356a 100644 --- a/src/main/java/fr/insee/pearljam/api/repository/ClosingCauseRepository.java +++ b/src/main/java/fr/insee/pearljam/api/repository/ClosingCauseRepository.java @@ -19,6 +19,7 @@ public interface ClosingCauseRepository extends JpaRepository getStateClosedByClosingCauseCount(@Param("campaignId") St @Query(value = "SELECT " + "SUM(CASE WHEN type='NPA' THEN 1 ELSE 0 END) AS npaCount, " + "SUM(CASE WHEN type='NPI' THEN 1 ELSE 0 END) AS npiCount, " + + "SUM(CASE WHEN type='NPX' THEN 1 ELSE 0 END) AS npxCount, " + "SUM(CASE WHEN type='ROW' THEN 1 ELSE 0 END) AS rowCount " + "FROM ( " + "SELECT DISTINCT(survey_unit_id), type, date FROM closing_cause WHERE (survey_unit_id, date) IN (" @@ -70,6 +72,7 @@ Map getgetStateClosedByClosingCauseCountByCampaign(@Param("ca @Query(value = "SELECT " + "SUM(CASE WHEN type='NPA' THEN 1 ELSE 0 END) AS npaCount, " + "SUM(CASE WHEN type='NPI' THEN 1 ELSE 0 END) AS npiCount, " + + "SUM(CASE WHEN type='NPX' THEN 1 ELSE 0 END) AS npxCount, " + "SUM(CASE WHEN type='ROW' THEN 1 ELSE 0 END) AS rowCount " + "FROM ( " + "SELECT DISTINCT(survey_unit_id), type, date FROM closing_cause WHERE (survey_unit_id, date) IN (" @@ -96,6 +99,7 @@ Map getClosingCauseCountNotAttributed(@Param("campaignId") St @Query(value = "SELECT " + "SUM(CASE WHEN type='NPA' THEN 1 ELSE 0 END) AS npaCount, " + "SUM(CASE WHEN type='NPI' THEN 1 ELSE 0 END) AS npiCount, " + + "SUM(CASE WHEN type='NPX' THEN 1 ELSE 0 END) AS npxCount, " + "SUM(CASE WHEN type='ROW' THEN 1 ELSE 0 END) AS rowCount " + "FROM ( " + "SELECT DISTINCT(survey_unit_id), type, date FROM closing_cause WHERE (survey_unit_id, date) IN (" @@ -121,6 +125,7 @@ Map getClosingCauseCountSumByInterviewer(@Param("campaignIds" @Query(value = "SELECT " + "SUM(CASE WHEN type='NPA' THEN 1 ELSE 0 END) AS npaCount, " + "SUM(CASE WHEN type='NPI' THEN 1 ELSE 0 END) AS npiCount, " + + "SUM(CASE WHEN type='NPX' THEN 1 ELSE 0 END) AS npxCount, " + "SUM(CASE WHEN type='ROW' THEN 1 ELSE 0 END) AS rowCount " + "FROM ( " + "SELECT DISTINCT(survey_unit_id), type, date FROM closing_cause WHERE (survey_unit_id, date) IN (" @@ -146,6 +151,7 @@ Map getClosingCauseCountNotAttributed(@Param("campaignIds") L @Query(value = "SELECT " + "SUM(CASE WHEN type='NPA' THEN 1 ELSE 0 END) AS npaCount, " + "SUM(CASE WHEN type='NPI' THEN 1 ELSE 0 END) AS npiCount, " + + "SUM(CASE WHEN type='NPX' THEN 1 ELSE 0 END) AS npxCount, " + "SUM(CASE WHEN type='ROW' THEN 1 ELSE 0 END) AS rowCount " + "FROM ( " + "SELECT DISTINCT(survey_unit_id), type, date FROM closing_cause WHERE (survey_unit_id, date) IN (" @@ -169,6 +175,7 @@ Map getClosingCauseCountNotAttributed(@Param("campaignIds") L @Query(value = "SELECT " + "SUM(CASE WHEN type='NPA' THEN 1 ELSE 0 END) AS npaCount, " + "SUM(CASE WHEN type='NPI' THEN 1 ELSE 0 END) AS npiCount, " + + "SUM(CASE WHEN type='NPX' THEN 1 ELSE 0 END) AS npxCount, " + "SUM(CASE WHEN type='ROW' THEN 1 ELSE 0 END) AS rowCount " + "FROM ( " + "SELECT DISTINCT(survey_unit_id), type, date FROM closing_cause WHERE (survey_unit_id, date) IN (" @@ -191,6 +198,7 @@ Map getClosingCauseCountNotAttributed(@Param("campaignIds") L @Query(value = "SELECT " + "SUM(CASE WHEN type='NPA' THEN 1 ELSE 0 END) AS npaCount, " + "SUM(CASE WHEN type='NPI' THEN 1 ELSE 0 END) AS npiCount, " + + "SUM(CASE WHEN type='NPX' THEN 1 ELSE 0 END) AS npxCount, " + "SUM(CASE WHEN type='ROW' THEN 1 ELSE 0 END) AS rowCount " + "FROM ( " + "SELECT DISTINCT(survey_unit_id), type, date " diff --git a/src/main/java/fr/insee/pearljam/api/repository/ContactOutcomeRepository.java b/src/main/java/fr/insee/pearljam/api/repository/ContactOutcomeRepository.java index 4bfdcc58..7d9cfabc 100644 --- a/src/main/java/fr/insee/pearljam/api/repository/ContactOutcomeRepository.java +++ b/src/main/java/fr/insee/pearljam/api/repository/ContactOutcomeRepository.java @@ -41,10 +41,11 @@ public interface ContactOutcomeRepository extends JpaRepository findContactOutcomeTypeByInterviewerAndCampaign(@Param("c + "SUM(CASE WHEN type='UCD' THEN 1 ELSE 0 END) AS ucdCount, " + "SUM(CASE WHEN type='UTR' THEN 1 ELSE 0 END) AS utrCount, " + "SUM(CASE WHEN type='ALA' THEN 1 ELSE 0 END) AS alaCount, " - + "SUM(CASE WHEN type='DUK' THEN 1 ELSE 0 END) AS acpCount, " + "SUM(CASE WHEN type='DCD' THEN 1 ELSE 0 END) AS dcdCount, " + "SUM(CASE WHEN type='NUH' THEN 1 ELSE 0 END) AS nuhCount, " - + "SUM(CASE WHEN type='NER' THEN 1 ELSE 0 END) AS nerCount, " + + "SUM(CASE WHEN type='DUK' THEN 1 ELSE 0 END) AS dukCount, " + + "SUM(CASE WHEN type='DUU' THEN 1 ELSE 0 END) AS duuCount, " + + "SUM(CASE WHEN type='NOA' THEN 1 ELSE 0 END) AS noaCount, " + "COUNT(1) AS total " + "FROM ( " + "SELECT DISTINCT(survey_unit_id), type, date FROM contact_outcome WHERE (survey_unit_id, date) IN (" @@ -115,10 +117,11 @@ Map findContactOutcomeTypeNotAttributed(@Param("campaignId") + "SUM(CASE WHEN type='UCD' THEN 1 ELSE 0 END) AS ucdCount, " + "SUM(CASE WHEN type='UTR' THEN 1 ELSE 0 END) AS utrCount, " + "SUM(CASE WHEN type='ALA' THEN 1 ELSE 0 END) AS alaCount, " - + "SUM(CASE WHEN type='DUK' THEN 1 ELSE 0 END) AS acpCount, " + "SUM(CASE WHEN type='DCD' THEN 1 ELSE 0 END) AS dcdCount, " + "SUM(CASE WHEN type='NUH' THEN 1 ELSE 0 END) AS nuhCount, " - + "SUM(CASE WHEN type='NER' THEN 1 ELSE 0 END) AS nerCount, " + + "SUM(CASE WHEN type='DUK' THEN 1 ELSE 0 END) AS dukCount, " + + "SUM(CASE WHEN type='DUU' THEN 1 ELSE 0 END) AS duuCount, " + + "SUM(CASE WHEN type='NOA' THEN 1 ELSE 0 END) AS noaCount, " + "COUNT(1) AS total " + "FROM ( " + "SELECT DISTINCT(survey_unit_id), type, date FROM contact_outcome WHERE (survey_unit_id, date) IN (" @@ -148,10 +151,11 @@ Map findContactOutcomeTypeNotAttributed(@Param("campaignId") + "SUM(CASE WHEN type='UCD' THEN 1 ELSE 0 END) AS ucdCount, " + "SUM(CASE WHEN type='UTR' THEN 1 ELSE 0 END) AS utrCount, " + "SUM(CASE WHEN type='ALA' THEN 1 ELSE 0 END) AS alaCount, " - + "SUM(CASE WHEN type='DUK' THEN 1 ELSE 0 END) AS acpCount, " + "SUM(CASE WHEN type='DCD' THEN 1 ELSE 0 END) AS dcdCount, " + "SUM(CASE WHEN type='NUH' THEN 1 ELSE 0 END) AS nuhCount, " - + "SUM(CASE WHEN type='NER' THEN 1 ELSE 0 END) AS nerCount, " + + "SUM(CASE WHEN type='DUK' THEN 1 ELSE 0 END) AS dukCount, " + + "SUM(CASE WHEN type='DUU' THEN 1 ELSE 0 END) AS duuCount, " + + "SUM(CASE WHEN type='NOA' THEN 1 ELSE 0 END) AS noaCount, " + "COUNT(1) AS total " + "FROM ( " + "SELECT DISTINCT(survey_unit_id), type, date FROM contact_outcome WHERE (survey_unit_id, date) IN (" diff --git a/src/main/java/fr/insee/pearljam/api/repository/SurveyUnitRepository.java b/src/main/java/fr/insee/pearljam/api/repository/SurveyUnitRepository.java index d5eef9d9..f8ee0d53 100644 --- a/src/main/java/fr/insee/pearljam/api/repository/SurveyUnitRepository.java +++ b/src/main/java/fr/insee/pearljam/api/repository/SurveyUnitRepository.java @@ -10,6 +10,7 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; +import fr.insee.pearljam.api.domain.IdentificationConfiguration; import fr.insee.pearljam.api.domain.SurveyUnit; /** @@ -78,26 +79,71 @@ public interface SurveyUnitRepository extends JpaRepository ")") List findAllSurveyUnitsInProcessingPhase(Long date); + @Query(value="SELECT su.id FROM SurveyUnit su " + + "WHERE su.organizationUnit.id IN (:lstOuId) " + + // in campaign with expected IdentificationConfiguration + "AND su.campaign.identificationConfiguration = :config " + + // in processing phase + "AND EXISTS (SELECT vi FROM Visibility vi " + + "WHERE vi.campaign.id = su.campaign.id " + + "AND vi.organizationUnit.id = su.organizationUnit.id " + + "AND vi.collectionEndDate < :date " + + "AND vi.endDate > :date) " + + "AND NOT EXISTS (" + + "SELECT st FROM State st WHERE " + + "st.surveyUnit.id = su.id " + + "AND st.type IN ('CLO', 'FIN', 'TBR') " + + ")") + List findSurveyUnitIdsOfOrganizationUnitsInProcessingPhaseByIdentificationConfiguration(@Param("date") Long date, @Param("lstOuId") List lstOuId, @Param("config") IdentificationConfiguration config); + + @Query(value="SELECT su FROM SurveyUnit su " + + "WHERE su.id IN (:ids) " + + // Contact outcome must be null or INA + "AND NOT EXISTS ( " + + "SELECT 1 FROM ContactOutcome co WHERE co.surveyUnit.id = su.id " + + "AND co.type <> 'INA' " + + ")") + List findClosableNoIdentSurveyUnitId(@Param("ids") List ids); + @Query(value="SELECT su FROM SurveyUnit su " + - "WHERE su.organizationUnit.id IN (:lstOuId) " + - - // Contact outcome must be null or INA - "AND NOT EXISTS ( " + + "WHERE su.id IN (:ids) " + + "AND ( "+ + // First case : Contact outcome is IMP and identification not finished + "( " + + "EXISTS ( " + + "SELECT 1 FROM ContactOutcome co WHERE co.surveyUnit.id = su.id " + + "AND co.type = 'IMP' " + + ") "+ + // and missing/incomplete identification + "AND ( "+ + "su.identification IS NULL " + + "OR (su.identification.identification IS NULL ) " + + "OR (su.identification.identification = 'IDENTIFIED' AND su.identification.access IS NULL ) " + + "OR (su.identification.identification = 'IDENTIFIED' AND su.identification.access IS NOT NULL AND su.identification.situation IS NULL ) " + + "OR (su.identification.identification = 'IDENTIFIED' AND su.identification.access IS NOT NULL AND su.identification.situation = 'ORDINARY' AND su.identification.category IS NULL ) " + + "OR (su.identification.identification = 'IDENTIFIED' AND su.identification.access IS NOT NULL AND su.identification.situation = 'ORDINARY' AND su.identification.category IN ('PRIMARY', 'OCCASIONAL', 'UNKNOWN') AND su.identification.occupant IS NULL ) " + + ") "+ + ") " + + "OR "+ + "( " + + // Second case : contact outcome must be null or INA or NOA + "NOT EXISTS ( "+ "SELECT 1 FROM ContactOutcome co WHERE co.surveyUnit.id = su.id " + - "AND co.type <> 'INA' ) " + - - "AND EXISTS (SELECT vi FROM Visibility vi " + - "WHERE vi.campaign.id = su.campaign.id " + - "AND vi.organizationUnit.id = su.organizationUnit.id " + - "AND vi.collectionEndDate < :date " + - "AND vi.endDate > :date) " + - "AND NOT EXISTS (" + - "SELECT st FROM State st WHERE " + - "st.surveyUnit.id = su.id " + - "AND st.type IN ('CLO', 'FIN', 'TBR') " + - ")") - List findAllSurveyUnitsOfOrganizationUnitsInProcessingPhase(@Param("date") Long date, @Param("lstOuId") List lstOuId); - + "AND co.type NOT IN('INA','NOA')"+ + " ) " + + // and identification is not started / incomplete / and doesn't lead to Out_of_scope (business rule here, not an enum) + "AND ( "+ + "su.identification IS NULL " + + "OR su.identification.identification IS NULL " + + "OR (su.identification.identification = 'IDENTIFIED' AND su.identification.access IS NULL ) " + + "OR (su.identification.identification = 'IDENTIFIED' AND su.identification.access IS NOT NULL AND su.identification.situation IS NULL ) " + + "OR (su.identification.identification = 'IDENTIFIED' AND su.identification.access IS NOT NULL AND su.identification.situation = 'ORDINARY' AND su.identification.category NOT IN ('SECONDARY', 'VACANT') ) " + + ") " + + ") " + + " )" + ) + List findClosableIascoSurveyUnitId(@Param("ids") List ids); + Set findByCampaignIdAndOrganizationUnitIdIn(String id, List lstOuId); List findByInterviewerIdIgnoreCase(String id); diff --git a/src/main/java/fr/insee/pearljam/api/service/impl/SurveyUnitServiceImpl.java b/src/main/java/fr/insee/pearljam/api/service/impl/SurveyUnitServiceImpl.java index deb08bb4..27d62eef 100644 --- a/src/main/java/fr/insee/pearljam/api/service/impl/SurveyUnitServiceImpl.java +++ b/src/main/java/fr/insee/pearljam/api/service/impl/SurveyUnitServiceImpl.java @@ -8,12 +8,14 @@ import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; +import java.util.stream.Stream; import javax.servlet.http.HttpServletRequest; import com.fasterxml.jackson.databind.JsonNode; import fr.insee.pearljam.api.domain.*; import fr.insee.pearljam.api.repository.*; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -439,26 +441,40 @@ public Set getSurveyUnitByCampaign(String campaignId, Str if (lstSurveyUnit.isEmpty()) { LOGGER.warn("No Survey Unit found for the user {}", userId); } - return lstSurveyUnit.stream().map(su -> new SurveyUnitCampaignDto(su)).collect(Collectors.toSet()); + return lstSurveyUnit.stream().map(SurveyUnitCampaignDto::new).collect(Collectors.toSet()); } public List getClosableSurveyUnits(HttpServletRequest request, String userId) { List lstOuId = userService.getUserOUs(userId, true).stream().map(OrganizationUnitDto::getId) .collect(Collectors.toList()); - List suList = surveyUnitRepository - .findAllSurveyUnitsOfOrganizationUnitsInProcessingPhase(System.currentTimeMillis(), lstOuId); - List lstResult = suList.stream().map(su -> new SurveyUnitCampaignDto(su)) + List noIdentSurveyUnitIds = surveyUnitRepository + .findSurveyUnitIdsOfOrganizationUnitsInProcessingPhaseByIdentificationConfiguration( + System.currentTimeMillis(), lstOuId, IdentificationConfiguration.NOIDENT); + List iascoSurveyUnitIds = surveyUnitRepository + .findSurveyUnitIdsOfOrganizationUnitsInProcessingPhaseByIdentificationConfiguration( + System.currentTimeMillis(), lstOuId, IdentificationConfiguration.IASCO); + + // apply different business rules to select SU + List noIdentSurveyUnitsToCheck=surveyUnitRepository.findClosableNoIdentSurveyUnitId(noIdentSurveyUnitIds); + List iascoSurveyUnitsToCheck = surveyUnitRepository.findClosableIascoSurveyUnitId(iascoSurveyUnitIds); + // merge lists + List suToCheck = Stream.concat(noIdentSurveyUnitsToCheck.stream(), iascoSurveyUnitsToCheck.stream()) .collect(Collectors.toList()); + Map mapQuestionnaireStateBySu = null; + try { mapQuestionnaireStateBySu = getQuestionnaireStatesFromDataCollection(request, - lstResult.stream().map(SurveyUnitCampaignDto::getId).collect(Collectors.toList())); + suToCheck.stream().map(SurveyUnit::getId).collect(Collectors.toList())); } catch (Exception e) { - LOGGER.error("Could not get data collection API"); + LOGGER.error("Could not get data collection API : " + e.getMessage()); LOGGER.error("All questionnaire states will be considered null"); } + List lstResult = suToCheck.stream().map(SurveyUnitCampaignDto::new) + .collect(Collectors.toList()); + Map map = mapQuestionnaireStateBySu; if (map != null) { lstResult.forEach(su -> su.setQuestionnaireState(map.get(su.getId()))); diff --git a/src/main/resources/dataset/insert_test_data.sql b/src/main/resources/dataset/insert_test_data.sql index 5000499c..a6e4d6b0 100644 --- a/src/main/resources/dataset/insert_test_data.sql +++ b/src/main/resources/dataset/insert_test_data.sql @@ -115,4 +115,5 @@ INSERT INTO public.comment (type, value, survey_unit_id) VALUES ('INTERVIEWER', INSERT INTO public.closing_cause (date, type, survey_unit_id) VALUES ((SELECT extract(epoch from (LOCALTIMESTAMP - INTERVAL '3 DAYS'))*1000), 'NPI', '11'); -INSERT INTO public.identification (survey_unit_id, identification,access,situation,category,occupant) VALUES ('11', 'IDENTIFIED', 'ACC', 'ORDINARY', 'PRIMARY', 'IDENTIFIED'); \ No newline at end of file +INSERT INTO public.identification (survey_unit_id, identification,access,situation,category,occupant) VALUES ('11', 'IDENTIFIED', 'ACC', 'ORDINARY', 'PRIMARY', 'IDENTIFIED'); +INSERT INTO public.identification (survey_unit_id, identification,access,situation,category,occupant) VALUES ('21', 'IDENTIFIED', 'ACC', 'ORDINARY', 'PRIMARY', 'IDENTIFIED'); \ No newline at end of file diff --git a/src/test/java/fr/insee/pearljam/api/authKeycloak/TestAuthKeyCloak.java b/src/test/java/fr/insee/pearljam/api/authKeycloak/TestAuthKeyCloak.java index f13e50a5..5d93b556 100644 --- a/src/test/java/fr/insee/pearljam/api/authKeycloak/TestAuthKeyCloak.java +++ b/src/test/java/fr/insee/pearljam/api/authKeycloak/TestAuthKeyCloak.java @@ -386,7 +386,6 @@ void testGetCampaignInterviewerStateCountNotAttributed() throws InterruptedExcep .assertThat().body("tbrCount", equalTo(1)).and() .assertThat().body("finCount", equalTo(0)).and() .assertThat().body("cloCount", equalTo(0)).and() - // .assertThat().body("qnaFinCount",equalTo(0)).and() .assertThat().body("nvaCount", equalTo(0)).and() .assertThat().body("npaCount", equalTo(0)).and() .assertThat().body("npiCount", equalTo(0)).and() @@ -406,10 +405,11 @@ void testGetContactOutcomeCountNotattributed() throws InterruptedException, Json .and().assertThat().body("ucdCount", equalTo(0)) .and().assertThat().body("utrCount", equalTo(0)) .and().assertThat().body("alaCount", equalTo(0)) - .and().assertThat().body("acpCount", equalTo(1)) .and().assertThat().body("dcdCount", equalTo(0)) .and().assertThat().body("nuhCount", equalTo(0)) - .and().assertThat().body("nerCount", equalTo(0)); + .and().assertThat().body("dukCount", equalTo(1)) + .and().assertThat().body("duuCount", equalTo(0)) + .and().assertThat().body("noaCount", equalTo(0)); } /** @@ -981,10 +981,11 @@ void testGetContactOutcomeCountByCampaign() throws InterruptedException, JsonPro .and().assertThat().body("organizationUnits[0].ucdCount", equalTo(0)) .and().assertThat().body("organizationUnits[0].utrCount", equalTo(0)) .and().assertThat().body("organizationUnits[0].alaCount", equalTo(0)) - .and().assertThat().body("organizationUnits[0].acpCount", equalTo(1)) .and().assertThat().body("organizationUnits[0].dcdCount", equalTo(0)) .and().assertThat().body("organizationUnits[0].nuhCount", equalTo(0)) - .and().assertThat().body("organizationUnits[0].nerCount", equalTo(0)); + .and().assertThat().body("organizationUnits[0].dukCount", equalTo(1)) + .and().assertThat().body("organizationUnits[0].duuCount", equalTo(0)) + .and().assertThat().body("organizationUnits[0].noaCount", equalTo(0)); } /** @@ -1007,10 +1008,12 @@ void testGetContactOutcomeCountAllCampaign() throws InterruptedException, JsonPr .and().assertThat().body("[0].ucdCount", equalTo(0)) .and().assertThat().body("[0].utrCount", equalTo(0)) .and().assertThat().body("[0].alaCount", equalTo(0)) - .and().assertThat().body("[0].acpCount", equalTo(1)) .and().assertThat().body("[0].dcdCount", equalTo(0)) .and().assertThat().body("[0].nuhCount", equalTo(0)) - .and().assertThat().body("[0].nerCount", equalTo(0)); + .and().assertThat().body("[0].nuhCount", equalTo(0)) + .and().assertThat().body("[0].dukCount", equalTo(1)) + .and().assertThat().body("[0].duuCount", equalTo(0)) + .and().assertThat().body("[0].noaCount", equalTo(0)); } /** @@ -1455,10 +1458,11 @@ void testGetContactOutcomeCountByCampaignAndInterviewer() .and().assertThat().body("ucdCount", equalTo(0)) .and().assertThat().body("utrCount", equalTo(0)) .and().assertThat().body("alaCount", equalTo(0)) - .and().assertThat().body("acpCount", equalTo(0)) .and().assertThat().body("dcdCount", equalTo(0)) .and().assertThat().body("nuhCount", equalTo(0)) - .and().assertThat().body("nerCount", equalTo(0)); + .and().assertThat().body("dukCount", equalTo(0)) + .and().assertThat().body("duuCount", equalTo(0)) + .and().assertThat().body("noaCount", equalTo(0)); } /**