diff --git a/src/main/java/org/sunbird/common/util/Constants.java b/src/main/java/org/sunbird/common/util/Constants.java index 9f3b9eab6..fe608c406 100644 --- a/src/main/java/org/sunbird/common/util/Constants.java +++ b/src/main/java/org/sunbird/common/util/Constants.java @@ -1078,6 +1078,8 @@ public class Constants { public static final String PROFILE_DESIGNATION_STATUS = "profileDesignationStatus"; public static final String NOT_MY_USER = "NOT-MY-USER"; public static final String SPACE = " "; + public static final String API_APPROVED_DOMAINS = "api.approved.domains"; + public static final String DOMAINS = "domains"; public static final String REQUEST_TYPE = "requestType"; public static final String INSIGHT_FIELD_KEY = ".insights.fields"; public static final String INSIGHT_REDIS_KEY_MAPPING = ".insights.redis.key.mapping"; diff --git a/src/main/java/org/sunbird/user/registration/controller/UserRegistrationController.java b/src/main/java/org/sunbird/user/registration/controller/UserRegistrationController.java index 5a9d6bfae..5c1d6d2c5 100644 --- a/src/main/java/org/sunbird/user/registration/controller/UserRegistrationController.java +++ b/src/main/java/org/sunbird/user/registration/controller/UserRegistrationController.java @@ -44,4 +44,10 @@ public ResponseEntity generateOTP(@RequestBody Map(response, response.getResponseCode()); } + + @GetMapping("/user/email/approved/domains") + public ResponseEntity getApprovedDomain() throws Exception { + SBApiResponse response = userRegService.getApprovedDomains(); + return new ResponseEntity<>(response, response.getResponseCode()); + } } diff --git a/src/main/java/org/sunbird/user/registration/service/UserRegistrationService.java b/src/main/java/org/sunbird/user/registration/service/UserRegistrationService.java index ea46c1868..27ae9b78c 100644 --- a/src/main/java/org/sunbird/user/registration/service/UserRegistrationService.java +++ b/src/main/java/org/sunbird/user/registration/service/UserRegistrationService.java @@ -48,4 +48,6 @@ public interface UserRegistrationService { public SBApiResponse generateOTP(Map otpRequests); + public SBApiResponse getApprovedDomains(); + } diff --git a/src/main/java/org/sunbird/user/registration/service/UserRegistrationServiceImpl.java b/src/main/java/org/sunbird/user/registration/service/UserRegistrationServiceImpl.java index c6a69643d..d091f88d9 100644 --- a/src/main/java/org/sunbird/user/registration/service/UserRegistrationServiceImpl.java +++ b/src/main/java/org/sunbird/user/registration/service/UserRegistrationServiceImpl.java @@ -5,6 +5,7 @@ import java.io.InputStreamReader; import java.util.*; import java.util.regex.Pattern; +import java.util.stream.Collectors; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.RandomStringUtils; @@ -305,6 +306,34 @@ public void initiateCreateUserFlow(String registrationCode) { } } + public SBApiResponse getApprovedDomains() { + SBApiResponse response = ProjectUtil.createDefaultResponse(Constants.API_APPROVED_DOMAINS); + String errMsg = ""; + try { + Set approvedDomains = getApprovedDomainsFromDB(); + if (CollectionUtils.isNotEmpty(approvedDomains)) { + Map result = new HashMap<>(); + result.put(Constants.DOMAINS, approvedDomains); + LOGGER.info("Fetched pre-approved and approved domains successfully"); + response.setResult(result); + response.getResult().put(Constants.RESPONSE, Constants.SUCCESS.toUpperCase()); + } else { + errMsg = "Failed to Fetch Approved Domains"; + } + } catch (Exception e) { + LOGGER.error(String.format("Exception in %s : %s", "getApprovedDomains", e.getMessage()), e); + errMsg = "Failed to process message. Exception: " + e.getMessage(); + } + + if (StringUtils.isNotBlank(errMsg)) { + LOGGER.error("Failed to Fetch Approved Domains, error message : ", errMsg); + response.getParams().setStatus(Constants.FAILED); + response.getParams().setErrmsg(errMsg); + response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR); + } + return response; + } + private SBApiResponse createDefaultResponse(String api) { SBApiResponse response = new SBApiResponse(); response.setId(api); @@ -588,4 +617,29 @@ private Boolean isApprovedDomains(String emailDomain, String domainType) { Constants.KEYSPACE_SUNBIRD, Constants.TABLE_MASTER_DATA, propertyMap, Arrays.asList(Constants.CONTEXT_TYPE, Constants.CONTEXT_NAME)); return CollectionUtils.isNotEmpty(listOfDomains); } + + private Set getApprovedDomainsFromDB() { + Set domains = new HashSet<>(); + + Set preApprovedDomains = fetchDomainsByContextType(Constants.USER_REGISTRATION_PRE_APPROVED_DOMAIN); + domains.addAll(preApprovedDomains); + + Set approvedDomains = fetchDomainsByContextType(Constants.USER_REGISTRATION_DOMAIN); + domains.addAll(approvedDomains); + + return domains; + } + + private Set fetchDomainsByContextType(String contextType) { + Map propertyMap = new HashMap<>(); + propertyMap.put(Constants.CONTEXT_TYPE, contextType); + + List> records = cassandraOperation.getRecordsByPropertiesWithoutFiltering( + Constants.KEYSPACE_SUNBIRD, Constants.TABLE_MASTER_DATA, propertyMap, Arrays.asList(Constants.CONTEXT_NAME)); + + return records.stream() + .map(map -> (String) map.get(Constants.CONTEXT_NAME)) + .collect(Collectors.toSet()); + } + }