Skip to content

Conversation

@dannda
Copy link
Contributor

@dannda dannda commented Dec 10, 2025

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

  • 🐛 Bug fix (non-breaking change that resolves an issue)
  • ✨ New feature (non-breaking change that adds functionality)
  • ⚡ Optimisation (non-breaking improvement to performance or efficiency)
  • 🧩 Documentation (adds or improves documentation)
  • 🧱 Maintenance (refactor, dependency update, CI/CD, etc.)
  • 🔥 Breaking change (fix or feature that causes existing functionality to change)

Checklist

  • All tests pass (eg. npm test)
  • Pre-commit hooks run successfully (eg. pre-commit run --all-files)
  • Documentation updated (if required)

Copy link

Copilot AI left a 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.

Comment on lines +127 to +131
...(a.publications || []).map((p) => new Date(p.date)),
0
);
const bLatest = Math.max(
...(b.publications || []).map((p) => new Date(p.date)),
Copy link

Copilot AI Dec 10, 2025

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.

Suggested change
...(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))),

Copilot uses AI. Check for mistakes.
Comment on lines +204 to +217
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 };
Copy link

Copilot AI Dec 10, 2025

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.

Copilot uses AI. Check for mistakes.
Comment on lines +120 to +122
const studies = await strapi.entityService.findMany(
'api::study.study',
ctx.query
Copy link

Copilot AI Dec 10, 2025

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants