Skip to content

Commit

Permalink
Merge pull request #14 from virtualidentityag/develop
Browse files Browse the repository at this point in the history
Update Staging
  • Loading branch information
koepferd authored Nov 27, 2024
2 parents 9ce177a + 62970d6 commit 4a5fff7
Show file tree
Hide file tree
Showing 25 changed files with 867 additions and 196 deletions.
73 changes: 73 additions & 0 deletions api/agencyservice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,68 @@ paths:
description: FORBIDDEN - no/invalid CSRF token
500:
description: INTERNAL SERVER ERROR - server encountered unexpected condition
/agencies/by-tenant:
get:
tags:
- agency-controller
summary: 'Returns a list of agencies from a specific tenant (retrieved from the security context).
The security context is retrieved from the bearer token, so it is not passed as a request parameter.
If no agency is found No Content is being returned. [Authorization: none]'
operationId: getTenantAgencies
parameters:
- name: postcode
in: query
required: true
description: The postcode the user entered
schema:
type: string
minLength: 5
maxLength: 5
example: "56789"
- name: topicId
in: query
required: true
description: The main topic of the registration form
schema:
type: integer
format: int32
example: 7
responses:
200:
description: OK - successfull operation
content:
'application/json':
schema:
type: array
items:
$ref: '#/components/schemas/FullAgencyResponseDTO'
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/topics:
get:
tags:
- agency-controller
summary: 'Returns a list of all the topics assigned 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 +260,17 @@ components:
type: boolean
example: "false"

AgencyTopicsDTO:
type: object
properties:
id:
type: integer
format: int64
example: 684
name:
type: string
example: "Adoption and fostering a child"

DemographicsDTO:
type: object
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public enum Authority {
TENANT_ADMIN("tenant-admin", AuthorityValue.TENANT_ADMIN),
RESTRICTED_AGENCY_ADMIN("restricted-agency-admin", AuthorityValue.RESTRICTED_AGENCY_ADMIN, AuthorityValue.SEARCH_AGENCIES),

RESTRICTED_CONSULTANT_ADMIN("restricted-consultant-admin", AuthorityValue.SEARCH_AGENCIES);
RESTRICTED_CONSULTANT_ADMIN("restricted-consultant-admin", AuthorityValue.SEARCH_AGENCIES),

ADVICE_SEEKER("user", AuthorityValue.SEARCH_AGENCIES_WITHIN_TENANT);

private final String roleName;
private final List<String> authorities;
Expand Down Expand Up @@ -55,6 +57,7 @@ private AuthorityValue() {}
public static final String SEARCH_AGENCIES = PREFIX + "SEARCH_AGENCIES";
public static final String TENANT_ADMIN = PREFIX + "TENANT_ADMIN";
public static final String RESTRICTED_AGENCY_ADMIN = PREFIX + "RESTRICTED_AGENCY_ADMIN";
public static final String SEARCH_AGENCIES_WITHIN_TENANT = PREFIX + "SEARCH_AGENCIES_WITHIN_TENANT";

}

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 @@ -54,6 +57,17 @@ public ResponseEntity<List<FullAgencyResponseDTO>> getAgencies(
: new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@Override
public ResponseEntity<List<FullAgencyResponseDTO>> getTenantAgencies(String postcode,
Integer topicId) {

var agencies = agencyService.getAgencies(postcode, topicId);

return !CollectionUtils.isEmpty(agencies)
? new ResponseEntity<>(agencies, HttpStatus.OK)
: new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

/**
* Returns information of the provided agencies.
*
Expand Down Expand Up @@ -84,4 +98,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.NO_CONTENT)
: 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 ORDER_BY_TOPIC = "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
+ ORDER_BY_TOPIC,
nativeQuery = true)
List<Integer> findAllAgenciesTopics(Long tenantId);

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
+ ORDER_BY_TOPIC,
nativeQuery = true)
List<Integer> findAllAgenciesTopics(Long tenantId);

@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 @@ -69,6 +69,8 @@ public class AgencyService {
@Value("${feature.multitenancy.with.single.domain.enabled}")
private boolean multitenancyWithSingleDomain;

private static final String DB_POSTCODES_ERROR = "Database error while getting postcodes";

/**
* Returns a list of {@link AgencyResponseDTO} which match the provided agencyIds.
*
Expand All @@ -81,7 +83,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 +103,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 +128,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 +144,18 @@ public List<FullAgencyResponseDTO> getAgencies(Optional<String> postCode,
return mutableResponseDTO;
}

public List<FullAgencyResponseDTO> getAgencies(String postCode, Integer topicId) {

var agencies = findAgenciesForCurrentTenant(postCode, topicId);
return agencies.stream()
.map(this::convertToFullAgencyResponseDTO)
.toList();
}

public List<Integer> getAgenciesTopics() {
return agencyRepository.findAllAgenciesTopics(TenantContext.getCurrentTenant());
}

private Optional<Integer> getConsultingTypeIdForSearch(int consultingTypeId) {
return multitenancyWithSingleDomain ? Optional.empty() : Optional.of(consultingTypeId);
}
Expand All @@ -153,14 +164,8 @@ private List<Agency> findAgencies(Optional<String> postCode, Optional<Integer> c
Optional<Integer> optionalTopicId, Optional<Integer> age,
Optional<String> gender, Optional<String> counsellingRelation) {

AgencySearch agencySearch = AgencySearch.builder()
.postCode(postCode)
.consultingTypeId(consultingTypeId)
.topicId(optionalTopicId)
.age(age)
.gender(gender)
.counsellingRelation(counsellingRelation)
.build();
AgencySearch agencySearch = buildAgencySearch(postCode,
consultingTypeId, optionalTopicId, age, gender, counsellingRelation);

if (demographicsFeatureEnabled) {
assertAgeAndGenderAreProvided(age, gender);
Expand All @@ -185,10 +190,32 @@ private List<Agency> findAgencies(AgencySearch agencySearch) {
TenantContext.getCurrentTenant());
} catch (DataAccessException ex) {
throw new InternalServerErrorException(LogService::logDatabaseError,
"Database error while getting postcodes");
DB_POSTCODES_ERROR);
}
}

private List<Agency> findAgenciesForCurrentTenant(String postCode, Integer topicId) {

AgencySearch agencySearch = buildAgencySearch(Optional.of(postCode),
Optional.empty(), Optional.of(topicId), Optional.empty(),
Optional.empty(), Optional.empty());

return findAgenciesWithTopicForCurrentTenant(agencySearch);
}

private static AgencySearch buildAgencySearch(Optional<String> postCode,
Optional<Integer> consultingTypeId, Optional<Integer> optionalTopicId, Optional<Integer> age,
Optional<String> gender, Optional<String> counsellingRelation) {
return AgencySearch.builder()
.postCode(postCode)
.consultingTypeId(consultingTypeId)
.topicId(optionalTopicId)
.age(age)
.gender(gender)
.counsellingRelation(counsellingRelation)
.build();
}

private void assertTopicIdIsProvided(Optional<Integer> topicId) {
if (!topicId.isPresent()) {
throw new BadRequestException("Topic id not provided in the search");
Expand Down Expand Up @@ -249,7 +276,23 @@ private List<Agency> findAgenciesWithTopic(AgencySearch agencySearch) {

} catch (DataAccessException ex) {
throw new InternalServerErrorException(LogService::logDatabaseError,
"Database error while getting postcodes");
DB_POSTCODES_ERROR);
}
}

private List<Agency> findAgenciesWithTopicForCurrentTenant(AgencySearch agencySearch) {
try {
return agencyRepository
.searchWithTopic(agencySearch.getPostCode().orElse(null), agencySearch.getPostCode().orElse("").length(),
agencySearch.getConsultingTypeId().orElse(null),
agencySearch.getTopicId().orElseThrow(),
agencySearch.getAge().orElse(null), agencySearch.getGender().orElse(null),
agencySearch.getCounsellingRelation().orElse(null),
TenantContext.getCurrentTenant());

} catch (DataAccessException ex) {
throw new InternalServerErrorException(LogService::logDatabaseError,
DB_POSTCODES_ERROR);
}
}

Expand Down
Loading

0 comments on commit 4a5fff7

Please sign in to comment.