feat(medusa): added admin create endpoint for nested categories (#2985)

What:

Introduces an admin endpoint that allows a user to create a product category

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 create category
- Creates a method in product category services to create a category

RESOLVES CORE-958
This commit is contained in:
Riqwan Thamir
2023-01-11 14:29:02 +01:00
committed by GitHub
parent 39c3513b2c
commit 8ed4eab73a
8 changed files with 297 additions and 7 deletions

View File

@@ -0,0 +1,126 @@
import { IsNotEmpty, IsOptional, IsString, IsBoolean } from "class-validator"
import { Request, Response } from "express"
import { EntityManager } from "typeorm"
import { ProductCategoryService } from "../../../../services"
import { AdminProductCategoriesReqBase } from "../../../../types/product-category"
import { FindParams } from "../../../../types/common"
/**
* @oas [post] /product-categories
* operationId: "PostProductCategories"
* summary: "Create a Product Category"
* description: "Creates a Product Category."
* x-authenticated: true
* parameters:
* - (query) expand {string} (Comma separated) Which fields should be expanded in each product category.
* - (query) fields {string} (Comma separated) Which fields should be retrieved in each product category.
* requestBody:
* content:
* application/json:
* schema:
* $ref: "#/components/schemas/AdminPostProductCategoriesReq"
* 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.create({
* name: 'Jeans',
* })
* .then(({ productCategory }) => {
* console.log(productCategory.id);
* });
* - lang: Shell
* label: cURL
* source: |
* curl --location --request POST 'https://medusa-url.com/admin/product-categories' \
* --header 'Authorization: Bearer {api_token}' \
* --header 'Content-Type: application/json' \
* --data-raw '{
* "name": "Jeans",
* }'
* security:
* - api_token: []
* - cookie_auth: []
* tags:
* - Product Category
* responses:
* "200":
* description: OK
* content:
* application/json:
* schema:
* type: object
* properties:
* productCategory:
* $ref: "#/components/schemas/ProductCategory"
* "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 { validatedBody } = req as {
validatedBody: AdminPostProductCategoriesReq
}
const productCategoryService: ProductCategoryService = req.scope.resolve(
"productCategoryService"
)
const manager: EntityManager = req.scope.resolve("manager")
const created = await manager.transaction(async (transactionManager) => {
return await productCategoryService
.withTransaction(transactionManager)
.create(validatedBody)
})
const productCategory = await productCategoryService.retrieve(
created.id,
req.retrieveConfig,
)
res.status(200).json({ product_category: productCategory })
}
/**
* @schema AdminPostProductCategoriesReq
* type: object
* required:
* - name
* properties:
* name:
* type: string
* description: The name to identify the Product Category by.
* handle:
* type: string
* description: An optional handle to be used in slugs, if none is provided we will kebab-case the title.
* is_internal:
* type: boolean
* description: A flag to make product category an internal category for admins
* is_active:
* type: boolean
* description: A flag to make product category visible/hidden in the store front
* parent_category_id:
* type: string
* description: The ID of the parent product category
*/
// eslint-disable-next-line max-len
export class AdminPostProductCategoriesReq extends AdminProductCategoriesReqBase {
@IsString()
@IsNotEmpty()
name: string
}
export class AdminPostProductCategoriesParams extends FindParams {}

View File

@@ -1,6 +1,6 @@
import { Router } from "express"
import middlewares, { transformQuery } from "../../../middlewares"
import middlewares, { transformQuery, transformBody } from "../../../middlewares"
import { isFeatureFlagEnabled } from "../../../middlewares/feature-flag-enabled"
import deleteProductCategory from "./delete-product-category"
@@ -12,6 +12,11 @@ import listProductCategories, {
AdminGetProductCategoriesParams,
} from "./list-product-categories"
import createProductCategory, {
AdminPostProductCategoriesReq,
AdminPostProductCategoriesParams,
} from "./create-product-category"
const route = Router()
export default (app) => {
@@ -21,6 +26,17 @@ export default (app) => {
route
)
route.post(
"/",
transformQuery(AdminPostProductCategoriesParams, {
defaultFields: defaultProductCategoryFields,
defaultRelations: defaultAdminProductCategoryRelations,
isList: false,
}),
transformBody(AdminPostProductCategoriesReq),
middlewares.wrap(createProductCategory)
)
route.get(
"/",
transformQuery(AdminGetProductCategoriesParams, {
@@ -48,6 +64,7 @@ export default (app) => {
export * from "./get-product-category"
export * from "./delete-product-category"
export * from "./list-product-categories"
export * from "./create-product-category"
export const defaultAdminProductCategoryRelations = [
"parent_category",
@@ -60,4 +77,6 @@ export const defaultProductCategoryFields = [
"handle",
"is_active",
"is_internal",
"created_at",
"updated_at",
]