From 5a36da59adaea08b3f0fa6166b68e4538eb2772f Mon Sep 17 00:00:00 2001 From: Derek Roberts Date: Tue, 19 Nov 2024 08:33:26 -0800 Subject: [PATCH] Revert "feat: user recent openings backend (#451)" This reverts commit 78336c9bdd6196384bf4bdac7396a973316d5770. --- .../results/oracle/SilvaOracleConstants.java | 1 - .../oracle/dto/OpeningSearchFiltersDto.java | 25 --- .../oracle/dto/OpeningSearchResponseDto.java | 1 - .../repository/OpeningSearchRepository.java | 12 -- .../postgres/dto/UserRecentOpeningDto.java | 15 -- .../endpoint/UserRecentOpeningEndpoint.java | 51 ------ .../entity/UserRecentOpeningEntity.java | 38 ----- .../UserRecentOpeningRepository.java | 16 -- .../service/UserRecentOpeningService.java | 107 ------------- .../service/UserRecentOpeningServiceTest.java | 150 ------------------ 10 files changed, 416 deletions(-) delete mode 100644 backend/src/main/java/ca/bc/gov/restapi/results/postgres/dto/UserRecentOpeningDto.java delete mode 100644 backend/src/main/java/ca/bc/gov/restapi/results/postgres/endpoint/UserRecentOpeningEndpoint.java delete mode 100644 backend/src/main/java/ca/bc/gov/restapi/results/postgres/entity/UserRecentOpeningEntity.java delete mode 100644 backend/src/main/java/ca/bc/gov/restapi/results/postgres/repository/UserRecentOpeningRepository.java delete mode 100644 backend/src/main/java/ca/bc/gov/restapi/results/postgres/service/UserRecentOpeningService.java delete mode 100644 backend/src/test/java/ca/bc/gov/restapi/results/postgres/service/UserRecentOpeningServiceTest.java diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/SilvaOracleConstants.java b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/SilvaOracleConstants.java index dc6abd60..68f3732c 100644 --- a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/SilvaOracleConstants.java +++ b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/SilvaOracleConstants.java @@ -9,7 +9,6 @@ public class SilvaOracleConstants { public static final String ORG_UNIT = "orgUnit"; public static final String CATEGORY = "category"; public static final String STATUS_LIST = "statusList"; - public static final String OPENING_IDS = "openingIds"; public static final String MY_OPENINGS = "myOpenings"; public static final String SUBMITTED_TO_FRPA = "submittedToFrpa"; public static final String DISTURBANCE_DATE_START = "disturbanceDateStart"; diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/OpeningSearchFiltersDto.java b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/OpeningSearchFiltersDto.java index b1444222..01165773 100644 --- a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/OpeningSearchFiltersDto.java +++ b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/OpeningSearchFiltersDto.java @@ -35,7 +35,6 @@ public class OpeningSearchFiltersDto { @Setter private String requestUserId; - private List openingIds; /** Creates an instance of the search opening filter dto. */ public OpeningSearchFiltersDto( @@ -69,7 +68,6 @@ public OpeningSearchFiltersDto( .toList()); } this.statusList = new ArrayList<>(); - this.openingIds = new ArrayList<>(); if (!Objects.isNull(statusList)) { this.statusList.addAll(statusList.stream().map(s -> String.format("'%s'", s)).toList()); } @@ -94,28 +92,6 @@ public OpeningSearchFiltersDto( Objects.isNull(mainSearchTerm) ? null : mainSearchTerm.toUpperCase().trim(); } - // Create a constructor with only the List openingIds - public OpeningSearchFiltersDto( - List openingIds) { - this.orgUnit = new ArrayList<>(); - this.category = new ArrayList<>(); - this.statusList = new ArrayList<>(); - this.openingIds = openingIds; - this.myOpenings = null; - this.submittedToFrpa = null; - this.disturbanceDateStart = null; - this.disturbanceDateEnd = null; - this.regenDelayDateStart = null; - this.regenDelayDateEnd = null; - this.freeGrowingDateStart = null; - this.freeGrowingDateEnd = null; - this.updateDateStart = null; - this.updateDateEnd = null; - this.cuttingPermitId = null; - this.cutBlockId = null; - this.timberMark = null; - this.mainSearchTerm = null; - } /** * Define if a property has value. * @@ -127,7 +103,6 @@ public boolean hasValue(String prop) { case SilvaOracleConstants.ORG_UNIT -> !this.orgUnit.isEmpty(); case SilvaOracleConstants.CATEGORY -> !this.category.isEmpty(); case SilvaOracleConstants.STATUS_LIST -> !this.statusList.isEmpty(); - case SilvaOracleConstants.OPENING_IDS -> !this.openingIds.isEmpty(); case SilvaOracleConstants.MY_OPENINGS -> !Objects.isNull(this.myOpenings); case SilvaOracleConstants.SUBMITTED_TO_FRPA -> !Objects.isNull(this.submittedToFrpa); case SilvaOracleConstants.DISTURBANCE_DATE_START -> diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/OpeningSearchResponseDto.java b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/OpeningSearchResponseDto.java index 394fff72..b658b5da 100644 --- a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/OpeningSearchResponseDto.java +++ b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/dto/OpeningSearchResponseDto.java @@ -43,5 +43,4 @@ public class OpeningSearchResponseDto { private Boolean submittedToFrpa; private String forestFileId; private Long silvaReliefAppId; - private LocalDateTime lastViewDate; } diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/repository/OpeningSearchRepository.java b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/repository/OpeningSearchRepository.java index cfaf0839..affccc59 100644 --- a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/repository/OpeningSearchRepository.java +++ b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/repository/OpeningSearchRepository.java @@ -271,12 +271,6 @@ private Query setQueryParameters(OpeningSearchFiltersDto filtersDto, String nati log.info("Setting statusList filter values"); // No need to set value since the query already dit it. Didn't work set through named param } - // similarly for openingIds - if (filtersDto.hasValue(SilvaOracleConstants.OPENING_IDS)) { - log.info("Setting openingIds filter values"); - // No need to set value since the query already dit it. Didn't work set through - // named param - } // 4. User entry id if (filtersDto.hasValue(SilvaOracleConstants.MY_OPENINGS)) { log.info("Setting myOpenings filter value"); @@ -396,12 +390,6 @@ private String createNativeSqlQuery(OpeningSearchFiltersDto filtersDto) { builder.append("WHERE 1=1 "); /* Filters */ - // List of openings from the openingIds of the filterDto object for the recent openings - if (filtersDto.hasValue(SilvaOracleConstants.OPENING_IDS)) { - String openingIds = String.join(",", filtersDto.getOpeningIds()); - log.info("Filter for openingIds detected! openingIds={}", openingIds); - builder.append(String.format("AND o.OPENING_ID IN (%s) ", openingIds)); - } // 0. Main number filter [opening_id, opening_number, timber_mark, file_id] // if it's a number, filter by openingId or fileId, otherwise filter by timber mark and opening // number diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/postgres/dto/UserRecentOpeningDto.java b/backend/src/main/java/ca/bc/gov/restapi/results/postgres/dto/UserRecentOpeningDto.java deleted file mode 100644 index 92855357..00000000 --- a/backend/src/main/java/ca/bc/gov/restapi/results/postgres/dto/UserRecentOpeningDto.java +++ /dev/null @@ -1,15 +0,0 @@ -package ca.bc.gov.restapi.results.postgres.dto; - -import java.time.LocalDateTime; -import lombok.Builder; -import lombok.With; - -@With -@Builder -public record UserRecentOpeningDto( - String userId, - String openingId, - LocalDateTime lastViewed -) { - -} diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/postgres/endpoint/UserRecentOpeningEndpoint.java b/backend/src/main/java/ca/bc/gov/restapi/results/postgres/endpoint/UserRecentOpeningEndpoint.java deleted file mode 100644 index c6b1e872..00000000 --- a/backend/src/main/java/ca/bc/gov/restapi/results/postgres/endpoint/UserRecentOpeningEndpoint.java +++ /dev/null @@ -1,51 +0,0 @@ -package ca.bc.gov.restapi.results.postgres.endpoint; - -import ca.bc.gov.restapi.results.common.pagination.PaginatedResult; -import ca.bc.gov.restapi.results.oracle.dto.OpeningSearchResponseDto; -import ca.bc.gov.restapi.results.postgres.service.UserRecentOpeningService; -import lombok.RequiredArgsConstructor; -import org.springframework.http.HttpStatus; -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.PutMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseStatus; -import org.springframework.web.bind.annotation.RestController; - -@RestController -@RequiredArgsConstructor -@RequestMapping("/api/openings/recent") -public class UserRecentOpeningEndpoint { - - private final UserRecentOpeningService userRecentOpeningService; - - /** - * Retrieves a list of recent openings viewed by the user, limited by the number of results. - * - * @param limit The maximum number of results to return. - * @return A list of opening IDs viewed by the user. - */ - @GetMapping - public ResponseEntity> getUserRecentOpenings( - @RequestParam(defaultValue = "10") int limit) { - // Fetch recent openings for the logged-in user with the specified limit - return ResponseEntity.ok(userRecentOpeningService.getAllRecentOpeningsForUser(limit)); - } - - /** - * Records the opening viewed by the user based on the provided opening ID. - * - * @param openingId The ID of the opening viewed by the user. - * @return A simple confirmation message or the HTTP code 204-No Content. - */ - @PutMapping("/{openingId}") - @ResponseStatus(HttpStatus.ACCEPTED) - public void recordUserViewedOpening( - @PathVariable String openingId) { - // Store the opening and return the DTO - userRecentOpeningService.storeViewedOpening(openingId); - } - -} \ No newline at end of file diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/postgres/entity/UserRecentOpeningEntity.java b/backend/src/main/java/ca/bc/gov/restapi/results/postgres/entity/UserRecentOpeningEntity.java deleted file mode 100644 index 733a8619..00000000 --- a/backend/src/main/java/ca/bc/gov/restapi/results/postgres/entity/UserRecentOpeningEntity.java +++ /dev/null @@ -1,38 +0,0 @@ -package ca.bc.gov.restapi.results.postgres.entity; - -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import jakarta.persistence.Table; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.With; - -import java.time.LocalDateTime; - -@Data -@NoArgsConstructor -@AllArgsConstructor -@With -@Builder -@Entity -@Table(schema = "silva", name = "user_recent_openings") -public class UserRecentOpeningEntity { - - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - @Column(name = "user_id", nullable = false) - private String userId; - - @Column(name = "opening_id", nullable = false) - private String openingId; - - @Column(name = "last_viewed", nullable = false) - private LocalDateTime lastViewed; -} diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/postgres/repository/UserRecentOpeningRepository.java b/backend/src/main/java/ca/bc/gov/restapi/results/postgres/repository/UserRecentOpeningRepository.java deleted file mode 100644 index 78b2483c..00000000 --- a/backend/src/main/java/ca/bc/gov/restapi/results/postgres/repository/UserRecentOpeningRepository.java +++ /dev/null @@ -1,16 +0,0 @@ -package ca.bc.gov.restapi.results.postgres.repository; - -import ca.bc.gov.restapi.results.postgres.entity.UserRecentOpeningEntity; -import java.util.List; - -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; - -@Repository -public interface UserRecentOpeningRepository extends JpaRepository { - UserRecentOpeningEntity findByUserIdAndOpeningId(String userId, String openingId); - // Add a method to fetch recent openings for a user with a limit and sorting by last_viewed in descending order - Page findByUserIdOrderByLastViewedDesc(String userId, Pageable pageable); -} diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/postgres/service/UserRecentOpeningService.java b/backend/src/main/java/ca/bc/gov/restapi/results/postgres/service/UserRecentOpeningService.java deleted file mode 100644 index c5e219d5..00000000 --- a/backend/src/main/java/ca/bc/gov/restapi/results/postgres/service/UserRecentOpeningService.java +++ /dev/null @@ -1,107 +0,0 @@ -package ca.bc.gov.restapi.results.postgres.service; - -import ca.bc.gov.restapi.results.common.pagination.PaginatedResult; -import ca.bc.gov.restapi.results.common.pagination.PaginationParameters; -import ca.bc.gov.restapi.results.common.security.LoggedUserService; -import ca.bc.gov.restapi.results.oracle.dto.OpeningSearchFiltersDto; -import ca.bc.gov.restapi.results.oracle.dto.OpeningSearchResponseDto; -import ca.bc.gov.restapi.results.oracle.service.OpeningService; -import ca.bc.gov.restapi.results.postgres.dto.UserRecentOpeningDto; -import ca.bc.gov.restapi.results.postgres.entity.UserRecentOpeningEntity; -import ca.bc.gov.restapi.results.postgres.repository.UserRecentOpeningRepository; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; - -import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.Comparator; -import java.util.Map; -import java.util.stream.Collectors; - -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Pageable; -import org.springframework.stereotype.Service; - -@Slf4j -@Service -@RequiredArgsConstructor -public class UserRecentOpeningService { - - private final LoggedUserService loggedUserService; - private final UserRecentOpeningRepository userRecentOpeningRepository; - private final OpeningService openingService; - - /** - * Stores the opening viewed by the user and returns the DTO. - * - * @param openingId The ID of the opening viewed by the user. - * @return A DTO with userId, openingId, and lastViewed timestamp. - */ - public UserRecentOpeningDto storeViewedOpening(String openingId) { - String userId = loggedUserService.getLoggedUserId(); - LocalDateTime lastViewed = LocalDateTime.now(); - - // Verify that the openingId String contains numbers only and no spaces - if (!openingId.matches("^[0-9]*$")) { - throw new IllegalArgumentException("Opening ID must contain numbers only!"); - } - - // Check if the user has already viewed this opening - UserRecentOpeningEntity existingEntity = userRecentOpeningRepository.findByUserIdAndOpeningId(userId, openingId); - - if (existingEntity != null) { - // Update the last viewed timestamp for the existing record - existingEntity.setLastViewed(lastViewed); - userRecentOpeningRepository.save(existingEntity); // Save the updated entity - } else { - // Create a new entity if this openingId is being viewed for the first time - UserRecentOpeningEntity newEntity = new UserRecentOpeningEntity(null, userId, openingId, lastViewed); - userRecentOpeningRepository.save(newEntity); // Save the new entity - } - - // Return the DTO - return new UserRecentOpeningDto(userId, openingId, lastViewed); - } - - /** - * Retrieves the recent openings viewed by the logged-in user, limited by the provided limit. - * - * @param limit The maximum number of recent openings to retrieve. - * @return A list of opening IDs the user has viewed, sorted by last viewed in descending order. - */ - public PaginatedResult getAllRecentOpeningsForUser(int limit) { - String userId = loggedUserService.getLoggedUserId(); - Pageable pageable = PageRequest.of(0, limit); // PageRequest object to apply limit - - // Fetch recent openings for the user - Page recentOpenings = userRecentOpeningRepository - .findByUserIdOrderByLastViewedDesc(userId, pageable); - - // Extract opening IDs as String - Map openingIds = recentOpenings.getContent().stream() - .collect(Collectors.toMap(UserRecentOpeningEntity::getOpeningId, UserRecentOpeningEntity::getLastViewed)); - log.info("User with the userId {} has the following openindIds {}", userId, openingIds); - if (openingIds.isEmpty()) { - return new PaginatedResult<>(); - } - - PaginatedResult pageResult = - openingService - .openingSearch( - new OpeningSearchFiltersDto(new ArrayList<>(openingIds.keySet())), - new PaginationParameters(0, 10) - ); - - return pageResult - .withData( - pageResult - .getData() - .stream() - .peek(result -> result.setLastViewDate(openingIds.get(result.getOpeningId().toString()))) - .sorted(Comparator.comparing(OpeningSearchResponseDto::getLastViewDate).reversed()) - .collect(Collectors.toList()) - ); - } - -} diff --git a/backend/src/test/java/ca/bc/gov/restapi/results/postgres/service/UserRecentOpeningServiceTest.java b/backend/src/test/java/ca/bc/gov/restapi/results/postgres/service/UserRecentOpeningServiceTest.java deleted file mode 100644 index ef0d56a1..00000000 --- a/backend/src/test/java/ca/bc/gov/restapi/results/postgres/service/UserRecentOpeningServiceTest.java +++ /dev/null @@ -1,150 +0,0 @@ -package ca.bc.gov.restapi.results.postgres.service; - -import ca.bc.gov.restapi.results.common.pagination.PaginatedResult; -import ca.bc.gov.restapi.results.common.pagination.PaginationParameters; -import ca.bc.gov.restapi.results.common.security.LoggedUserService; -import ca.bc.gov.restapi.results.oracle.dto.OpeningSearchFiltersDto; -import ca.bc.gov.restapi.results.oracle.dto.OpeningSearchResponseDto; -import ca.bc.gov.restapi.results.oracle.service.OpeningService; -import ca.bc.gov.restapi.results.postgres.dto.UserRecentOpeningDto; -import ca.bc.gov.restapi.results.postgres.entity.UserRecentOpeningEntity; -import ca.bc.gov.restapi.results.postgres.repository.UserRecentOpeningRepository; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageImpl; -import org.springframework.data.domain.PageRequest; - -import java.time.LocalDateTime; -import java.util.List; -import java.util.Map; -import java.util.Optional; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; - -class UserRecentOpeningServiceTest { - - @Mock - private LoggedUserService loggedUserService; - - @Mock - private UserRecentOpeningRepository userRecentOpeningRepository; - - @Mock - private OpeningService openingService; - - @InjectMocks - private UserRecentOpeningService userRecentOpeningService; - - @BeforeEach - void setUp() { - MockitoAnnotations.openMocks(this); - } - - @Test - void storeViewedOpening_newOpening_savesEntity() { - String userId = "user123"; - String openingId = "123"; - LocalDateTime lastViewed = LocalDateTime.now(); - - when(loggedUserService.getLoggedUserId()).thenReturn(userId); - when(userRecentOpeningRepository.findByUserIdAndOpeningId(userId, openingId)).thenReturn(null); - - UserRecentOpeningDto result = userRecentOpeningService.storeViewedOpening(openingId); - - assertNotNull(result); - assertEquals(userId, result.userId()); - assertEquals(openingId, result.openingId()); - - verify(userRecentOpeningRepository, times(1)).save(any(UserRecentOpeningEntity.class)); - } - - @Test - void storeViewedOpening_existingOpening_updatesEntity() { - String userId = "user123"; - String openingId = "123"; - LocalDateTime lastViewed = LocalDateTime.now(); - UserRecentOpeningEntity existingEntity = new UserRecentOpeningEntity(1L, userId, openingId, lastViewed.minusDays(1)); - - when(loggedUserService.getLoggedUserId()).thenReturn(userId); - when(userRecentOpeningRepository.findByUserIdAndOpeningId(userId, openingId)).thenReturn(existingEntity); - - UserRecentOpeningDto result = userRecentOpeningService.storeViewedOpening(openingId); - - assertNotNull(result); - assertEquals(userId, result.userId()); - assertEquals(openingId, result.openingId()); - - verify(userRecentOpeningRepository, times(1)).save(existingEntity); - } - - @Test - void storeViewedOpening_invalidOpeningId_throwsException() { - String invalidOpeningId = "abc"; - - Exception exception = assertThrows(IllegalArgumentException.class, () -> { - userRecentOpeningService.storeViewedOpening(invalidOpeningId); - }); - - assertEquals("Opening ID must contain numbers only!", exception.getMessage()); - } - - @Test - void getAllRecentOpeningsForUser_noRecentOpenings_returnsEmptyResult() { - String userId = "idir@jasgrewa"; - int limit = 10; - - // Arrange - when(loggedUserService.getLoggedUserId()).thenReturn(userId); - when(userRecentOpeningRepository.findByUserIdOrderByLastViewedDesc(eq(userId), any(PageRequest.class))) - .thenReturn(Page.empty()); // Mocking an empty page of recent openings - - // Act - PaginatedResult result = userRecentOpeningService.getAllRecentOpeningsForUser(limit); - - // Assert - assertNotNull(result); - - // Check if data is null and assert empty - assertTrue(result.getData() == null || result.getData().isEmpty(), "Data should be empty or null"); - } - - - @Test - void getAllRecentOpeningsForUser_withRecentOpenings_returnsSortedResult() { - String userId = "user123"; - int limit = 10; - LocalDateTime now = LocalDateTime.now(); - - UserRecentOpeningEntity opening1 = new UserRecentOpeningEntity(1L, userId, "123", now.minusDays(2)); - UserRecentOpeningEntity opening2 = new UserRecentOpeningEntity(2L, userId, "456", now.minusDays(1)); - - List openings = List.of(opening1, opening2); - when(loggedUserService.getLoggedUserId()).thenReturn(userId); - when(userRecentOpeningRepository.findByUserIdOrderByLastViewedDesc(eq(userId), any(PageRequest.class))) - .thenReturn(new PageImpl<>(openings)); - - OpeningSearchResponseDto dto1 = new OpeningSearchResponseDto(); - dto1.setOpeningId(123); - - OpeningSearchResponseDto dto2 = new OpeningSearchResponseDto(); - dto2.setOpeningId(456); - - PaginatedResult pageResult = new PaginatedResult<>(); - pageResult.setData(List.of(dto1, dto2)); - - when(openingService.openingSearch(any(OpeningSearchFiltersDto.class), any(PaginationParameters.class))) - .thenReturn(pageResult); - - PaginatedResult result = userRecentOpeningService.getAllRecentOpeningsForUser(limit); - - assertNotNull(result); - assertEquals(2, result.getData().size()); - assertEquals((long) 456L, (long) result.getData().get(0).getOpeningId()); // Most recent first - assertEquals((long) 123L, (long) result.getData().get(1).getOpeningId()); // Least recent last - } -}