From 3a287be4e827c0ab34370deece502fb9eeb999c2 Mon Sep 17 00:00:00 2001 From: tkuzynow Date: Thu, 22 Aug 2024 15:52:52 +0200 Subject: [PATCH 1/3] fix: delete rooms even if user is already deleted from MariaDB --- .../rocketchat/RocketChatService.java | 48 ++++++++++++++++--- .../rocketchat/RocketChatGroupOperation.java | 22 +++++++-- ...etChatRemoveFromGroupOperationService.java | 7 +-- .../api/facade/RocketChatFacade.java | 11 +++++ .../assignsession/AssignEnquiryFacade.java | 2 +- ...atRemoveFromGroupOperationServiceTest.java | 8 ++-- .../AssignSessionFacadeTest.java | 21 +++++--- 7 files changed, 93 insertions(+), 26 deletions(-) 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..e08560e11 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,10 +74,11 @@ private void performGroupsRemove(Session session, List consultants) } /** Removes the given consultant from Rocket.Chat group of given session. */ - public void removeFromGroup() { + public void removeFromGroupAndIgnoreGroupNotFound() { this.consultantsToRemoveFromSessions.forEach( ((session, consultants) -> - removeConsultantsFromSessionGroup(session.getGroupId(), consultants))); + removeConsultantsFromSessionGroupAndIgnoreGroupNotFound( + session.getGroupId(), consultants))); } /** @@ -104,7 +105,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/main/java/de/caritas/cob/userservice/api/facade/assignsession/AssignEnquiryFacade.java b/src/main/java/de/caritas/cob/userservice/api/facade/assignsession/AssignEnquiryFacade.java index 9983253bb..060369c44 100644 --- a/src/main/java/de/caritas/cob/userservice/api/facade/assignsession/AssignEnquiryFacade.java +++ b/src/main/java/de/caritas/cob/userservice/api/facade/assignsession/AssignEnquiryFacade.java @@ -163,7 +163,7 @@ private void removeUnauthorizedMembers( .onSessionConsultants(Map.of(session, consultantsToRemoveFromRocketChat)); if (rcGroupId.equalsIgnoreCase(session.getGroupId())) { - rocketChatRemoveFromGroupOperationService.removeFromGroup(); + rocketChatRemoveFromGroupOperationService.removeFromGroupAndIgnoreGroupNotFound(); } if (rcGroupId.equalsIgnoreCase(session.getFeedbackGroupId())) { rocketChatRemoveFromGroupOperationService.removeFromFeedbackGroup(); 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)) From c07de3bd99af967c70848b9e119a723060bbb214 Mon Sep 17 00:00:00 2001 From: tkuzynow Date: Thu, 22 Aug 2024 16:14:51 +0200 Subject: [PATCH 2/3] fix: delete rooms even if user is already deleted from MariaDB --- .../RocketChatRemoveFromGroupOperationService.java | 6 ++++++ .../api/facade/assignsession/AssignEnquiryFacade.java | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) 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 e08560e11..b677724fb 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 @@ -81,6 +81,12 @@ public void removeFromGroupAndIgnoreGroupNotFound() { session.getGroupId(), consultants))); } + public void removeFromGroup() { + this.consultantsToRemoveFromSessions.forEach( + ((session, consultants) -> + removeConsultantsFromSessionGroup(session.getGroupId(), consultants))); + } + /** * Removes the given consultant from Rocket.Chat group of given session with rollback on error. */ diff --git a/src/main/java/de/caritas/cob/userservice/api/facade/assignsession/AssignEnquiryFacade.java b/src/main/java/de/caritas/cob/userservice/api/facade/assignsession/AssignEnquiryFacade.java index 060369c44..9983253bb 100644 --- a/src/main/java/de/caritas/cob/userservice/api/facade/assignsession/AssignEnquiryFacade.java +++ b/src/main/java/de/caritas/cob/userservice/api/facade/assignsession/AssignEnquiryFacade.java @@ -163,7 +163,7 @@ private void removeUnauthorizedMembers( .onSessionConsultants(Map.of(session, consultantsToRemoveFromRocketChat)); if (rcGroupId.equalsIgnoreCase(session.getGroupId())) { - rocketChatRemoveFromGroupOperationService.removeFromGroupAndIgnoreGroupNotFound(); + rocketChatRemoveFromGroupOperationService.removeFromGroup(); } if (rcGroupId.equalsIgnoreCase(session.getFeedbackGroupId())) { rocketChatRemoveFromGroupOperationService.removeFromFeedbackGroup(); From b9fdf7d11d70ca5d0e8b5de97136178c57cd90ac Mon Sep 17 00:00:00 2001 From: tkuzynow Date: Thu, 22 Aug 2024 16:15:04 +0200 Subject: [PATCH 3/3] fix: tests --- .../RocketChatRemoveFromGroupOperationService.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 b677724fb..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 @@ -82,10 +82,10 @@ public void removeFromGroupAndIgnoreGroupNotFound() { } public void removeFromGroup() { - this.consultantsToRemoveFromSessions.forEach( - ((session, consultants) -> - removeConsultantsFromSessionGroup(session.getGroupId(), consultants))); - } + this.consultantsToRemoveFromSessions.forEach( + ((session, consultants) -> + removeConsultantsFromSessionGroup(session.getGroupId(), consultants))); + } /** * Removes the given consultant from Rocket.Chat group of given session with rollback on error.