Skip to content

Commit

Permalink
fix: deletion routine changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tkuzynow committed Sep 27, 2024
1 parent 10d2119 commit d5a0030
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ public class RocketChatService implements MessageClient {
private static final String USER_LIST_GET_FIELD_SELECTION = "{\"_id\":1}";
private static final Integer PAGE_SIZE = 100;
private static final String ERROR_ROOM_NOT_FOUND = "error-room-not-found";
private static final String COULD_NOT_REMOVE_USER_FROM_ROCKET_CHAT_GROUP =
"Could not remove user %s from Rocket.Chat group with id %s";
private final LocalDateTime localDateTime1900 = LocalDateTime.of(1900, 1, 1, 0, 0);

private final LocalDateTime localDateTimeFuture = nowInUtc().plusYears(1L);
Expand Down Expand Up @@ -662,12 +664,11 @@ public void removeUserFromGroup(String rcUserId, String rcGroupId)
rcGroupId,
ex);
throw new RocketChatRemoveUserFromGroupException(
String.format(
"Could not remove user %s from Rocket.Chat group with id %s", rcUserId, rcGroupId));
String.format(COULD_NOT_REMOVE_USER_FROM_ROCKET_CHAT_GROUP, rcUserId, rcGroupId));
}

if (response != null && !response.isSuccess()) {
var error = "Could not remove user %s from Rocket.Chat group with id %s";
var error = COULD_NOT_REMOVE_USER_FROM_ROCKET_CHAT_GROUP;
throw new RocketChatRemoveUserFromGroupException(String.format(error, rcUserId, rcGroupId));
}
}
Expand Down Expand Up @@ -802,6 +803,7 @@ public List<GroupMemberDTO> getChatUsers(String chatId) {
* @return al members of the group
* @deprecated use getChatUsers
*/
@Deprecated
public List<GroupMemberDTO> getMembersOfGroup(String rcGroupId)
throws RocketChatGetGroupMembersException {

Expand Down Expand Up @@ -1365,12 +1367,11 @@ public void removeUserFromGroupIgnoreGroupNotFound(String rcUserId, String rcGro
rcGroupId,
ex);
throw new RocketChatRemoveUserFromGroupException(
String.format(
"Could not remove user %s from Rocket.Chat group with id %s", rcUserId, rcGroupId));
String.format(COULD_NOT_REMOVE_USER_FROM_ROCKET_CHAT_GROUP, rcUserId, rcGroupId));
}

if (response != null && !response.isSuccess()) {
var error = "Could not remove user %s from Rocket.Chat group with id %s";
var error = COULD_NOT_REMOVE_USER_FROM_ROCKET_CHAT_GROUP;
throw new RocketChatRemoveUserFromGroupException(String.format(error, rcUserId, rcGroupId));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private boolean shouldEnrichTopic(SessionDTO session) {
}

private void enrichSession(Map<Long, TopicDTO> availableTopics, SessionDTO sessionDTO) {
var topicData = availableTopics.get(Long.valueOf(sessionDTO.getTopic().getId()));
var topicData = availableTopics.get(sessionDTO.getTopic().getId());
if (topicData != null) {
log.debug("Enriching session with id {} with topicData {}", sessionDTO.getId(), topicData);
sessionDTO.setTopic(convertToSessionTopicDTO(topicData));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
@RequiredArgsConstructor
abstract class DeleteRoomsAndSessionAction {

private static final String USER_SERVICE_DELETE_WORKFLOW_ERROR =
"UserService delete workflow error: ";
protected final @NonNull SessionRepository sessionRepository;
protected final @NonNull SessionDataRepository sessionDataRepository;
protected final @NonNull RocketChatService rocketChatService;
Expand All @@ -29,7 +31,7 @@ void deleteRocketChatGroup(String rcGroupId, List<DeletionWorkflowError> workflo
try {
this.rocketChatService.deleteGroupAsTechnicalUser(rcGroupId);
} catch (RocketChatDeleteGroupException e) {
log.error("UserService delete workflow error: ", e);
log.error(USER_SERVICE_DELETE_WORKFLOW_ERROR, e);
workflowErrors.add(
DeletionWorkflowError.builder()
.deletionSourceType(ASKER)
Expand All @@ -47,7 +49,7 @@ void deleteSessionData(Session session, List<DeletionWorkflowError> workflowErro
var sessionData = this.sessionDataRepository.findBySessionId(session.getId());
this.sessionDataRepository.deleteAll(sessionData);
} catch (Exception e) {
log.error("UserService delete workflow error: ", e);
log.error(USER_SERVICE_DELETE_WORKFLOW_ERROR, e);
workflowErrors.add(
DeletionWorkflowError.builder()
.deletionSourceType(ASKER)
Expand All @@ -63,7 +65,7 @@ protected void deleteSession(Session session, List<DeletionWorkflowError> workfl
try {
this.sessionRepository.delete(session);
} catch (Exception e) {
log.error("UserService delete workflow error: ", e);
log.error(USER_SERVICE_DELETE_WORKFLOW_ERROR, e);
workflowErrors.add(
DeletionWorkflowError.builder()
.deletionSourceType(ASKER)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package de.caritas.cob.userservice.api.workflow.delete.action.asker;

import static de.caritas.cob.userservice.api.helper.CustomLocalDateTime.nowInUtc;
import static de.caritas.cob.userservice.api.workflow.delete.model.DeletionSourceType.ASKER;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

import de.caritas.cob.userservice.api.actions.ActionCommand;
import de.caritas.cob.userservice.api.adapters.rocketchat.RocketChatService;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatDeleteGroupException;
import de.caritas.cob.userservice.api.model.Session;
import de.caritas.cob.userservice.api.workflow.delete.model.DeletionTargetType;
import de.caritas.cob.userservice.api.workflow.delete.model.DeletionWorkflowError;
import de.caritas.cob.userservice.api.workflow.delete.model.RocketchatRoomDeletionWorkflowDTO;
import de.caritas.cob.userservice.api.workflow.delete.model.SessionDeletionWorkflowDTO;
import java.util.List;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class DeleteSingleRoomAction implements ActionCommand<RocketchatRoomDeletionWorkflowDTO> {

protected final @NonNull RocketChatService rocketChatService;

public DeleteSingleRoomAction(@NonNull RocketChatService rocketChatService) {
this.rocketChatService = rocketChatService;
}

void deleteRocketChatGroup(String rcGroupId, List<DeletionWorkflowError> workflowErrors) {
if (isNotBlank(rcGroupId)) {
try {
this.rocketChatService.deleteGroupAsTechnicalUser(rcGroupId);
} catch (RocketChatDeleteGroupException e) {
log.error("UserService delete workflow error: ", e);
workflowErrors.add(
DeletionWorkflowError.builder()
.deletionSourceType(ASKER)
.deletionTargetType(DeletionTargetType.ROCKET_CHAT)
.identifier(rcGroupId)
.reason("Deletion of Rocket.Chat group failed")
.timestamp(nowInUtc())
.build());
}
}
}

/**
* Deletes the given {@link Session} in the database with the related Rocket.Chat room containing
* all messages and uploads.
*
* @param actionTarget the {@link SessionDeletionWorkflowDTO} with the session to delete
*/
@Override
public void execute(RocketchatRoomDeletionWorkflowDTO actionTarget) {
deleteRocketChatGroup(
actionTarget.getRocketchatRoomId(), actionTarget.getDeletionWorkflowErrors());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.caritas.cob.userservice.api.workflow.delete.model;

import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class RocketchatRoomDeletionWorkflowDTO {

private String rocketchatRoomId;
private List<DeletionWorkflowError> deletionWorkflowErrors;
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ private List<DeletionWorkflowError> performSessionDeletion(
String rcGroupId, List<Session> userSessionList) {
List<DeletionWorkflowError> workflowErrors = new ArrayList<>();
Optional<Session> session = findSessionInUserSessionList(rcGroupId, userSessionList);
session.ifPresent(s -> workflowErrors.addAll(deleteSessionService.performSessionDeletion(s)));
session.ifPresentOrElse(
s -> workflowErrors.addAll(deleteSessionService.performSessionDeletion(s)),
() ->
workflowErrors.addAll(
deleteSessionService.performRocketchatSessionDeletion(rcGroupId)));

return workflowErrors;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

import de.caritas.cob.userservice.api.actions.registry.ActionsRegistry;
import de.caritas.cob.userservice.api.model.Session;
import de.caritas.cob.userservice.api.workflow.delete.action.asker.DeleteSingleRoomAction;
import de.caritas.cob.userservice.api.workflow.delete.action.asker.DeleteSingleRoomAndSessionAction;
import de.caritas.cob.userservice.api.workflow.delete.model.DeletionWorkflowError;
import de.caritas.cob.userservice.api.workflow.delete.model.RocketchatRoomDeletionWorkflowDTO;
import de.caritas.cob.userservice.api.workflow.delete.model.SessionDeletionWorkflowDTO;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import lombok.NonNull;
Expand Down Expand Up @@ -42,4 +45,20 @@ public List<DeletionWorkflowError> performSessionDeletion(Session session) {

return deletionWorkflowDTO.getDeletionWorkflowErrors();
}

public Collection<? extends DeletionWorkflowError> performRocketchatSessionDeletion(
String rcGroupId) {
if (isNull(rcGroupId)) {
return Collections.emptyList();
}

var deletionWorkflowDTO = new RocketchatRoomDeletionWorkflowDTO(rcGroupId, new ArrayList<>());

this.actionsRegistry
.buildContainerForType(RocketchatRoomDeletionWorkflowDTO.class)
.addActionToExecute(DeleteSingleRoomAction.class)
.executeActions(deletionWorkflowDTO);

return deletionWorkflowDTO.getDeletionWorkflowErrors();
}
}

0 comments on commit d5a0030

Please sign in to comment.