Skip to content

Commit

Permalink
Merging cbrelease-4.8.14
Browse files Browse the repository at this point in the history
  • Loading branch information
karthik-tarento committed Jul 12, 2024
2 parents d04b023 + 03bc99f commit a103a74
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/sunbird/common/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,10 @@ public ResponseEntity<SBApiResponse> generateOTP(@RequestBody Map<String, Object
SBApiResponse response = userRegService.generateOTP(otpRequests);
return new ResponseEntity<>(response, response.getResponseCode());
}

@GetMapping("/user/email/approved/domains")
public ResponseEntity<SBApiResponse> getApprovedDomain() throws Exception {
SBApiResponse response = userRegService.getApprovedDomains();
return new ResponseEntity<>(response, response.getResponseCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ public interface UserRegistrationService {

public SBApiResponse generateOTP(Map<String,Object> otpRequests);

public SBApiResponse getApprovedDomains();

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -305,6 +306,34 @@ public void initiateCreateUserFlow(String registrationCode) {
}
}

public SBApiResponse getApprovedDomains() {
SBApiResponse response = ProjectUtil.createDefaultResponse(Constants.API_APPROVED_DOMAINS);
String errMsg = "";
try {
Set<String> approvedDomains = getApprovedDomainsFromDB();
if (CollectionUtils.isNotEmpty(approvedDomains)) {
Map<String, Object> 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);
Expand Down Expand Up @@ -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<String> getApprovedDomainsFromDB() {
Set<String> domains = new HashSet<>();

Set<String> preApprovedDomains = fetchDomainsByContextType(Constants.USER_REGISTRATION_PRE_APPROVED_DOMAIN);
domains.addAll(preApprovedDomains);

Set<String> approvedDomains = fetchDomainsByContextType(Constants.USER_REGISTRATION_DOMAIN);
domains.addAll(approvedDomains);

return domains;
}

private Set<String> fetchDomainsByContextType(String contextType) {
Map<String, Object> propertyMap = new HashMap<>();
propertyMap.put(Constants.CONTEXT_TYPE, contextType);

List<Map<String, Object>> 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());
}

}

0 comments on commit a103a74

Please sign in to comment.