Skip to content

Commit

Permalink
fix(SILVA-516): fixing my recent actions (#454)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulushcgcj authored Nov 9, 2024
1 parent 984fe72 commit 59a2ec8
Show file tree
Hide file tree
Showing 30 changed files with 613 additions and 292 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,24 @@ public class SilvaConfiguration {
private ExternalApiAddress forestClientApi;
@NestedConfigurationProperty
private ExternalApiAddress openMaps;
@NestedConfigurationProperty
private SilvaDataLimits limits;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class ExternalApiAddress {

private String address;
private String key;
}

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class SilvaDataLimits {
private Integer maxActionsResults;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ca.bc.gov.restapi.results.oracle.endpoint;

import ca.bc.gov.restapi.results.oracle.service.UserActionsService;
import ca.bc.gov.restapi.results.postgres.dto.MyRecentActionsRequestsDto;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/users")
@RequiredArgsConstructor
@Slf4j
public class UserActionsEndpoint {

private final UserActionsService userActionsService;

@GetMapping("/recent-actions")
public ResponseEntity<List<MyRecentActionsRequestsDto>> getUserRecentOpeningsActions() {
List<MyRecentActionsRequestsDto> actionsDto =
userActionsService.getResultsAuditActivity();

log.info("Returning {} recent actions", actionsDto.size());

if (actionsDto.isEmpty()) {
return ResponseEntity.noContent().build();
}

return ResponseEntity.ok(actionsDto);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ca.bc.gov.restapi.results.oracle.entity;

import java.time.LocalDateTime;

public interface ResultsAuditEventProjection {
Long getResultsAuditEventId();
Long getOpeningId();
String getActionCode();
String getActionCodeDescription();
String getCategoryCode();
String getCategoryCodeDescription();
LocalDateTime getEntryTimestamp();
String getEntryUserid();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ca.bc.gov.restapi.results.oracle.enums;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;

@JsonFormat(shape = JsonFormat.Shape.OBJECT)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public enum AuditActionCodeEnum {
UPD("UPD", "Update"),
COR("COR", "Correction"),
O("O", "Original"),
SEC197("197", "Section 197"),
AMG("AMG", "Amalgamate"),
ES("ES", "E-submission"),
MIL("MIL", "Milestone"),
MIN("MIN", "Amended (Minor)"),
SPA("SPA", "Site Plan Amendment"),
VAR("VAR", "Variation"),
AMD("AMD", "Amended"),
APP("APP", "Approved"),
DEL("DEL", "Deleted"),
REJ("REJ", "Rejected"),
RET("RET", "Retired"),
RMD("RMD", "Removed"),
SUB("SUB", "Submitted");

private final String code;
private final String description;

public static AuditActionCodeEnum of(String code) {
for (AuditActionCodeEnum value : AuditActionCodeEnum.values()) {
if (value.getCode().equals(code)) {
return value;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ca.bc.gov.restapi.results.oracle.repository;

import ca.bc.gov.restapi.results.oracle.entity.OpeningEntity;
import ca.bc.gov.restapi.results.oracle.entity.ResultsAuditEventProjection;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface ResultsAuditActivityRepository extends JpaRepository<OpeningEntity, Long> {

@Query(
value = """
SELECT
rae.results_audit_event_id,
rae.opening_id,
rae.results_audit_action_code as action_code,
raac.description as action_code_description,
o.open_category_code as category_code,
occ.description as category_code_description,
rae.entry_timestamp,
rae.entry_userid
FROM THE.RESULTS_AUDIT_EVENT rae
LEFT JOIN THE.RESULTS_AUDIT_ACTION_CODE raac ON raac.RESULTS_AUDIT_ACTION_CODE = rae.RESULTS_AUDIT_ACTION_CODE
LEFT JOIN THE.OPENING o ON o.OPENING_ID = rae.OPENING_ID
LEFT JOIN THE.OPEN_CATEGORY_CODE occ ON occ.OPEN_CATEGORY_CODE = o.OPEN_CATEGORY_CODE
WHERE rae.ENTRY_USERID = ?1
ORDER BY rae.ENTRY_TIMESTAMP DESC
FETCH FIRST ?2 ROWS ONLY""",
nativeQuery = true)
List<ResultsAuditEventProjection> findUserRecentAuditEvents(String userId, Integer limit);


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ca.bc.gov.restapi.results.oracle.service;

import ca.bc.gov.restapi.results.common.configuration.SilvaConfiguration;
import ca.bc.gov.restapi.results.common.security.LoggedUserService;
import ca.bc.gov.restapi.results.oracle.repository.ResultsAuditActivityRepository;
import ca.bc.gov.restapi.results.postgres.dto.MyRecentActionsRequestsDto;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ocpsoft.prettytime.PrettyTime;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserActionsService {

private final ResultsAuditActivityRepository auditActivityRepository;
private final LoggedUserService loggedUserService;
private final SilvaConfiguration silvaConfiguration;
private final PrettyTime prettyTime = new PrettyTime();

public List<MyRecentActionsRequestsDto> getResultsAuditActivity() {
String userId = loggedUserService.getLoggedUserId();

log.info("Getting recent audit events for user {}", userId);

return
auditActivityRepository
.findUserRecentAuditEvents(userId, silvaConfiguration.getLimits().getMaxActionsResults())
.stream()
.map(entity ->
new MyRecentActionsRequestsDto(
entity.getActionCodeDescription(),
entity.getOpeningId(),
entity.getCategoryCode(),
entity.getCategoryCodeDescription(),
prettyTime.format(entity.getEntryTimestamp()),
entity.getEntryTimestamp()
)
)
.toList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import ca.bc.gov.restapi.results.common.util.TimestampUtil;
import ca.bc.gov.restapi.results.postgres.dto.DashboardFiltersDto;
import ca.bc.gov.restapi.results.postgres.dto.FreeGrowingMilestonesDto;
import ca.bc.gov.restapi.results.postgres.dto.MyRecentActionsRequestsDto;
import ca.bc.gov.restapi.results.postgres.dto.OpeningsPerYearDto;
import ca.bc.gov.restapi.results.postgres.service.DashboardMetricsService;
import java.util.List;
Expand Down Expand Up @@ -98,20 +97,4 @@ public ResponseEntity<List<FreeGrowingMilestonesDto>> getFreeGrowingMilestonesDa
return ResponseEntity.ok(milestonesDto);
}

/**
* Gets the last 5 most recent updated openings for the request user.
*
* @return A list of values to populate the chart or 204 no content if no data.
*/
@GetMapping("/my-recent-actions/requests")
public ResponseEntity<List<MyRecentActionsRequestsDto>> getUserRecentOpeningsActions() {
List<MyRecentActionsRequestsDto> actionsDto =
dashboardMetricsService.getUserRecentOpeningsActions();

if (actionsDto.isEmpty()) {
return ResponseEntity.noContent().build();
}

return ResponseEntity.ok(actionsDto);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import ca.bc.gov.restapi.results.postgres.entity.OpeningsActivityEntity;
import ca.bc.gov.restapi.results.postgres.entity.OpeningsActivityEntityId;
import java.util.List;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

/**
Expand All @@ -12,7 +12,5 @@
public interface OpeningsActivityRepository
extends JpaRepository<OpeningsActivityEntity, OpeningsActivityEntityId> {

List<OpeningsActivityEntity> findAllByOpeningIdIn(List<Long> openingIds);

List<OpeningsActivityEntity> findAllByEntryUserid(String userId, Sort sort);
List<OpeningsActivityEntity> findAllByEntryUserid(String userId, Pageable page);
}
Loading

0 comments on commit 59a2ec8

Please sign in to comment.