Skip to content

Commit 40625c8

Browse files
authored
fix(core-flows): properly delete variant inventory item (#12958)
* fix(core-flows): properly delete variant inventory item * fix: rm unused code
1 parent 7669dbb commit 40625c8

File tree

4 files changed

+127
-29
lines changed

4 files changed

+127
-29
lines changed

.changeset/cuddly-dragons-attend.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/core-flows": patch
3+
---
4+
5+
fix(core-flows): correctly delete related inventory item when deleting variant

integration-tests/http/__tests__/product/admin/product.spec.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2159,6 +2159,99 @@ medusaIntegrationTestRunner({
21592159
})
21602160
})
21612161

2162+
it("should recreate a variant with the same sku after deletion", async () => {
2163+
const payload = {
2164+
title: "Test sku",
2165+
shipping_profile_id: shippingProfile.id,
2166+
variants: [
2167+
{
2168+
sku: "test-sku-should-persist",
2169+
manage_inventory: true,
2170+
title: "Recreate variant",
2171+
prices: [
2172+
{
2173+
currency_code: "usd",
2174+
amount: 100,
2175+
},
2176+
],
2177+
},
2178+
],
2179+
}
2180+
2181+
const created = (
2182+
await api.post(
2183+
"/admin/products?fields=*variants.inventory_items.inventory",
2184+
getProductFixture(payload),
2185+
adminHeaders
2186+
)
2187+
).data.product
2188+
2189+
const createdVariant = created.variants.find(
2190+
(v) => v.sku === "test-sku-should-persist"
2191+
)
2192+
2193+
const inventoryItem = createdVariant.inventory_items[0]
2194+
2195+
expect(created).toEqual(
2196+
expect.objectContaining({
2197+
variants: expect.arrayContaining([
2198+
expect.objectContaining({
2199+
sku: "test-sku-should-persist",
2200+
inventory_items: expect.arrayContaining([
2201+
expect.objectContaining({
2202+
inventory: expect.objectContaining({
2203+
sku: "test-sku-should-persist",
2204+
}),
2205+
}),
2206+
]),
2207+
title: "Recreate variant",
2208+
}),
2209+
]),
2210+
})
2211+
)
2212+
2213+
await api.delete(
2214+
`/admin/products/${created.id}/variants/${createdVariant.id}`,
2215+
adminHeaders
2216+
)
2217+
2218+
const recreated = (
2219+
await api.post(
2220+
`/admin/products/${created.id}/variants?fields=+variants.inventory_items.inventory.sku`,
2221+
{
2222+
sku: "test-sku-should-persist",
2223+
manage_inventory: true,
2224+
title: "Recreate variant",
2225+
prices: [
2226+
{
2227+
currency_code: "usd",
2228+
amount: 100,
2229+
},
2230+
],
2231+
},
2232+
adminHeaders
2233+
)
2234+
).data.product
2235+
2236+
expect(recreated).toEqual(
2237+
expect.objectContaining({
2238+
variants: expect.arrayContaining([
2239+
expect.objectContaining({
2240+
sku: "test-sku-should-persist",
2241+
inventory_items: expect.arrayContaining([
2242+
expect.objectContaining({
2243+
inventory: expect.objectContaining({
2244+
sku: "test-sku-should-persist",
2245+
}),
2246+
}),
2247+
]),
2248+
title: "Recreate variant",
2249+
}),
2250+
]),
2251+
})
2252+
)
2253+
})
2254+
21622255
it("updates products sales channels", async () => {
21632256
const salesChannel1 = (
21642257
await api.post(

packages/core/core-flows/src/product/workflows/create-product-variants.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ import { createProductVariantsStep } from "../steps/create-product-variants"
2626
import { createVariantPricingLinkStep } from "../steps/create-variant-pricing-link"
2727

2828
/**
29-
*
29+
*
3030
* The data to create one or more product variants, along with custom data that's passed to the workflow's hooks.
31-
*
31+
*
3232
* @privateRemarks
3333
* TODO: Create separate typings for the workflow input
3434
*/
@@ -51,9 +51,9 @@ export type CreateProductVariantsWorkflowInput = {
5151
*/
5252
inventory_item_id: string
5353
/**
54-
* The number of units a single quantity is equivalent to. For example, if a customer orders one quantity of the variant,
55-
* Medusa checks the availability of the quantity multiplied by the value set for `required_quantity`.
56-
* When the customer orders the quantity, Medusa reserves the ordered quantity multiplied by the value
54+
* The number of units a single quantity is equivalent to. For example, if a customer orders one quantity of the variant,
55+
* Medusa checks the availability of the quantity multiplied by the value set for `required_quantity`.
56+
* When the customer orders the quantity, Medusa reserves the ordered quantity multiplied by the value
5757
* set for `required_quantity`.
5858
*/
5959
required_quantity?: number
@@ -201,19 +201,19 @@ const buildVariantItemCreateMap = (data: {
201201
export const createProductVariantsWorkflowId = "create-product-variants"
202202
/**
203203
* This workflow creates one or more product variants. It's used by the [Create Product Variant Admin API Route](https://docs.medusajs.com/api/admin#products_postproductsidvariants).
204-
*
205-
* This workflow has a hook that allows you to perform custom actions on the created product variants. For example, you can pass under `additional_data` custom data that
204+
*
205+
* This workflow has a hook that allows you to perform custom actions on the created product variants. For example, you can pass under `additional_data` custom data that
206206
* allows you to create custom data models linked to the product variants.
207-
*
207+
*
208208
* You can also use this workflow within your customizations or your own custom workflows, allowing you to wrap custom logic around product-variant creation.
209-
*
209+
*
210210
* :::note
211-
*
212-
* Learn more about adding rules to the product variant's prices in the Pricing Module's
211+
*
212+
* Learn more about adding rules to the product variant's prices in the Pricing Module's
213213
* [Price Rules](https://docs.medusajs.com/resources/commerce-modules/pricing/price-rules) documentation.
214-
*
214+
*
215215
* :::
216-
*
216+
*
217217
* @example
218218
* const { result } = await createProductVariantsWorkflow(container)
219219
* .run({
@@ -239,11 +239,11 @@ export const createProductVariantsWorkflowId = "create-product-variants"
239239
* }
240240
* }
241241
* })
242-
*
242+
*
243243
* @summary
244-
*
244+
*
245245
* Create one or more product variants.
246-
*
246+
*
247247
* @property hooks.productVariantsCreated - This hook is executed after the product variants are created. You can consume this hook to perform custom actions on the created product variants.
248248
*/
249249
export const createProductVariantsWorkflow = createWorkflow(

packages/core/core-flows/src/product/workflows/delete-product-variants.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { deleteInventoryItemWorkflow } from "../../inventory"
2020
/**
2121
* The data to delete one or more product variants.
2222
*/
23-
export type DeleteProductVariantsWorkflowInput = {
23+
export type DeleteProductVariantsWorkflowInput = {
2424
/**
2525
* The IDs of the variants to delete.
2626
*/
@@ -29,35 +29,31 @@ export type DeleteProductVariantsWorkflowInput = {
2929

3030
export const deleteProductVariantsWorkflowId = "delete-product-variants"
3131
/**
32-
* This workflow deletes one or more product variants. It's used by the
32+
* This workflow deletes one or more product variants. It's used by the
3333
* [Delete Product Variants Admin API Route](https://docs.medusajs.com/api/admin#products_deleteproductsidvariantsvariant_id).
34-
*
35-
* This workflow has a hook that allows you to perform custom actions after the product variants are deleted. For example,
34+
*
35+
* This workflow has a hook that allows you to perform custom actions after the product variants are deleted. For example,
3636
* you can delete custom records linked to the product variants.
37-
*
37+
*
3838
* You can also use this workflow within your own custom workflows, allowing you to wrap custom logic around product-variant deletion.
39-
*
39+
*
4040
* @example
4141
* const { result } = await deleteProductVariantsWorkflow(container)
4242
* .run({
4343
* input: {
4444
* ids: ["variant_123"],
4545
* }
4646
* })
47-
*
47+
*
4848
* @summary
49-
*
49+
*
5050
* Delete one or more product variants.
51-
*
51+
*
5252
* @property hooks.productVariantsDeleted - This hook is executed after the variants are deleted. You can consume this hook to perform custom actions on the deleted variants.
5353
*/
5454
export const deleteProductVariantsWorkflow = createWorkflow(
5555
deleteProductVariantsWorkflowId,
5656
(input: WorkflowData<DeleteProductVariantsWorkflowInput>) => {
57-
removeRemoteLinkStep({
58-
[Modules.PRODUCT]: { variant_id: input.ids },
59-
}).config({ name: "remove-variant-link-step" })
60-
6157
const variantsWithInventoryStepResponse = useQueryGraphStep({
6258
entity: "variants",
6359
fields: [
@@ -71,6 +67,10 @@ export const deleteProductVariantsWorkflow = createWorkflow(
7167
},
7268
})
7369

70+
removeRemoteLinkStep({
71+
[Modules.PRODUCT]: { variant_id: input.ids },
72+
}).config({ name: "remove-variant-link-step" })
73+
7474
const toDeleteInventoryItemIds = transform(
7575
{ variants: variantsWithInventoryStepResponse.data },
7676
(data) => {

0 commit comments

Comments
 (0)