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(promotion): eval conditions for rules are made accurate #10915

Merged
merged 7 commits into from
Jan 21, 2025
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/five-plants-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/promotion": patch
---

fix(promotion): eval conditions for rules are corrected
65 changes: 65 additions & 0 deletions integration-tests/http/__fixtures__/product.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { ProductStatus } from "@medusajs/utils"

export const medusaTshirtProduct = {
title: "Medusa T-Shirt",
handle: "t-shirt",
status: ProductStatus.PUBLISHED,
options: [
{
title: "Size",
values: ["S"],
},
{
title: "Color",
values: ["Black", "White"],
},
],
variants: [
{
title: "S / Black",
sku: "SHIRT-S-BLACK",
options: {
Size: "S",
Color: "Black",
},
manage_inventory: false,
prices: [
{
amount: 1500,
currency_code: "usd",
},
{
amount: 1500,
currency_code: "eur",
},
{
amount: 1300,
currency_code: "dkk",
},
],
},
{
title: "S / White",
sku: "SHIRT-S-WHITE",
options: {
Size: "S",
Color: "White",
},
manage_inventory: false,
prices: [
{
amount: 1500,
currency_code: "usd",
},
{
amount: 1500,
currency_code: "eur",
},
{
amount: 1300,
currency_code: "dkk",
},
],
},
],
}
71 changes: 4 additions & 67 deletions integration-tests/http/__tests__/cart/store/cart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
Modules,
PriceListStatus,
PriceListType,
ProductStatus,
PromotionRuleOperator,
PromotionStatus,
PromotionType,
Expand All @@ -15,6 +14,7 @@ import {
} from "../../../../helpers/create-admin-user"
import { setupTaxStructure } from "../../../../modules/__tests__/fixtures"
import { createAuthenticatedCustomer } from "../../../../modules/helpers/create-authenticated-customer"
import { medusaTshirtProduct } from "../../../__fixtures__/product"

jest.setTimeout(100000)

Expand All @@ -30,70 +30,6 @@ const shippingAddressData = {
postal_code: "94016",
}

const productData = {
title: "Medusa T-Shirt",
handle: "t-shirt",
status: ProductStatus.PUBLISHED,
options: [
{
title: "Size",
values: ["S"],
},
{
title: "Color",
values: ["Black", "White"],
},
],
variants: [
{
title: "S / Black",
sku: "SHIRT-S-BLACK",
options: {
Size: "S",
Color: "Black",
},
manage_inventory: false,
prices: [
{
amount: 1500,
currency_code: "usd",
},
{
amount: 1500,
currency_code: "eur",
},
{
amount: 1300,
currency_code: "dkk",
},
],
},
{
title: "S / White",
sku: "SHIRT-S-WHITE",
options: {
Size: "S",
Color: "White",
},
manage_inventory: false,
prices: [
{
amount: 1500,
currency_code: "usd",
},
{
amount: 1500,
currency_code: "eur",
},
{
amount: 1300,
currency_code: "dkk",
},
],
},
],
}

medusaIntegrationTestRunner({
env,
testSuite: ({ dbConnection, getContainer, api }) => {
Expand Down Expand Up @@ -150,8 +86,9 @@ medusaIntegrationTestRunner({
)
).data.region

product = (await api.post("/admin/products", productData, adminHeaders))
.data.product
product = (
await api.post("/admin/products", medusaTshirtProduct, adminHeaders)
).data.product

salesChannel = (
await api.post(
Expand Down
157 changes: 146 additions & 11 deletions integration-tests/http/__tests__/promotions/admin/promotions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import { PromotionStatus, PromotionType } from "@medusajs/utils"
import { createAdminUser } from "../../../../helpers/create-admin-user"
import {
createAdminUser,
generatePublishableKey,
generateStoreHeaders,
} from "../../../../helpers/create-admin-user"
import { medusaTshirtProduct } from "../../../__fixtures__/product"

jest.setTimeout(50000)

Expand Down Expand Up @@ -219,6 +224,23 @@ medusaIntegrationTestRunner({
)
})

it("should throw error when an incorrect status is passed", async () => {
const { response } = await api
.post(
`/admin/promotions`,
{ ...standardPromotionPayload, status: "does-not-exist" },
adminHeaders
)
.catch((e) => e)

expect(response.status).toEqual(400)
expect(response.data).toEqual({
type: "invalid_data",
message:
"Invalid request: Expected: 'draft, active, inactive' for field 'status', but got: 'does-not-exist'",
})
})

it("should create a standard promotion successfully", async () => {
const response = await api.post(
`/admin/promotions`,
Expand Down Expand Up @@ -466,20 +488,133 @@ medusaIntegrationTestRunner({
)
})

it("should throw error when an incorrect status is passed", async () => {
const { response } = await api
.post(
describe("with cart", () => {
it("should add promotion to cart only when gte rule matches", async () => {
const publishableKey = await generatePublishableKey(appContainer)
const storeHeaders = generateStoreHeaders({ publishableKey })

const salesChannel = (
await api.post(
"/admin/sales-channels",
{ name: "Webshop", description: "channel" },
adminHeaders
)
).data.sales_channel

const region = (
await api.post(
"/admin/regions",
{ name: "US", currency_code: "usd", countries: ["us"] },
adminHeaders
)
).data.region

const product = (
await api.post(
"/admin/products",
medusaTshirtProduct,
adminHeaders
)
).data.product

const cart = (
await api.post(
`/store/carts`,
{
currency_code: "usd",
sales_channel_id: salesChannel.id,
region_id: region.id,
items: [{ variant_id: product.variants[0].id, quantity: 1 }],
promo_codes: [promotion.code],
},
storeHeaders
)
).data.cart

const response = await api.post(
`/admin/promotions`,
{ ...standardPromotionPayload, status: "does-not-exist" },
{
code: "TEST",
type: PromotionType.STANDARD,
status: PromotionStatus.ACTIVE,
is_automatic: true,
application_method: {
target_type: "items",
type: "fixed",
allocation: "each",
currency_code: "USD",
value: 100,
max_quantity: 100,
},
rules: [
{
attribute: "subtotal",
operator: "gte",
values: "2000",
},
],
},
adminHeaders
)
.catch((e) => e)

expect(response.status).toEqual(400)
expect(response.data).toEqual({
type: "invalid_data",
message:
"Invalid request: Expected: 'draft, active, inactive' for field 'status', but got: 'does-not-exist'",
expect(response.status).toEqual(200)
expect(response.data.promotion).toEqual(
expect.objectContaining({
id: expect.any(String),
code: "TEST",
type: "standard",
is_automatic: true,
application_method: expect.objectContaining({
value: 100,
max_quantity: 100,
type: "fixed",
target_type: "items",
allocation: "each",
target_rules: [],
}),
rules: [
expect.objectContaining({
operator: "gte",
attribute: "subtotal",
values: expect.arrayContaining([
expect.objectContaining({ value: "2000" }),
]),
}),
],
})
)

const cartWithPromotion1 = (
await api.post(
`/store/carts/${cart.id}`,
{ promo_codes: [promotion.code] },
storeHeaders
)
).data.cart

expect(cartWithPromotion1).toEqual(
expect.objectContaining({
promotions: [],
})
)

const cartWithPromotion2 = (
await api.post(
`/store/carts/${cart.id}/line-items`,
{ variant_id: product.variants[0].id, quantity: 40 },
storeHeaders
)
).data.cart
console.log("cartWithPromotion2 -- ", cartWithPromotion2.promotions)
expect(cartWithPromotion2).toEqual(
expect.objectContaining({
promotions: [
expect.objectContaining({
code: response.data.promotion.code,
}),
],
})
)
})
})
})
Expand Down
Loading
Loading