From c0e947f47a009dcdf189bd4564485385b3019045 Mon Sep 17 00:00:00 2001 From: Sebastian Mateos Nicolajsen <80953876+sebastiannicolajsen@users.noreply.github.com> Date: Wed, 13 Oct 2021 12:05:58 +0200 Subject: [PATCH] fix: update product images (#494) --- .../api/__tests__/admin/product.js | 23 +++++++++++++++++++ packages/medusa/src/services/product.js | 6 ++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/integration-tests/api/__tests__/admin/product.js b/integration-tests/api/__tests__/admin/product.js index 1e398b65c5..06ff71aee5 100644 --- a/integration-tests/api/__tests__/admin/product.js +++ b/integration-tests/api/__tests__/admin/product.js @@ -592,6 +592,28 @@ describe("/admin/products", () => { ) }) + it("updates product (removes images when empty array included)", async () => { + const api = useApi() + + const payload = { + images: [], + } + + const response = await api + .post("/admin/products/test-product", payload, { + headers: { + Authorization: "Bearer test_token", + }, + }) + .catch((err) => { + console.log(err) + }) + + expect(response.status).toEqual(200) + + expect(response.data.product.images.length).toEqual(0) + }) + it("fails to update product with invalid status", async () => { const api = useApi() @@ -696,6 +718,7 @@ describe("/admin/products", () => { ) }) }) + describe("testing for soft-deletion + uniqueness on handles, collection and variant properties", () => { beforeEach(async () => { try { diff --git a/packages/medusa/src/services/product.js b/packages/medusa/src/services/product.js index 45b7375af4..d46a5968c5 100644 --- a/packages/medusa/src/services/product.js +++ b/packages/medusa/src/services/product.js @@ -295,7 +295,7 @@ class ProductService extends BaseService { let product = productRepo.create(rest) - if (images && images.length) { + if (images) { product.images = await this.upsertImages_(images) } @@ -380,11 +380,11 @@ class ProductService extends BaseService { ...rest } = update - if (!product.thumbnail && !update.thumbnail && images && images.length) { + if (!product.thumbnail && !update.thumbnail && images?.length) { product.thumbnail = images[0] } - if (images && images.length) { + if (images) { product.images = await this.upsertImages_(images) }