Skip to content

Commit

Permalink
Merge pull request #11 from openfoodfacts:filter-by-product
Browse files Browse the repository at this point in the history
Fix matching on a product column
  • Loading branch information
john-gom authored Oct 12, 2023
2 parents 35b2a1f + 5e02974 commit 8e61d5d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/domain/services/query.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,20 @@ describe('aggregate', () => {
});
});

it('should group products when filtering by a product field', async () => {
await createTestingModule([DomainModule], async (app) => {
const { aminoValue, creatorValue } = await createTestTags(app);
const queryService = app.get(QueryService);
const response = await queryService.aggregate([
{ $match: { creator: creatorValue } },
{ $group: { _id: '$amino_acids_tags' } },
]);
const myTag = response.find((r) => r._id === aminoValue);
expect(myTag).toBeTruthy();
expect(parseInt(myTag.count)).toBe(1);
});
});

it('should be able to do not filtering', async () => {
await createTestingModule([DomainModule], async (app) => {
const { originValue, aminoValue } = await createTestTags(app);
Expand Down
6 changes: 5 additions & 1 deletion src/domain/services/query.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class QueryService {
const qbWhere = this.em
.createQueryBuilder(matchEntity, 'pt2')
.select('*')
.where(`pt2.product_id = pt.${parentId} and pt2.${matchColumn} = ?`, [
.where(`pt2.${this.productId(matchEntity)} = pt.${this.productId(parentEntity)} and pt2.${matchColumn} = ?`, [
whereValue,
]);
qb.andWhere(`${not ? 'NOT ' : ''}EXISTS (${qbWhere.getKnexQuery()})`);
Expand All @@ -86,6 +86,10 @@ export class QueryService {
return whereLog;
}

productId(entity) {
return entity === Product ? 'id': 'product_id';
}

obsoleteWhere(body: any) {
const obsolete = !!body?.obsolete;
delete body?.obsolete;
Expand Down

0 comments on commit 8e61d5d

Please sign in to comment.