feat: Create product category flow (#7034)
* feat: Create product category * address PR comments
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||
import { validateAndTransformBody } from "../../utils/validate-body"
|
||||
import { validateAndTransformQuery } from "../../utils/validate-query"
|
||||
import * as QueryConfig from "./query-config"
|
||||
import {
|
||||
AdminCreateProductCategory,
|
||||
AdminProductCategoriesParams,
|
||||
AdminProductCategoryParams,
|
||||
} from "./validators"
|
||||
@@ -33,4 +35,15 @@ export const adminProductCategoryRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/product-categories",
|
||||
middlewares: [
|
||||
validateAndTransformBody(AdminCreateProductCategory),
|
||||
validateAndTransformQuery(
|
||||
AdminProductCategoryParams,
|
||||
QueryConfig.retrieveProductCategoryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
@@ -11,10 +11,24 @@ export const defaults = [
|
||||
"updated_at",
|
||||
"metadata",
|
||||
|
||||
"parent_category.id",
|
||||
"parent_category.name",
|
||||
"category_children.id",
|
||||
"category_children.name",
|
||||
"*category_children",
|
||||
]
|
||||
|
||||
export const allowed = [
|
||||
"id",
|
||||
"name",
|
||||
"description",
|
||||
"handle",
|
||||
"is_active",
|
||||
"is_internal",
|
||||
"rank",
|
||||
"parent_category_id",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"metadata",
|
||||
|
||||
"*parent_category",
|
||||
"*category_children",
|
||||
]
|
||||
|
||||
export const retrieveProductCategoryConfig = {
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
import { AdminProductCategoryListResponse } from "@medusajs/types"
|
||||
import { createProductCategoryWorkflow } from "@medusajs/core-flows"
|
||||
import {
|
||||
AdminProductCategoryListResponse,
|
||||
AdminProductCategoryResponse,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
remoteQueryObjectFromString,
|
||||
@@ -7,7 +11,10 @@ import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../types/routing"
|
||||
import { AdminProductCategoriesParamsType } from "./validators"
|
||||
import {
|
||||
AdminCreateProductCategoryType,
|
||||
AdminProductCategoriesParamsType,
|
||||
} from "./validators"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest<AdminProductCategoriesParamsType>,
|
||||
@@ -33,3 +40,33 @@ export const GET = async (
|
||||
limit: metadata.take,
|
||||
})
|
||||
}
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminCreateProductCategoryType>,
|
||||
res: MedusaResponse<AdminProductCategoryResponse>
|
||||
) => {
|
||||
const { result, errors } = await createProductCategoryWorkflow(req.scope).run(
|
||||
{
|
||||
input: { product_category: req.validatedBody },
|
||||
throwOnError: false,
|
||||
}
|
||||
)
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "product_category",
|
||||
variables: {
|
||||
filters: { id: result.id },
|
||||
},
|
||||
fields: req.remoteQueryConfig.fields,
|
||||
})
|
||||
|
||||
const [product_category] = await remoteQuery(queryObject)
|
||||
|
||||
res.status(200).json({ product_category })
|
||||
}
|
||||
|
||||
@@ -43,6 +43,14 @@ export const AdminProductCategoriesParams = createFindParams({
|
||||
(val: any) => optionalBooleanMapper.get(val?.toLowerCase()),
|
||||
z.boolean().optional()
|
||||
),
|
||||
is_internal: z.preprocess(
|
||||
(val: any) => optionalBooleanMapper.get(val?.toLowerCase()),
|
||||
z.boolean().optional()
|
||||
),
|
||||
is_active: z.preprocess(
|
||||
(val: any) => optionalBooleanMapper.get(val?.toLowerCase()),
|
||||
z.boolean().optional()
|
||||
),
|
||||
created_at: createOperatorMap().optional(),
|
||||
updated_at: createOperatorMap().optional(),
|
||||
deleted_at: createOperatorMap().optional(),
|
||||
@@ -50,3 +58,19 @@ export const AdminProductCategoriesParams = createFindParams({
|
||||
$or: z.lazy(() => AdminProductCategoriesParams.array()).optional(),
|
||||
})
|
||||
)
|
||||
|
||||
export const AdminCreateProductCategory = z
|
||||
.object({
|
||||
name: z.string(),
|
||||
description: z.string().optional(),
|
||||
handle: z.string().optional(),
|
||||
is_internal: z.boolean().optional(),
|
||||
is_active: z.boolean().optional(),
|
||||
parent_category_id: z.string().optional(),
|
||||
metadata: z.record(z.unknown()).optional(),
|
||||
})
|
||||
.strict()
|
||||
|
||||
export type AdminCreateProductCategoryType = z.infer<
|
||||
typeof AdminCreateProductCategory
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user