Skip to content

Commit

Permalink
Finalize filterIncludesByEnv implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagojsag committed Jul 15, 2021
1 parent a70343b commit 04ce6c4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 49 deletions.
4 changes: 2 additions & 2 deletions src/routes/api/v1/dataset.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down
30 changes: 21 additions & 9 deletions src/services/relationships.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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}`;
Expand Down Expand Up @@ -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
});
Expand All @@ -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
});
Expand Down
73 changes: 35 additions & 38 deletions test/e2e/dataset-get-includes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
})
Expand All @@ -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'
})
Expand All @@ -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'
})
Expand All @@ -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'
})
Expand All @@ -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'
})
Expand All @@ -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'
})
Expand All @@ -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'
})
Expand All @@ -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'
});
Expand All @@ -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'
})
Expand All @@ -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'
})
Expand All @@ -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'
})
Expand All @@ -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'
})
Expand All @@ -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'
})
Expand All @@ -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'
})
Expand All @@ -288,17 +273,28 @@ 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'
})
.reply(200, graphFindById(fakeDatasetOne));


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);
Expand Down Expand Up @@ -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] });
Expand Down Expand Up @@ -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] });
Expand Down Expand Up @@ -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')
Expand All @@ -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] });
Expand Down Expand Up @@ -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);
});

});
Expand Down

0 comments on commit 04ce6c4

Please sign in to comment.