diff --git a/controller/app/controllers/usermanagement/UserController.java b/controller/app/controllers/usermanagement/UserController.java index e905e0e4ff..ed2f558221 100644 --- a/controller/app/controllers/usermanagement/UserController.java +++ b/controller/app/controllers/usermanagement/UserController.java @@ -485,4 +485,11 @@ public CompletionStage updateUserDeclarations(Http.Request httpRequest) true, httpRequest); } + + public CompletionStage getUserLoggedInDetails(String userId, Http.Request httpRequest) { + return handleGetUserProfileV3( + ActorOperations.GET_USER_LOGIN_V1.getValue(), + ProjectUtil.getLmsUserId(userId), + httpRequest); + } } diff --git a/controller/conf/routes b/controller/conf/routes index 53609d57df..db600837f2 100644 --- a/controller/conf/routes +++ b/controller/conf/routes @@ -27,6 +27,7 @@ GET /v3/user/read/:uid @controllers.usermanagement.UserController.g GET /v4/user/read/:uid @controllers.usermanagement.UserController.getUserByIdV4(uid:String, request: play.mvc.Http.Request) GET /v5/user/read/:uid @controllers.usermanagement.UserController.getUserByIdV5(uid:String, request: play.mvc.Http.Request) GET /private/user/v1/read/:externalId @controllers.usermanagement.UserController.getUserByIdV3(externalId:String, request: play.mvc.Http.Request) +GET /v1/user/login/:uid @controllers.usermanagement.UserController.getUserLoggedInDetails(uid:String, request: play.mvc.Http.Request) GET /v1/user/managed/:lua_uuid @controllers.usermanagement.UserController.getManagedUsers(lua_uuid:String, request: play.mvc.Http.Request) POST /v1/user/getuser @controllers.usermanagement.UserController.getUserByLoginId(request: play.mvc.Http.Request) diff --git a/core/platform-common/src/main/java/org/sunbird/keys/JsonKey.java b/core/platform-common/src/main/java/org/sunbird/keys/JsonKey.java index 0034490936..6166d89ceb 100644 --- a/core/platform-common/src/main/java/org/sunbird/keys/JsonKey.java +++ b/core/platform-common/src/main/java/org/sunbird/keys/JsonKey.java @@ -640,6 +640,9 @@ public final class JsonKey { public static final String USER_READ_API_V2_MANDATORY_FIELDS = "user_read_api_v2_mandatory_fields"; public static final String USER_READ_API_V2_NON_MANDATORY_FIELDS = "user_read_api_v2_non_mandatory_fields"; public static final String PROFILE_UPDATE_COMPLETION = "profileUpdateCompletion"; + public static final String LAST_LOGIN = "last_login"; + public static final String FIRST_LOGIN = "first_login"; + public static final String EDATA = "edata"; private JsonKey() {} } diff --git a/core/platform-common/src/main/java/org/sunbird/operations/ActorOperations.java b/core/platform-common/src/main/java/org/sunbird/operations/ActorOperations.java index 60750efd56..ae6f95ddbd 100644 --- a/core/platform-common/src/main/java/org/sunbird/operations/ActorOperations.java +++ b/core/platform-common/src/main/java/org/sunbird/operations/ActorOperations.java @@ -20,6 +20,7 @@ public enum ActorOperations { GET_USER_PROFILE_V3("getUserProfileV3", "USRRED"), GET_USER_PROFILE_V4("getUserProfileV4", "USRRED"), + GET_USER_LOGIN_V1("getUserLoggedInDetails", "USRRED"), GET_USER_PROFILE_V5("getUserProfileV5", "USRRED"), UPDATE_USER_INFO_ELASTIC("updateUserInfoToElastic", "UBKGUPD"), diff --git a/service/src/main/java/org/sunbird/actor/user/UserProfileReadActor.java b/service/src/main/java/org/sunbird/actor/user/UserProfileReadActor.java index e07a5b9a7c..2cc5d2fe3e 100644 --- a/service/src/main/java/org/sunbird/actor/user/UserProfileReadActor.java +++ b/service/src/main/java/org/sunbird/actor/user/UserProfileReadActor.java @@ -44,11 +44,19 @@ public void onReceive(Request request) throws Throwable { case "getUserByKey": getUserByKey(request); break; + case "getUserLoggedInDetails": + getUserLoggedInDetails(request); + break; default: onReceiveUnsupportedOperation(); } } + private void getUserLoggedInDetails(Request actorMessage) throws Exception { + Response response = profileReadService.getUserLoggedInDetails(actorMessage); + sender().tell(response, self()); + } + private void getUserProfileV3(Request actorMessage) { Response response = profileReadService.getUserProfileData(actorMessage); sender().tell(response, self()); 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 7989a4155b..e0c9ccde4a 100644 --- a/service/src/main/java/org/sunbird/service/user/UserProfileReadService.java +++ b/service/src/main/java/org/sunbird/service/user/UserProfileReadService.java @@ -12,6 +12,7 @@ import org.sunbird.exception.ResponseCode; import org.sunbird.exception.ResponseMessage; import org.sunbird.helper.ServiceFactory; +import org.sunbird.kafka.InstructionEventGenerator; import org.sunbird.keys.JsonKey; import org.sunbird.logging.LoggerUtil; import org.sunbird.operations.ActorOperations; @@ -27,6 +28,7 @@ import org.sunbird.util.user.UserTncUtil; import org.sunbird.util.user.UserUtil; +import java.sql.Timestamp; import java.text.MessageFormat; import java.time.LocalDate; import java.util.*; @@ -143,7 +145,7 @@ public Response getUserProfileData(Request actorMessage) { result.put(JsonKey.IDENTIFIER, userId); mapUserRoles(result); - + // Record the start time for measuring the execution time. long startTime = System.currentTimeMillis(); // Convert the 'result' object to a JsonNode using the ObjectMapper. @@ -735,4 +737,29 @@ private void mapUserRoles(Map result) { result.put(JsonKey.ROLES, roleList); } } + + public Response getUserLoggedInDetails(Request actorMessage) throws Exception { + String userId = (String) actorMessage.getRequest().get(JsonKey.USER_ID); + Map map1 = new HashMap<>(); + map1.putIfAbsent(JsonKey.ID, userId); + Response response = cassandraOperation.getRecordsByProperties(JsonKey.SUNBIRD, JsonKey.USER, map1, actorMessage.getRequestContext()); + Map trymap = new HashMap<>(); + List> list = (List>) response.get(JsonKey.RESPONSE); + list.forEach(a -> a.forEach(trymap::putIfAbsent)); + Map map = new HashMap<>(); + if (trymap.get("first_login") == null) { + map.put(JsonKey.ID, userId); + map.put(JsonKey.LAST_LOGIN, new Timestamp(Calendar.getInstance().getTime().getTime())); + map.put(JsonKey.FIRST_LOGIN, new Timestamp(Calendar.getInstance().getTime().getTime())); + cassandraOperation.upsertRecord(JsonKey.SUNBIRD, JsonKey.USER, map, actorMessage.getRequestContext()); + Map data = new HashMap<>(); + data.put(JsonKey.EDATA, map); + InstructionEventGenerator.pushInstructionEvent("user_first_login_topic", data); + } else { + map.put(JsonKey.ID, userId); + map.put(JsonKey.LAST_LOGIN, new Timestamp(Calendar.getInstance().getTime().getTime())); + cassandraOperation.upsertRecord(JsonKey.SUNBIRD, JsonKey.USER, map, actorMessage.getRequestContext()); + } + return null; + } }