Skip to content

Commit

Permalink
feat: consultant displayName for session lists
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippFr committed Oct 12, 2022
1 parent 583405b commit 8952541
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -323,19 +323,7 @@ public ResponseEntity<UserSessionListResponseDTO> getSessionsForAuthenticatedUse
sessionListFacade.retrieveSortedSessionsForAuthenticatedUser(
user.getUserId(), rocketChatCredentials);

userSessionsDTO
.getSessions()
.forEach(
session -> {
var consultant = session.getConsultant();
if (nonNull(consultant) && nonNull(consultant.getUsername())) {
accountManager
.findConsultantByUsername(consultant.getUsername())
.ifPresent(
consultantMap ->
consultant.setDisplayName(userDtoMapper.displayNameOf(consultantMap)));
}
});
consultantDataFacade.addConsultantDisplayNameToSessionList(userSessionsDTO);

return isNotEmpty(userSessionsDTO.getSessions())
? new ResponseEntity<>(userSessionsDTO, HttpStatus.OK)
Expand Down Expand Up @@ -370,6 +358,8 @@ public ResponseEntity<GroupSessionListResponseDTO> getSessionsForGroupOrFeedback
user.getUserId(), rcGroupIds, rocketChatCredentials, authenticatedUser.getRoles());
}

consultantDataFacade.addConsultantDisplayNameToSessionList(groupSessionList);

return isNotEmpty(groupSessionList.getSessions())
? new ResponseEntity<>(groupSessionList, HttpStatus.OK)
: new ResponseEntity<>(HttpStatus.NO_CONTENT);
Expand Down Expand Up @@ -399,6 +389,8 @@ public ResponseEntity<GroupSessionListResponseDTO> getSessionForId(
authenticatedUser.getRoles());
}

consultantDataFacade.addConsultantDisplayNameToSessionList(groupSessionList);

return isNotEmpty(groupSessionList.getSessions())
? new ResponseEntity<>(groupSessionList, HttpStatus.OK)
: new ResponseEntity<>(HttpStatus.NO_CONTENT);
Expand Down Expand Up @@ -429,6 +421,8 @@ public ResponseEntity<GroupSessionListResponseDTO> getChatById(String rcToken, L
singletonList(chatId), rocketChatCredentials);
}

consultantDataFacade.addConsultantDisplayNameToSessionList(groupSessionList);

return isNotEmpty(groupSessionList.getSessions())
? new ResponseEntity<>(groupSessionList, HttpStatus.OK)
: new ResponseEntity<>(HttpStatus.NO_CONTENT);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package de.caritas.cob.userservice.api.facade.userdata;

import static java.util.Objects.nonNull;
import static org.apache.commons.lang3.BooleanUtils.isTrue;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

import de.caritas.cob.userservice.api.adapters.web.dto.AbsenceDTO;
import de.caritas.cob.userservice.api.adapters.web.dto.GroupSessionListResponseDTO;
import de.caritas.cob.userservice.api.adapters.web.dto.UserSessionListResponseDTO;
import de.caritas.cob.userservice.api.adapters.web.mapping.UserDtoMapper;
import de.caritas.cob.userservice.api.helper.Helper;
import de.caritas.cob.userservice.api.model.Consultant;
import de.caritas.cob.userservice.api.port.in.AccountManaging;
import de.caritas.cob.userservice.api.service.ConsultantService;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
Expand All @@ -16,6 +21,8 @@
public class ConsultantDataFacade {

private final @NonNull ConsultantService consultantService;
private final @NonNull AccountManaging accountManager;
private final @NonNull UserDtoMapper userDtoMapper;

/**
* Updates a {@link Consultant} with the absence data from a (@Link AbsenceDTO).
Expand All @@ -32,4 +39,40 @@ public Consultant updateConsultantAbsent(Consultant consultant, AbsenceDTO absen
}
return this.consultantService.saveConsultant(consultant);
}

public GroupSessionListResponseDTO addConsultantDisplayNameToSessionList(
GroupSessionListResponseDTO groupSessionList) {
groupSessionList
.getSessions()
.forEach(
session -> {
var consultant = session.getConsultant();
if (nonNull(consultant) && nonNull(consultant.getUsername())) {
accountManager
.findConsultantByUsername(consultant.getUsername())
.ifPresent(
consultantMap ->
consultant.setDisplayName(userDtoMapper.displayNameOf(consultantMap)));
}
});
return groupSessionList;
}

public UserSessionListResponseDTO addConsultantDisplayNameToSessionList(
UserSessionListResponseDTO userSessionsDTO) {
userSessionsDTO
.getSessions()
.forEach(
session -> {
var consultant = session.getConsultant();
if (nonNull(consultant) && nonNull(consultant.getUsername())) {
accountManager
.findConsultantByUsername(consultant.getUsername())
.ifPresent(
consultantMap ->
consultant.setDisplayName(userDtoMapper.displayNameOf(consultantMap)));
}
});
return userSessionsDTO;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,28 @@
import static de.caritas.cob.userservice.api.testHelper.TestConstants.ABSENCE_DTO_WITH_HTML_AND_JS;
import static de.caritas.cob.userservice.api.testHelper.TestConstants.ABSENCE_DTO_WITH_NULL_MESSAGE;
import static de.caritas.cob.userservice.api.testHelper.TestConstants.CONSULTANT;
import static de.caritas.cob.userservice.api.testHelper.TestConstants.GROUP_SESSION_RESPONSE_DTO;
import static de.caritas.cob.userservice.api.testHelper.TestConstants.MESSAGE;
import static de.caritas.cob.userservice.api.testHelper.TestConstants.USER_SESSION_RESPONSE_DTO;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import de.caritas.cob.userservice.api.AccountManager;
import de.caritas.cob.userservice.api.adapters.web.dto.GroupSessionListResponseDTO;
import de.caritas.cob.userservice.api.adapters.web.dto.GroupSessionResponseDTO;
import de.caritas.cob.userservice.api.adapters.web.dto.UserSessionListResponseDTO;
import de.caritas.cob.userservice.api.adapters.web.dto.UserSessionResponseDTO;
import de.caritas.cob.userservice.api.adapters.web.mapping.UserDtoMapper;
import de.caritas.cob.userservice.api.model.Consultant;
import de.caritas.cob.userservice.api.service.ConsultantService;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
Expand All @@ -26,8 +39,9 @@
public class ConsultantDataFacadeTest {

@InjectMocks private ConsultantDataFacade consultantDataFacade;

@Mock private ConsultantService consultantService;
@Mock private AccountManager accountManager;
@Mock private UserDtoMapper userDtoMapper;

@Test
public void updateConsultantAbsent_Should_UpdateAbsenceMessageAndIsAbsence() {
Expand Down Expand Up @@ -71,4 +85,46 @@ public void updateConsultantAbsent_Should_UpdateAbsenceMessageAndIsAbsence() {
verify(consultantService).saveConsultant(captor.capture());
assertNull(captor.getValue().getAbsenceMessage());
}

@Test
public void addConsultantDisplayNameToSessionList_GroupSession_Should_AddConsultantDisplayName() {

List<GroupSessionResponseDTO> sessions = new ArrayList<>();
sessions.add(GROUP_SESSION_RESPONSE_DTO);

GroupSessionListResponseDTO response = new GroupSessionListResponseDTO().sessions(sessions);

var userName = RandomStringUtils.randomAlphanumeric(16);
sessions.get(0).getConsultant().setUsername(userName);
var displayName = RandomStringUtils.randomAlphanumeric(16);

Map<String, Object> map = Map.of("displayName", displayName);
when(userDtoMapper.displayNameOf(map)).thenReturn(displayName);
when(accountManager.findConsultantByUsername(userName)).thenReturn(Optional.of(map));

response = consultantDataFacade.addConsultantDisplayNameToSessionList(response);

assertEquals(displayName, response.getSessions().get(0).getConsultant().getDisplayName());
}

@Test
public void addConsultantDisplayNameToSessionList_UserSession_Should_AddConsultantDisplayName() {

List<UserSessionResponseDTO> sessions = new ArrayList<>();
sessions.add(USER_SESSION_RESPONSE_DTO);

UserSessionListResponseDTO response = new UserSessionListResponseDTO().sessions(sessions);

var userName = RandomStringUtils.randomAlphanumeric(16);
sessions.get(0).getConsultant().setUsername(userName);
var displayName = RandomStringUtils.randomAlphanumeric(16);

Map<String, Object> map = Map.of("displayName", displayName);
when(userDtoMapper.displayNameOf(map)).thenReturn(displayName);
when(accountManager.findConsultantByUsername(userName)).thenReturn(Optional.of(map));

response = consultantDataFacade.addConsultantDisplayNameToSessionList(response);

assertEquals(displayName, response.getSessions().get(0).getConsultant().getDisplayName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import de.caritas.cob.userservice.api.adapters.web.dto.ChatDTO;
import de.caritas.cob.userservice.api.adapters.web.dto.ConsultantSessionResponseDTO;
import de.caritas.cob.userservice.api.adapters.web.dto.CreateChatResponseDTO;
import de.caritas.cob.userservice.api.adapters.web.dto.GroupSessionConsultantDTO;
import de.caritas.cob.userservice.api.adapters.web.dto.GroupSessionResponseDTO;
import de.caritas.cob.userservice.api.adapters.web.dto.SessionAttachmentDTO;
import de.caritas.cob.userservice.api.adapters.web.dto.SessionConsultantForUserDTO;
import de.caritas.cob.userservice.api.adapters.web.dto.SessionDTO;
Expand Down Expand Up @@ -527,6 +529,8 @@ public class TestConstants {
LanguageCode.de);
public static final SessionConsultantForUserDTO CONSULTANT_DTO =
new SessionConsultantForUserDTO();
public static final GroupSessionConsultantDTO GROUP_SESSION_CONSULTANT_DTO =
new GroupSessionConsultantDTO();
public static final AbsenceDTO ABSENCE_DTO_WITH_NULL_MESSAGE = new AbsenceDTO().absent(true);
public static final GroupMemberDTO GROUP_MEMBER_USER_1 =
new GroupMemberDTO(RC_USER_ID, RC_STATUS_ONLINE, USERNAME, USERNAME, RC_UTC_OFFSET);
Expand Down Expand Up @@ -1298,6 +1302,12 @@ public class TestConstants {
USER_SESSION_RESPONSE_DTO_3,
USER_CHAT_RESPONSE_DTO,
USER_CHAT_RESPONSE_DTO_2);
public static final GroupSessionResponseDTO GROUP_SESSION_RESPONSE_DTO =
new GroupSessionResponseDTO()
.session(SESSION_DTO_1)
.agency(EMPTY_AGENCY_DTO)
.consultant(GROUP_SESSION_CONSULTANT_DTO)
.latestMessage(NOW);
public static final Map<String, Boolean> MESSAGES_READ_MAP_WITH_UNREADS =
new HashMap<>() {
{
Expand Down

0 comments on commit 8952541

Please sign in to comment.