feat: V2 batch update products in categories (#7125)
* wip * wip * fix: batch category update * update tet * fix type --------- Co-authored-by: Stevche Radevski <sradevski@live.com>
This commit is contained in:
co-authored by
Stevche Radevski
parent
2446151420
commit
10e120062b
@@ -0,0 +1,35 @@
|
||||
import { batchLinkProductsToCategoryWorkflow } from "@medusajs/core-flows"
|
||||
import {
|
||||
AdminProductCategoryResponse,
|
||||
LinkMethodRequest,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../types/routing"
|
||||
import { refetchCategory } from "../../helpers"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<LinkMethodRequest>,
|
||||
res: MedusaResponse<AdminProductCategoryResponse>
|
||||
) => {
|
||||
const { id } = req.params
|
||||
|
||||
const { errors } = await batchLinkProductsToCategoryWorkflow(req.scope).run({
|
||||
input: { id, ...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 })
|
||||
}
|
||||
@@ -2,6 +2,7 @@ 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 { createLinkBody } from "../../utils/validators"
|
||||
import * as QueryConfig from "./query-config"
|
||||
import {
|
||||
AdminCreateProductCategory,
|
||||
@@ -58,4 +59,15 @@ export const adminProductCategoryRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/product-categories/:id/products",
|
||||
middlewares: [
|
||||
validateAndTransformBody(createLinkBody()),
|
||||
validateAndTransformQuery(
|
||||
AdminProductCategoryParams,
|
||||
QueryConfig.retrieveProductCategoryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
@@ -194,6 +194,10 @@ export const AdminBatchUpdateProductVariant = AdminUpdateProductVariant.extend({
|
||||
id: z.string(),
|
||||
})
|
||||
|
||||
export const AdminCreateProductProductCategory = z.object({
|
||||
id: z.string(),
|
||||
})
|
||||
|
||||
export type AdminCreateProductType = z.infer<typeof AdminCreateProduct>
|
||||
export const AdminCreateProduct = z
|
||||
.object({
|
||||
@@ -208,6 +212,7 @@ export const AdminCreateProduct = z
|
||||
status: statusEnum.optional().default(ProductStatus.DRAFT),
|
||||
type_id: z.string().nullable().optional(),
|
||||
collection_id: z.string().nullable().optional(),
|
||||
categories: z.array(AdminCreateProductProductCategory).optional(),
|
||||
tags: z.array(AdminUpdateProductTag).optional(),
|
||||
options: z.array(AdminCreateProductOption).optional(),
|
||||
variants: z.array(AdminCreateProductVariant).optional(),
|
||||
|
||||
Reference in New Issue
Block a user