diff --git a/src/main/java/de/caritas/cob/userservice/api/adapters/rocketchat/RocketChatService.java b/src/main/java/de/caritas/cob/userservice/api/adapters/rocketchat/RocketChatService.java index ee3d590b4..095ef87b9 100644 --- a/src/main/java/de/caritas/cob/userservice/api/adapters/rocketchat/RocketChatService.java +++ b/src/main/java/de/caritas/cob/userservice/api/adapters/rocketchat/RocketChatService.java @@ -652,13 +652,7 @@ public void removeUserFromGroup(String rcUserId, String rcGroupId) GroupResponseDTO response; try { - RocketChatCredentials technicalUser = rcCredentialHelper.getTechnicalUser(); - var header = getStandardHttpHeaders(technicalUser); - var body = new GroupRemoveUserBodyDTO(rcUserId, rcGroupId); - HttpEntity request = new HttpEntity<>(body, header); - - var url = rocketChatConfig.getApiUrl(ENDPOINT_GROUP_KICK); - response = restTemplate.postForObject(url, request, GroupResponseDTO.class); + response = tryRemoveUserFromGroup(rcUserId, rcGroupId); } catch (Exception ex) { log.error( @@ -677,6 +671,19 @@ public void removeUserFromGroup(String rcUserId, String rcGroupId) } } + private GroupResponseDTO tryRemoveUserFromGroup(String rcUserId, String rcGroupId) + throws RocketChatUserNotInitializedException { + GroupResponseDTO response; + RocketChatCredentials technicalUser = rcCredentialHelper.getTechnicalUser(); + var header = getStandardHttpHeaders(technicalUser); + var body = new GroupRemoveUserBodyDTO(rcUserId, rcGroupId); + HttpEntity request = new HttpEntity<>(body, header); + + var url = rocketChatConfig.getApiUrl(ENDPOINT_GROUP_KICK); + response = restTemplate.postForObject(url, request, GroupResponseDTO.class); + return response; + } + public boolean removeUserFromSession(String chatUserId, String chatId) { try { addTechnicalUserToGroup(chatId); @@ -1340,4 +1347,31 @@ public boolean saveRoomSettings(String chatId, boolean encrypted) { return false; } } + + public void removeUserFromGroupIgnoreGroupNotFound(String rcUserId, String rcGroupId) + throws RocketChatRemoveUserFromGroupException { + { + GroupResponseDTO response; + try { + response = tryRemoveUserFromGroup(rcUserId, rcGroupId); + } catch (Exception ex) { + if (ex.getMessage().contains("error-group-not-found")) { + return; + } + log.error( + "Rocket.Chat Error: Could not remove user {} from Rocket.Chat group with id {}. Reason: ", + rcUserId, + rcGroupId, + ex); + throw new RocketChatRemoveUserFromGroupException( + String.format( + "Could not remove user %s from Rocket.Chat group with id %s", rcUserId, rcGroupId)); + } + + if (response != null && !response.isSuccess()) { + var error = "Could not remove user %s from Rocket.Chat group with id %s"; + throw new RocketChatRemoveUserFromGroupException(String.format(error, rcUserId, rcGroupId)); + } + } + } } diff --git a/src/main/java/de/caritas/cob/userservice/api/admin/service/rocketchat/RocketChatGroupOperation.java b/src/main/java/de/caritas/cob/userservice/api/admin/service/rocketchat/RocketChatGroupOperation.java index d07516bf4..372f249d9 100644 --- a/src/main/java/de/caritas/cob/userservice/api/admin/service/rocketchat/RocketChatGroupOperation.java +++ b/src/main/java/de/caritas/cob/userservice/api/admin/service/rocketchat/RocketChatGroupOperation.java @@ -9,6 +9,7 @@ import de.caritas.cob.userservice.api.port.out.IdentityClient; import de.caritas.cob.userservice.api.service.LogService; import java.util.List; +import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.stream.Collectors; import lombok.NonNull; @@ -54,16 +55,27 @@ String resolveTypeOfSession(Session session) { } void removeConsultantsFromSessionGroups(Session session, List consultants) { - removeConsultantsFromRocketChatGroup(session.getGroupId(), consultants); - removeConsultantsFromRocketChatGroup(session.getFeedbackGroupId(), consultants); + removeConsultantsFromRocketChatGroup( + session.getGroupId(), consultants, rocketChatFacade::removeUserFromGroup); + removeConsultantsFromRocketChatGroup( + session.getFeedbackGroupId(), consultants, rocketChatFacade::removeUserFromGroup); } void removeConsultantsFromSessionGroup(String rcGroupId, List consultants) { - removeConsultantsFromRocketChatGroup(rcGroupId, consultants); + removeConsultantsFromRocketChatGroup( + rcGroupId, consultants, rocketChatFacade::removeUserFromGroup); } - private void removeConsultantsFromRocketChatGroup( + void removeConsultantsFromSessionGroupAndIgnoreGroupNotFound( String rcGroupId, List consultants) { + removeConsultantsFromRocketChatGroup( + rcGroupId, consultants, rocketChatFacade::removeUserFromGroupIgnoreGroupNotFound); + } + + private void removeConsultantsFromRocketChatGroup( + String rcGroupId, + List consultants, + BiConsumer removeFromRocketchatGroupMethod) { if (rcGroupId == null) { return; } @@ -73,7 +85,7 @@ private void removeConsultantsFromRocketChatGroup( consultants.stream() .map(Consultant::getRocketChatId) .filter(groupMemberList::contains) - .forEach(rcUserId -> rocketChatFacade.removeUserFromGroup(rcUserId, rcGroupId)); + .forEach(rcUserId -> removeFromRocketchatGroupMethod.accept(rcUserId, rcGroupId)); rocketChatFacade.leaveFromGroupAsTechnicalUser(rcGroupId); } diff --git a/src/main/java/de/caritas/cob/userservice/api/admin/service/rocketchat/RocketChatRemoveFromGroupOperationService.java b/src/main/java/de/caritas/cob/userservice/api/admin/service/rocketchat/RocketChatRemoveFromGroupOperationService.java index e91a7da6b..f52f759c6 100644 --- a/src/main/java/de/caritas/cob/userservice/api/admin/service/rocketchat/RocketChatRemoveFromGroupOperationService.java +++ b/src/main/java/de/caritas/cob/userservice/api/admin/service/rocketchat/RocketChatRemoveFromGroupOperationService.java @@ -74,6 +74,13 @@ private void performGroupsRemove(Session session, List consultants) } /** Removes the given consultant from Rocket.Chat group of given session. */ + public void removeFromGroupAndIgnoreGroupNotFound() { + this.consultantsToRemoveFromSessions.forEach( + ((session, consultants) -> + removeConsultantsFromSessionGroupAndIgnoreGroupNotFound( + session.getGroupId(), consultants))); + } + public void removeFromGroup() { this.consultantsToRemoveFromSessions.forEach( ((session, consultants) -> @@ -104,7 +111,7 @@ public void removeFromFeedbackGroupOrRollbackOnFailure() { private void performGroupRemove(Session session, List consultants) { try { - removeConsultantsFromSessionGroup(session.getGroupId(), consultants); + removeConsultantsFromSessionGroupAndIgnoreGroupNotFound(session.getGroupId(), consultants); } catch (Exception e) { rollback(); throw new InternalServerErrorException( diff --git a/src/main/java/de/caritas/cob/userservice/api/facade/RocketChatFacade.java b/src/main/java/de/caritas/cob/userservice/api/facade/RocketChatFacade.java index 48a30022f..e77fdd5eb 100644 --- a/src/main/java/de/caritas/cob/userservice/api/facade/RocketChatFacade.java +++ b/src/main/java/de/caritas/cob/userservice/api/facade/RocketChatFacade.java @@ -139,6 +139,17 @@ public void removeUserFromGroup(String rcUserId, String groupId) { } } + public void removeUserFromGroupIgnoreGroupNotFound(String rcUserId, String groupId) { + try { + this.rocketChatService.removeUserFromGroupIgnoreGroupNotFound(rcUserId, groupId); + } catch (RocketChatRemoveUserFromGroupException e) { + var message = + String.format( + "Could not remove user with id %s from Rocket.Chat group id %s", rcUserId, groupId); + throw new InternalServerErrorException(message, LogService::logInternalServerError); + } + } + /** * Get all standard members (all users except system user and technical user) of a rocket chat * group. diff --git a/src/test/java/de/caritas/cob/userservice/api/admin/service/rocketchat/RocketChatRemoveFromGroupOperationServiceTest.java b/src/test/java/de/caritas/cob/userservice/api/admin/service/rocketchat/RocketChatRemoveFromGroupOperationServiceTest.java index 9dd34df30..740f1a11c 100644 --- a/src/test/java/de/caritas/cob/userservice/api/admin/service/rocketchat/RocketChatRemoveFromGroupOperationServiceTest.java +++ b/src/test/java/de/caritas/cob/userservice/api/admin/service/rocketchat/RocketChatRemoveFromGroupOperationServiceTest.java @@ -133,7 +133,7 @@ public void removeFromGroupOrRollbackOnFailure_Should_executeRemoveForRocketChat this.removeService.removeFromGroupOrRollbackOnFailure(); - verify(this.rocketChatFacade, times(1)).removeUserFromGroup("rcId", "group"); + verify(this.rocketChatFacade, times(1)).removeUserFromGroupIgnoreGroupNotFound("rcId", "group"); verify(this.rocketChatFacade, never()).removeUserFromGroup("rcId", "feedback"); } @@ -149,7 +149,7 @@ public void removeFromGroupOrRollbackOnFailure_Should_executeRemoveForRocketChat .thenReturn(singletonList(groupMemberDTO)); doThrow(new RuntimeException("")) .when(this.rocketChatFacade) - .removeUserFromGroup(anyString(), anyString()); + .removeUserFromGroupIgnoreGroupNotFound(anyString(), anyString()); doThrow(new RuntimeException("")) .when(this.rocketChatFacade) .addUserToRocketChatGroup(anyString(), anyString()); @@ -172,7 +172,9 @@ public void removeFromGroupOrRollbackOnFailure_Should_executeRemoveForRocketChat groupMemberDTO.set_id(this.consultant.getRocketChatId()); when(this.rocketChatFacade.retrieveRocketChatMembers(any())) .thenReturn(singletonList(groupMemberDTO)); - doThrow(new RuntimeException("")).when(this.rocketChatFacade).removeUserFromGroup(any(), any()); + doThrow(new RuntimeException("")) + .when(this.rocketChatFacade) + .removeUserFromGroupIgnoreGroupNotFound(any(), any()); try { this.removeService.removeFromGroupOrRollbackOnFailure(); diff --git a/src/test/java/de/caritas/cob/userservice/api/facade/assignsession/AssignSessionFacadeTest.java b/src/test/java/de/caritas/cob/userservice/api/facade/assignsession/AssignSessionFacadeTest.java index 019c03fb1..eeac66b94 100644 --- a/src/test/java/de/caritas/cob/userservice/api/facade/assignsession/AssignSessionFacadeTest.java +++ b/src/test/java/de/caritas/cob/userservice/api/facade/assignsession/AssignSessionFacadeTest.java @@ -159,11 +159,13 @@ public void assignSession_Should_removeAllUnauthorizedMembers_When_sessionIsNotA verifyAsync( a -> verify(this.rocketChatFacade, atLeastOnce()) - .removeUserFromGroup(consultantToRemove.getRocketChatId(), session.getGroupId())); + .removeUserFromGroupIgnoreGroupNotFound( + consultantToRemove.getRocketChatId(), session.getGroupId())); verifyAsync( a -> verify(this.rocketChatFacade, atLeastOnce()) - .removeUserFromGroup(consultantToRemove.getRocketChatId(), session.getGroupId())); + .removeUserFromGroupIgnoreGroupNotFound( + consultantToRemove.getRocketChatId(), session.getGroupId())); verify(this.emailNotificationFacade, times(1)) .sendAssignEnquiryEmailNotification(any(), any(), any(), any()); } @@ -281,23 +283,28 @@ public void assignSession_ShouldNot_removeTeamMembers_When_sessionIsTeamSession( verifyAsync( a -> verify(this.rocketChatFacade, atLeastOnce()) - .removeUserFromGroup(consultantToRemove.getRocketChatId(), session.getGroupId())); + .removeUserFromGroupIgnoreGroupNotFound( + consultantToRemove.getRocketChatId(), session.getGroupId())); verifyAsync( a -> verify(this.rocketChatFacade, never()) - .removeUserFromGroup("teamConsultantRcId", session.getGroupId())); + .removeUserFromGroupIgnoreGroupNotFound( + "teamConsultantRcId", session.getGroupId())); verifyAsync( a -> verify(this.rocketChatFacade, never()) - .removeUserFromGroup("teamConsultantRcId", session.getFeedbackGroupId())); + .removeUserFromGroupIgnoreGroupNotFound( + "teamConsultantRcId", session.getFeedbackGroupId())); verifyAsync( a -> verify(this.rocketChatFacade, never()) - .removeUserFromGroup("teamConsultantRcId2", session.getGroupId())); + .removeUserFromGroupIgnoreGroupNotFound( + "teamConsultantRcId2", session.getGroupId())); verifyAsync( a -> verify(this.rocketChatFacade, never()) - .removeUserFromGroup("teamConsultantRcId2", session.getFeedbackGroupId())); + .removeUserFromGroupIgnoreGroupNotFound( + "teamConsultantRcId2", session.getFeedbackGroupId())); verifyAsync( a -> verify(this.emailNotificationFacade, times(1))