From 4b5a1f0413a2cc0eb3c6e795290e0fc7dcaab4b6 Mon Sep 17 00:00:00 2001 From: AshRough <65593661+BeyondOT@users.noreply.github.com> Date: Mon, 14 Oct 2024 11:33:46 +0200 Subject: [PATCH] fix(product): fix product variant update price with min_quantity and 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 --- .../api/__tests__/admin/product.js | 44 +++++++++++++++ .../products/__tests__/update-variant.js | 54 +++++++++++++++++++ .../src/services/__tests__/product-variant.js | 1 + .../medusa/src/services/product-variant.ts | 1 + 4 files changed, 100 insertions(+) diff --git a/integration-tests/api/__tests__/admin/product.js b/integration-tests/api/__tests__/admin/product.js index fc6c23ab03386..b21c6c525ec3c 100644 --- a/integration-tests/api/__tests__/admin/product.js +++ b/integration-tests/api/__tests__/admin/product.js @@ -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" diff --git a/packages/medusa/src/api/routes/admin/products/__tests__/update-variant.js b/packages/medusa/src/api/routes/admin/products/__tests__/update-variant.js index 5ffbdac1f4302..e26e1043bd13a 100644 --- a/packages/medusa/src/api/routes/admin/products/__tests__/update-variant.js +++ b/packages/medusa/src/api/routes/admin/products/__tests__/update-variant.js @@ -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, + }, + ], + }) + ) + }) + }) }) diff --git a/packages/medusa/src/services/__tests__/product-variant.js b/packages/medusa/src/services/__tests__/product-variant.js index 37d1c2f8f0e1b..3409739ec2e96 100644 --- a/packages/medusa/src/services/__tests__/product-variant.js +++ b/packages/medusa/src/services/__tests__/product-variant.js @@ -811,6 +811,7 @@ describe("ProductVariantService", () => { { id: IdMap.getId("dkk") }, { amount: 850, + currency_code: "dkk", } ) }) diff --git a/packages/medusa/src/services/product-variant.ts b/packages/medusa/src/services/product-variant.ts index f8084b6d979fb..a6a9aa61221c1 100644 --- a/packages/medusa/src/services/product-variant.ts +++ b/packages/medusa/src/services/product-variant.ts @@ -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, })