Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sending empty order condition in discount rules #4717

Merged
merged 1 commit into from
Mar 11, 2024
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/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
Loading