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(product): fix product variant update price with min_quantity and max_quantity #9381

Open
wants to merge 4 commits into
base: v1.x
Choose a base branch
from
Open
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
44 changes: 44 additions & 0 deletions integration-tests/api/__tests__/admin/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -2072,6 +2072,50 @@ medusaIntegrationTestRunner({
)
})

it("successfully updates a variant's default prices by changing an existing price (currency_code) with min_quantity and max_quantity", async () => {
const data = {
prices: [
{
currency_code: "usd",
amount: 1500,
min_quantity: 1,
max_quantity: 5,
},
],
}

const response = await api.post(
`/admin/products/${baseProduct.id}/variants/${baseProduct.variants[0].id}`,
data,
adminHeaders
)

expect(
baseProduct.variants[0].prices.find(
(p) => p.currency_code === "usd"
).amount
).toEqual(100)
expect(response.status).toEqual(200)
expect(response.data).toEqual({
product: expect.objectContaining({
id: baseProduct.id,
variants: expect.arrayContaining([
expect.objectContaining({
id: baseProduct.variants[0].id,
prices: expect.arrayContaining([
expect.objectContaining({
amount: 1500,
currency_code: "usd",
min_quantity: 1,
max_quantity: 5,
}),
]),
}),
]),
}),
})
})

it("successfully updates a variant's prices by adding a new price", async () => {
const usdPrice = baseProduct.variants[0].prices.find(
(p) => p.currency_code === "usd"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,58 @@ describe("POST /admin/products/:id/variants/:variantId", () => {
expect(subject.body.product.id).toEqual(IdMap.getId("productWithOptions"))
})
})

describe("successful updates variant prices with min_quantity and max_quantity", () => {
let subject

beforeAll(async () => {
jest.clearAllMocks()
subject = await request(
"POST",
`/admin/products/${IdMap.getId(
"productWithOptions"
)}/variants/${IdMap.getId("variant1")}`,
{
payload: {
title: "hi",
prices: [
{
currency_code: "DKK",
amount: 100,
min_quantity: 1,
max_quantity: 5,
},
],
},
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
}
)
})

it("returns 200", () => {
expect(subject.status).toEqual(200)
})

it("filters prices", () => {
expect(ProductVariantServiceMock.update).toHaveBeenCalledTimes(1)
expect(ProductVariantServiceMock.update).toHaveBeenCalledWith(
IdMap.getId("variant1"),
expect.objectContaining({
title: "hi",
prices: [
{
currency_code: "DKK",
amount: 100,
min_quantity: 1,
max_quantity: 5,
},
],
})
)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,7 @@ describe("ProductVariantService", () => {
{ id: IdMap.getId("dkk") },
{
amount: 850,
currency_code: "dkk",
}
)
})
Expand Down
1 change: 1 addition & 0 deletions packages/medusa/src/services/product-variant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ class ProductVariantService extends TransactionBaseService {
// No need to update if the amount is the same
if (moneyAmount.amount !== price.amount) {
dataToUpdate.push({
...price,
id: moneyAmount.id,
amount: price.amount,
})
Expand Down
Loading