Skip to content

Commit 1190261

Browse files
authored
fix: 🐛 correctly return documents when filtering on a field that is not returned (#413)
1 parent c189748 commit 1190261

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/services/resources-getter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ function ResourcesGetter(model, opts, params) {
2828
this.perform = () => getSegmentCondition()
2929
.then(async (segment) => {
3030
const jsonQuery = [];
31-
await queryBuilder.addProjection(jsonQuery);
3231
await queryBuilder.addFiltersAndJoins(jsonQuery, segment);
3332

3433
if (params.search) {
@@ -44,6 +43,8 @@ function ResourcesGetter(model, opts, params) {
4443
queryBuilder.addSortToQuery(jsonQuery);
4544
}
4645

46+
await queryBuilder.addProjection(jsonQuery);
47+
4748
queryBuilder.addSkipAndLimitToQuery(jsonQuery);
4849

4950
await queryBuilder.joinAllReferences(jsonQuery);

test/tests/services/resources-getter.test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ describe('service > resources-getter', () => {
5555
type: 'String',
5656
get: (film) => `${film.title} ${film.duration}`,
5757
},
58+
{ field: 'rating', type: 'Number' },
5859
],
5960
},
6061
},
@@ -78,6 +79,7 @@ describe('service > resources-getter', () => {
7879
_id: { type: 'ObjectId' },
7980
title: { type: String },
8081
duration: { type: Number },
82+
rating: { type: Number },
8183
});
8284

8385
OrderModel = mongoose.model('Order', OrderSchema);
@@ -134,11 +136,13 @@ describe('service > resources-getter', () => {
134136
_id: '41224d776a326fb40f000011',
135137
duration: 149,
136138
title: 'Terminator',
139+
rating: 4.5,
137140
},
138141
{
139142
_id: '41224d776a326fb40f000012',
140143
duration: 360,
141144
title: 'Titanic',
145+
rating: 4,
142146
},
143147
{
144148
_id: '41224d776a326fb40f000013',
@@ -390,5 +394,56 @@ describe('service > resources-getter', () => {
390394
expect(durations).toHaveLength(0);
391395
});
392396
});
397+
398+
describe('with a condition on a non-filtered field', () => {
399+
it('should return filtered results on the rating', async () => {
400+
expect.assertions(1);
401+
402+
const parameters = {
403+
fields: { films: 'title' },
404+
page: { number: '1', size: '15' },
405+
filters: '{"field":"rating","operator":"present","value":null}',
406+
timezone: 'Europe/Paris',
407+
};
408+
409+
const result = await new ResourcesGetter(FilmModel, options, parameters).perform();
410+
411+
expect(result[0]).toHaveLength(2);
412+
});
413+
414+
it('should return sorted results by rating (asc)', async () => {
415+
expect.assertions(3);
416+
417+
const parameters = {
418+
fields: { films: 'title' },
419+
page: { number: '1', size: '15' },
420+
sort: 'rating',
421+
timezone: 'Europe/Paris',
422+
};
423+
424+
const result = await new ResourcesGetter(FilmModel, options, parameters).perform();
425+
426+
expect(result[0]).toHaveLength(3);
427+
expect(result[0][0].title).toBe('Matrix');
428+
expect(result[0][1].title).toBe('Titanic');
429+
});
430+
431+
it('should return sorted results by rating (desc)', async () => {
432+
expect.assertions(3);
433+
434+
const parameters = {
435+
fields: { films: 'title' },
436+
page: { number: '1', size: '15' },
437+
sort: '-rating',
438+
timezone: 'Europe/Paris',
439+
};
440+
441+
const result = await new ResourcesGetter(FilmModel, options, parameters).perform();
442+
443+
expect(result[0]).toHaveLength(3);
444+
expect(result[0][0].title).toBe('Terminator');
445+
expect(result[0][1].title).toBe('Titanic');
446+
});
447+
});
393448
});
394449
});

0 commit comments

Comments
 (0)