Skip to content

Commit 405ec2a

Browse files
authored
Merge pull request #62 from haniffalab/fix/collection-datasets
Fix datasets by collection query
2 parents f9db686 + 0e75237 commit 405ec2a

2 files changed

Lines changed: 45 additions & 39 deletions

File tree

src/api/dataset/controllers/dataset.js

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,14 @@ const _ = require('lodash');
1111
module.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) {

src/api/study/controllers/study.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,6 @@ const { NotFoundError } = require('@strapi/utils').errors;
1010
module.exports = createCoreController('api::study.study', ({ strapi }) => ({
1111
async find(ctx) {
1212

13-
// Check if 'collection' query parameter is present
14-
const { collection } = ctx.query;
15-
if (collection) {
16-
const collectionEntry = await strapi.db.query('api::collection.collection').findOne({
17-
where: { name: collection },
18-
populate: { studies: { select: ['id'] } },
19-
});
20-
21-
const ids = collectionEntry?.studies.map(({ id }) => id) || [];
22-
if (!ids?.length) { return this.transformResponse([], {
23-
pagination: { page: 1, total: 0, pageCount: 0, pageSize: ctx.query.pagination?.pageSize || 10 }
24-
}); }
25-
26-
ctx.query.filters = {
27-
...ctx.query.filters,
28-
id: { $in: ids },
29-
};
30-
}
31-
3213
ctx.query.filters = {
3314
...ctx.query.filters,
3415
is_listed: true,
@@ -87,6 +68,27 @@ module.exports = createCoreController('api::study.study', ({ strapi }) => ({
8768
}
8869
},
8970
};
71+
72+
// Check if 'collection' query parameter is present
73+
// Add to query filters last to avoid spreading the ids array into an object
74+
const { collection } = ctx.query;
75+
if (collection) {
76+
const collectionEntry = await strapi.db.query('api::collection.collection').findOne({
77+
where: { name: collection },
78+
populate: { studies: { select: ['id'] } },
79+
});
80+
81+
const ids = collectionEntry?.studies.map(({ id }) => id) || [];
82+
if (!ids?.length) { return this.transformResponse([], {
83+
pagination: { page: 1, total: 0, pageCount: 0, pageSize: ctx.query.pagination?.pageSize || 10 }
84+
}); }
85+
86+
ctx.query.filters = {
87+
...ctx.query.filters,
88+
id: { $in: ids },
89+
};
90+
}
91+
9092
return await super.find(ctx);
9193
},
9294
async findOne(ctx) {

0 commit comments

Comments
 (0)