@@ -117,15 +117,18 @@ module.exports = createCoreController('api::study.study', ({ strapi }) => ({
117117 // as nested sorting returns duplicates (https://github.com/strapi/strapi/issues/11892)
118118 const [ sort , order = 'asc' ] = ctx . query . sort ?. split ( ':' ) || [ ] ;
119119 if ( sort === 'publications.date' ) {
120- const studies = await strapi . entityService . findMany (
120+ const studiesDates = await strapi . entityService . findMany (
121121 'api::study.study' ,
122122 {
123- ...ctx . query ,
124- sort : null
123+ fields : [ 'id' ] ,
124+ populate : {
125+ publications : { fields : [ 'date' ] } ,
126+ } ,
127+ filters : ctx . query . filters ,
125128 }
126129 ) ;
127130
128- studies . sort ( ( a , b ) => {
131+ studiesDates . sort ( ( a , b ) => {
129132 const aLatest = Math . max (
130133 ...( a . publications || [ ] ) . map ( ( p ) => new Date ( p . date ) ) ,
131134 0
@@ -137,10 +140,23 @@ module.exports = createCoreController('api::study.study', ({ strapi }) => ({
137140 return order === 'asc' ? aLatest - bLatest : bLatest - aLatest ;
138141 } ) ;
139142
140- const { results : paginatedStudies , meta } =
141- strapi . config . functions . paginate ( studies , ctx . query . pagination || { } ) ;
143+ const { results : paginatedStudiesDates , meta } =
144+ strapi . config . functions . paginate ( studiesDates , ctx . query . pagination || { } ) ;
145+
146+ const paginatedIds = paginatedStudiesDates . map ( ( s ) => s . id ) ;
147+
148+ let studies = await strapi . entityService . findMany ( 'api::study.study' , {
149+ ...ctx . query ,
150+ filters : {
151+ ...ctx . query . filters ,
152+ id : { $in : paginatedIds } ,
153+ } ,
154+ sort : null ,
155+ } ) ;
156+
157+ studies = paginatedIds . map ( ( id ) => studies . find ( ( s ) => s . id === id ) ) ;
142158
143- return this . transformResponse ( paginatedStudies , meta ) ;
159+ return this . transformResponse ( studies , meta ) ;
144160 }
145161
146162 return await super . find ( ctx ) ;
0 commit comments