From bbbbc182430e6b366015960d01e1c885dae7726b Mon Sep 17 00:00:00 2001
From: Reshmi V Nair <54312456+reshmi-nair@users.noreply.github.com>
Date: Tue, 9 Nov 2021 22:11:53 +0530
Subject: [PATCH] SB-27485 [Update API] New v3 upadte API is allowing to give
 subtype for teacher, student and parent (#989)

* SB-27485 [Update API] New v3 upadte API is allowing to give subtype for teacher, student and parent
---
 .../java/org/sunbird/exception/ResponseCode.java |  1 +
 .../org/sunbird/exception/ResponseMessage.java   |  2 ++
 .../user/validator/UserRequestValidator.java     | 16 +++++++++++++++-
 .../service/user/UserProfileReadService.java     |  2 ++
 .../user/validator/UserRequestValidatorTest.java |  5 +++++
 5 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/core/platform-common/src/main/java/org/sunbird/exception/ResponseCode.java b/core/platform-common/src/main/java/org/sunbird/exception/ResponseCode.java
index f2ec17cc0..109d8a450 100644
--- a/core/platform-common/src/main/java/org/sunbird/exception/ResponseCode.java
+++ b/core/platform-common/src/main/java/org/sunbird/exception/ResponseCode.java
@@ -61,6 +61,7 @@ public enum ResponseCode {
       ResponseMessage.Key.ERROR_ATTRIBUTE_CONFLICT,
       ResponseMessage.Message.ERROR_ATTRIBUTE_CONFLICT),
   rolesRequired(ResponseMessage.Key.ROLES_MISSING, ResponseMessage.Message.ROLES_MISSING),
+  profileUserTypesRequired(ResponseMessage.Key.PROFILE_USER_TYPES_MISSING, ResponseMessage.Message.PROFILE_USER_TYPES_MISSING),
   emptyRolesProvided(
       ResponseMessage.Key.EMPTY_ROLES_PROVIDED, ResponseMessage.Message.EMPTY_ROLES_PROVIDED),
   contentTypeRequiredError(
diff --git a/core/platform-common/src/main/java/org/sunbird/exception/ResponseMessage.java b/core/platform-common/src/main/java/org/sunbird/exception/ResponseMessage.java
index 18ec57606..a7a970ad4 100644
--- a/core/platform-common/src/main/java/org/sunbird/exception/ResponseMessage.java
+++ b/core/platform-common/src/main/java/org/sunbird/exception/ResponseMessage.java
@@ -47,6 +47,7 @@ public interface ResponseMessage {
     String DATA_TYPE_ERROR = "Data type of {0} should be {1}.";
     String ERROR_ATTRIBUTE_CONFLICT = "Either pass attribute {0} or {1} but not both.";
     String ROLES_MISSING = "user role is required.";
+    String PROFILE_USER_TYPES_MISSING = "User type is required.";
     String EMPTY_ROLES_PROVIDED = "Roles cannot be empty.";
     String CHANNEL_REG_FAILED = "Channel Registration failed.";
     String SLUG_IS_NOT_UNIQUE =
@@ -215,6 +216,7 @@ public interface ResponseMessage {
     String DATA_TYPE_ERROR = "DATA_TYPE_ERROR";
     String ERROR_ATTRIBUTE_CONFLICT = "ERROR_ATTRIBUTE_CONFLICT";
     String ROLES_MISSING = "ROLES_REQUIRED_ERROR";
+    String PROFILE_USER_TYPES_MISSING = "PROFILE_USER_TYPES_REQUIRED_ERROR";
     String EMPTY_ROLES_PROVIDED = "EMPTY_ROLES_PROVIDED";
     String CONTENT_TYPE_ERROR = "CONTENT_TYPE_ERROR";
     String INVALID_PROPERTY_ERROR = "INVALID_PROPERTY_ERROR";
diff --git a/service/src/main/java/org/sunbird/actor/user/validator/UserRequestValidator.java b/service/src/main/java/org/sunbird/actor/user/validator/UserRequestValidator.java
index 96b8e38cf..8f55f88f4 100644
--- a/service/src/main/java/org/sunbird/actor/user/validator/UserRequestValidator.java
+++ b/service/src/main/java/org/sunbird/actor/user/validator/UserRequestValidator.java
@@ -12,6 +12,7 @@ import org.sunbird.exception.ResponseCode;
 import org.sunbird.exception.ResponseMessage;
 import org.sunbird.keys.JsonKey;
 import org.sunbird.logging.LoggerUtil;
+import org.sunbird.operations.ActorOperations;
 import org.sunbird.request.Request;
 import org.sunbird.request.RequestContext;
 import org.sunbird.util.DataCacheHandler;
@@ -430,12 +431,25 @@ public class UserRequestValidator extends BaseRequestValidator {
             ERROR_CODE);
       }
     }
+    if (userRequest
+            .getOperation()
+            .equalsIgnoreCase(ActorOperations.UPDATE_USER.getValue()) || userRequest
+            .getOperation()
+            .equalsIgnoreCase(ActorOperations.UPDATE_USER_V2.getValue())) {
+      if (userRequest.getRequest().containsKey(JsonKey.PROFILE_USERTYPES)){
+        ProjectCommonException.throwClientErrorException(ResponseCode.invalidParameter,JsonKey.PROFILE_USERTYPES);
+      }
+    }else{
+      if (userRequest.getRequest().containsKey(JsonKey.PROFILE_USERTYPE)){
+        ProjectCommonException.throwClientErrorException(ResponseCode.invalidParameter,JsonKey.PROFILE_USERTYPE);
+      }
+    }
     if (userRequest.getRequest().containsKey(JsonKey.PROFILE_USERTYPES)
             && null != userRequest.getRequest().get(JsonKey.PROFILE_USERTYPES)) {
       if (userRequest.getRequest().get(JsonKey.PROFILE_USERTYPES) instanceof List){
         List profileusertypes = (List) userRequest.getRequest().get(JsonKey.PROFILE_USERTYPES);
         if (CollectionUtils.isEmpty(profileusertypes)) {
-          ProjectCommonException.throwClientErrorException(ResponseCode.rolesRequired);
+          ProjectCommonException.throwClientErrorException(ResponseCode.profileUserTypesRequired);
         }else {
           try {
             List<Map<String, String>> profUserTypeList =
diff --git a/service/src/main/java/org/sunbird/service/user/UserProfileReadService.java b/service/src/main/java/org/sunbird/service/user/UserProfileReadService.java
index 8b72f2eae..5b45c4691 100644
--- a/service/src/main/java/org/sunbird/service/user/UserProfileReadService.java
+++ b/service/src/main/java/org/sunbird/service/user/UserProfileReadService.java
@@ -77,6 +77,8 @@ public class UserProfileReadService {
             || readVersion.equalsIgnoreCase(ActorOperations.GET_USER_PROFILE_V5.getValue()))) {
       Util.getOrgDefaultValue().keySet().forEach(rootOrg::remove);
       Util.getUserDefaultValue().keySet().forEach(result::remove);
+    }else{
+      result.putAll(Util.getUserDefaultValue());
     }
     result.put(JsonKey.ROOT_ORG, rootOrg);
     Map<String, List<String>> userOrgRoles = null;
diff --git a/service/src/test/java/org/sunbird/actor/user/validator/UserRequestValidatorTest.java b/service/src/test/java/org/sunbird/actor/user/validator/UserRequestValidatorTest.java
index 6eb7af3cb..ac93d887f 100644
--- a/service/src/test/java/org/sunbird/actor/user/validator/UserRequestValidatorTest.java
+++ b/service/src/test/java/org/sunbird/actor/user/validator/UserRequestValidatorTest.java
@@ -16,6 +16,7 @@ import org.sunbird.exception.ProjectCommonException;
 import org.sunbird.exception.ResponseCode;
 import org.sunbird.exception.ResponseMessage;
 import org.sunbird.keys.JsonKey;
+import org.sunbird.operations.ActorOperations;
 import org.sunbird.request.Request;
 import org.sunbird.request.RequestContext;
 import org.sunbird.validator.RequestValidator;
@@ -375,6 +376,7 @@ public class UserRequestValidatorTest {
     Map<String, Object> requestObj = request.getRequest();
     requestObj.remove(JsonKey.USERNAME);
     requestObj.put(JsonKey.USER_ID, "userId");
+    request.setOperation(ActorOperations.UPDATE_USER.getValue());
 
     List<String> roles = new ArrayList<String>();
     roles.add("PUBLIC");
@@ -400,6 +402,7 @@ public class UserRequestValidatorTest {
     Map<String, Object> requestObj = request.getRequest();
     requestObj.remove(JsonKey.USERNAME);
     requestObj.put(JsonKey.USER_ID, "userId");
+    request.setOperation(ActorOperations.UPDATE_USER_V3.getValue());
 
     List<Map<String, String>> usertypes = new ArrayList();
     Map<String, String> typemap = new HashMap<>();
@@ -430,6 +433,7 @@ public class UserRequestValidatorTest {
     Map<String, Object> requestObj = request.getRequest();
     requestObj.remove(JsonKey.USERNAME);
     requestObj.put(JsonKey.USER_ID, "userId");
+    request.setOperation(ActorOperations.UPDATE_USER_V2.getValue());
 
     List<Map<String, String>> usertypes = new ArrayList();
     Map<String, String> typemap = new HashMap<>();
@@ -460,6 +464,7 @@ public class UserRequestValidatorTest {
     Map<String, Object> requestObj = request.getRequest();
     requestObj.remove(JsonKey.USERNAME);
     requestObj.put(JsonKey.USER_ID, "userId");
+    request.setOperation(ActorOperations.UPDATE_USER.getValue());
 
     List usertypes = new ArrayList();
     usertypes.add("teacher");
-- 
GitLab