Skip to content

Commit

Permalink
fix(product): fix product variant update price with min_quantity and …
Browse files Browse the repository at this point in the history
…max_quantity (#9381)

* test(product): add product-variant price update test with quantity

* fix(product): fix product update variant price with quantity

* test(product): fix test not passing due to change
  • Loading branch information
BeyondOT authored Oct 14, 2024
1 parent 50a9068 commit 4b5a1f0
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
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,
},
],
})
)
})
})
})
1 change: 1 addition & 0 deletions packages/medusa/src/services/__tests__/product-variant.js
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

0 comments on commit 4b5a1f0

Please sign in to comment.