-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add sorting to studies by publication date #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Conversation
feat: add paginate function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds support for sorting studies by publication date to work around a Strapi limitation where nested sorting returns duplicate results. The implementation fetches all matching studies, manually sorts them by the latest publication date in memory, and applies custom pagination.
Key changes:
- Implemented manual sorting by publications.date to avoid Strapi duplicate issue
- Added a reusable paginate utility function for in-memory pagination
- Minor code formatting improvements (spacing fixes)
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/api/study/controllers/study.js | Added logic to detect publications.date sorting, manually sort studies by latest publication date, and apply custom pagination |
| config/functions.js | Added paginate utility function to handle in-memory pagination of results and minor formatting improvements |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| ...(a.publications || []).map((p) => new Date(p.date)), | ||
| 0 | ||
| ); | ||
| const bLatest = Math.max( | ||
| ...(b.publications || []).map((p) => new Date(p.date)), |
Copilot
AI
Dec 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential bug when a study has publications with empty or invalid dates. If a publication has an invalid date string, new Date(p.date) will return Invalid Date, which when spread into Math.max() will cause the entire expression to evaluate to NaN. This could lead to incorrect sorting behavior.
Consider adding a filter to handle invalid dates before mapping, or add a fallback when the date is invalid. For example, you could use new Date(p.date).getTime() || 0 to ensure invalid dates are treated as 0.
| ...(a.publications || []).map((p) => new Date(p.date)), | |
| 0 | |
| ); | |
| const bLatest = Math.max( | |
| ...(b.publications || []).map((p) => new Date(p.date)), | |
| ...((a.publications || []) | |
| .map((p) => new Date(p.date).getTime()) | |
| .filter((t) => !isNaN(t))), | |
| 0 | |
| ); | |
| const bLatest = Math.max( | |
| ...((b.publications || []) | |
| .map((p) => new Date(p.date).getTime()) | |
| .filter((t) => !isNaN(t))), |
| paginate(results, { page = 1, pageSize = 25 }) { | ||
| const paginatedResults = results.slice( | ||
| (page - 1) * pageSize, | ||
| page * pageSize | ||
| ); | ||
| const meta = { | ||
| pagination: { | ||
| page: page, | ||
| total: results.length, | ||
| pageCount: Math.ceil(results.length / pageSize), | ||
| pageSize: pageSize, | ||
| } | ||
| }; | ||
| return { results: paginatedResults, meta }; |
Copilot
AI
Dec 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Edge case issue: When pageSize is 0 or negative, the paginate function will produce unexpected results. Math.ceil(results.length / 0) will return Infinity, and the slice operation with negative pageSize could produce unexpected pagination behavior. Consider adding validation to ensure pageSize is a positive number.
| const studies = await strapi.entityService.findMany( | ||
| 'api::study.study', | ||
| ctx.query |
Copilot
AI
Dec 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pagination parameters are not removed from ctx.query before fetching all studies. When strapi.entityService.findMany is called with ctx.query, it may include pagination settings (like page and pageSize) that could limit the results returned from the database. This would mean the manual sort is only applied to a subset of studies, not all of them, leading to incorrect sorting across pages.
You should explicitly remove pagination parameters from the query before fetching: const { pagination, sort, ...queryWithoutPagination } = ctx.query; and then pass queryWithoutPagination to findMany.
Description
manually sort studies if sorting by 'publications.date' (as nested sorting returns duplicates)
add paginate function
Related to https://github.com/haniffalab/cellatlas-io-client/issues/206
Type of change
Checklist
npm test)pre-commit run --all-files)