diff --git a/src/main/java/de/caritas/cob/userservice/api/facade/CreateSessionFacade.java b/src/main/java/de/caritas/cob/userservice/api/facade/CreateSessionFacade.java index f4fae5ed9..000df97ec 100644 --- a/src/main/java/de/caritas/cob/userservice/api/facade/CreateSessionFacade.java +++ b/src/main/java/de/caritas/cob/userservice/api/facade/CreateSessionFacade.java @@ -2,6 +2,7 @@ import static de.caritas.cob.userservice.api.helper.SessionDataProvider.fromUserDTO; import static java.util.Objects.isNull; +import static java.util.Objects.nonNull; import static java.util.Objects.requireNonNull; import static org.apache.commons.lang3.BooleanUtils.isTrue; @@ -130,11 +131,14 @@ private void initializeMonitoring( } private Session initializeSession(UserDTO userDTO, User user, AgencyDTO agencyDTO) { + var sessionData = fromUserDTO(userDTO); + var isTeaming = isTrue(agencyDTO.getTeamAgency()); + boolean initialized = false; try { - var session = - sessionService.initializeSession(user, userDTO, isTrue(agencyDTO.getTeamAgency())); - sessionDataService.saveSessionData(session, fromUserDTO(userDTO)); + var session = sessionService.initializeSession(user, userDTO, isTeaming); + initialized = nonNull(session) && nonNull(session.getId()); + sessionDataService.saveSessionData(session, sessionData); return session; } catch (Exception ex) { @@ -145,9 +149,9 @@ private Session initializeSession(UserDTO userDTO, User user, AgencyDTO agencyDT .rollBackUserAccount(Boolean.parseBoolean(userDTO.getTermsAccepted())) .build()); - throw new InternalServerErrorException( - String.format( - "Could not create session for user %s. %s", user.getUsername(), ex.getMessage())); + var stepWord = initialized ? "save session data" : "initialize session"; + var message = String.format("Could not %s for user %s.", stepWord, user.getUsername()); + throw new InternalServerErrorException(message, ex); } } diff --git a/src/main/java/de/caritas/cob/userservice/api/tenant/MultitenancyWithSingleDomainTenantResolver.java b/src/main/java/de/caritas/cob/userservice/api/tenant/MultitenancyWithSingleDomainTenantResolver.java index e69cd6441..007b410c5 100644 --- a/src/main/java/de/caritas/cob/userservice/api/tenant/MultitenancyWithSingleDomainTenantResolver.java +++ b/src/main/java/de/caritas/cob/userservice/api/tenant/MultitenancyWithSingleDomainTenantResolver.java @@ -49,7 +49,10 @@ public Optional resolve(HttpServletRequest request) { } private Optional resolveTenantFromConsultantRequestParameter() { + // temporarily set technical tenant to be able to run query during tenant determination + TenantContext.setCurrentTenant(0L); Optional consultant = consultantService.getConsultant(getConsultantId()); + TenantContext.clear(); if (consultant.isPresent()) { return Optional.of(consultant.get().getTenantId()); } diff --git a/src/test/java/de/caritas/cob/userservice/api/tenant/MultitenancyWithSingleDomainTenantResolverTest.java b/src/test/java/de/caritas/cob/userservice/api/tenant/MultitenancyWithSingleDomainTenantResolverTest.java index 7a55efeda..19aad5716 100644 --- a/src/test/java/de/caritas/cob/userservice/api/tenant/MultitenancyWithSingleDomainTenantResolverTest.java +++ b/src/test/java/de/caritas/cob/userservice/api/tenant/MultitenancyWithSingleDomainTenantResolverTest.java @@ -14,6 +14,7 @@ import javax.servlet.http.HttpServletRequest; import org.jeasy.random.EasyRandom; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -42,6 +43,11 @@ class MultitenancyWithSingleDomainTenantResolverTest { @Mock private ServletRequestAttributes requestAttributes; + @BeforeEach + public void initialize() { + TenantContext.clear(); + } + @AfterEach public void tearDown() { resetRequestAttributes(); @@ -87,6 +93,7 @@ void resolve_Should_ResolveToEmpty_When_FeatureMultitenancyWithSingleDomainIsDis assertThat(multitenancyWithSingleDomainTenantResolver.canResolve(request)).isTrue(); assertThat(multitenancyWithSingleDomainTenantResolver.resolve(request)) .isEqualTo(Optional.of(ANOTHER_TENANT)); + assertThat(TenantContext.getCurrentTenant()).isNull(); } @Test @@ -106,6 +113,7 @@ void resolve_Should_ResolveToEmpty_When_FeatureMultitenancyWithSingleDomainIsDis // when assertThat(multitenancyWithSingleDomainTenantResolver.canResolve(request)).isFalse(); assertThat(multitenancyWithSingleDomainTenantResolver.resolve(request)).isEmpty(); + assertThat(TenantContext.getCurrentTenant()).isNull(); } @Test