feat(medusa): Nested Categories Admin Delete Endpoint (#2975)
**What:** Introduces an admin endpoint that allows a user to delete a product category, given an ID. Why: This is part of a greater goal of allowing products to be added to multiple categories. How: - Creates a route on the admin scope to delete category - Creates a method in product category services to delete a category RESOLVES CORE-957
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
import { Request, Response } from "express"
|
||||
import { EntityManager } from "typeorm"
|
||||
|
||||
import { ProductCategoryService } from "../../../../services"
|
||||
|
||||
/**
|
||||
* @oas [delete] /product-categories/{id}
|
||||
* operationId: "DeleteProductCategoriesCategory"
|
||||
* summary: "Delete a Product Category"
|
||||
* description: "Deletes a ProductCategory."
|
||||
* x-authenticated: true
|
||||
* parameters:
|
||||
* - (path) id=* {string} The ID of the Product Category
|
||||
* x-codeSamples:
|
||||
* - lang: JavaScript
|
||||
* label: JS Client
|
||||
* source: |
|
||||
* import Medusa from "@medusajs/medusa-js"
|
||||
* const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
|
||||
* // must be previously logged in or use api token
|
||||
* medusa.admin.productCategories.delete(product_category_id)
|
||||
* .then(({ id, object, deleted }) => {
|
||||
* console.log(id);
|
||||
* });
|
||||
* - lang: Shell
|
||||
* label: cURL
|
||||
* source: |
|
||||
* curl --location --request DELETE 'https://medusa-url.com/admin/product-categories/{id}' \
|
||||
* --header 'Authorization: Bearer {api_token}'
|
||||
* security:
|
||||
* - api_token: []
|
||||
* - cookie_auth: []
|
||||
* tags:
|
||||
* - Product Category
|
||||
* responses:
|
||||
* 200:
|
||||
* description: OK
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* id:
|
||||
* type: string
|
||||
* description: The ID of the deleted product category.
|
||||
* object:
|
||||
* type: string
|
||||
* description: The type of the object that was deleted.
|
||||
* default: product_category
|
||||
* deleted:
|
||||
* type: boolean
|
||||
* description: Whether the product category was deleted successfully or not.
|
||||
* default: true
|
||||
* "400":
|
||||
* $ref: "#/components/responses/400_error"
|
||||
* "401":
|
||||
* $ref: "#/components/responses/unauthorized"
|
||||
* "404":
|
||||
* $ref: "#/components/responses/not_found_error"
|
||||
* "409":
|
||||
* $ref: "#/components/responses/invalid_state_error"
|
||||
* "422":
|
||||
* $ref: "#/components/responses/invalid_request_error"
|
||||
* "500":
|
||||
* $ref: "#/components/responses/500_error"
|
||||
*/
|
||||
|
||||
export default async (req: Request, res: Response) => {
|
||||
const { id } = req.params
|
||||
|
||||
const productCategoryService: ProductCategoryService = req.scope.resolve(
|
||||
"productCategoryService"
|
||||
)
|
||||
|
||||
const manager: EntityManager = req.scope.resolve("manager")
|
||||
|
||||
await manager.transaction(async (transactionManager) => {
|
||||
return await productCategoryService
|
||||
.withTransaction(transactionManager)
|
||||
.delete(id)
|
||||
})
|
||||
|
||||
res.json({
|
||||
id: id,
|
||||
object: "product_category",
|
||||
deleted: true,
|
||||
})
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
import { Router } from "express"
|
||||
|
||||
import middlewares, { transformQuery } from "../../../middlewares"
|
||||
import { isFeatureFlagEnabled } from "../../../middlewares/feature-flag-enabled"
|
||||
import deleteProductCategory from "./delete-product-category"
|
||||
|
||||
import getProductCategory, {
|
||||
AdminGetProductCategoryParams,
|
||||
@@ -38,10 +40,13 @@ export default (app) => {
|
||||
middlewares.wrap(getProductCategory)
|
||||
)
|
||||
|
||||
route.delete("/:id", middlewares.wrap(deleteProductCategory))
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
export * from "./get-product-category"
|
||||
export * from "./delete-product-category"
|
||||
export * from "./list-product-categories"
|
||||
|
||||
export const defaultAdminProductCategoryRelations = [
|
||||
|
||||
Reference in New Issue
Block a user