Skip to content

Commit

Permalink
Fix sending empty order condition in discount rules (#4717)
Browse files Browse the repository at this point in the history
* Fix sending empty conditions, write test to cover this case

* Add changset

Co-authored-by: Paweł Chyła <chyla1988@gmail.com>
  • Loading branch information
github-actions[bot] and poulch authored Mar 11, 2024
1 parent 15546c5 commit a98053b
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/slimy-ants-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"saleor-dashboard": patch
---

Improve condtions filtering in discount rules to prevent sending empty conditions
60 changes: 60 additions & 0 deletions src/discounts/models/CatalogRule/preparePredicate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Condition } from "../Condition";
import { prepareCataloguePredicate } from "./preparePredicate";

describe("prepareCataloguePredicate", () => {
it("should return empty object when conditions are empty", () => {
const conditions: Condition[] = [];

const result = prepareCataloguePredicate(conditions);

expect(result).toEqual({});
});

it("should return object with filtered conditions", () => {
const conditions = [
{
id: "",
type: "is",
value: null,
},
{
id: "product",
type: "is",
value: [],
},
{
id: "category",
type: "is",
value: [
{ label: "1", value: "1" },
{ label: "2", value: "2" },
],
},
{
id: "collection",
type: "is",
value: [
{ label: "3", value: "3" },
{ label: "4", value: "4" },
],
},
] as Condition[];

const result = prepareCataloguePredicate(conditions);

expect(result).toEqual({
OR: [
{
categoryPredicate: {
ids: ["1", "2"],
},
},
{
collectionPredicate: {
ids: ["3", "4"],
},
},
],
});
});
});
8 changes: 7 additions & 1 deletion src/discounts/models/CatalogRule/preparePredicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ export function prepareCataloguePredicate(
): CataloguePredicateInput {
const ruleConditions = conditions
.map(condition => {
if (!condition.id || !condition?.value?.length) {
if (!condition.id) {
return undefined;
}

if (Array.isArray(condition.value) && condition.value.length === 0) {
return undefined;
} else if (!condition.value) {
return undefined;
}

Expand Down
4 changes: 2 additions & 2 deletions src/discounts/models/OrderRule/prepareConditions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { OrderPredicateAPI } from "@dashboard/discounts/types";
import { prepareOrderConditions } from "./prepareConditions";

describe("prepareOrderConditions", () => {
it("should return empty array when cataloguePredicate is empty", () => {
it("should return empty array when orderPredicate is empty", () => {
const orderPredicate = {} as OrderPredicateAPI;

const result = prepareOrderConditions(orderPredicate);

expect(result).toEqual([]);
});

it("should return array of conditions when cataloguePredicate is not empty", () => {
it("should return array of conditions when orderPredicate is not empty", () => {
const orderPredicate = {
discountedObjectPredicate: {
AND: [
Expand Down
64 changes: 64 additions & 0 deletions src/discounts/models/OrderRule/preparePredicate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Condition } from "../Condition";
import { prepareOrderPredicate } from "./preparePredicate";

describe("prepareOrderPredicate", () => {
it("should return empty object when conditions are empty", () => {
const conditions: Condition[] = [];

const result = prepareOrderPredicate(conditions);

expect(result).toEqual({
discountedObjectPredicate: {},
});
});

it("should return object with filtered conditions", () => {
const conditions = [
{
id: "",
type: "is",
value: null,
},
{
id: "baseTotalPrice",
type: "is",
value: "1",
},
{
id: "",
type: "is",
value: null,
},
{
id: "baseSubtotalPrice",
type: "range",
value: {
gte: "50",
lte: "100",
},
},
] as Condition[];

const result = prepareOrderPredicate(conditions);

expect(result).toEqual({
discountedObjectPredicate: {
OR: [
{
baseTotalPrice: {
eq: "1",
},
},
{
baseSubtotalPrice: {
eq: {
gte: "50",
lte: "100",
},
},
},
],
},
});
});
});
12 changes: 11 additions & 1 deletion src/discounts/models/OrderRule/preparePredicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ export function prepareOrderPredicate(
): OrderPredicateInput {
const ruleConditions = conditions
.map(condition => {
if (!condition.type) {
if (!condition.id) {
return undefined;
}

if (!condition.id) {
return undefined;
}

if (Array.isArray(condition.value) && condition.value.length === 0) {
return undefined;
} else if (!condition.value) {
return undefined;
}

Expand Down

0 comments on commit a98053b

Please sign in to comment.