feat(medusa): Support deleting prices from a price list by product or variant (#1555)
This commit is contained in:
1 parent
ad9cfedf04
commit
fa031fd28b
16 files changed
+602
-78
No files matched your search
@@ -253,6 +253,28 @@ export const adminHandlers = [
|
||||
)
|
||||
}),
|
||||
|
||||
rest.delete("/admin/price-lists/:id/products/:product_id/prices", (req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
ids: [],
|
||||
object: "money-amount",
|
||||
deleted: true,
|
||||
})
|
||||
)
|
||||
}),
|
||||
|
||||
rest.delete("/admin/price-lists/:id/variants/:variant_id/prices", (req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
ids: [],
|
||||
object: "money-amount",
|
||||
deleted: true,
|
||||
})
|
||||
)
|
||||
}),
|
||||
|
||||
rest.post("/admin/return-reasons/", (req, res, ctx) => {
|
||||
const body = req.body as Record<string, any>
|
||||
return res(
|
||||
|
||||
@@ -6,12 +6,16 @@ import {
|
||||
AdminDeletePriceListPricesPricesReq,
|
||||
AdminPriceListDeleteRes,
|
||||
AdminPriceListDeleteBatchRes,
|
||||
AdminPriceListDeleteProductPricesRes,
|
||||
AdminPriceListDeleteVariantPricesRes,
|
||||
} from "@medusajs/medusa"
|
||||
import { Response } from "@medusajs/medusa-js"
|
||||
import { useMutation, UseMutationOptions, useQueryClient } from "react-query"
|
||||
import { useMedusa } from "../../../contexts/medusa"
|
||||
import { buildOptions } from "../../utils/buildOptions"
|
||||
import { adminPriceListKeys } from "./queries"
|
||||
import { adminProductKeys } from "../products"
|
||||
import { adminVariantKeys } from "../variants"
|
||||
|
||||
export const useAdminCreatePriceList = (
|
||||
options?: UseMutationOptions<
|
||||
@@ -118,3 +122,53 @@ export const useAdminDeletePriceListPrices = (
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
export const useAdminDeletePriceListProductPrices = (
|
||||
id: string,
|
||||
productId: string,
|
||||
options?: UseMutationOptions<
|
||||
Response<AdminPriceListDeleteProductPricesRes>,
|
||||
Error
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation(
|
||||
() => client.admin.priceLists.deleteProductPrices(id, productId),
|
||||
buildOptions(
|
||||
queryClient,
|
||||
[
|
||||
adminPriceListKeys.detail(id),
|
||||
adminPriceListKeys.lists(),
|
||||
adminProductKeys.detail(productId)
|
||||
],
|
||||
options
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
export const useAdminDeletePriceListVariantPrices = (
|
||||
id: string,
|
||||
variantId: string,
|
||||
options?: UseMutationOptions<
|
||||
Response<AdminPriceListDeleteVariantPricesRes>,
|
||||
Error
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation(
|
||||
() => client.admin.priceLists.deleteVariantPrices(id, variantId),
|
||||
buildOptions(
|
||||
queryClient,
|
||||
[
|
||||
adminPriceListKeys.detail(id),
|
||||
adminPriceListKeys.lists(),
|
||||
adminVariantKeys.detail(variantId)
|
||||
],
|
||||
options
|
||||
)
|
||||
)
|
||||
}
|
||||
@@ -21,7 +21,7 @@ export const adminPriceListKeys = {
|
||||
"products" as const,
|
||||
{ ...(query || {}) },
|
||||
] as const
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type PriceListQueryKeys = typeof adminPriceListKeys
|
||||
|
||||
@@ -3,6 +3,8 @@ import {
|
||||
useAdminCreatePriceListPrices,
|
||||
useAdminDeletePriceList,
|
||||
useAdminDeletePriceListPrices,
|
||||
useAdminDeletePriceListProductPrices,
|
||||
useAdminDeletePriceListVariantPrices,
|
||||
useAdminUpdatePriceList,
|
||||
} from "../../../../src"
|
||||
import { renderHook } from "@testing-library/react-hooks"
|
||||
@@ -137,3 +139,53 @@ describe("useAdminDeletePriceList hook", () => {
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("useAdminDeletePriceListProductPrices hook", () => {
|
||||
test("should delete prices from a price list for all the variants related to the specified product", async () => {
|
||||
const { result, waitFor } = renderHook(
|
||||
() => useAdminDeletePriceListProductPrices(
|
||||
fixtures.get("price_list").id,
|
||||
fixtures.get("product").id
|
||||
),
|
||||
{
|
||||
wrapper: createWrapper(),
|
||||
}
|
||||
)
|
||||
|
||||
result.current.mutate()
|
||||
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
expect(result.current.data.response.status).toEqual(200)
|
||||
expect(result.current.data).toEqual(expect.objectContaining({
|
||||
ids: [],
|
||||
object: "money-amount",
|
||||
deleted: true
|
||||
}))
|
||||
})
|
||||
})
|
||||
|
||||
describe("useAdminDeletePriceListVariantPrices hook", () => {
|
||||
test("should delete prices from a price list for the specified variant", async () => {
|
||||
const { result, waitFor } = renderHook(
|
||||
() => useAdminDeletePriceListVariantPrices(
|
||||
fixtures.get("price_list").id,
|
||||
fixtures.get("product_variant").id
|
||||
),
|
||||
{
|
||||
wrapper: createWrapper(),
|
||||
}
|
||||
)
|
||||
|
||||
result.current.mutate()
|
||||
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
expect(result.current.data.response.status).toEqual(200)
|
||||
expect(result.current.data).toEqual(expect.objectContaining({
|
||||
ids: [],
|
||||
object: "money-amount",
|
||||
deleted: true
|
||||
}))
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user