@@ -11,31 +11,14 @@ const _ = require('lodash');
1111module . exports = createCoreController ( 'api::dataset.dataset' , ( { strapi } ) => ( {
1212 async find ( ctx ) {
1313
14- // Check if 'collection' query parameter is present
15- const { collection } = ctx . query ;
16- if ( collection ) {
17- const collectionEntry = await strapi . db . query ( 'api::collection.collection' ) . findOne ( {
18- where : { name : collection } ,
19- populate : { studies : { populate : { datasets : { select : [ 'id' ] } } } }
20- } ) ;
21-
22- const ids = _ . flatMap ( collectionEntry ?. studies , s => s . datasets . map ( d => d . id ) ) || [ ] ;
23- if ( ! ids ?. length ) { return this . transformResponse ( [ ] , {
24- pagination : { page : 1 , total : 0 , pageCount : 0 , pageSize : ctx . query . pagination ?. pageSize || 10 }
25- } ) ; }
26-
27- ctx . query . filters = {
28- ...ctx . query . filters ,
29- id : { $in : ids } ,
30- } ;
31- }
32-
3314 // If not providing a study id, return only datasets from studies that are listed
3415 if ( ! ctx . query . filters ?. study ?. id ?. $eq ) {
3516 ctx . query . filters = {
3617 ...ctx . query . filters ,
3718 study : {
38- is_listed : true } ,
19+ ...( ctx . query . filters ?. study || { } ) ,
20+ is_listed : true ,
21+ } ,
3922 } ;
4023 }
4124
@@ -57,6 +40,27 @@ module.exports = createCoreController('api::dataset.dataset', ({ strapi }) => ({
5740 }
5841 }
5942 } ;
43+
44+ // Check if 'collection' query parameter is present
45+ // Add to query filters last to avoid spreading the ids array into an object
46+ const { collection } = ctx . query ;
47+ if ( collection ) {
48+ const collectionEntry = await strapi . db . query ( 'api::collection.collection' ) . findOne ( {
49+ where : { name : collection } ,
50+ populate : { studies : { populate : { datasets : { select : [ 'id' ] } } } }
51+ } ) ;
52+
53+ const ids = _ . flatMap ( collectionEntry ?. studies , s => s . datasets . map ( d => d . id ) ) || [ ] ;
54+ if ( ! ids ?. length ) { return this . transformResponse ( [ ] , {
55+ pagination : { page : 1 , total : 0 , pageCount : 0 , pageSize : ctx . query . pagination ?. pageSize || 10 }
56+ } ) ; }
57+
58+ ctx . query . filters = {
59+ ...ctx . query . filters ,
60+ id : { $in : ids } ,
61+ } ;
62+ }
63+
6064 return await super . find ( ctx ) ;
6165 } ,
6266 async findOne ( ctx ) {
0 commit comments