Skip to content

Filter queries to only books with book-format files, add deduplication and restriction-aware paging#6

Merged
dinosmm merged 2 commits into
dmsfrom
codex/investigate-book-count-discrepancy-159r5k
Apr 24, 2026
Merged

Filter queries to only books with book-format files, add deduplication and restriction-aware paging#6
dinosmm merged 2 commits into
dmsfrom
codex/investigate-book-count-discrepancy-159r5k

Conversation

@dinosmm

@dinosmm dinosmm commented Apr 24, 2026

Copy link
Copy Markdown
Owner

Motivation

  • Ensure repository/specification queries only return books that actually have at least one book-format file (isBookFormat = true).
  • Prevent duplicate book rows caused by joins when loading lists/pages and ensure stable API DTO lists.
  • Make paged results accurate when ContentRestrictionService filters out items by recomputing totals for restricted users.

Description

  • Tighten AppBookSpecification.hasDigitalFile to use a subquery EXISTS on BookFileEntity where isBookFormat = true instead of isNotEmpty on the collection.
  • Update multiple BookRepository queries (including findAllWithMetadata*, paged queries and countByLibraryId) to add an EXISTS subquery that ensures bf.isBookFormat = true and to use COUNT(DISTINCT b) where appropriate.
  • Add existsByUserId to UserContentRestrictionRepository and expose hasRestrictions(Long) in ContentRestrictionService to quickly detect whether a user has any restrictions.
  • Add deduplication logic deduplicateById(List<BookEntity>) to BookQueryService and apply it to list and paged flows, and recompute Page totals when the requesting user has content restrictions.
  • Add/adjust tests: extend BookRepositoryDataJpaTest with tests for countByLibraryId, hasDigitalFile spec, and findAllWithMetadata behavior; add BookQueryServiceTest to verify deduplication and restriction-aware paging behavior.

Testing

  • Ran repository integration tests in BookRepositoryDataJpaTest that cover countByLibraryId, hasDigitalFile spec, and findAllWithMetadata, and they succeeded.
  • Ran unit tests in BookQueryServiceTest covering deduplication and recomputed page totals for restricted users, and they succeeded.
  • Existing test suites that touch the modified repository/service code were executed and passed locally.

Codex Task

@dinosmm dinosmm merged commit 0f90faa into dms Apr 24, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

💡 Codex Review

List<BookEntity> allCandidates = bookRepository.findAllWithMetadataByLibraryIds(libraryIds);
totalElements = contentRestrictionService.applyRestrictions(allCandidates, userId).size();

P1 Badge Deduplicate restricted totals before computing page count

When restrictions are present, totalElements is computed from applyRestrictions(allCandidates, userId).size() using findAllWithMetadataByLibraryIds, but this same query path is already being deduplicated elsewhere because it can return duplicate BookEntity rows. In those cases, page content is deduplicated while totalElements is not, so restricted users receive inflated totals and inconsistent pagination metadata. Deduplicate before counting (or switch this total to a distinct-count strategy).

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant