fix(medusa): Delete ProductOption on Product without Variants (#1846)
**What** Solves admin issue [166](https://github.com/medusajs/admin/issues/166) Deleting a product option on a product without variants currently throws, because we are cleaning up variant options as well. **How** Only do variant clean up, if product has variants
This commit is contained in:
1 parent
37056b8066
commit
d14a0398fb
3 files changed
+63
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fixes an error thrown when deleting product options on a product without variants
|
||||||
@@ -12,6 +12,7 @@ const {
|
|||||||
MoneyAmount,
|
MoneyAmount,
|
||||||
} = require("@medusajs/medusa")
|
} = require("@medusajs/medusa")
|
||||||
const priceListSeeder = require("../../helpers/price-list-seeder")
|
const priceListSeeder = require("../../helpers/price-list-seeder")
|
||||||
|
const { simpleProductFactory } = require("../../factories")
|
||||||
|
|
||||||
jest.setTimeout(50000)
|
jest.setTimeout(50000)
|
||||||
|
|
||||||
@@ -1433,6 +1434,58 @@ describe("/admin/products", () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("DELETE /admin/products/:id/options/:option_id", () => {
|
||||||
|
beforeEach(async () => {
|
||||||
|
try {
|
||||||
|
await simpleProductFactory(dbConnection, {
|
||||||
|
id: "test-product-without-variants",
|
||||||
|
variants: [],
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
id: "test-product-option",
|
||||||
|
title: "Test option",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
await adminSeeder(dbConnection)
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err)
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
const db = useDb()
|
||||||
|
await db.teardown()
|
||||||
|
})
|
||||||
|
|
||||||
|
it("deletes a product option", async () => {
|
||||||
|
const api = useApi()
|
||||||
|
|
||||||
|
const response = await api
|
||||||
|
.delete(
|
||||||
|
"/admin/products/test-product-without-variants/options/test-product-option",
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: "Bearer test_token",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.catch((err) => {
|
||||||
|
console.log(err)
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(response.status).toEqual(200)
|
||||||
|
expect(response.data.product).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
options: [],
|
||||||
|
id: "test-product-without-variants",
|
||||||
|
variants: [],
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe("GET /admin/products/:id/variants", () => {
|
describe("GET /admin/products/:id/variants", () => {
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
await productSeeder(dbConnection)
|
await productSeeder(dbConnection)
|
||||||
|
|||||||
@@ -818,6 +818,10 @@ class ProductService extends TransactionBaseService {
|
|||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// In case the product does not contain variants, we can safely delete the option
|
||||||
|
// If it does contain variants, we need to make sure no variant exist for the
|
||||||
|
// product option to delete
|
||||||
|
if (product?.variants?.length) {
|
||||||
// For the option we want to delete, make sure that all variants have the
|
// For the option we want to delete, make sure that all variants have the
|
||||||
// same option values. The reason for doing is, that we want to avoid
|
// same option values. The reason for doing is, that we want to avoid
|
||||||
// duplicate variants. For example, if we have a product with size and
|
// duplicate variants. For example, if we have a product with size and
|
||||||
@@ -845,6 +849,7 @@ class ProductService extends TransactionBaseService {
|
|||||||
`To delete an option, first delete all variants, such that when an option is deleted, no duplicate variants will exist.`
|
`To delete an option, first delete all variants, such that when an option is deleted, no duplicate variants will exist.`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// If we reach this point, we can safely delete the product option
|
// If we reach this point, we can safely delete the product option
|
||||||
await productOptionRepo.softRemove(productOption)
|
await productOptionRepo.softRemove(productOption)
|
||||||
|
|||||||
Reference in new issue
Block a user