Skip to content

Commit e455a60

Browse files
authored
fix: master variant predicates and AND predicates (#316)
- vars are now passed to both sides of AND predicate - masterVariants are now included in variants in predicate
1 parent bfab2e7 commit e455a60

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

.changeset/green-queens-rule.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@labdigital/commercetools-mock": patch
3+
---
4+
5+
Fixes a bug where main variants were not included in where predicates. Fixes a bug where vars were not read in predicates with AND statements

src/lib/predicateParser.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ const resolveValue = (obj: any, val: TypeSymbol): any => {
8888
throw new PredicateError("Internal error");
8989
}
9090

91+
// variants() includes both masterVariant and variants for predicates
92+
if (
93+
val.value === "variants" &&
94+
obj.masterVariant &&
95+
obj.variants !== undefined
96+
) {
97+
return [obj.masterVariant, ...(obj.variants ?? [])];
98+
}
99+
91100
if (!(val.value in obj)) {
92101
if (Array.isArray(obj)) {
93102
return Object.values(obj)
@@ -230,7 +239,7 @@ const generateMatchFunc = (predicate: string): MatchFunc => {
230239

231240
.led("AND", 5, ({ left, bp }) => {
232241
const expr = parser.parse({ terminals: [bp - 1] });
233-
return (obj: any) => left(obj) && expr(obj);
242+
return (obj: any, vars: object) => left(obj, vars) && expr(obj, vars);
234243
})
235244
.led("OR", 5, ({ left, token, bp }) => {
236245
const expr = parser.parse({ terminals: [bp - 1] });

src/services/product-projection.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ beforeEach(async () => {
9797
name: "number",
9898
value: 4 as any,
9999
},
100+
{
101+
name: "store",
102+
value: ["test-store"],
103+
},
100104
],
101105
},
102106
variants: [
@@ -252,6 +256,30 @@ describe("Product Projection Query - Generic", () => {
252256
}
253257
});
254258

259+
test("Filter on complex query", async () => {
260+
{
261+
const response = await supertest(ctMock.app)
262+
.get("/dummy/product-projections")
263+
.query({
264+
limit: 50,
265+
where: [
266+
'slug(nl-NL=:slug) and variants(attributes(name="store" and value="test-store"))',
267+
],
268+
"var.slug": "test-product",
269+
"var.store": "test-store",
270+
});
271+
272+
const result: ProductProjectionPagedSearchResponse = response.body;
273+
expect(result).toEqual({
274+
count: 1,
275+
limit: 50,
276+
offset: 0,
277+
total: 1,
278+
results: [productProjection],
279+
});
280+
}
281+
});
282+
255283
test("Filter on invalid slug", async () => {
256284
{
257285
const response = await supertest(ctMock.app)

0 commit comments

Comments
 (0)