Skip to content

Commit

Permalink
fix(SILVA-551): fixing dashboard issues (#448)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulushcgcj authored Nov 8, 2024
1 parent 268827b commit 984fe72
Show file tree
Hide file tree
Showing 21 changed files with 157 additions and 410 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(path = "/api/openings/favorites", produces = MediaType.APPLICATION_JSON_VALUE)
@RequestMapping(path = "/api/openings/favourites", produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class OpeningFavoriteEndpoint {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,6 @@ public class UserOpeningEndpoint {

private final UserOpeningService userOpeningService;

/**
* Gets up to three tracked Openings to a user.
*
* @return A list of openings or the http code 204-No Content.
*/
@GetMapping("/dashboard-track-openings")
public ResponseEntity<List<MyRecentActionsRequestsDto>> getUserTrackedOpenings() {
List<MyRecentActionsRequestsDto> userOpenings = userOpeningService.getUserTrackedOpenings();
if (userOpenings.isEmpty()) {
return ResponseEntity.noContent().build();
}

return ResponseEntity.ok(userOpenings);
}

/**
* Saves one Opening ID as favourite to an user.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ca.bc.gov.restapi.results.postgres.entity.UserOpeningEntity;
import ca.bc.gov.restapi.results.postgres.entity.UserOpeningEntityId;
import java.util.List;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

/**
Expand All @@ -11,6 +12,7 @@
public interface UserOpeningRepository
extends JpaRepository<UserOpeningEntity, UserOpeningEntityId> {

List<UserOpeningEntity> findAllByUserId(String userId);

List<UserOpeningEntity> findAllByUserId(String userId, Pageable page);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,17 @@

import ca.bc.gov.restapi.results.common.exception.OpeningNotFoundException;
import ca.bc.gov.restapi.results.common.exception.UserFavoriteNotFoundException;
import ca.bc.gov.restapi.results.common.exception.UserOpeningNotFoundException;
import ca.bc.gov.restapi.results.common.security.LoggedUserService;
import ca.bc.gov.restapi.results.oracle.dto.OpeningSearchResponseDto;
import ca.bc.gov.restapi.results.oracle.entity.OpeningEntity;
import ca.bc.gov.restapi.results.oracle.enums.OpeningCategoryEnum;
import ca.bc.gov.restapi.results.oracle.enums.OpeningStatusEnum;
import ca.bc.gov.restapi.results.oracle.repository.OpeningRepository;
import ca.bc.gov.restapi.results.postgres.dto.MyRecentActionsRequestsDto;
import ca.bc.gov.restapi.results.postgres.entity.OpeningsActivityEntity;
import ca.bc.gov.restapi.results.postgres.entity.UserOpeningEntity;
import ca.bc.gov.restapi.results.postgres.entity.UserOpeningEntityId;
import ca.bc.gov.restapi.results.postgres.repository.OpeningsActivityRepository;
import ca.bc.gov.restapi.results.postgres.repository.UserOpeningRepository;
import jakarta.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ocpsoft.prettytime.PrettyTime;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

/**
Expand All @@ -35,64 +27,13 @@ public class UserOpeningService {

private final UserOpeningRepository userOpeningRepository;

private final OpeningsActivityRepository openingsActivityRepository;

private final OpeningRepository openingRepository;

/**
* Gets user's tracked Openings.
*
* @return A list of {@link MyRecentActionsRequestsDto} containing the found records.
*/
public List<MyRecentActionsRequestsDto> getUserTrackedOpenings() {
log.info("Getting all user openings for the Track openings table");

String userId = loggedUserService.getLoggedUserId();
List<UserOpeningEntity> userList = userOpeningRepository.findAllByUserId(userId);

if (userList.isEmpty()) {
log.info("No saved openings for the current user!");
return List.of();
}

List<Long> openingIds = userList.stream().map(UserOpeningEntity::getOpeningId).toList();
List<OpeningsActivityEntity> openingActivities =
openingsActivityRepository.findAllByOpeningIdIn(openingIds);

if (openingActivities.isEmpty()) {
log.info("No records found on the opening activity table for the opening ID list!");
return List.of();
}

List<MyRecentActionsRequestsDto> resultList = new ArrayList<>();

PrettyTime prettyTime = new PrettyTime();

for (OpeningsActivityEntity activityEntity : openingActivities) {
MyRecentActionsRequestsDto requestsDto =
new MyRecentActionsRequestsDto(
activityEntity.getActivityTypeDesc(),
activityEntity.getOpeningId(),
activityEntity.getStatusCode(),
activityEntity.getStatusDesc(),
prettyTime.format(activityEntity.getLastUpdated()),
activityEntity.getLastUpdated());

resultList.add(requestsDto);

if (resultList.size() == 3) {
break;
}
}

return resultList;
}

public List<Long> listUserFavoriteOpenings() {
log.info("Loading user favorite openings for {}", loggedUserService.getLoggedUserId());

List<UserOpeningEntity> userList = userOpeningRepository
.findAllByUserId(loggedUserService.getLoggedUserId());
.findAllByUserId(loggedUserService.getLoggedUserId(), PageRequest.of(0, 32));

if (userList.isEmpty()) {
log.info("No saved openings for {}", loggedUserService.getLoggedUserId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ class OpeningFavoriteEndpointIntegrationTest extends AbstractTestContainerIntegr

@Test
@Order(1)
@DisplayName("No favorites to begin with")
@DisplayName("No favourites to begin with")
void shouldBeEmpty() throws Exception {

mockMvc
.perform(
MockMvcRequestBuilders.get("/api/openings/favorites")
MockMvcRequestBuilders.get("/api/openings/favourites")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$").isEmpty());
Expand All @@ -52,7 +52,7 @@ void shouldBeEmpty() throws Exception {
void shouldAddToFavorite() throws Exception {
mockMvc
.perform(
MockMvcRequestBuilders.put("/api/openings/favorites/{openingId}", 101)
MockMvcRequestBuilders.put("/api/openings/favourites/{openingId}", 101)
.with(csrf().asHeader())
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isAccepted())
Expand All @@ -70,7 +70,7 @@ void shouldAddToFavorite() throws Exception {
void shouldNotAddIfDoesNotExist() throws Exception {
mockMvc
.perform(
MockMvcRequestBuilders.put("/api/openings/favorites/{openingId}", 987)
MockMvcRequestBuilders.put("/api/openings/favourites/{openingId}", 987)
.with(csrf().asHeader())
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound())
Expand All @@ -87,11 +87,11 @@ void shouldAddToFavoriteAgain() throws Exception {

@Test
@Order(5)
@DisplayName("Should see list of favorites")
@DisplayName("Should see list of favourites")
void shouldBeAbleToSeeOpening() throws Exception {
mockMvc
.perform(
MockMvcRequestBuilders.get("/api/openings/favorites")
MockMvcRequestBuilders.get("/api/openings/favourites")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
Expand All @@ -104,7 +104,7 @@ void shouldBeAbleToSeeOpening() throws Exception {
void shouldRemoveFromFavorites() throws Exception {
mockMvc
.perform(
MockMvcRequestBuilders.delete("/api/openings/favorites/{openingId}", 101)
MockMvcRequestBuilders.delete("/api/openings/favourites/{openingId}", 101)
.with(csrf().asHeader())
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent())
Expand All @@ -121,7 +121,7 @@ void shouldRemoveFromFavorites() throws Exception {
void shouldThrownErrorIfNoFavoriteFound() throws Exception {
mockMvc
.perform(
MockMvcRequestBuilders.delete("/api/openings/favorites/{openingId}", 101)
MockMvcRequestBuilders.delete("/api/openings/favourites/{openingId}", 101)
.with(csrf().asHeader())
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound())
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@
import static org.mockito.Mockito.when;

import ca.bc.gov.restapi.results.common.exception.UserFavoriteNotFoundException;
import ca.bc.gov.restapi.results.common.exception.UserOpeningNotFoundException;
import ca.bc.gov.restapi.results.common.security.LoggedUserService;
import ca.bc.gov.restapi.results.oracle.entity.OpeningEntity;
import ca.bc.gov.restapi.results.oracle.repository.OpeningRepository;
import ca.bc.gov.restapi.results.postgres.dto.MyRecentActionsRequestsDto;
import ca.bc.gov.restapi.results.postgres.entity.OpeningsActivityEntity;
import ca.bc.gov.restapi.results.postgres.entity.UserOpeningEntity;
import ca.bc.gov.restapi.results.postgres.repository.OpeningsActivityRepository;
import ca.bc.gov.restapi.results.postgres.repository.UserOpeningRepository;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -28,13 +23,17 @@
@ExtendWith(MockitoExtension.class)
class UserOpeningServiceTest {

@Mock LoggedUserService loggedUserService;
@Mock
LoggedUserService loggedUserService;

@Mock UserOpeningRepository userOpeningRepository;
@Mock
UserOpeningRepository userOpeningRepository;

@Mock OpeningsActivityRepository openingsActivityRepository;
@Mock
OpeningsActivityRepository openingsActivityRepository;

@Mock OpeningRepository openingRepository;
@Mock
OpeningRepository openingRepository;

private UserOpeningService userOpeningService;

Expand All @@ -44,53 +43,8 @@ class UserOpeningServiceTest {
void setup() {
this.userOpeningService =
new UserOpeningService(
loggedUserService, userOpeningRepository, openingsActivityRepository,openingRepository);
}

@Test
@DisplayName("Get user tracked openings happy path should succeed")
void getUserTrackedOpenings_happyPath_shouldSucceed() {
when(loggedUserService.getLoggedUserId()).thenReturn(USER_ID);

UserOpeningEntity entity = new UserOpeningEntity();
entity.setUserId(USER_ID);
entity.setOpeningId(223344L);

when(userOpeningRepository.findAllByUserId(USER_ID)).thenReturn(List.of(entity));

LocalDateTime now = LocalDateTime.now().minusMinutes(2);
OpeningsActivityEntity openingEntity = new OpeningsActivityEntity();
openingEntity.setOpeningId(entity.getOpeningId());
openingEntity.setActivityTypeCode("UPD");
openingEntity.setActivityTypeDesc("Update");
openingEntity.setStatusCode("APP");
openingEntity.setStatusDesc("Approved");
openingEntity.setLastUpdated(now);
openingEntity.setEntryUserid(USER_ID);

when(openingsActivityRepository.findAllByOpeningIdIn(List.of(223344L)))
.thenReturn(List.of(openingEntity));

List<MyRecentActionsRequestsDto> openings = userOpeningService.getUserTrackedOpenings();

Assertions.assertFalse(openings.isEmpty());
Assertions.assertEquals("Update", openings.get(0).activityType());
Assertions.assertEquals(223344L, openings.get(0).openingId());
Assertions.assertEquals("APP", openings.get(0).statusCode());
Assertions.assertEquals("Approved", openings.get(0).statusDescription());
Assertions.assertEquals("2 minutes ago", openings.get(0).lastUpdatedLabel());
}

@Test
@DisplayName("Get user tracked openings no data should succeed")
void getUserTrackedOpenings_noData_shouldSucceed() {
when(loggedUserService.getLoggedUserId()).thenReturn(USER_ID);

when(userOpeningRepository.findAllByUserId(USER_ID)).thenReturn(List.of());

List<MyRecentActionsRequestsDto> openings = userOpeningService.getUserTrackedOpenings();

Assertions.assertTrue(openings.isEmpty());
loggedUserService, userOpeningRepository,
openingRepository);
}

@Test
Expand Down
Loading

0 comments on commit 984fe72

Please sign in to comment.