-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,9 +9,8 @@ const { NotFoundError } = require('@strapi/utils').errors; | |||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| module.exports = createCoreController('api::study.study', ({ strapi }) => ({ | ||||||||||||||||||||||||||||||
| async find(ctx) { | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // If not providing a dataset id, return only studies that are listed | ||||||||||||||||||||||||||||||
| if (!ctx.query.filters?.datasets?.id?.$eq){ | ||||||||||||||||||||||||||||||
| if (!ctx.query.filters?.datasets?.id?.$eq) { | ||||||||||||||||||||||||||||||
| ctx.query.filters = { | ||||||||||||||||||||||||||||||
| ...ctx.query.filters, | ||||||||||||||||||||||||||||||
| is_listed: true, | ||||||||||||||||||||||||||||||
|
|
@@ -79,7 +78,7 @@ module.exports = createCoreController('api::study.study', ({ strapi }) => ({ | |||||||||||||||||||||||||||||
| fields: ['name', 'description', 'type', 'category'], | ||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||
| cover_dataset: { | ||||||||||||||||||||||||||||||
| fields: [false], | ||||||||||||||||||||||||||||||
| fields: ['id'], | ||||||||||||||||||||||||||||||
| populate: ['media'], | ||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||
|
|
@@ -114,6 +113,33 @@ module.exports = createCoreController('api::study.study', ({ strapi }) => ({ | |||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // If sorting by publication date, get studies and manually sort | ||||||||||||||||||||||||||||||
| // as nested sorting returns duplicates (https://github.com/strapi/strapi/issues/11892) | ||||||||||||||||||||||||||||||
| const [sort, order = 'asc'] = ctx.query.sort?.split(':') || []; | ||||||||||||||||||||||||||||||
| if (sort === 'publications.date') { | ||||||||||||||||||||||||||||||
| const studies = await strapi.entityService.findMany( | ||||||||||||||||||||||||||||||
| 'api::study.study', | ||||||||||||||||||||||||||||||
| ctx.query | ||||||||||||||||||||||||||||||
|
Comment on lines
+120
to
+122
|
||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| studies.sort((a, b) => { | ||||||||||||||||||||||||||||||
| const aLatest = Math.max( | ||||||||||||||||||||||||||||||
| ...(a.publications || []).map((p) => new Date(p.date)), | ||||||||||||||||||||||||||||||
| 0 | ||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||
| const bLatest = Math.max( | ||||||||||||||||||||||||||||||
| ...(b.publications || []).map((p) => new Date(p.date)), | ||||||||||||||||||||||||||||||
|
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)), | |
| ...((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))), |
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
pageSizeis 0 or negative, the paginate function will produce unexpected results.Math.ceil(results.length / 0)will returnInfinity, and the slice operation with negative pageSize could produce unexpected pagination behavior. Consider adding validation to ensure pageSize is a positive number.