diff --git a/config/functions.js b/config/functions.js index b2f3f0f..b46b3dc 100644 --- a/config/functions.js +++ b/config/functions.js @@ -106,7 +106,7 @@ module.exports = { if (authType === 'Basic') { const base64Credentials = authHeader.split(' ')[1]; const decodedCredentials = Buffer.from(base64Credentials, 'base64').toString('ascii'); - const [_username, password] = decodedCredentials.split(':'); + const [, password] = decodedCredentials.split(':'); if (!password) { return 'Password is required'; @@ -199,5 +199,21 @@ module.exports = { }, {})); return terms; - } -}; \ No newline at end of file + }, + + 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 }; + }, +}; diff --git a/src/api/study/controllers/study.js b/src/api/study/controllers/study.js index 6404637..1f73d5b 100644 --- a/src/api/study/controllers/study.js +++ b/src/api/study/controllers/study.js @@ -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 + ); + + 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)), + 0 + ); + return order === 'asc' ? aLatest - bLatest : bLatest - aLatest; + }); + + const { results: paginatedStudies, meta } = + strapi.config.functions.paginate(studies, ctx.query.pagination || {}); + + return this.transformResponse(paginatedStudies, meta); + } + return await super.find(ctx); }, async findOne(ctx) { @@ -200,7 +226,7 @@ module.exports = createCoreController('api::study.study', ({ strapi }) => ({ populate: ['media', 'data', 'resources'], }, resources: true, - media: { fields: ['title', 'type'], populate: ['file']}, + media: { fields: ['title', 'type'], populate: ['file'] }, cover_dataset: { fields: [], populate: ['media'],