Skip to content

Commit

Permalink
Issue #LR-676 feat: Delete User API
Browse files Browse the repository at this point in the history
  • Loading branch information
Jayaprakash8887 authored and Jayaprakash8887 committed Dec 6, 2023
1 parent 750047c commit 6c73fa1
Showing 1 changed file with 36 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.sunbird.actor.user;

import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.sunbird.actor.core.BaseActor;
import org.sunbird.common.ElasticSearchHelper;
import org.sunbird.dto.SearchDTO;
Expand All @@ -24,31 +27,51 @@ public void onReceive(Request request) throws Throwable {
private void inputKafkaTopic(Request request) throws Exception {
Map<String, Object> userDetails = request.getRequest();
String userId = (String) userDetails.get(JsonKey.USER_ID);
ArrayList roles = (ArrayList) userDetails.get(JsonKey.USER_ROLES);
ArrayList<String> roles = (ArrayList) userDetails.get(JsonKey.USER_ROLES);
logger.info("UserDeletionBackgroundJobActor::inputKafkaTopic:: userId:: " + userId);
logger.info("UserDeletionBackgroundJobActor::inputKafkaTopic:: roles size:: " + roles.size());
User user = userService.getUserById(userId, request.getRequestContext());
String rootOrgId = user.getRootOrgId();

ArrayList<Map<String, Object>> suggestedUsersList = new ArrayList<>();
List<Map<String, Object>> suggestedUsersList = new ArrayList<>();
Map<String, Object> searchQueryMap = new HashMap<>();
Map<String, Object> searchFilter = new HashMap<>();
searchFilter.put(JsonKey.ROOT_ORG_ID, rootOrgId);

if (!roles.isEmpty()) {
suggestedUsersList = fetchUsersByRoles(roles, rootOrgId, request);
}
if (!roles.contains(JsonKey.ORG_ADMIN)) roles.add(JsonKey.ORG_ADMIN);

if (!roles.contains(JsonKey.ORG_ADMIN)) {
// Add logic to fetch and add orgAdmins to suggestedUsersList
ArrayList<Map<String, Object>> orgAdminUsersList =
fetchUsersByRoles((ArrayList) Arrays.asList(JsonKey.ORG_ADMIN), rootOrgId, request);
if (!orgAdminUsersList.isEmpty()) suggestedUsersList.addAll(orgAdminUsersList);
}
// for each role in the organisation, fetch list of other users to pass it as part of event
List<String> queryFields = new ArrayList<>();
queryFields.add(JsonKey.USER_ID);
searchQueryMap.put(JsonKey.FIELDS, queryFields);
logger.info("UserDeletionBackgroundJobActor::inputKafkaTopic:: roles size:: " + roles.size());
roles.forEach(
role -> {
searchFilter.put(JsonKey.ROLES + "." + JsonKey.ROLE, role);
searchQueryMap.put(JsonKey.FILTERS, searchFilter);
SearchDTO searchDto = ElasticSearchHelper.createSearchDTO(searchQueryMap);
Map<String, Object> result =
userService.searchUser(searchDto, request.getRequestContext());
List<Map<String, Object>> userMapList =
(List<Map<String, Object>>) result.get(JsonKey.CONTENT);

if (!userMapList.isEmpty()) {
Map<String, Object> roleUsersMap = new HashMap<>();
roleUsersMap.put(JsonKey.ROLE, role);
List<String> roleUsersList = new ArrayList<>();
for (Map<String, Object> userMap : userMapList) {
roleUsersList.add((String) userMap.get(JsonKey.USER_ID));
}
roleUsersMap.put(JsonKey.USERS, roleUsersList);
suggestedUsersList.add(roleUsersMap);
}
});

logger.info(
"UserDeletionBackgroundJobActor::inputKafkaTopic:: suggestedUsersList size:: "
+ suggestedUsersList.size());

/* Fetch Managed Users - START */
Map<String, Object> searchQueryMap = new HashMap<>();
List<String> managedUsersList = new ArrayList<>();
Map<String, Object> managedUsersSearchFilter = new HashMap<>();
managedUsersSearchFilter.put(JsonKey.ROOT_ORG_ID, rootOrgId);
Expand Down Expand Up @@ -97,39 +120,4 @@ private void inputKafkaTopic(Request request) throws Exception {
"UserDeletionBackgroundJobActor::inputKafkaTopic:: InstructionEventGenerator.pushInstructionEvent:: ");
InstructionEventGenerator.pushInstructionEvent(userDeletionTopic, data);
}

private ArrayList fetchUsersByRoles(ArrayList roles, String rootOrgId, Request request) {
ArrayList<Map<String, Object>> suggestedUsersList = new ArrayList<>();
Map<String, Object> searchQueryMap = new HashMap<>();
Map<String, Object> searchFilter = new HashMap<>();
searchFilter.put(JsonKey.ROOT_ORG_ID, rootOrgId);

// for each role in the organisation, fetch list of other users to pass it as part of event
ArrayList<String> queryFields = new ArrayList<>();
queryFields.add(JsonKey.USER_ID);
searchQueryMap.put(JsonKey.FIELDS, queryFields);
logger.info("UserDeletionBackgroundJobActor::inputKafkaTopic:: roles size:: " + roles.size());
roles.forEach(
role -> {
searchFilter.put(JsonKey.ROLES + "." + JsonKey.ROLE, role);
searchQueryMap.put(JsonKey.FILTERS, searchFilter);
SearchDTO searchDto = ElasticSearchHelper.createSearchDTO(searchQueryMap);
Map<String, Object> result =
userService.searchUser(searchDto, request.getRequestContext());
ArrayList<Map<String, Object>> userMapList =
(ArrayList<Map<String, Object>>) result.get(JsonKey.CONTENT);

if (!userMapList.isEmpty()) {
Map<String, Object> roleUsersMap = new HashMap<>();
roleUsersMap.put(JsonKey.ROLE, role);
ArrayList<String> roleUsersList = new ArrayList<>();
for (Map<String, Object> userMap : userMapList) {
roleUsersList.add((String) userMap.get(JsonKey.USER_ID));
}
roleUsersMap.put(JsonKey.USERS, roleUsersList);
suggestedUsersList.add(roleUsersMap);
}
});
return suggestedUsersList;
}
}

0 comments on commit 6c73fa1

Please sign in to comment.