Skip to content

Commit

Permalink
feat: CARITAS-286
Browse files Browse the repository at this point in the history
* create new endpoint to return all topics from all agencies of a tenant
  • Loading branch information
Leandro13Silva13 committed Oct 28, 2024
1 parent f2c977f commit 6758ae3
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 5 deletions.
32 changes: 32 additions & 0 deletions api/agencyservice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,27 @@ paths:
description: FORBIDDEN - no/invalid CSRF token
500:
description: INTERNAL SERVER ERROR - server encountered unexpected condition
/agencies/topics:
get:
tags:
- agency-controller
summary: 'Returns a list of all the topics assign to all the agencies of a specific tenant'
operationId: getAgenciesTopics
responses:
200:
description: OK - successfull operation
content:
'application/json':
schema:
type: array
items:
$ref: '#/components/schemas/AgencyTopicsDTO'
400:
description: BAD REQUEST - invalid/incomplete request or body object
403:
description: FORBIDDEN - no/invalid CSRF token
500:
description: INTERNAL SERVER ERROR - server encountered unexpected condition
/agencies/{agencyIds}:
get:
tags:
Expand Down Expand Up @@ -198,6 +219,17 @@ components:
type: boolean
example: "false"

AgencyTopicsDTO:
type: object
properties:
id:
type: integer
format: int64
example: 684
name:
type: string
example: "Counselling"

DemographicsDTO:
type: object
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import static java.util.Optional.ofNullable;

import de.caritas.cob.agencyservice.api.model.AgencyResponseDTO;
import de.caritas.cob.agencyservice.api.model.AgencyTopicsDTO;
import de.caritas.cob.agencyservice.api.model.FullAgencyResponseDTO;
import de.caritas.cob.agencyservice.api.service.AgencyService;
import de.caritas.cob.agencyservice.api.service.TopicEnrichmentService;
import de.caritas.cob.agencyservice.generated.api.controller.AgenciesApi;
import io.swagger.annotations.Api;
import java.util.List;
Expand All @@ -27,6 +29,7 @@
public class AgencyController implements AgenciesApi {

private final @NonNull AgencyService agencyService;
private final @NonNull TopicEnrichmentService topicEnrichmentService;

/**
* Gets a randomly sorted list of AgencyResponseDTOs (from database) and returns the list and a
Expand Down Expand Up @@ -84,4 +87,19 @@ public ResponseEntity<List<AgencyResponseDTO>> getAgenciesByConsultingType(

return new ResponseEntity<>(agencies, HttpStatus.OK);
}

/**
* Returns all the topics from all the agencies of a specific tenant
*
* @return lest of topics
*/
@Override
public ResponseEntity<List<AgencyTopicsDTO>> getAgenciesTopics() {

var topics = this.agencyService.getAgenciesTopics();
var enrichedTopics = topicEnrichmentService.enrichTopicIdsWithTopicData(topics);

return enrichedTopics.isEmpty() ? new ResponseEntity<>(HttpStatus.NOT_FOUND)
: new ResponseEntity<>(enrichedTopics, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@ public interface AgencyRepository extends JpaRepository<Agency, Long> {
+ "AND ((:gender IS NULL) OR (a.genders LIKE CONCAT('%,',:gender,'%') OR a.genders LIKE CONCAT(:gender,'%'))) "
+ "AND a.delete_date IS NULL ";

String SELECT_ALL_AGENCIES_TOPICS = "SELECT distinct(at.topic_id) FROM agency_topic at "
+ "INNER JOIN agency a ON a.id = at.agency_id ";

String GROUP_BY_ORDER_BY = "GROUP BY a.id "
+ "ORDER BY a.postcode DESC";

String TOPIC_ORDER_BY = "ORDER BY at.topic_id";

/**
* Returns a list of {@link Agency}s that are assigned to the given post code.
*
Expand Down Expand Up @@ -80,6 +85,12 @@ List<Agency> searchWithTopic(@Param(value = "postcode") String postCode,
@Param(value = "counselling_relation") String counsellingRelation,
Long tenantId);

@Query(
value = SELECT_ALL_AGENCIES_TOPICS
+ TOPIC_ORDER_BY,
nativeQuery = true)
List<Integer> findAllAgenciesTopics();

Optional<Agency> findByIdAndDeleteDateNull(Long agencyId);

List<Agency> findByIdIn(List<Long> agencyIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

/**
Expand Down Expand Up @@ -51,6 +50,13 @@ List<Agency> searchWithTopic(@Param(value = "postcode") String postCode,
@Param(value = "counselling_relation") String counsellingRelation,
Long tenantId);

@Query(
value = SELECT_ALL_AGENCIES_TOPICS
+ AND_A_TENANT_ID_FILTER
+ TOPIC_ORDER_BY,
nativeQuery = true)
List<Integer> findAllAgenciesTopics();

@Query("select a from Agency as a where a.id = :agencyId ")
Optional<Agency> findById(Long agencyId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ List<Agency> searchWithTopic(@Param(value = "postcode") String postCode,
@Param(value = "gender") String gender,
Long tenantId);

@Query(
value = SELECT_ALL_AGENCIES_TOPICS
+ TOPIC_ORDER_BY,
nativeQuery = true)
List<Integer> findAllAgenciesTopics();

Optional<Agency> findByIdAndDeleteDateNull(Long agencyId);

List<Agency> findByIdIn(List<Long> agencyIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public List<AgencyResponseDTO> getAgencies(List<Long> agencyIds) {
.toList();
}


/**
* Returns a list of {@link AgencyResponseDTO} which match the provided consulting type.
*
Expand All @@ -102,7 +101,6 @@ public List<AgencyResponseDTO> getAgencies(int consultingTypeId) {
}
}


public List<FullAgencyResponseDTO> getAgencies(Optional<String> postCode, int consultingTypeId,
Optional<Integer> topicId) {
return getAgencies(postCode, consultingTypeId, topicId, Optional.empty(), Optional.empty(), Optional.empty());
Expand All @@ -128,7 +126,6 @@ public List<FullAgencyResponseDTO> getAgencies(Optional<String> postCode,
return Collections.emptyList();
}


var agencies = findAgencies(postCode, getConsultingTypeIdForSearch(consultingTypeId), topicId,
age, gender, counsellingRelation);
Collections.shuffle(agencies);
Expand All @@ -145,6 +142,10 @@ public List<FullAgencyResponseDTO> getAgencies(Optional<String> postCode,
return mutableResponseDTO;
}

public List<Integer> getAgenciesTopics() {
return getAgencyRepositoryForSearch().findAllAgenciesTopics();
}

private Optional<Integer> getConsultingTypeIdForSearch(int consultingTypeId) {
return multitenancyWithSingleDomain ? Optional.empty() : Optional.of(consultingTypeId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package de.caritas.cob.agencyservice.api.service;

import de.caritas.cob.agencyservice.api.model.AgencyTopicsDTO;
import de.caritas.cob.agencyservice.topicservice.generated.web.model.TopicDTO;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@ConditionalOnExpression("${feature.topics.enabled:true}")
@Slf4j
public class TopicEnrichmentService {

private final @NonNull TopicService topicService;

public List<AgencyTopicsDTO> enrichTopicIdsWithTopicData(List<Integer> topicIds) {
log.debug("Enriching topic ids with topics short titles");
var availableTopics = topicService.getAllTopics();
log.debug("Enriching topic ids list with size: {} ", topicIds.size());
log.debug("Available topics list has size: {} ", availableTopics.size());
return enrichTopicIds(availableTopics, topicIds);
}

private List<AgencyTopicsDTO> enrichTopicIds(List<TopicDTO> availableTopics, List<Integer> topicIds) {
// Create a map of availableTopics to quickly access TopicDTO by id
Map<Long, String> topicMap = availableTopics.stream()
.collect(Collectors.toMap(TopicDTO::getId, TopicDTO::getName));

// Filter and map the topicIds to AgencyTopicsDTOs, using the topicMap for fast lookup
return topicIds.stream()
.map(id -> {
Long topicId = Long.valueOf(id);
String topicName = topicMap.get(topicId);

// Only create an AgencyTopicsDTO if the topicName exists
if (topicName != null) {
return new AgencyTopicsDTO()
.id(topicId)
.name(topicName);
}
return null;
})
.filter(Objects::nonNull)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,4 @@ private Agency newAgencyWithTopics(ArrayList<AgencyTopic> agencyTopics) {
agency.setAgencyTopics(agencyTopics);
return agency;
}
}
}

0 comments on commit 6758ae3

Please sign in to comment.