diff --git a/api/userservice.yaml b/api/userservice.yaml index a39acc5e3..a6c3a9813 100644 --- a/api/userservice.yaml +++ b/api/userservice.yaml @@ -2589,6 +2589,10 @@ components: description: "Datetime stamp when the user accepted data privacy" emailNotifications: $ref: "#/components/schemas/EmailNotificationsDTO" + sessions: + type: array + items: + $ref: "#/components/schemas/SessionDTO" PatchUserDTO: type: object diff --git a/src/main/java/de/caritas/cob/userservice/api/adapters/web/dto/UserDataResponseDTO.java b/src/main/java/de/caritas/cob/userservice/api/adapters/web/dto/UserDataResponseDTO.java index 891ff9cf0..a6c9b80d2 100644 --- a/src/main/java/de/caritas/cob/userservice/api/adapters/web/dto/UserDataResponseDTO.java +++ b/src/main/java/de/caritas/cob/userservice/api/adapters/web/dto/UserDataResponseDTO.java @@ -89,4 +89,6 @@ public class UserDataResponseDTO { private Boolean available; private EmailNotificationsDTO emailNotifications; + + private Set sessions; } diff --git a/src/main/java/de/caritas/cob/userservice/api/facade/userdata/AskerDataProvider.java b/src/main/java/de/caritas/cob/userservice/api/facade/userdata/AskerDataProvider.java index 62edfddd4..52c489047 100644 --- a/src/main/java/de/caritas/cob/userservice/api/facade/userdata/AskerDataProvider.java +++ b/src/main/java/de/caritas/cob/userservice/api/facade/userdata/AskerDataProvider.java @@ -3,6 +3,7 @@ import de.caritas.cob.userservice.api.adapters.web.dto.AgencyDTO; import de.caritas.cob.userservice.api.adapters.web.dto.LanguageCode; import de.caritas.cob.userservice.api.adapters.web.dto.UserDataResponseDTO; +import de.caritas.cob.userservice.api.adapters.web.dto.UserDataResponseDTO.UserDataResponseDTOBuilder; import de.caritas.cob.userservice.api.helper.AuthenticatedUser; import de.caritas.cob.userservice.api.helper.SessionDataProvider; import de.caritas.cob.userservice.api.manager.consultingtype.ConsultingTypeManager; @@ -11,6 +12,9 @@ import de.caritas.cob.userservice.api.model.UserAgency; import de.caritas.cob.userservice.api.port.out.IdentityClientConfig; import de.caritas.cob.userservice.api.service.agency.AgencyService; +import de.caritas.cob.userservice.api.service.session.SessionMapper; +import de.caritas.cob.userservice.api.service.session.SessionService; +import de.caritas.cob.userservice.api.service.session.SessionTopicEnrichmentService; import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashMap; @@ -37,8 +41,12 @@ public class AskerDataProvider { private final @NonNull ConsultingTypeManager consultingTypeManager; private final @NonNull IdentityClientConfig identityClientConfig; + private final @NonNull SessionService sessionService; + private final @NonNull EmailNotificationMapper emailNotificationMapper; + private final @NonNull SessionTopicEnrichmentService sessionTopicEnrichmentService; + /** * Retrieve the user data of an asker, e.g. username, email, name, ... * @@ -46,24 +54,40 @@ public class AskerDataProvider { * @return the user data */ public UserDataResponseDTO retrieveData(User user) { - return UserDataResponseDTO.builder() - .userId(user.getUserId()) - .userName(user.getUsername()) - .email(observeUserEmailAddress(user)) - .isAbsent(false) - .encourage2fa(user.getEncourage2fa()) - .isFormalLanguage(user.isLanguageFormal()) - .preferredLanguage(LanguageCode.fromValue(user.getLanguageCode().toString())) - .isInTeamAgency(false) - .userRoles(authenticatedUser.getRoles()) - .grantedAuthorities(authenticatedUser.getGrantedAuthorities()) - .consultingTypes(getConsultingTypes(user)) - .hasAnonymousConversations(false) - .hasArchive(false) - .dataPrivacyConfirmation(user.getDataPrivacyConfirmation()) - .termsAndConditionsConfirmation(user.getTermsAndConditionsConfirmation()) - .emailNotifications(emailNotificationMapper.toEmailNotificationsDTO(user)) - .build(); + var userDataResponseDTOBuilder = + UserDataResponseDTO.builder() + .userId(user.getUserId()) + .userName(user.getUsername()) + .email(observeUserEmailAddress(user)) + .isAbsent(false) + .encourage2fa(user.getEncourage2fa()) + .isFormalLanguage(user.isLanguageFormal()) + .preferredLanguage(LanguageCode.fromValue(user.getLanguageCode().toString())) + .isInTeamAgency(false) + .userRoles(authenticatedUser.getRoles()) + .grantedAuthorities(authenticatedUser.getGrantedAuthorities()) + .consultingTypes(getConsultingTypes(user)) + .hasAnonymousConversations(false) + .hasArchive(false) + .dataPrivacyConfirmation(user.getDataPrivacyConfirmation()) + .termsAndConditionsConfirmation(user.getTermsAndConditionsConfirmation()) + .emailNotifications(emailNotificationMapper.toEmailNotificationsDTO(user)); + + enrichWithUserSessions(user, userDataResponseDTOBuilder); + return userDataResponseDTOBuilder.build(); + } + + private void enrichWithUserSessions( + User user, UserDataResponseDTOBuilder userDataResponseDTOBuilder) { + List sessionsByUser = sessionService.findSessionsByUser(user); + if (CollectionUtils.isNotEmpty(sessionsByUser)) { + SessionMapper sessionMapper = new SessionMapper(); + userDataResponseDTOBuilder.sessions( + sessionsByUser.stream() + .map(sessionMapper::convertToSessionDTO) + .map(sessionTopicEnrichmentService::enrichSessionWithTopicData) + .collect(Collectors.toSet())); + } } private String observeUserEmailAddress(User user) { diff --git a/src/main/java/de/caritas/cob/userservice/api/service/session/SessionService.java b/src/main/java/de/caritas/cob/userservice/api/service/session/SessionService.java index 177233fe9..a5a588f74 100644 --- a/src/main/java/de/caritas/cob/userservice/api/service/session/SessionService.java +++ b/src/main/java/de/caritas/cob/userservice/api/service/session/SessionService.java @@ -747,6 +747,13 @@ public Optional findSessionByConsultantAndUserAndConsultingType( return Optional.empty(); } + public List findSessionsByUser(User user) { + if (nonNull(user)) { + return sessionRepository.findByUser(user); + } + return emptyList(); + } + public String findGroupIdByConsultantAndUser(String consultantId, String askerId) { Optional consultant = consultantService.getConsultant(consultantId); diff --git a/src/test/java/de/caritas/cob/userservice/api/facade/userdata/AskerDataProviderTest.java b/src/test/java/de/caritas/cob/userservice/api/facade/userdata/AskerDataProviderTest.java index 912322dc7..88154a116 100644 --- a/src/test/java/de/caritas/cob/userservice/api/facade/userdata/AskerDataProviderTest.java +++ b/src/test/java/de/caritas/cob/userservice/api/facade/userdata/AskerDataProviderTest.java @@ -18,8 +18,11 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import com.google.api.client.util.Sets; +import com.google.common.collect.Lists; import com.neovisionaries.i18n.LanguageCode; import de.caritas.cob.userservice.api.adapters.web.dto.AgencyDTO; +import de.caritas.cob.userservice.api.adapters.web.dto.SessionDTO; import de.caritas.cob.userservice.api.adapters.web.dto.UserDataResponseDTO; import de.caritas.cob.userservice.api.config.auth.UserRole; import de.caritas.cob.userservice.api.exception.httpresponses.InternalServerErrorException; @@ -27,9 +30,15 @@ import de.caritas.cob.userservice.api.helper.SessionDataProvider; import de.caritas.cob.userservice.api.manager.consultingtype.ConsultingTypeManager; import de.caritas.cob.userservice.api.model.Session; +import de.caritas.cob.userservice.api.model.Session.RegistrationType; +import de.caritas.cob.userservice.api.model.Session.SessionStatus; import de.caritas.cob.userservice.api.model.User; import de.caritas.cob.userservice.api.port.out.IdentityClientConfig; import de.caritas.cob.userservice.api.service.agency.AgencyService; +import de.caritas.cob.userservice.api.service.session.SessionService; +import de.caritas.cob.userservice.api.service.session.SessionTopicEnrichmentService; +import java.time.LocalDateTime; +import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; @@ -61,6 +70,10 @@ public class AskerDataProviderTest { @Mock EmailNotificationMapper emailNotificationMapper; + @Mock SessionService sessionService; + + @Mock SessionTopicEnrichmentService sessionTopicEnrichmentService; + @Test public void retrieveData_Should_ReturnUserDataWithAgency_When_ProvidedWithUserWithAgencyInSession() { @@ -104,6 +117,41 @@ public void retrieveData_Should_ReturnUserDataWithAgency_When_ProvidedWithUserWi assertEquals(AGENCY_DTO_KREUZBUND, agency); } + @Test + public void retrieveData_Should_ReturnUserDataWithSessions_When_ProvidedWithUserWithSessions() { + givenAnEmailDummySuffixConfig(); + when(authenticatedUser.getRoles()).thenReturn(asSet(UserRole.USER.getValue())); + ArrayList inputSessions = + Lists.newArrayList( + Session.builder() + .id(1L) + .registrationType(RegistrationType.ANONYMOUS) + .languageCode(LanguageCode.de) + .postcode("12111") + .status(SessionStatus.NEW) + .createDate(LocalDateTime.now()) + .build(), + Session.builder() + .id(2L) + .registrationType(RegistrationType.REGISTERED) + .languageCode(LanguageCode.de) + .postcode("11111") + .createDate(LocalDateTime.now()) + .status(SessionStatus.NEW) + .build()); + when(sessionService.findSessionsByUser(Mockito.any(User.class))).thenReturn(inputSessions); + when(sessionTopicEnrichmentService.enrichSessionWithTopicData(Mockito.any(SessionDTO.class))) + .thenAnswer(invocation -> invocation.getArgument(0)); + Set sessions = askerDataProvider.retrieveData(USER).getSessions(); + + assertEquals(2, sessions.size()); + var expectedSessionsIds = Sets.newHashSet(); + expectedSessionsIds.add(1L); + expectedSessionsIds.add(2L); + var sessionIds = sessions.stream().map(SessionDTO::getId).collect(Collectors.toSet()); + assertEquals(sessionIds, expectedSessionsIds); + } + @Test(expected = InternalServerErrorException.class) public void retrieveData_GetConsultingTypes_Should_ThrowInternalServerErrorException_When_AgencyServiceHelperFails() {