diff --git a/src/routes/api/v1/dataset.router.js b/src/routes/api/v1/dataset.router.js index 717504e..e09c0d7 100644 --- a/src/routes/api/v1/dataset.router.js +++ b/src/routes/api/v1/dataset.router.js @@ -377,7 +377,7 @@ class DatasetRouter { } const metadataIds = await RelationshipsService.filterByMetadata(search, metadataSort); - const searchBySynonymsIds = await RelationshipsService.searchBySynonyms(serializeObjToQuery(query)); + const searchBySynonymsIds = await RelationshipsService.searchBySynonyms(query); const datasetBySearchIds = await DatasetService.getDatasetIdsBySearch(search.split(' ')); searchIds = metadataIds.concat(searchBySynonymsIds).concat(datasetBySearchIds); } @@ -386,7 +386,7 @@ class DatasetRouter { || sort.indexOf('most-favorited') >= 0 || sort.indexOf('most-viewed') >= 0 ) { - conceptIds = await RelationshipsService.filterByConcepts(serializeObjToQuery(query)); + conceptIds = await RelationshipsService.filterByConcepts(query); } if ((searchIds && searchIds.length === 0) || (conceptIds && conceptIds.length === 0)) { ctx.body = DatasetSerializer.serialize([], null); diff --git a/src/services/relationships.service.js b/src/services/relationships.service.js index 951177f..6ce2861 100644 --- a/src/services/relationships.service.js +++ b/src/services/relationships.service.js @@ -11,23 +11,35 @@ const serializeObjToQuery = (obj) => Object.keys(obj).reduce((a, k) => { class RelationshipsService { - static treatQuery(query) { + /** + * - Clones the query object + * - Strips a few things that should not be passed over to other MSs + * - Encodes query into a URL param format + * + * @TODO: rawQuery is passed by reference, so we should evaluate cloning at an earlier point + * + * @param rawQuery + * @returns {string} + */ + static prepareAndFormatQuery(rawQuery) { + const query = { ...rawQuery }; if (!query.application && query.app) { query.application = query.app; } + const filterIncludesByEnv = query.filterIncludesByEnv ? query.filterIncludesByEnv : false; + if (!filterIncludesByEnv) { + delete query.env + } + delete query.filterIncludesByEnv; delete query.includes; delete query['user.role']; - return query; + return serializeObjToQuery(query); } static async getResources(ids, includes, query = '', users = [], isAdmin = false) { logger.info(`Getting resources of ids: ${ids}`); delete query.ids; delete query.usersRole; - const filterIncludesByEnv = query.filterIncludesByEnv ? query.filterIncludesByEnv : false; - if(!filterIncludesByEnv) { - delete query.env - } let resources = includes.map(async (include) => { const obj = {}; if (INCLUDES.indexOf(include) >= 0) { @@ -53,7 +65,7 @@ class RelationshipsService { uri = '/auth'; } - let uriQuery = serializeObjToQuery(RelationshipsService.treatQuery(query)); + let uriQuery = RelationshipsService.prepareAndFormatQuery(query); if (uriQuery.length > 0) { uriQuery = `?${uriQuery}`; @@ -289,7 +301,7 @@ class RelationshipsService { static async filterByConcepts(query) { try { const result = await RWAPIMicroservice.requestToMicroservice({ - uri: `/v1/graph/query/search-datasets-ids?${query}`, + uri: `/v1/graph/query/search-datasets-ids?${serializeObjToQuery(query)}`, method: 'GET', json: true }); @@ -302,7 +314,7 @@ class RelationshipsService { static async searchBySynonyms(query) { try { const result = await RWAPIMicroservice.requestToMicroservice({ - uri: `/v1/graph/query/search-by-label-synonyms?${query}`, + uri: `/v1/graph/query/search-by-label-synonyms?${RelationshipsService.prepareAndFormatQuery(query)}`, method: 'GET', json: true }); diff --git a/test/e2e/dataset-get-includes.spec.js b/test/e2e/dataset-get-includes.spec.js index fe16556..a458bae 100644 --- a/test/e2e/dataset-get-includes.spec.js +++ b/test/e2e/dataset-get-includes.spec.js @@ -89,10 +89,8 @@ describe('Get datasets with includes', () => { .get('/v1/graph/query/search-by-label-synonyms') .query({ application: 'rw', - env: 'production', - includes: 'layer,metadata,vocabulary,widget,graph,user', language: 'en', - page: { number: '1,1', size: '12,12' }, + page: { number: '1', size: '12' }, published: true, search: 'human' }) @@ -103,9 +101,8 @@ describe('Get datasets with includes', () => { .post('/auth/user/find-by-ids', { ids: [fakeDatasetOne.userId] }) .query({ application: 'rw', - env: 'production', language: 'en', - page: { number: '1,1', size: '12,12' }, + page: { number: '1', size: '12' }, published: true, search: 'human' }) @@ -116,9 +113,8 @@ describe('Get datasets with includes', () => { .post('/v1/widget/find-by-ids', { ids: [fakeDatasetOne.id], app: 'rw' }) .query({ application: 'rw', - env: 'production', language: 'en', - page: { number: '1,1', size: '12,12' }, + page: { number: '1', size: '12' }, published: true, search: 'human' }) @@ -129,9 +125,8 @@ describe('Get datasets with includes', () => { .post('/v1/dataset/vocabulary/find-by-ids', { ids: [fakeDatasetOne.id] }) .query({ application: 'rw', - env: 'production', language: 'en', - page: { number: '1,1', size: '12,12' }, + page: { number: '1', size: '12' }, published: true, search: 'human' }) @@ -142,9 +137,8 @@ describe('Get datasets with includes', () => { .post('/v1/dataset/metadata/find-by-ids', { ids: [fakeDatasetOne.id] }) .query({ application: 'rw', - env: 'production', language: 'en', - page: { number: '1,1', size: '12,12' }, + page: { number: '1', size: '12' }, published: true, search: 'human' }) @@ -154,9 +148,8 @@ describe('Get datasets with includes', () => { .post('/v1/layer/find-by-ids', { ids: [fakeDatasetOne.id], app: 'rw' }) .query({ application: 'rw', - env: 'production', language: 'en', - page: { number: '1,1', size: '12,12' }, + page: { number: '1', size: '12' }, published: true, search: 'human' }) @@ -166,9 +159,8 @@ describe('Get datasets with includes', () => { .post('/v1/graph/find-by-ids', { ids: [fakeDatasetOne.id], app: 'rw' }) .query({ application: 'rw', - env: 'production', language: 'en', - page: { number: '1,1', size: '12,12' }, + page: { number: '1', size: '12' }, published: true, search: 'human' }) @@ -182,7 +174,7 @@ describe('Get datasets with includes', () => { env: 'production', includes: ['layer', 'metadata', 'vocabulary', 'widget', 'graph', 'user'].join(','), language: 'en', - page: { number: '1,1', size: '12,12' }, + page: { number: '1', size: '12' }, published: true, search: 'human' }); @@ -208,10 +200,8 @@ describe('Get datasets with includes', () => { .get('/v1/graph/query/search-by-label-synonyms') .query({ application: 'rw', - env: 'production', - includes: 'layer,metadata,vocabulary,widget,graph,user', language: 'en', - page: { number: '1,1', size: '12,12' }, + page: { number: '1', size: '12' }, published: true, search: 'human' }) @@ -221,9 +211,8 @@ describe('Get datasets with includes', () => { .post('/auth/user/find-by-ids', { ids: [fakeDatasetOne.userId] }) .query({ application: 'rw', - env: 'production', language: 'en', - page: { number: '1,1', size: '12,12' }, + page: { number: '1', size: '12' }, published: true, search: 'human' }) @@ -238,9 +227,8 @@ describe('Get datasets with includes', () => { .post('/v1/widget/find-by-ids', { ids: [fakeDatasetOne.id], app: 'rw' }) .query({ application: 'rw', - env: 'production', language: 'en', - page: { number: '1,1', size: '12,12' }, + page: { number: '1', size: '12' }, published: true, search: 'human' }) @@ -251,9 +239,8 @@ describe('Get datasets with includes', () => { .post('/v1/dataset/vocabulary/find-by-ids', { ids: [fakeDatasetOne.id] }) .query({ application: 'rw', - env: 'production', language: 'en', - page: { number: '1,1', size: '12,12' }, + page: { number: '1', size: '12' }, published: true, search: 'human' }) @@ -264,9 +251,8 @@ describe('Get datasets with includes', () => { .post('/v1/dataset/metadata/find-by-ids', { ids: [fakeDatasetOne.id] }) .query({ application: 'rw', - env: 'production', language: 'en', - page: { number: '1,1', size: '12,12' }, + page: { number: '1', size: '12' }, published: true, search: 'human' }) @@ -276,9 +262,8 @@ describe('Get datasets with includes', () => { .post('/v1/layer/find-by-ids', { ids: [fakeDatasetOne.id], app: 'rw' }) .query({ application: 'rw', - env: 'production', language: 'en', - page: { number: '1,1', size: '12,12' }, + page: { number: '1', size: '12' }, published: true, search: 'human' }) @@ -288,9 +273,8 @@ describe('Get datasets with includes', () => { .post('/v1/graph/find-by-ids', { ids: [fakeDatasetOne.id], app: 'rw' }) .query({ application: 'rw', - env: 'production', language: 'en', - page: { number: '1,1', size: '12,12' }, + page: { number: '1', size: '12' }, published: true, search: 'human' }) @@ -298,7 +282,19 @@ describe('Get datasets with includes', () => { const response = await requester - .get(`/api/v1/dataset?application=rw&env=production&includes=layer,metadata,vocabulary,widget,graph,user&language=en&page[number]=1&page[size]=12&published=true&search=human&page[size]=12&page[number]=1`) + .get(`/api/v1/dataset`) + .query({ + application: 'rw', + env: 'production', + includes: 'layer,metadata,vocabulary,widget,graph,user', + language: 'en', + page: { + number: 1, + size: 12, + }, + published: true, + search: 'human' + }) .set('Authorization', `Bearer abcd`); response.status.should.equal(200); @@ -384,7 +380,6 @@ describe('Get datasets with includes', () => { nock(process.env.GATEWAY_URL) .get('/v1/graph/query/search-by-label-synonyms') .query({ - includes: 'layer,metadata,vocabulary,widget,graph,user', search: 'human' }) .reply(200, { data: [fakeDatasetOne.id] }); @@ -461,7 +456,6 @@ describe('Get datasets with includes', () => { nock(process.env.GATEWAY_URL) .get('/v1/graph/query/search-by-label-synonyms') .query({ - includes: 'layer,metadata,vocabulary,widget,graph,user', search: 'human' }) .reply(200, { data: [fakeDatasetOne.id] }); @@ -525,11 +519,13 @@ describe('Get datasets with includes', () => { response.body.should.have.property('data').with.lengthOf(1); response.body.should.have.property('links').and.be.an('object'); - response.body.data[0].should.deep.equal(datasetGetIncludeAllAnonymous(fakeDatasetOne)); + const expectedData = datasetGetIncludeAllAnonymous(fakeDatasetOne); + expectedData.attributes.env = 'custom'; + response.body.data[0].should.deep.equal(expectedData); }); it('Get datasets with includes and search, with filterIncludesByEnv set to true and custom env, should load included data filtered by env', async () => { - const fakeDatasetOne = await new Dataset(createDataset('cartodb')).save(); + const fakeDatasetOne = await new Dataset(createDataset('cartodb', { env: 'custom' })).save(); nock(process.env.GATEWAY_URL) .get('/v1/metadata') @@ -540,7 +536,6 @@ describe('Get datasets with includes', () => { .get('/v1/graph/query/search-by-label-synonyms') .query({ env: 'custom', - includes: 'layer,metadata,vocabulary,widget,graph,user', search: 'human' }) .reply(200, { data: [fakeDatasetOne.id] }); @@ -611,7 +606,9 @@ describe('Get datasets with includes', () => { response.body.should.have.property('data').with.lengthOf(1); response.body.should.have.property('links').and.be.an('object'); - response.body.data[0].should.deep.equal(datasetGetIncludeAllAnonymous(fakeDatasetOne)); + const expectedData = datasetGetIncludeAllAnonymous(fakeDatasetOne); + expectedData.attributes.env = 'custom'; + response.body.data[0].should.deep.equal(expectedData); }); });