Skip to content

Commit

Permalink
KB-4794 and KB-4969 fix (#582)
Browse files Browse the repository at this point in the history
  • Loading branch information
karthik-tarento authored Jun 4, 2024
1 parent 6fc3c2c commit c629082
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 118 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/sunbird/common/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,7 @@ public class Constants {
public static final String PROFILE_STATUS_UPDATED_MSG_VIEWED = "isProfileUpdatedMsgViewed";
public static final String PROFILE_GROUP_STATUS = "profileGroupStatus";
public static final String PROFILE_DESIGNATION_STATUS = "profileDesignationStatus";
public static final String NOT_MY_USER = "NOT-MY-USER";

private Constants() {
throw new IllegalStateException("Utility class");
Expand Down
253 changes: 135 additions & 118 deletions src/main/java/org/sunbird/profile/service/ProfileServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1767,137 +1767,154 @@ public SBApiResponse profileMDOAdminUpdate(Map<String, Object> request, String u
Map<String, Object> responseMap = userUtilityService.getUsersReadData(userId, StringUtils.EMPTY,
StringUtils.EMPTY);
Map<String, Object> existingProfileDetails = (Map<String, Object>) responseMap.get(Constants.PROFILE_DETAILS);
String updatedProfileStatus = null;
String updatedGroupVal = null;
String updatedDesignationVal = null;

if (!profileDetailsMap.isEmpty()) {
{
List<String> listOfChangedDetails = new ArrayList<>();
for (String keys : profileDetailsMap.keySet()) {
listOfChangedDetails.add(keys);
}
for (String changedObj : listOfChangedDetails) {
if (profileDetailsMap.get(changedObj) instanceof String) {
existingProfileDetails.put(changedObj, profileDetailsMap.get(changedObj));
} else if (profileDetailsMap.get(changedObj) instanceof ArrayList) {
// KB-3718
if (Constants.PROFESSIONAL_DETAILS.equalsIgnoreCase(changedObj)) {
List<Map<String, Object>> professionalList = (List<Map<String, Object>>) existingProfileDetails
List<String> listOfChangedDetails = new ArrayList<>();
for (String keys : profileDetailsMap.keySet()) {
listOfChangedDetails.add(keys);
}
boolean isGroupOrDesignationUpdated = false;
for (String changedObj : listOfChangedDetails) {
if (profileDetailsMap.get(changedObj) instanceof String) {
existingProfileDetails.put(changedObj, profileDetailsMap.get(changedObj));
if (Constants.PROFILE_STATUS.equalsIgnoreCase(changedObj)) {
updatedProfileStatus = (String) profileDetailsMap.get(changedObj);
}
} else if (profileDetailsMap.get(changedObj) instanceof ArrayList) {
// KB-3718
if (Constants.PROFESSIONAL_DETAILS.equalsIgnoreCase(changedObj)) {
List<Map<String, Object>> professionalList = (List<Map<String, Object>>) existingProfileDetails
.get(Constants.PROFESSIONAL_DETAILS);
Map<String, Object> existingProfessionalDetailsMap = null;

// professional detail is empty... just replace...
if (CollectionUtils.isEmpty(professionalList)) {
existingProfileDetails.put(changedObj, profileDetailsMap.get(changedObj));
professionalList = (List<Map<String, Object>>) existingProfileDetails
.get(Constants.PROFESSIONAL_DETAILS);
Map<String, Object> existingProfessionalDetailsMap = null;
boolean isGroupOrDesignationUpdated = false;
// professional detail is empty... just replace...
if (CollectionUtils.isEmpty(professionalList)) {
existingProfileDetails.put(changedObj, profileDetailsMap.get(changedObj));
professionalList = (List<Map<String, Object>>) existingProfileDetails
.get(Constants.PROFESSIONAL_DETAILS);
existingProfessionalDetailsMap = professionalList.get(0);
isGroupOrDesignationUpdated = true;
} else {
existingProfessionalDetailsMap = professionalList.get(0);
Map<String, Object> updatedProfessionalDetailsMap = ((List<Map<String, Object>>) profileDetailsMap
.get(changedObj)).get(0);
for (String childKey : updatedProfessionalDetailsMap.keySet()) {
String updatedValue = (String) updatedProfessionalDetailsMap.get(childKey);
if ((Constants.GROUP.equalsIgnoreCase(childKey)
|| Constants.DESIGNATION.equalsIgnoreCase(childKey)) &&
!updatedValue.equalsIgnoreCase(
(String) existingProfessionalDetailsMap.get(childKey))) {
isGroupOrDesignationUpdated = true;
}
existingProfessionalDetailsMap.put(childKey,
updatedProfessionalDetailsMap.get(childKey));
}
}

if (isGroupOrDesignationUpdated) {
if (StringUtils
.isNotBlank((String) existingProfessionalDetailsMap.get(Constants.GROUP))) {
existingProfileDetails.put(Constants.PROFILE_GROUP_STATUS, Constants.VERIFIED);
} else {
existingProfileDetails.put(Constants.PROFILE_GROUP_STATUS,
Constants.NOT_VERIFIED);
}
if (StringUtils
.isNotBlank((String) existingProfessionalDetailsMap
.get(Constants.DESIGNATION))) {
existingProfileDetails.put(Constants.PROFILE_DESIGNATION_STATUS,
Constants.VERIFIED);
} else {
existingProfileDetails.put(Constants.PROFILE_DESIGNATION_STATUS,
Constants.NOT_VERIFIED);
}

if (Constants.VERIFIED.equalsIgnoreCase(
(String) existingProfileDetails.get(Constants.PROFILE_GROUP_STATUS)) &&
Constants.VERIFIED.equalsIgnoreCase((String) existingProfileDetails
.get(Constants.PROFILE_DESIGNATION_STATUS))) {
existingProfileDetails.put(Constants.PROFILE_STATUS, Constants.VERIFIED);
} else {
existingProfileDetails.put(Constants.PROFILE_STATUS, Constants.NOT_VERIFIED);
}

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH.mm.ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT+05:30"));
String timeStamp = sdf.format(new java.util.Date());
existingProfileDetails.put(Constants.PROFILE_STATUS_UPDATED_ON, timeStamp);
Map<String, Object> additionalProperties = (Map<String, Object>) existingProfileDetails
.get(Constants.ADDITIONAL_PROPERTIES);
if (ObjectUtils.isEmpty(additionalProperties)) {
additionalProperties = new HashMap<>();
existingProfessionalDetailsMap = professionalList.get(0);
isGroupOrDesignationUpdated = true;
} else {
existingProfessionalDetailsMap = professionalList.get(0);
Map<String, Object> updatedProfessionalDetailsMap = ((List<Map<String, Object>>) profileDetailsMap
.get(changedObj)).get(0);
for (String childKey : updatedProfessionalDetailsMap.keySet()) {
String updatedValue = (String) updatedProfessionalDetailsMap.get(childKey);
if ((Constants.GROUP.equalsIgnoreCase(childKey)
|| Constants.DESIGNATION.equalsIgnoreCase(childKey)) &&
!updatedValue.equalsIgnoreCase(
(String) existingProfessionalDetailsMap.get(childKey))) {
isGroupOrDesignationUpdated = true;
}
additionalProperties.put(Constants.PROFILE_STATUS_UPDATED_MSG_VIEWED, false);
existingProfileDetails.put(Constants.ADDITIONAL_PROPERTIES, additionalProperties);
existingProfessionalDetailsMap.put(childKey,
updatedProfessionalDetailsMap.get(childKey));
}
} else {
existingProfileDetails.put(changedObj, profileDetailsMap.get(changedObj));
}
} else if (profileDetailsMap.get(changedObj) instanceof Boolean) {
existingProfileDetails.put(changedObj, profileDetailsMap.get(changedObj));
updatedGroupVal = (String) existingProfessionalDetailsMap.get(Constants.GROUP);
updatedDesignationVal = (String) existingProfessionalDetailsMap.get(Constants.DESIGNATION);
} else {
if (existingProfileDetails.containsKey(changedObj)) {
Map<String, Object> existingProfileChild = (Map<String, Object>) existingProfileDetails
.get(changedObj);
Map<String, Object> requestedProfileChild = (Map<String, Object>) profileDetailsMap
.get(changedObj);
for (String childKey : requestedProfileChild.keySet()) {
existingProfileChild.put(childKey, requestedProfileChild.get(childKey));
}
} else {
existingProfileDetails.put(changedObj, profileDetailsMap.get(changedObj));
existingProfileDetails.put(changedObj, profileDetailsMap.get(changedObj));
}
} else if (profileDetailsMap.get(changedObj) instanceof Boolean) {
existingProfileDetails.put(changedObj, profileDetailsMap.get(changedObj));
} else {
if (existingProfileDetails.containsKey(changedObj)) {
Map<String, Object> existingProfileChild = (Map<String, Object>) existingProfileDetails
.get(changedObj);
Map<String, Object> requestedProfileChild = (Map<String, Object>) profileDetailsMap
.get(changedObj);
for (String childKey : requestedProfileChild.keySet()) {
existingProfileChild.put(childKey, requestedProfileChild.get(childKey));
}
} else {
existingProfileDetails.put(changedObj, profileDetailsMap.get(changedObj));
}
}

// Additional Condition for updating personal Details directly to user object
if (Constants.PERSONAL_DETAILS.equalsIgnoreCase(changedObj)) {
getModifiedPersonalDetails(profileDetailsMap.get(changedObj), requestData);
}
// Additional Condition for updating personal Details directly to user object
if (Constants.PERSONAL_DETAILS.equalsIgnoreCase(changedObj)) {
getModifiedPersonalDetails(profileDetailsMap.get(changedObj), requestData);
}
}

HashMap<String, String> headerValue = new HashMap<>();
headerValues.put(Constants.AUTH_TOKEN, authToken);
headerValues.put(Constants.CONTENT_TYPE, Constants.APPLICATION_JSON);
String updatedUrl = serverConfig.getSbUrl() + serverConfig.getLmsUserUpdatePrivatePath();
Map<String, Object> updateRequestValue = requestData;
updateRequestValue.put(Constants.PROFILE_DETAILS, existingProfileDetails);
Map<String, Object> updateRequest = new HashMap<>();
updateRequest.put(Constants.REQUEST, updateRequestValue);
Map<String, Object> updateResponse = outboundRequestHandlerService.fetchResultUsingPatch(updatedUrl, updateRequest, headerValue);

if (Constants.OK.equalsIgnoreCase((String) updateResponse.get(Constants.RESPONSE_CODE))) {
response.setResponseCode(HttpStatus.OK);
response.getResult().put(Constants.RESPONSE, Constants.SUCCESS);
response.getParams().setStatus(Constants.SUCCESS);
if (Constants.VERIFIED.equalsIgnoreCase(updatedProfileStatus)) {
existingProfileDetails.put(Constants.PROFILE_GROUP_STATUS, Constants.VERIFIED);
existingProfileDetails.put(Constants.PROFILE_DESIGNATION_STATUS, Constants.VERIFIED);
isGroupOrDesignationUpdated = true;
} else if (Constants.NOT_VERIFIED.equalsIgnoreCase(updatedProfileStatus)
|| Constants.NOT_MY_USER.equalsIgnoreCase(updatedProfileStatus)) {
//If marked as "NOT-VERIFIED" then no need to change the details.
isGroupOrDesignationUpdated = false;
existingProfileDetails.put(Constants.PROFILE_GROUP_STATUS, Constants.NOT_VERIFIED);
existingProfileDetails.put(Constants.PROFILE_DESIGNATION_STATUS, Constants.NOT_VERIFIED);
}
if (isGroupOrDesignationUpdated) {
if (StringUtils.isNotBlank(updatedGroupVal)) {
existingProfileDetails.put(Constants.PROFILE_GROUP_STATUS, Constants.VERIFIED);
} else {
if (updateResponse != null && Constants.CLIENT_ERROR.equalsIgnoreCase((String) updateResponse.get(Constants.RESPONSE_CODE))) {
response.setResponseCode(HttpStatus.BAD_REQUEST);
} else {
response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR);
}
response.getParams().setStatus(Constants.FAILED);
String errMsg = (String) ((Map<String, Object>) updateResponse.get(Constants.PARAMS)).get(Constants.ERROR_MESSAGE);
errMsg = PropertiesCache.getInstance().readCustomError(errMsg);
response.getParams().setErrmsg(errMsg);
log.error(errMsg, new Exception(errMsg));
return response;
existingProfileDetails.put(Constants.PROFILE_GROUP_STATUS,
Constants.NOT_VERIFIED);
}
if (StringUtils
.isNotBlank(updatedDesignationVal)) {
existingProfileDetails.put(Constants.PROFILE_DESIGNATION_STATUS,
Constants.VERIFIED);
} else {
existingProfileDetails.put(Constants.PROFILE_DESIGNATION_STATUS,
Constants.NOT_VERIFIED);
}

if (Constants.VERIFIED.equalsIgnoreCase(
(String) existingProfileDetails.get(Constants.PROFILE_GROUP_STATUS)) &&
Constants.VERIFIED.equalsIgnoreCase((String) existingProfileDetails
.get(Constants.PROFILE_DESIGNATION_STATUS))) {
existingProfileDetails.put(Constants.PROFILE_STATUS, Constants.VERIFIED);
} else {
existingProfileDetails.put(Constants.PROFILE_STATUS, Constants.NOT_VERIFIED);
}

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH.mm.ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT+05:30"));
String timeStamp = sdf.format(new java.util.Date());
existingProfileDetails.put(Constants.PROFILE_STATUS_UPDATED_ON, timeStamp);
Map<String, Object> additionalProperties = (Map<String, Object>) existingProfileDetails
.get(Constants.ADDITIONAL_PROPERTIES);
if (ObjectUtils.isEmpty(additionalProperties)) {
additionalProperties = new HashMap<>();
}
additionalProperties.put(Constants.PROFILE_STATUS_UPDATED_MSG_VIEWED, false);
existingProfileDetails.put(Constants.ADDITIONAL_PROPERTIES, additionalProperties);
}

HashMap<String, String> headerValue = new HashMap<>();
headerValues.put(Constants.AUTH_TOKEN, authToken);
headerValues.put(Constants.CONTENT_TYPE, Constants.APPLICATION_JSON);
String updatedUrl = serverConfig.getSbUrl() + serverConfig.getLmsUserUpdatePrivatePath();
Map<String, Object> updateRequestValue = requestData;
updateRequestValue.put(Constants.PROFILE_DETAILS, existingProfileDetails);
Map<String, Object> updateRequest = new HashMap<>();
updateRequest.put(Constants.REQUEST, updateRequestValue);
Map<String, Object> updateResponse = outboundRequestHandlerService.fetchResultUsingPatch(updatedUrl, updateRequest, headerValue);

if (Constants.OK.equalsIgnoreCase((String) updateResponse.get(Constants.RESPONSE_CODE))) {
response.setResponseCode(HttpStatus.OK);
response.getResult().put(Constants.RESPONSE, Constants.SUCCESS);
response.getParams().setStatus(Constants.SUCCESS);
} else {
if (updateResponse != null && Constants.CLIENT_ERROR.equalsIgnoreCase((String) updateResponse.get(Constants.RESPONSE_CODE))) {
response.setResponseCode(HttpStatus.BAD_REQUEST);
} else {
response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR);
}
response.getParams().setStatus(Constants.FAILED);
String errMsg = (String) ((Map<String, Object>) updateResponse.get(Constants.PARAMS)).get(Constants.ERROR_MESSAGE);
errMsg = PropertiesCache.getInstance().readCustomError(errMsg);
response.getParams().setErrmsg(errMsg);
log.error(errMsg, new Exception(errMsg));
return response;
}
}
} catch (Exception e) {
Expand Down

0 comments on commit c629082

Please sign in to comment.