From e1c966b6d7e74d1e357d38d1329d1af137b690f7 Mon Sep 17 00:00:00 2001 From: Sag Date: Tue, 19 Sep 2023 22:42:27 +0200 Subject: [PATCH] Added filter option to the Recommendations Admin API's browse endpoint (#18233) closes https://github.com/TryGhost/Product/issues/3907 --- .../server/api/endpoints/recommendations.js | 3 ++- .../e2e-api/admin/recommendations.test.js | 20 +++++++++++++++++++ .../src/RecommendationController.ts | 3 ++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/ghost/core/core/server/api/endpoints/recommendations.js b/ghost/core/core/server/api/endpoints/recommendations.js index 1010b1d540..4bcc3b3c9c 100644 --- a/ghost/core/core/server/api/endpoints/recommendations.js +++ b/ghost/core/core/server/api/endpoints/recommendations.js @@ -10,7 +10,8 @@ module.exports = { options: [ 'limit', 'page', - 'include' + 'include', + 'filter' ], permissions: true, validation: {}, diff --git a/ghost/core/test/e2e-api/admin/recommendations.test.js b/ghost/core/test/e2e-api/admin/recommendations.test.js index 66a33e54fd..1eb3658c67 100644 --- a/ghost/core/test/e2e-api/admin/recommendations.test.js +++ b/ghost/core/test/e2e-api/admin/recommendations.test.js @@ -260,6 +260,26 @@ describe('Recommendations Admin API', function () { assert.equal(page1.recommendations.length, 0); }); + + it('can fetch recommendations filtered by an exact title', async function () { + await addDummyRecommendations(5); + + const {body} = await agent.get(`recommendations/?filter=title:'Recommendation 1'`) + .expectStatus(200); + + assert.equal(body.recommendations.length, 1); + assert.equal(body.recommendations[0].title, 'Recommendation 1'); + }); + + it('can fetch recommendations filtered by a partial URL', async function () { + await addDummyRecommendations(5); + + const {body} = await agent.get(`recommendations/?filter=url:~'recommendation1.com'`) + .expectStatus(200); + + assert.equal(body.recommendations.length, 1); + assert.equal(body.recommendations[0].url, 'https://recommendation1.com/'); + }); }); describe('read', function () { diff --git a/ghost/recommendations/src/RecommendationController.ts b/ghost/recommendations/src/RecommendationController.ts index 62cca54428..9bac9e8045 100644 --- a/ghost/recommendations/src/RecommendationController.ts +++ b/ghost/recommendations/src/RecommendationController.ts @@ -82,6 +82,7 @@ export class RecommendationController { const page = options.optionalKey('page')?.integer ?? 1; const limit = options.optionalKey('limit')?.integer ?? 5; const include = options.optionalKey('withRelated')?.array.map(item => item.enum(['count.clicks', 'count.subscribers'])) ?? []; + const filter = options.optionalKey('filter')?.string; const order = [ { @@ -91,7 +92,7 @@ export class RecommendationController { ]; const count = await this.service.countRecommendations({}); - const recommendations = (await this.service.listRecommendations({page, limit, order, include})); + const recommendations = (await this.service.listRecommendations({page, limit, filter, include, order})); return this.#serialize( recommendations,