feat: Add support for category deletion (#7679)

This commit is contained in:
Stevche Radevski
2024-06-11 16:49:42 +02:00
committed by GitHub
parent dd0b9f0805
commit 0e731dbad0
8 changed files with 164 additions and 35 deletions
@@ -0,0 +1,28 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IProductModuleService } from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
export const deleteProductCategoryStepId = "delete-product-category"
export const deleteProductCategoryStep = createStep(
deleteProductCategoryStepId,
async (id: string, { container }) => {
const service = container.resolve<IProductModuleService>(
ModuleRegistrationName.PRODUCT
)
await service.deleteCategory(id)
return new StepResponse(void 0, id)
},
async (prevId, { container }) => {
if (!prevId) {
return
}
const service = container.resolve<IProductModuleService>(
ModuleRegistrationName.PRODUCT
)
// TODO: There is no soft delete support for categories yet
// await service.restoreCategory(prevId)
}
)
@@ -1 +1,3 @@
export * from "./create-product-category"
export * from "./update-product-category"
export * from "./delete-product-category"
@@ -0,0 +1,10 @@
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { deleteProductCategoryStep } from "../steps"
export const deleteProductCategoryWorkflowId = "delete-product-category"
export const deleteProductCategoryWorkflow = createWorkflow(
deleteProductCategoryWorkflowId,
(input: WorkflowData<string>) => {
return deleteProductCategoryStep(input)
}
)
@@ -1,2 +1,3 @@
export * from "./create-product-category"
export * from "./update-product-category"
export * from "./delete-product-category"
@@ -1,6 +1,6 @@
import { ProductCategoryWorkflow } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { updateProductCategoryStep } from "../steps/update-product-category"
import { updateProductCategoryStep } from "../steps"
type WorkflowInputData =
ProductCategoryWorkflow.UpdateProductCategoryWorkflowInput
@@ -1,4 +1,7 @@
import { updateProductCategoryWorkflow } from "@medusajs/core-flows"
import {
deleteProductCategoryWorkflow,
updateProductCategoryWorkflow,
} from "@medusajs/core-flows"
import { AdminProductCategoryResponse } from "@medusajs/types"
import {
AuthenticatedMedusaRequest,
@@ -9,6 +12,7 @@ import {
AdminProductCategoryParamsType,
AdminUpdateProductCategoryType,
} from "../validators"
import { MedusaError } from "@medusajs/utils"
export const GET = async (
req: AuthenticatedMedusaRequest<AdminProductCategoryParamsType>,
@@ -21,6 +25,13 @@ export const GET = async (
req.remoteQueryConfig.fields
)
if (!category) {
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
`Product category with id: ${req.params.id} was not found`
)
}
res.json({ product_category: category })
}
@@ -44,3 +55,20 @@ export const POST = async (
res.status(200).json({ product_category: category })
}
export const DELETE = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const id = req.params.id
await deleteProductCategoryWorkflow(req.scope).run({
input: id,
})
res.status(200).json({
id,
object: "product_category",
deleted: true,
})
}
@@ -53,6 +53,11 @@ export const adminProductCategoryRoutesMiddlewares: MiddlewareRoute[] = [
),
],
},
{
method: ["DELETE"],
matcher: "/admin/product-categories/:id",
middlewares: [],
},
{
method: ["POST"],
matcher: "/admin/product-categories/:id/products",