feat: Update product category (#7092)
* feat: Update product category * Disable V2 FF in product category tes * clean up
This commit is contained in:
1 parent
5c71e8304d
commit
67a21c3e45
15 files changed
+383
-110
No files matched your search
@@ -1,10 +1,14 @@
|
||||
import { updateProductCategoryWorkflow } from "@medusajs/core-flows"
|
||||
import { AdminProductCategoryResponse } from "@medusajs/types"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../types/routing"
|
||||
import { AdminProductCategoryParamsType } from "../validators"
|
||||
import { refetchCategory } from "../helpers"
|
||||
import {
|
||||
AdminProductCategoryParamsType,
|
||||
AdminUpdateProductCategoryType,
|
||||
} from "../validators"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest<AdminProductCategoryParamsType>,
|
||||
@@ -13,8 +17,35 @@ export const GET = async (
|
||||
const category = await refetchCategory(
|
||||
req.params.id,
|
||||
req.scope,
|
||||
req.remoteQueryConfig.fields
|
||||
req.remoteQueryConfig.fields,
|
||||
req.filterableFields
|
||||
)
|
||||
|
||||
res.json({ product_category: category })
|
||||
}
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminUpdateProductCategoryType>,
|
||||
res: MedusaResponse<AdminProductCategoryResponse>
|
||||
) => {
|
||||
const { id } = req.params
|
||||
|
||||
// TODO: Should be converted to bulk update
|
||||
const { errors } = await updateProductCategoryWorkflow(req.scope).run({
|
||||
input: { id, data: req.validatedBody },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const category = await refetchCategory(
|
||||
req.params.id,
|
||||
req.scope,
|
||||
req.remoteQueryConfig.fields,
|
||||
req.filterableFields
|
||||
)
|
||||
|
||||
res.status(200).json({ product_category: category })
|
||||
}
|
||||
@@ -7,13 +7,14 @@ import {
|
||||
export const refetchCategory = async (
|
||||
categoryId: string,
|
||||
scope: MedusaContainer,
|
||||
fields: string[]
|
||||
fields: string[],
|
||||
filterableFields: Record<string, any> = {}
|
||||
) => {
|
||||
const remoteQuery = scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "product_category",
|
||||
variables: {
|
||||
filters: { id: categoryId },
|
||||
filters: { ...filterableFields, id: categoryId },
|
||||
},
|
||||
fields: fields,
|
||||
})
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
AdminCreateProductCategory,
|
||||
AdminProductCategoriesParams,
|
||||
AdminProductCategoryParams,
|
||||
AdminUpdateProductCategory,
|
||||
} from "./validators"
|
||||
|
||||
export const adminProductCategoryRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
@@ -46,4 +47,15 @@ export const adminProductCategoryRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/product-categories/:id",
|
||||
middlewares: [
|
||||
validateAndTransformBody(AdminUpdateProductCategory),
|
||||
validateAndTransformQuery(
|
||||
AdminProductCategoryParams,
|
||||
QueryConfig.retrieveProductCategoryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
]
|
||||
@@ -11,11 +11,11 @@ import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../types/routing"
|
||||
import { refetchCategory } from "./helpers"
|
||||
import {
|
||||
AdminCreateProductCategoryType,
|
||||
AdminProductCategoriesParamsType,
|
||||
} from "./validators"
|
||||
import { refetchCategory } from "./helpers"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest<AdminProductCategoriesParamsType>,
|
||||
@@ -60,7 +60,8 @@ export const POST = async (
|
||||
const category = await refetchCategory(
|
||||
result.id,
|
||||
req.scope,
|
||||
req.remoteQueryConfig.fields
|
||||
req.remoteQueryConfig.fields,
|
||||
req.filterableFields
|
||||
)
|
||||
|
||||
res.status(200).json({ product_category: category })
|
||||
|
||||
@@ -68,9 +68,27 @@ export const AdminCreateProductCategory = z
|
||||
is_active: z.boolean().optional(),
|
||||
parent_category_id: z.string().optional(),
|
||||
metadata: z.record(z.unknown()).optional(),
|
||||
rank: z.number().nonnegative().optional(),
|
||||
})
|
||||
.strict()
|
||||
|
||||
export type AdminCreateProductCategoryType = z.infer<
|
||||
typeof AdminCreateProductCategory
|
||||
>
|
||||
|
||||
export const AdminUpdateProductCategory = z
|
||||
.object({
|
||||
name: z.string().optional(),
|
||||
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(),
|
||||
rank: z.number().nonnegative().optional(),
|
||||
})
|
||||
.strict()
|
||||
|
||||
export type AdminUpdateProductCategoryType = z.infer<
|
||||
typeof AdminUpdateProductCategory
|
||||
>
|
||||
@@ -1,12 +1,13 @@
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../types/routing"
|
||||
import {
|
||||
deleteProductsWorkflow,
|
||||
updateProductsWorkflow,
|
||||
} from "@medusajs/core-flows"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../types/routing"
|
||||
|
||||
import { UpdateProductDTO } from "@medusajs/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
remoteQueryObjectFromString,
|
||||
@@ -17,7 +18,6 @@ import {
|
||||
remapProductResponse,
|
||||
} from "../helpers"
|
||||
import { AdminUpdateProductType } from "../validators"
|
||||
import { UpdateProductDTO } from "@medusajs/types"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { selectorConstraintsToString } from "@medusajs/utils"
|
||||
import { isDefined, MedusaError } from "medusa-core-utils"
|
||||
import { Between, EntityManager, MoreThanOrEqual, Not } from "typeorm"
|
||||
import { EventBusService } from "."
|
||||
@@ -17,7 +18,6 @@ import {
|
||||
UpdateProductCategoryInput,
|
||||
} from "../types/product-category"
|
||||
import { buildQuery, nullableValue, setMetadata } from "../utils"
|
||||
import {selectorConstraintsToString} from "@medusajs/utils";
|
||||
|
||||
type InjectedDependencies = {
|
||||
manager: EntityManager
|
||||
|
||||
Reference in new issue
Block a user