Fix/cascading product variant deletion (#570)
* testing for deletes of variants when a product is deleted * remove variants with products * test using direct db query * formatting * removed unused imports
This commit is contained in:
1 parent
b0226b1975
commit
6b83653bf6
2 files changed
+40
-1
No files matched your search
@@ -6,6 +6,7 @@ const { initDb, useDb } = require("../../../helpers/use-db")
|
|||||||
|
|
||||||
const adminSeeder = require("../../helpers/admin-seeder")
|
const adminSeeder = require("../../helpers/admin-seeder")
|
||||||
const productSeeder = require("../../helpers/product-seeder")
|
const productSeeder = require("../../helpers/product-seeder")
|
||||||
|
const { ProductVariant } = require("@medusajs/medusa")
|
||||||
|
|
||||||
jest.setTimeout(50000)
|
jest.setTimeout(50000)
|
||||||
|
|
||||||
@@ -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 () => {
|
it("successfully creates product with soft-deleted product handle and deletes it again", async () => {
|
||||||
const api = useApi()
|
const api = useApi()
|
||||||
|
|
||||||
|
|||||||
@@ -468,7 +468,10 @@ class ProductService extends BaseService {
|
|||||||
const productRepo = manager.getCustomRepository(this.productRepository_)
|
const productRepo = manager.getCustomRepository(this.productRepository_)
|
||||||
|
|
||||||
// Should not fail, if product does not exist, since delete is idempotent
|
// 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()
|
if (!product) return Promise.resolve()
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user