diff --git a/integration-tests/api/__tests__/admin/product.js b/integration-tests/api/__tests__/admin/product.js index c40f92a12b..68a80bdc8d 100644 --- a/integration-tests/api/__tests__/admin/product.js +++ b/integration-tests/api/__tests__/admin/product.js @@ -6,6 +6,7 @@ const { initDb, useDb } = require("../../../helpers/use-db") const adminSeeder = require("../../helpers/admin-seeder") const productSeeder = require("../../helpers/product-seeder") +const { ProductVariant } = require("@medusajs/medusa") jest.setTimeout(50000) @@ -105,7 +106,7 @@ describe("/admin/products", () => { status: "proposed", } - //update test-product status to proposed + // update test-product status to proposed await api .post("/admin/products/test-product", payload, { headers: { @@ -423,7 +424,7 @@ describe("/admin/products", () => { ], variants: [ { - id: "test-variant", //expect.stringMatching(/^test-variant*/), + id: "test-variant", // expect.stringMatching(/^test-variant*/), created_at: expect.any(String), updated_at: expect.any(String), product_id: expect.stringMatching(/^test-*/), @@ -446,7 +447,7 @@ describe("/admin/products", () => { ], }, { - id: "test-variant_2", //expect.stringMatching(/^test-variant*/), + id: "test-variant_2", // expect.stringMatching(/^test-variant*/), created_at: expect.any(String), updated_at: expect.any(String), product_id: expect.stringMatching(/^test-*/), @@ -519,7 +520,7 @@ describe("/admin/products", () => { options: [], variants: [ { - id: "test-variant_4", //expect.stringMatching(/^test-variant*/), + id: "test-variant_4", // expect.stringMatching(/^test-variant*/), created_at: expect.any(String), updated_at: expect.any(String), product_id: expect.stringMatching(/^test-*/), @@ -542,7 +543,7 @@ describe("/admin/products", () => { ], }, { - id: "test-variant_3", //expect.stringMatching(/^test-variant*/), + id: "test-variant_3", // expect.stringMatching(/^test-variant*/), created_at: expect.any(String), updated_at: expect.any(String), product_id: expect.stringMatching(/^test-*/), @@ -1074,6 +1075,41 @@ describe("/admin/products", () => { ) }) + it("successfully deletes a product and variants", async () => { + const api = useApi() + + const variantPre = await dbConnection.manager.findOne(ProductVariant, { + id: "test-variant", + }) + + expect(variantPre).not.toEqual(undefined) + + const response = await api + .delete("/admin/products/test-product", { + headers: { + Authorization: "Bearer test_token", + }, + }) + .catch((err) => { + console.log(err) + }) + + expect(response.status).toEqual(200) + + expect(response.data).toEqual( + expect.objectContaining({ + id: "test-product", + deleted: true, + }) + ) + + const variant = await dbConnection.manager.findOne(ProductVariant, { + id: "test-variant", + }) + + expect(variant).toEqual(undefined) + }) + it("successfully creates product with soft-deleted product handle and deletes it again", async () => { const api = useApi() diff --git a/packages/medusa/src/services/product.js b/packages/medusa/src/services/product.js index 876cb33801..54e1ebea6f 100644 --- a/packages/medusa/src/services/product.js +++ b/packages/medusa/src/services/product.js @@ -468,7 +468,10 @@ class ProductService extends BaseService { const productRepo = manager.getCustomRepository(this.productRepository_) // Should not fail, if product does not exist, since delete is idempotent - const product = await productRepo.findOne({ where: { id: productId } }) + const product = await productRepo.findOne( + { id: productId }, + { relations: ["variants"] } + ) if (!product) return Promise.resolve()