Skip to content

Commit

Permalink
Merge pull request #670 from Onlineberatung/OB-5382-add-tenant-name-a…
Browse files Browse the repository at this point in the history
…nd-id-to-consultant-endpoint

Ob 5382 add tenant name and id to consultant endpoint
  • Loading branch information
tkuzynow authored Sep 6, 2023
2 parents 2c3a383 + 7627140 commit e6701d1
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 55 deletions.
6 changes: 6 additions & 0 deletions api/useradminservice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1331,6 +1331,10 @@ components:
$ref: './../services/agencyadminservice.yaml#/components/schemas/AgencyAdminResponseDTO'
isGroupchatConsultant:
$ref: '#/components/schemas/IsGroupchatConsultant'
tenantId:
type: integer
tenantName:
type: string

CreateAdminDTO:
type: object
Expand Down Expand Up @@ -1425,6 +1429,8 @@ components:
type: string
tenantId:
type: string
tenantName:
type: string
tenantSubdomain:
type: string
agencies:
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/de/caritas/cob/userservice/api/AccountManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static java.util.Objects.isNull;

import de.caritas.cob.userservice.api.admin.service.tenant.TenantService;
import de.caritas.cob.userservice.api.exception.httpresponses.InternalServerErrorException;
import de.caritas.cob.userservice.api.helper.UsernameTranscoder;
import de.caritas.cob.userservice.api.model.Consultant;
Expand Down Expand Up @@ -44,6 +45,8 @@ public class AccountManager implements AccountManaging {

private final AgencyService agencyService;

private final TenantService tenantService;

private final ConsultantAgencyRepository consultantAgencyRepository;

private final SessionRepository sessionRepository;
Expand Down Expand Up @@ -105,7 +108,18 @@ public Map<String, Object> findConsultantsByInfix(
var agencyIds = userServiceMapper.agencyIdsOf(consultingAgencies);
var agencies = agencyService.getAgenciesWithoutCaching(agencyIds);

return userServiceMapper.mapOf(consultantPage, fullConsultants, agencies, consultingAgencies);
var tenantIdsToNameMap =
fullConsultants.stream()
.filter(consultant -> consultant.getTenantId() != null)
.collect(
Collectors.toMap(
Consultant::getTenantId,
consultant ->
tenantService.getRestrictedTenantData(consultant.getTenantId()).getName(),
(existing, replacement) -> existing));

return userServiceMapper.mapOf(
consultantPage, fullConsultants, agencies, consultingAgencies, tenantIdsToNameMap);
}

@Override
Expand Down
40 changes: 30 additions & 10 deletions src/main/java/de/caritas/cob/userservice/api/UserServiceMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -98,7 +99,8 @@ public Map<String, Object> mapOf(
Page<ConsultantBase> consultantPage,
List<Consultant> fullConsultants,
List<AgencyDTO> agencyDTOS,
List<ConsultantAgencyBase> consultantAgencies) {
List<ConsultantAgencyBase> consultantAgencies,
Map<Long, String> tenantIdsToNameMap) {

var agencyLookupMap =
agencyDTOS.stream().collect(Collectors.toMap(AgencyDTO::getId, Function.identity()));
Expand All @@ -115,7 +117,7 @@ public Map<String, Object> mapOf(
consultantBase -> {
var fullConsultant = fullConsultantLookupMap.get(consultantBase.getId());
var agencies = mapOf(fullConsultant, agencyLookupMap, consultantAgencyLookupMap);
var consultantMap = mapOf(consultantBase, fullConsultant, agencies);
var consultantMap = mapOf(consultantBase, fullConsultant, agencies, tenantIdsToNameMap);
consultants.add(consultantMap);
});

Expand All @@ -134,7 +136,8 @@ public Map<String, Object> mapOfAdmin(
Page<AdminBase> adminsPage,
List<Admin> fullAdmins,
List<AgencyDTO> agencyDTOs,
List<AdminAgencyBase> agenciesOfAdmin) {
List<AdminAgencyBase> agenciesOfAdmin,
Map<Long, String> tenantIdsToNameMap) {
var agencyLookupMap =
agencyDTOs.stream().collect(Collectors.toMap(AgencyDTO::getId, Function.identity()));

Expand All @@ -148,9 +151,9 @@ public Map<String, Object> mapOfAdmin(
adminsPage.forEach(
adminBase -> {
var fullAdmin = fullAdminLookupMap.get(adminBase.getId());
var agencies = mapOfAdmin(fullAdmin, agencyLookupMap, adminAgencyLookupMap);
var consultantMap = mapOfAdmin(adminBase, fullAdmin, agencies);
admins.add(consultantMap);
var agencies = mapOfAgencies(fullAdmin, agencyLookupMap, adminAgencyLookupMap);
var adminMap = mapOfAdmin(adminBase, fullAdmin, agencies, tenantIdsToNameMap);
admins.add(adminMap);
});

return Map.of(
Expand Down Expand Up @@ -190,7 +193,7 @@ && isAgencyUnique(agencyIdsAdded, agencyId)) {
return agencies;
}

private List<Map<String, Object>> mapOfAdmin(
private List<Map<String, Object>> mapOfAgencies(
Admin admin,
Map<Long, AgencyDTO> agencyLookupMap,
Map<String, List<AdminAgencyBase>> aaLookupMap) {
Expand Down Expand Up @@ -232,7 +235,8 @@ private Map<String, Object> mapOf(AgencyDTO agencyDTO) {
public Map<String, Object> mapOf(
ConsultantBase consultantBase,
Consultant fullConsultant,
List<Map<String, Object>> agencies) {
List<Map<String, Object>> agencies,
Map<Long, String> tenantIdsToNameMap) {
var status =
isNull(fullConsultant.getStatus())
? ConsultantStatus.ERROR.toString()
Expand All @@ -259,12 +263,21 @@ public Map<String, Object> mapOf(
"deletedAt",
nonNull(fullConsultant.getDeleteDate()) ? fullConsultant.getDeleteDate().toString() : null);
map.put("agencies", agencies);

Long tenantId = fullConsultant.getTenantId();
map.put("tenantId", tenantId);
map.put(
"tenantName",
tenantIdsToNameMap.containsKey(tenantId)
? tenantIdsToNameMap.get(tenantId)
: StringUtils.EMPTY);
return map;
}

public Map<String, Object> mapOfAdmin(
AdminBase adminBase, Admin fullAdmin, List<Map<String, Object>> agencies) {
AdminBase adminBase,
Admin fullAdmin,
List<Map<String, Object>> agencies,
Map<Long, String> tenantIdsToNameMap) {

Map<String, Object> map = new HashMap<>();
map.put("id", adminBase.getId());
Expand All @@ -273,6 +286,7 @@ public Map<String, Object> mapOfAdmin(
map.put("lastName", adminBase.getLastName());
map.put("username", fullAdmin.getUsername());
map.put("tenantId", fullAdmin.getTenantId());
map.put("tenantName", getTenantName(fullAdmin, tenantIdsToNameMap));
map.put(
"createdAt",
nonNull(fullAdmin.getCreateDate()) ? fullAdmin.getCreateDate().toString() : null);
Expand All @@ -284,6 +298,12 @@ public Map<String, Object> mapOfAdmin(
return map;
}

private String getTenantName(Admin fullAdmin, Map<Long, String> tenantIdsToNameMap) {
return tenantIdsToNameMap.containsKey(fullAdmin.getTenantId())
? tenantIdsToNameMap.get(fullAdmin.getTenantId())
: StringUtils.EMPTY;
}

public Optional<Map<String, Object>> mapOf(Optional<Session> optionalSession) {
if (optionalSession.isEmpty()) {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,13 @@ private void enrichResponseWithTenantInformation(
Long tenantId = (Long) adminUserMap.get("tenantId");
adminDTO.setTenantId(String.valueOf(tenantId));
if (tenantId != null) {
enrichWithTenantSubdomain(adminDTO, tenantId);
enrichWithTenantSubdomainAndName(adminDTO, tenantId);
}
}

private void enrichWithTenantSubdomain(AdminDTO adminDTO, Long tenantId) {
private void enrichWithTenantSubdomainAndName(AdminDTO adminDTO, Long tenantId) {
RestrictedTenantDTO restrictedTenantData = tenantService.getRestrictedTenantData(tenantId);
adminDTO.setTenantSubdomain(restrictedTenantData.getSubdomain());
adminDTO.setTenantName(restrictedTenantData.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ public ConsultantDTO consultantDtoOf(Map<String, Object> consultantMap) {
consultant.setCreateDate((String) consultantMap.get("createdAt"));
consultant.setUpdateDate((String) consultantMap.get("updatedAt"));
consultant.setDeleteDate((String) consultantMap.get("deletedAt"));
Long tenantId = (Long) consultantMap.get("tenantId");
if (tenantId != null) {
consultant.setTenantId(tenantId.intValue());
}
consultant.setTenantName((String) consultantMap.get("tenantName"));
val isGroupChatConsultant =
identityClient.userHasRole(consultant.getId(), UserRole.GROUP_CHAT_CONSULTANT.getValue());
consultant.setIsGroupchatConsultant(isGroupChatConsultant);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import de.caritas.cob.userservice.api.admin.service.admin.delete.DeleteAdminService;
import de.caritas.cob.userservice.api.admin.service.admin.search.RetrieveAdminService;
import de.caritas.cob.userservice.api.admin.service.admin.update.UpdateAdminService;
import de.caritas.cob.userservice.api.admin.service.tenant.TenantService;
import de.caritas.cob.userservice.api.model.Admin;
import de.caritas.cob.userservice.api.model.Admin.AdminBase;
import de.caritas.cob.userservice.api.model.AdminAgency.AdminAgencyBase;
Expand All @@ -32,6 +33,7 @@ public class AgencyAdminUserService {
private final @NonNull DeleteAdminService deleteAdminService;
private final @NonNull UserServiceMapper userServiceMapper;
private final @NonNull AgencyService agencyService;
private final @NonNull TenantService tenantService;

public AdminResponseDTO createNewAgencyAdmin(final CreateAdminDTO createAgencyAdminDTO) {
final Admin newAdmin = createAdminService.createNewAgencyAdmin(createAgencyAdminDTO);
Expand Down Expand Up @@ -63,6 +65,15 @@ public Map<String, Object> findAgencyAdminsByInfix(String infix, PageRequest pag
var adminIds = adminsPage.stream().map(AdminBase::getId).collect(Collectors.toSet());
var fullAdmins = retrieveAdminService.findAllById(adminIds);

var tenantIdsToNameMap =
adminsPage.stream()
.filter(admin -> admin.getTenantId() != null)
.collect(
Collectors.toMap(
AdminBase::getTenantId,
admin -> tenantService.getRestrictedTenantData(admin.getTenantId()).getName(),
(existing, replacement) -> existing));

var agenciesOfAdmin = retrieveAdminService.agenciesOfAdmin(adminIds);
var agencyIds =
agenciesOfAdmin.stream()
Expand All @@ -72,7 +83,8 @@ public Map<String, Object> findAgencyAdminsByInfix(String infix, PageRequest pag

var agencies = agencyService.getAgenciesWithoutCaching(agencyIds);

return userServiceMapper.mapOfAdmin(adminsPage, fullAdmins, agencies, agenciesOfAdmin);
return userServiceMapper.mapOfAdmin(
adminsPage, fullAdmins, agencies, agenciesOfAdmin, tenantIdsToNameMap);
}

public AdminResponseDTO patchAgencyAdmin(String adminId, PatchAdminDTO patchAdminDTO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,17 @@ public Map<String, Object> findTenantAdminsByInfix(String infix, PageRequest pag
var adminIds = adminsPage.stream().map(AdminBase::getId).collect(Collectors.toSet());
var fullAdmins = retrieveAdminService.findAllById(adminIds);

var tenantIdsToNameMap =
fullAdmins.stream()
.filter(consultant -> consultant.getTenantId() != null)
.collect(
Collectors.toMap(
Admin::getTenantId,
admin -> tenantService.getRestrictedTenantData(admin.getTenantId()).getName(),
(existing, replacement) -> existing));

return userServiceMapper.mapOfAdmin(
adminsPage, fullAdmins, Lists.newArrayList(), Lists.newArrayList());
adminsPage, fullAdmins, Lists.newArrayList(), Lists.newArrayList(), tenantIdsToNameMap);
}

public List<AdminResponseDTO> findTenantAdmins(Long tenantId) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/de/caritas/cob/userservice/api/model/Admin.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,7 @@ public interface AdminBase {
String getLastName();

String getEmail();

Long getTenantId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface AdminRepository extends CrudRepository<Admin, String> {

@Query(
value =
"SELECT a.id as id, a.firstName as firstName, a.lastName as lastName, a.email as email "
"SELECT a.id as id, a.firstName as firstName, a.lastName as lastName, a.email as email, a.tenantId as tenantId "
+ "FROM Admin a "
+ "WHERE"
+ " type = ?2 "
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.caritas.cob.userservice.api;

import com.google.common.collect.Lists;
import de.caritas.cob.userservice.api.admin.service.tenant.TenantService;
import de.caritas.cob.userservice.api.model.Consultant;
import de.caritas.cob.userservice.api.port.out.ConsultantAgencyRepository;
import de.caritas.cob.userservice.api.port.out.ConsultantRepository;
Expand All @@ -27,6 +28,8 @@ class AccountManagerTest {

@Mock AgencyService agencyService;

@Mock TenantService tenantService;

@Mock Page<Consultant.ConsultantBase> page;

@Test
Expand Down
Loading

0 comments on commit e6701d1

Please sign in to comment.