Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KB-1722 - Create an API to get the user login details and raise the kafka event. #52

Merged
merged 2 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions controller/app/controllers/usermanagement/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -485,4 +485,11 @@ public CompletionStage<Result> updateUserDeclarations(Http.Request httpRequest)
true,
httpRequest);
}

public CompletionStage<Result> getUserLoggedInDetails(String userId, Http.Request httpRequest) {
return handleGetUserProfileV3(
ActorOperations.GET_USER_LOGIN_V1.getValue(),
ProjectUtil.getLmsUserId(userId),
httpRequest);
}
}
1 change: 1 addition & 0 deletions controller/conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.*;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -735,4 +737,29 @@ private void mapUserRoles(Map<String, Object> result) {
result.put(JsonKey.ROLES, roleList);
}
}

public Response getUserLoggedInDetails(Request actorMessage) throws Exception {
String userId = (String) actorMessage.getRequest().get(JsonKey.USER_ID);
Map<String, Object> map1 = new HashMap<>();
map1.putIfAbsent(JsonKey.ID, userId);
Response response = cassandraOperation.getRecordsByProperties(JsonKey.SUNBIRD, JsonKey.USER, map1, actorMessage.getRequestContext());
Map<String, Object> trymap = new HashMap<>();
List<Map<String, Object>> list = (List<Map<String, Object>>) response.get(JsonKey.RESPONSE);
list.forEach(a -> a.forEach(trymap::putIfAbsent));
Map<String, Object> 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<String, Object> 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;
}
}
Loading