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

fix: optimize endpoint GET contacts/{id} #88

Merged
merged 3 commits into from
Oct 21, 2024
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</parent>
<groupId>fr.insee.survey</groupId>
<artifactId>platine-management</artifactId>
<version>2.6.0</version>
<version>2.6.1</version>
<name>platine-management</name>
<description>REST API for communication between DB and Platine-Management UI and Platine-My-Surveys UI</description>
<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ public ContactPage getContacts(
@GetMapping(value = Constants.API_CONTACTS_ID)
@PreAuthorize(AuthorityPrivileges.HAS_MANAGEMENT_PRIVILEGES + " || " + AuthorityPrivileges.HAS_REPONDENT_LIMITATED_PRIVILEGES)
public ContactDetailsDto getContact(@PathVariable("id") String id) {
Contact contact = contactService.findByIdentifier(StringUtils.upperCase(id));
List<String> listCampaigns = questioningAccreditationService.findCampaignsForContactId(id);
String idContact = StringUtils.upperCase(id);
Contact contact = contactService.findByIdentifier(idContact);
List<String> listCampaigns = viewService.findDistinctCampaignByIdentifier(idContact);
return convertToContactDetailsDto(contact, listCampaigns);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,11 @@

import fr.insee.survey.datacollectionmanagement.questioning.domain.QuestioningAccreditation;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface QuestioningAccreditationRepository extends JpaRepository<QuestioningAccreditation, Long> {

String QUERY_FIND_IDCONTACT =
"select id_contact from questioning_accreditation qa "
+ " join questioning q "
+ " on q.id =qa.questioning_id "
+ " where q.id_partitioning = ?1 ";

String QUERY_FIND_IDPARTIONING =
"select q.id_partitioning from questioning q "
+ "join questioning_accreditation qa "
+ "on q.id =qa.questioning_id "
+ "where qa.id_contact =?1 ";

String FIND_METADATA_COPY =
"select "
+ " id_contact "
+ "from "
+ " questioning_accreditation qa "
+ "join questioning q on q.id = qa.questioning_id "
+" join metadata_copy mc on mc.id_partitioning = q.id_partitioning ";


String QUERY_SOURCE_YEAR_PERIOD = FIND_METADATA_COPY + " where mc.id = ?1 and mc.year_value = ?2 and mc.period_value = ?3 ";

String QUERY_SOURCE = FIND_METADATA_COPY + " where mc.id = ?1 ";

String QUERY_YEAR = FIND_METADATA_COPY + " where mc.year_value = ?1 ";

String QUERY_PERIOD = FIND_METADATA_COPY + " where mc.period_value = ?1 ";

List<QuestioningAccreditation> findByIdContact(String idContact);

@Query(nativeQuery = true, value = QUERY_FIND_IDCONTACT)
List<String> findIdContactsByPartitionigAccredications(String idPartitioning);

@Query(nativeQuery = true, value = QUERY_FIND_IDPARTIONING)
List<String> findIdPartitioningsByContactAccreditations(String idContact);

@Query(nativeQuery = true, value = QUERY_SOURCE)
List<String> findIdContactsByIdSource(String idSource);

@Query(nativeQuery = true, value = QUERY_YEAR)
List<String> findIdContactsByYear(Integer year);

@Query(nativeQuery = true, value = QUERY_PERIOD)
List<String> findIdContactsByPeriod(String period);

@Query(nativeQuery = true, value = QUERY_SOURCE_YEAR_PERIOD)
List<String> findIdContactsBySourceYearPeriod(String source, Integer year, String period);

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ public interface QuestioningAccreditationService {

List<QuestioningAccreditation> findByContactIdentifier(String id);


Page<QuestioningAccreditation> findAll(Pageable pageable);

QuestioningAccreditation findById(Long id);

QuestioningAccreditation saveQuestioningAccreditation(QuestioningAccreditation questioningAccreditation);

void deleteAccreditation(QuestioningAccreditation c);

List<String> findCampaignsForContactId(String id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@

import fr.insee.survey.datacollectionmanagement.exception.NotFoundException;
import fr.insee.survey.datacollectionmanagement.metadata.service.PartitioningService;
import fr.insee.survey.datacollectionmanagement.questioning.domain.Questioning;
import fr.insee.survey.datacollectionmanagement.questioning.domain.QuestioningAccreditation;
import fr.insee.survey.datacollectionmanagement.questioning.domain.SurveyUnit;
import fr.insee.survey.datacollectionmanagement.questioning.repository.QuestioningAccreditationRepository;
import fr.insee.survey.datacollectionmanagement.questioning.service.QuestioningAccreditationService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
@RequiredArgsConstructor
Expand All @@ -27,13 +23,6 @@ public List<QuestioningAccreditation> findByContactIdentifier(String id) {
return questioningAccreditationRepository.findByIdContact(id);
}

public Set<QuestioningAccreditation> findBySurveyUnit(SurveyUnit su) {
Set<QuestioningAccreditation> setReturn = new HashSet<>();
for (Questioning qu : su.getQuestionings()) {
setReturn.addAll(qu.getQuestioningAccreditations());
}
return setReturn;
}

@Override
public Page<QuestioningAccreditation> findAll(Pageable pageable) {
Expand All @@ -55,10 +44,4 @@ public void deleteAccreditation(QuestioningAccreditation acc) {
questioningAccreditationRepository.deleteById(acc.getId());
}

@Override
public List<String> findCampaignsForContactId(String id) {
List<QuestioningAccreditation> listContactAccreditations = findByContactIdentifier(id);
return listContactAccreditations.stream().map(acc -> partitioningService.findById(acc.getQuestioning().getIdPartitioning()).getCampaign().getId()).distinct().toList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,41 @@
@Repository
public interface ViewRepository extends PagingAndSortingRepository<View, Long>, JpaRepository<View, Long> {

static final String FIND_DISTNCT_VIEW_BY_IDENTIFIER = "select "
+ " distinct on "
+ " (v.identifier) "
+ " v.id as id, "
+ " v.identifier as identifier, "
+ " v.campaign_id as campaign_id, "
+ " v.id_su as id_su "
+ "from "
+ " \"view\" v "
+ "where "
+ " campaign_id =?1 ";
String FIND_DISTNCT_VIEW_BY_IDENTIFIER = """
select
distinct on
(v.identifier)
v.id as id,
v.identifier as identifier,
v.campaign_id as campaign_id,
v.id_su as id_su
from
view v
where
campaign_id =?1""";

String FIND_DISTNCT_CAMPAIGN_BY_IDENTIFIER = """
select
distinct v.campaign_id
from
view v
where
v.identifier = ?1""";

View findFirstByIdentifier(String identifier);

List<View> findByIdentifier(String identifier);
@Query(nativeQuery = true, value=FIND_DISTNCT_VIEW_BY_IDENTIFIER)

@Query(nativeQuery = true, value = FIND_DISTNCT_VIEW_BY_IDENTIFIER)
List<View> findDistinctViewByCampaignId(String campaignId);

@Query(nativeQuery = true, value = FIND_DISTNCT_CAMPAIGN_BY_IDENTIFIER)
List<String> findDistinctCampaignByIdentifier(String campaignId);

List<View> findByIdSu(String idSu);

List<View> findByIdSuContaining(String field);

Long countViewByIdentifierAndIdSuAndCampaignId(String identifier, String idSu, String campaignId);

List<View> findByIdentifierContainingAndIdSuNotNull(String identifier);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package fr.insee.survey.datacollectionmanagement.view.service;

import java.util.List;

import org.springframework.stereotype.Service;

import fr.insee.survey.datacollectionmanagement.metadata.domain.Campaign;
import fr.insee.survey.datacollectionmanagement.view.domain.View;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public interface ViewService {
Expand All @@ -18,6 +17,8 @@ public interface ViewService {

List<View> findViewByCampaignId(String campaignId);

List<String> findDistinctCampaignByIdentifier(String identifier);

List<View> findViewByIdSu(String idSu);

Long countViewByIdentifierIdSuCampaignId(String identifier, String idSu, String campaignId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ public List<View> findViewByCampaignId(String campaignId) {
return viewRepository.findDistinctViewByCampaignId(campaignId);
}

@Override
public List<String> findDistinctCampaignByIdentifier(String identifier) {
return viewRepository.findDistinctCampaignByIdentifier(identifier);
}



@Override
public List<View> findViewByIdSu(String idSu) {
return viewRepository.findByIdSu(idSu);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,4 @@ public QuestioningAccreditation saveQuestioningAccreditation(QuestioningAccredit
public void deleteAccreditation(QuestioningAccreditation c) {
}

@Override
public List<String> findCampaignsForContactId(String id) {
return null;
}
}
Loading