Done ticket GEPAFINBE-128
This commit is contained in:
@@ -353,5 +353,7 @@ public class GepafinConstant {
|
||||
public static final String NOTIFICATION_DELETED_SUCCESSFULLY="notification.deleted.successfully";
|
||||
public static final String NOTIFICATION_UPDATED_SUCCESSFULLY="notification.updated.successfully";
|
||||
public static final String USER_WITH_COMPANY_NOT_FOUND = "user.with.company.not.found";
|
||||
|
||||
public static final String USER_ACTION_FETCHED_SUCCESSFULLY = "user.action.fetched.successfully";
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
package net.gepafin.tendermanagement.dao;
|
||||
|
||||
import jakarta.persistence.criteria.Predicate;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import net.gepafin.tendermanagement.entities.RoleActionContextEntity;
|
||||
import net.gepafin.tendermanagement.entities.UserActionEntity;
|
||||
import net.gepafin.tendermanagement.entities.UserEntity;
|
||||
import net.gepafin.tendermanagement.enums.TimePeriodEnum;
|
||||
import net.gepafin.tendermanagement.model.response.SummaryPageResponseBean;
|
||||
import net.gepafin.tendermanagement.model.response.UserActionResponseBean;
|
||||
import net.gepafin.tendermanagement.repositories.AssignedApplicationsRepository;
|
||||
import net.gepafin.tendermanagement.repositories.RoleActionContextRepository;
|
||||
import net.gepafin.tendermanagement.repositories.UserActionsRepository;
|
||||
import net.gepafin.tendermanagement.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
public class UserActionDao {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private UserActionsRepository userActionsRepository;
|
||||
|
||||
@Autowired
|
||||
private AssignedApplicationsRepository assignedApplicationsRepository;
|
||||
|
||||
@Autowired
|
||||
private RoleActionContextRepository roleActionContextRepository;
|
||||
|
||||
public SummaryPageResponseBean getUserAction(HttpServletRequest request, UserEntity userEntity, TimePeriodEnum timeFilter, String actionContext){
|
||||
Long numberOfLoginAttempts = userActionsRepository.countUserLoginAttempts(userEntity.getId());
|
||||
Long applicationsProcessed = assignedApplicationsRepository.countAssignedApplicationsByUserId(userEntity.getId());
|
||||
|
||||
List<RoleActionContextEntity> actionContextLabel = roleActionContextRepository.findByRoleIdAndIsDeletedFalse(userEntity.getRoleEntity().getId());
|
||||
|
||||
List<UserActionEntity> userActions = getFilterUserActions(userEntity.getId(),timeFilter,actionContext);
|
||||
|
||||
return createSummaryPageResponse(userEntity,numberOfLoginAttempts,applicationsProcessed,actionContextLabel,userActions);
|
||||
}
|
||||
|
||||
public SummaryPageResponseBean createSummaryPageResponse(UserEntity user, Long numberOfLoginAttempts, Long applicationsProcessed, List<RoleActionContextEntity> actionContextLabel, List<UserActionEntity> userActions){
|
||||
SummaryPageResponseBean response = new SummaryPageResponseBean();
|
||||
response.setRole(user.getRoleEntity().getRoleName());
|
||||
response.setLastLogin(user.getLastLogin());
|
||||
response.setRegistrationDate(user.getCreatedDate());
|
||||
response.setUsername(user.getFirstName());
|
||||
response.setEmail(user.getEmail());
|
||||
response.setNumberOfLoginAttempts(numberOfLoginAttempts);
|
||||
response.setApplicationsProcessed(applicationsProcessed);
|
||||
List<String> actionContextNames = actionContextLabel.stream()
|
||||
.map(RoleActionContextEntity::getActionContext)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
response.setActionContextLabels(actionContextNames);
|
||||
|
||||
List<UserActionResponseBean> userAction = convertEntityToResponse(userActions);
|
||||
response.setUserActions(userAction);
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
public List<UserActionEntity> getFilterUserActions(Long userId ,TimePeriodEnum timeFilter, String actionContext) {
|
||||
LocalDateTime endDate = LocalDateTime.now();
|
||||
LocalDateTime startDate = (timeFilter != null) ? getStartTimeFromFilter(timeFilter) : null;
|
||||
Pageable pageable = PageRequest.of(0, 25);
|
||||
|
||||
Specification<UserActionEntity> spec = getUserActionsSpecification(userId, startDate, endDate, actionContext);
|
||||
Page<UserActionEntity> pageResult = userActionsRepository.findAll(spec, pageable);
|
||||
|
||||
return pageResult.getContent();
|
||||
}
|
||||
|
||||
private LocalDateTime getStartTimeFromFilter(TimePeriodEnum timeFilter) {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
|
||||
if (timeFilter.equals(TimePeriodEnum.LAST_WEEK)) {
|
||||
return now.minusDays(7);
|
||||
} else if (timeFilter.equals(TimePeriodEnum.LAST_QUARTER)) {
|
||||
return now.minusMonths(4);
|
||||
} else if (timeFilter.equals(TimePeriodEnum.LAST_SEMESTER)) {
|
||||
return now.minusMonths(6);
|
||||
} else if (timeFilter.equals(TimePeriodEnum.LAST_YEAR)) {
|
||||
return now.minusYears(1);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Specification<UserActionEntity> getUserActionsSpecification(Long userId, LocalDateTime startDate, LocalDateTime endDate, String actionContext) {
|
||||
return (root, query, builder) -> {
|
||||
Predicate predicate = builder.isFalse(root.get("isDeleted"));
|
||||
|
||||
predicate = builder.and(predicate, builder.equal(root.get("userId"), userId));
|
||||
|
||||
if (startDate != null && endDate != null) {
|
||||
predicate = builder.and(predicate, builder.between(root.get("createdDate"), startDate, endDate));
|
||||
}
|
||||
|
||||
if (actionContext != null) {
|
||||
predicate = builder.and(predicate, builder.equal(root.get("actionContext"), actionContext));
|
||||
}
|
||||
|
||||
query.orderBy(builder.desc(root.get("createdDate")));
|
||||
|
||||
return predicate;
|
||||
};
|
||||
}
|
||||
|
||||
private List<UserActionResponseBean> convertEntityToResponse(List<UserActionEntity> userActions) {
|
||||
return userActions.stream().map(action -> {
|
||||
UserActionResponseBean responseBean = new UserActionResponseBean();
|
||||
responseBean.setId(action.getId());
|
||||
responseBean.setUserId(action.getUserId());
|
||||
responseBean.setActionType(action.getActionType());
|
||||
responseBean.setRequestBody(action.getRequestBody());
|
||||
responseBean.setLoginAttemptId(action.getLoginAttemptId());
|
||||
responseBean.setIpAddress(action.getIpAddress());
|
||||
responseBean.setActionContext(action.getActionContext());
|
||||
responseBean.setHubId(action.getHubId());
|
||||
responseBean.setUrl(action.getUrl());
|
||||
responseBean.setResponse(action.getResponse());
|
||||
responseBean.setCreatedDate(action.getCreatedDate());
|
||||
responseBean.setUpdatedDate(action.getUpdatedDate());
|
||||
return responseBean;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package net.gepafin.tendermanagement.entities;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Table;
|
||||
import lombok.Data;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
@Table(name ="role_action_context")
|
||||
public class RoleActionContextEntity extends BaseEntity {
|
||||
|
||||
@Column(name = "action_context")
|
||||
private String actionContext;
|
||||
|
||||
@Column(name = "role_id")
|
||||
private Long roleId;
|
||||
|
||||
@Column(name="is_deleted")
|
||||
private Boolean isDeleted;
|
||||
|
||||
@Column(name = "is_viewed")
|
||||
private Boolean isViewed;
|
||||
|
||||
@Column(name = "description")
|
||||
private String description;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package net.gepafin.tendermanagement.enums;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
|
||||
public enum TimePeriodEnum {
|
||||
|
||||
LAST_WEEK ("LAST_WEEK"),
|
||||
LAST_QUARTER("LAST_QUARTER"),
|
||||
LAST_SEMESTER("LAST_SEMESTER"),
|
||||
LAST_YEAR("LAST_YEAR");
|
||||
|
||||
private String value;
|
||||
|
||||
TimePeriodEnum(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@JsonValue
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -161,7 +161,9 @@ public enum UserActionContextEnum {
|
||||
/** appointment action context **/
|
||||
CHECK_OR_CREATE_NDG_CODE("CHECK_OR_CREATE_NDG_CODE"),
|
||||
CREATE_APPOINTMENT("CREATE_APPOINTMENT"),
|
||||
UPLOAD_DOCUMENT_TO_EXTERNAL_SYSTEM("UPLOAD_DOCUMENT_TO_EXTERNAL_SYSTEM");
|
||||
UPLOAD_DOCUMENT_TO_EXTERNAL_SYSTEM("UPLOAD_DOCUMENT_TO_EXTERNAL_SYSTEM"),
|
||||
|
||||
GET_USER_ACTION("GET_USER_ACTION");
|
||||
|
||||
private final String value;
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package net.gepafin.tendermanagement.model.response;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class SummaryPageResponseBean {
|
||||
private String username;
|
||||
private String email;
|
||||
private String role;
|
||||
private LocalDateTime lastLogin;
|
||||
private LocalDateTime registrationDate;
|
||||
private Long numberOfLoginAttempts;
|
||||
private Long applicationsProcessed;
|
||||
private List<String> actionContextLabels;
|
||||
private List<UserActionResponseBean> userActions;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package net.gepafin.tendermanagement.model.response;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class UserActionResponseBean {
|
||||
private Long id;
|
||||
private Long userId;
|
||||
private String actionType;
|
||||
private String requestBody;
|
||||
private Long loginAttemptId;
|
||||
private String actionContext;
|
||||
private String ipAddress;
|
||||
private String methodType;
|
||||
private Long hubId;
|
||||
private String url;
|
||||
private String response;
|
||||
private LocalDateTime createdDate;
|
||||
private LocalDateTime updatedDate;
|
||||
}
|
||||
@@ -20,6 +20,6 @@ public interface AssignedApplicationsRepository extends JpaRepository<AssignedAp
|
||||
@Param("applicationId") Long applicationId,
|
||||
@Param("id") Long id);
|
||||
|
||||
|
||||
|
||||
@Query("SELECT COUNT(aa) FROM AssignedApplicationsEntity aa WHERE aa.isDeleted = false AND aa.userId = :userId AND aa.status <> 'CLOSE'")
|
||||
Long countAssignedApplicationsByUserId(@Param("userId") Long userId);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package net.gepafin.tendermanagement.repositories;
|
||||
|
||||
import net.gepafin.tendermanagement.entities.RoleActionContextEntity;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface RoleActionContextRepository extends JpaRepository<RoleActionContextEntity,Long>, JpaSpecificationExecutor<RoleActionContextEntity> {
|
||||
|
||||
List<RoleActionContextEntity> findByRoleIdAndIsDeletedFalse(Long roleId);
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -2,9 +2,23 @@ package net.gepafin.tendermanagement.repositories;
|
||||
|
||||
import net.gepafin.tendermanagement.entities.UserActionEntity;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface UserActionsRepository extends JpaRepository<UserActionEntity, Long> {
|
||||
public interface UserActionsRepository extends JpaRepository<UserActionEntity, Long> , JpaSpecificationExecutor<UserActionEntity> {
|
||||
UserActionEntity findUserActionById(Long id);
|
||||
|
||||
@Query("SELECT COUNT(u) FROM UserActionEntity u " +
|
||||
"WHERE u.userId = :userId " +
|
||||
"AND u.actionContext = 'USER_LOGIN' " +
|
||||
"AND u.isDeleted = false")
|
||||
Long countUserLoginAttempts(@Param("userId") Long userId);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package net.gepafin.tendermanagement.service;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import net.gepafin.tendermanagement.enums.TimePeriodEnum;
|
||||
import net.gepafin.tendermanagement.model.response.SummaryPageResponseBean;
|
||||
|
||||
public interface UserActionService {
|
||||
public SummaryPageResponseBean getUserAction(HttpServletRequest request, Long userId, TimePeriodEnum timeFilter, String actionContext);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package net.gepafin.tendermanagement.service.impl;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import net.gepafin.tendermanagement.dao.UserActionDao;
|
||||
import net.gepafin.tendermanagement.entities.UserEntity;
|
||||
import net.gepafin.tendermanagement.enums.TimePeriodEnum;
|
||||
import net.gepafin.tendermanagement.model.response.SummaryPageResponseBean;
|
||||
import net.gepafin.tendermanagement.service.UserActionService;
|
||||
import net.gepafin.tendermanagement.util.Validator;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class UserActionServiceImpl implements UserActionService {
|
||||
|
||||
@Autowired
|
||||
private UserActionDao userActionDao;
|
||||
|
||||
@Autowired
|
||||
private Validator validator;
|
||||
|
||||
|
||||
@Override
|
||||
public SummaryPageResponseBean getUserAction(HttpServletRequest request, Long userId, TimePeriodEnum timeFilter, String actionContext) {
|
||||
UserEntity user = validator.validateUserId(request, userId);
|
||||
return userActionDao.getUserAction(request,user,timeFilter,actionContext);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package net.gepafin.tendermanagement.web.rest.api;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.media.Content;
|
||||
import io.swagger.v3.oas.annotations.media.ExampleObject;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import net.gepafin.tendermanagement.enums.TimePeriodEnum;
|
||||
import net.gepafin.tendermanagement.model.response.SummaryPageResponseBean;
|
||||
import net.gepafin.tendermanagement.model.util.Response;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.ErrorConstants;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
public interface UserActionApi {
|
||||
@Operation(summary = "Api to get user action",
|
||||
responses = {
|
||||
@ApiResponse(responseCode = "200", description = "OK"),
|
||||
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
|
||||
@ExampleObject(value = ErrorConstants.NOTFOUND_ERROR_EXAMPLE) })),
|
||||
@ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
|
||||
@ExampleObject(value = ErrorConstants.UNAUTHORIZED_ERROR_EXAMPLE) })),
|
||||
@ApiResponse(responseCode = "400", description = "Bad Request", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
|
||||
@ExampleObject(value = ErrorConstants.BADREQUEST_ERROR_EXAMPLE) })) })
|
||||
@GetMapping(value = "/{userId}", produces = { "application/json" })
|
||||
ResponseEntity<Response<SummaryPageResponseBean>> getUserAction(HttpServletRequest request, @Parameter(description = "The user id", required = true) @PathVariable("userId") Long userId,
|
||||
@Parameter(description = "Time Filter") @RequestParam(value = "timeFilter", required = false) TimePeriodEnum timeFilter,
|
||||
@Parameter(description = "Action Context") @RequestParam(value = "actionContext", required = false) String actionContext);
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package net.gepafin.tendermanagement.web.rest.api.impl;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import net.gepafin.tendermanagement.config.Translator;
|
||||
import net.gepafin.tendermanagement.constants.GepafinConstant;
|
||||
import net.gepafin.tendermanagement.enums.TimePeriodEnum;
|
||||
import net.gepafin.tendermanagement.enums.UserActionContextEnum;
|
||||
import net.gepafin.tendermanagement.enums.UserActionLogsEnum;
|
||||
import net.gepafin.tendermanagement.model.request.UserActionRequest;
|
||||
import net.gepafin.tendermanagement.model.response.SummaryPageResponseBean;
|
||||
import net.gepafin.tendermanagement.model.util.Response;
|
||||
import net.gepafin.tendermanagement.service.UserActionService;
|
||||
import net.gepafin.tendermanagement.util.LoggingUtil;
|
||||
import net.gepafin.tendermanagement.web.rest.api.UserActionApi;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("${openapi.gepafin.base-path:/v1/userAction}")
|
||||
public class UserActionApiController implements UserActionApi {
|
||||
|
||||
@Autowired
|
||||
private UserActionService userActionService;
|
||||
|
||||
@Autowired
|
||||
private LoggingUtil loggingUtil;
|
||||
|
||||
@Override
|
||||
public ResponseEntity<Response<SummaryPageResponseBean>> getUserAction(HttpServletRequest request, Long userId, TimePeriodEnum timeFilter, String actionContext) {
|
||||
|
||||
/** This code is responsible for creating user action logs for the "get user action" operation. **/
|
||||
loggingUtil.logUserAction(UserActionRequest.builder().request(request).actionType(UserActionLogsEnum.VIEW)
|
||||
.actionContext(UserActionContextEnum.GET_USER_ACTION).build());
|
||||
SummaryPageResponseBean userActionResponse= userActionService.getUserAction(request,userId,timeFilter,actionContext);
|
||||
return ResponseEntity.status(HttpStatus.OK)
|
||||
.body(new Response<>(userActionResponse, Status.SUCCESS, Translator.toLocale(GepafinConstant.USER_ACTION_FETCHED_SUCCESSFULLY)));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user