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
+45
-6
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)
|
||||||
|
|
||||||
@@ -105,7 +106,7 @@ describe("/admin/products", () => {
|
|||||||
status: "proposed",
|
status: "proposed",
|
||||||
}
|
}
|
||||||
|
|
||||||
//update test-product status to proposed
|
// update test-product status to proposed
|
||||||
await api
|
await api
|
||||||
.post("/admin/products/test-product", payload, {
|
.post("/admin/products/test-product", payload, {
|
||||||
headers: {
|
headers: {
|
||||||
@@ -423,7 +424,7 @@ describe("/admin/products", () => {
|
|||||||
],
|
],
|
||||||
variants: [
|
variants: [
|
||||||
{
|
{
|
||||||
id: "test-variant", //expect.stringMatching(/^test-variant*/),
|
id: "test-variant", // expect.stringMatching(/^test-variant*/),
|
||||||
created_at: expect.any(String),
|
created_at: expect.any(String),
|
||||||
updated_at: expect.any(String),
|
updated_at: expect.any(String),
|
||||||
product_id: expect.stringMatching(/^test-*/),
|
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),
|
created_at: expect.any(String),
|
||||||
updated_at: expect.any(String),
|
updated_at: expect.any(String),
|
||||||
product_id: expect.stringMatching(/^test-*/),
|
product_id: expect.stringMatching(/^test-*/),
|
||||||
@@ -519,7 +520,7 @@ describe("/admin/products", () => {
|
|||||||
options: [],
|
options: [],
|
||||||
variants: [
|
variants: [
|
||||||
{
|
{
|
||||||
id: "test-variant_4", //expect.stringMatching(/^test-variant*/),
|
id: "test-variant_4", // expect.stringMatching(/^test-variant*/),
|
||||||
created_at: expect.any(String),
|
created_at: expect.any(String),
|
||||||
updated_at: expect.any(String),
|
updated_at: expect.any(String),
|
||||||
product_id: expect.stringMatching(/^test-*/),
|
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),
|
created_at: expect.any(String),
|
||||||
updated_at: expect.any(String),
|
updated_at: expect.any(String),
|
||||||
product_id: expect.stringMatching(/^test-*/),
|
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 () => {
|
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