Skip to content

Commit

Permalink
fix: filter agencies of consultants
Browse files Browse the repository at this point in the history
  • Loading branch information
tkuzynow committed Nov 29, 2022
1 parent 38f036b commit 0300c30
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public Optional<Map<String, Object>> findConsultantByUsername(String username) {

public Map<String, Object> findConsultantsByInfix(
String infix,
boolean shouldFilterByAgencies,
Collection<Long> agenciesToFilterConsultants,
int pageNumber,
int pageSize,
Expand All @@ -88,7 +89,7 @@ public Map<String, Object> findConsultantsByInfix(
var direction = isAscending ? Direction.ASC : Direction.DESC;
var pageRequest = PageRequest.of(pageNumber, pageSize, direction, fieldName);
Page<ConsultantBase> consultantPage;
if (agenciesToFilterConsultants.isEmpty()) {
if (!shouldFilterByAgencies) {
consultantPage = consultantRepository.findAllByInfix(infix, pageRequest);
} else {
consultantPage =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
import de.caritas.cob.userservice.api.actions.user.DeactivateKeycloakUserActionCommand;
import de.caritas.cob.userservice.api.adapters.rocketchat.RocketChatCredentials;
import de.caritas.cob.userservice.api.adapters.web.dto.AbsenceDTO;
import de.caritas.cob.userservice.api.adapters.web.dto.AgencyAdminResponseDTO;
import de.caritas.cob.userservice.api.adapters.web.dto.ChatDTO;
import de.caritas.cob.userservice.api.adapters.web.dto.ChatInfoResponseDTO;
import de.caritas.cob.userservice.api.adapters.web.dto.ChatMembersResponseDTO;
import de.caritas.cob.userservice.api.adapters.web.dto.ConsultantAdminResponseDTO;
import de.caritas.cob.userservice.api.adapters.web.dto.ConsultantResponseDTO;
import de.caritas.cob.userservice.api.adapters.web.dto.ConsultantSearchResultDTO;
import de.caritas.cob.userservice.api.adapters.web.dto.ConsultantSessionDTO;
Expand Down Expand Up @@ -108,6 +110,7 @@
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.ws.rs.InternalServerErrorException;
Expand Down Expand Up @@ -813,6 +816,7 @@ public ResponseEntity<ConsultantSearchResultDTO> searchConsultants(
var resultMap =
accountManager.findConsultantsByInfix(
decodedInfix,
authenticatedUser.hasRestrictedAgencyPriviliges(),
getAgenciesToFilterConsultants(),
page - 1,
perPage,
Expand All @@ -822,9 +826,28 @@ public ResponseEntity<ConsultantSearchResultDTO> searchConsultants(
var result =
consultantDtoMapper.consultantSearchResultOf(resultMap, query, page, perPage, field, order);

if (authenticatedUser.hasRestrictedAgencyPriviliges()) {
if (result.getEmbedded() != null) {
result.getEmbedded().stream()
.forEach(
response ->
removeAgenciesWithoutAccessRight(response, getAgenciesToFilterConsultants()));
}
}

return ResponseEntity.ok(result);
}

private void removeAgenciesWithoutAccessRight(
ConsultantAdminResponseDTO response, Collection<Long> agenciesToFilterConsultants) {
List<AgencyAdminResponseDTO> agencies = response.getEmbedded().getAgencies();
List<AgencyAdminResponseDTO> filteredAgencies =
agencies.stream()
.filter(agency -> agenciesToFilterConsultants.contains(agency.getId()))
.collect(Collectors.toList());
response.getEmbedded().setAgencies(filteredAgencies);
}

private Collection<Long> getAgenciesToFilterConsultants() {
Collection<Long> agenciesToFilterConsultants = Lists.newArrayList();
if (authenticatedUser.hasRestrictedAgencyPriviliges()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public interface AccountManaging {

Map<String, Object> findConsultantsByInfix(
String infix,
boolean shouldFilterByAgencies,
Collection<Long> agenciesToFilterConsultants,
int pageNumber,
int pageSize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ void findConsultantsByInfix_Should_NotFilterByAgenciesIfAgencyListIsEmpty() {
.thenReturn(page);

// when
accountManager.findConsultantsByInfix("infix", Lists.newArrayList(), 1, 10, "email", true);
accountManager.findConsultantsByInfix(
"infix", false, Lists.newArrayList(), 1, 10, "email", true);

// then
Mockito.verify(consultantRepository)
Expand All @@ -54,7 +55,8 @@ void findConsultantsByInfix_Should_FilterByAgenciesIfAgencyListIsNotEmpty() {
.thenReturn(page);

// when
accountManager.findConsultantsByInfix("infix", Lists.newArrayList(1L), 1, 10, "email", true);
accountManager.findConsultantsByInfix(
"infix", true, Lists.newArrayList(1L), 1, 10, "email", true);

// then
Mockito.verify(consultantRepository)
Expand Down

0 comments on commit 0300c30

Please sign in to comment.