Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/green-queens-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@labdigital/commercetools-mock": patch
---

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
11 changes: 10 additions & 1 deletion src/lib/predicateParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ const resolveValue = (obj: any, val: TypeSymbol): any => {
throw new PredicateError("Internal error");
}

// variants() includes both masterVariant and variants for predicates
if (
val.value === "variants" &&
obj.masterVariant &&
obj.variants !== undefined
) {
return [obj.masterVariant, ...(obj.variants ?? [])];
}

if (!(val.value in obj)) {
if (Array.isArray(obj)) {
return Object.values(obj)
Expand Down Expand Up @@ -230,7 +239,7 @@ const generateMatchFunc = (predicate: string): MatchFunc => {

.led("AND", 5, ({ left, bp }) => {
const expr = parser.parse({ terminals: [bp - 1] });
return (obj: any) => left(obj) && expr(obj);
return (obj: any, vars: object) => left(obj, vars) && expr(obj, vars);
})
.led("OR", 5, ({ left, token, bp }) => {
const expr = parser.parse({ terminals: [bp - 1] });
Expand Down
28 changes: 28 additions & 0 deletions src/services/product-projection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ beforeEach(async () => {
name: "number",
value: 4 as any,
},
{
name: "store",
value: ["test-store"],
},
],
},
variants: [
Expand Down Expand Up @@ -252,6 +256,30 @@ describe("Product Projection Query - Generic", () => {
}
});

test("Filter on complex query", async () => {
{
const response = await supertest(ctMock.app)
.get("/dummy/product-projections")
.query({
limit: 50,
where: [
'slug(nl-NL=:slug) and variants(attributes(name="store" and value="test-store"))',
],
"var.slug": "test-product",
"var.store": "test-store",
});

const result: ProductProjectionPagedSearchResponse = response.body;
expect(result).toEqual({
count: 1,
limit: 50,
offset: 0,
total: 1,
results: [productProjection],
});
}
});

test("Filter on invalid slug", async () => {
{
const response = await supertest(ctMock.app)
Expand Down
Loading