feat(medusa): Products can be added to categories in batch request (#3123)
* wip * chore: fix issues with join table * chore: fix issues * chore: fix ordering issue on random failing test * chore: revert table name * chore: added oas for category * chore: update categories for a product * chore: add remove category test * chore: added changeset * chore: address review comments * chore: Products can be added to categories in batch request * chore: address review comments + add unit specs * chore: make template optional
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
import { IsArray, ValidateNested } from "class-validator"
|
||||
import { Request, Response } from "express"
|
||||
|
||||
import { EntityManager } from "typeorm"
|
||||
import { ProductBatchProductCategory } from "../../../../types/product-category"
|
||||
import { ProductCategoryService } from "../../../../services"
|
||||
import { Type } from "class-transformer"
|
||||
import { FindParams } from "../../../../types/common"
|
||||
|
||||
/**
|
||||
* @oas [post] /product-categories/{id}/products/batch
|
||||
* operationId: "PostProductCategoriesCategoryProductsBatch"
|
||||
* summary: "Add Products to a category"
|
||||
* description: "Assign a batch of products to a product category."
|
||||
* x-authenticated: true
|
||||
* parameters:
|
||||
* - (path) id=* {string} The ID of the Product Category.
|
||||
* - (query) expand {string} (Comma separated) Category fields to be expanded in the response.
|
||||
* - (query) fields {string} (Comma separated) Category fields to be retrieved in the response.
|
||||
* requestBody:
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: "#/components/schemas/AdminPostProductCategoriesCategoryProductsBatchReq"
|
||||
* x-codegen:
|
||||
* method: addProducts
|
||||
* x-codeSamples:
|
||||
* - lang: Shell
|
||||
* label: cURL
|
||||
* source: |
|
||||
* curl --location \
|
||||
* --request POST 'https://medusa-url.com/admin/product-categories/{product_category_id}/products/batch' \
|
||||
* --header 'Authorization: Bearer {api_token}' \
|
||||
* --header 'Content-Type: application/json' \
|
||||
* --data-raw '{
|
||||
* "product_ids": [
|
||||
* {
|
||||
* "id": "{product_id}"
|
||||
* }
|
||||
* ]
|
||||
* }'
|
||||
* security:
|
||||
* - api_token: []
|
||||
* - cookie_auth: []
|
||||
* tags:
|
||||
* - Product Category
|
||||
* responses:
|
||||
* 200:
|
||||
* description: OK
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* product_category:
|
||||
* $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): Promise<void> => {
|
||||
const validatedBody =
|
||||
req.validatedBody as AdminPostProductCategoriesCategoryProductsBatchReq
|
||||
|
||||
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)
|
||||
.addProducts(
|
||||
id,
|
||||
validatedBody.product_ids.map((p) => p.id),
|
||||
)
|
||||
})
|
||||
|
||||
const productCategory = await productCategoryService.retrieve(
|
||||
id,
|
||||
req.retrieveConfig
|
||||
)
|
||||
|
||||
res.status(200).json({ product_category: productCategory })
|
||||
}
|
||||
|
||||
/**
|
||||
* @schema AdminPostProductCategoriesCategoryProductsBatchReq
|
||||
* type: object
|
||||
* required:
|
||||
* - product_ids
|
||||
* properties:
|
||||
* product_ids:
|
||||
* description: The IDs of the products to add to the Product Category
|
||||
* type: array
|
||||
* items:
|
||||
* type: object
|
||||
* required:
|
||||
* - id
|
||||
* properties:
|
||||
* id:
|
||||
* type: string
|
||||
* description: The ID of the product
|
||||
*/
|
||||
export class AdminPostProductCategoriesCategoryProductsBatchReq {
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => ProductBatchProductCategory)
|
||||
product_ids: ProductBatchProductCategory[]
|
||||
}
|
||||
|
||||
export class AdminPostProductCategoriesCategoryProductsBatchParams extends FindParams {}
|
||||
@@ -4,8 +4,10 @@ import middlewares, {
|
||||
transformQuery,
|
||||
transformBody,
|
||||
} from "../../../middlewares"
|
||||
|
||||
import { isFeatureFlagEnabled } from "../../../middlewares/feature-flag-enabled"
|
||||
import deleteProductCategory from "./delete-product-category"
|
||||
import { validateProductsExist } from "../../../middlewares/validators/product-existence"
|
||||
|
||||
import getProductCategory, {
|
||||
AdminGetProductCategoryParams,
|
||||
@@ -25,9 +27,26 @@ import updateProductCategory, {
|
||||
AdminPostProductCategoriesCategoryParams,
|
||||
} from "./update-product-category"
|
||||
|
||||
import addProductsBatch, {
|
||||
AdminPostProductCategoriesCategoryProductsBatchReq,
|
||||
AdminPostProductCategoriesCategoryProductsBatchParams,
|
||||
} from "./add-products-batch"
|
||||
|
||||
const route = Router()
|
||||
|
||||
export default (app) => {
|
||||
const retrieveTransformQueryConfig = {
|
||||
defaultFields: defaultProductCategoryFields,
|
||||
defaultRelations: defaultAdminProductCategoryRelations,
|
||||
allowedRelations: allowedAdminProductCategoryRelations,
|
||||
isList: false,
|
||||
}
|
||||
|
||||
const listTransformQueryConfig = {
|
||||
...retrieveTransformQueryConfig,
|
||||
isList: true,
|
||||
}
|
||||
|
||||
app.use(
|
||||
"/product-categories",
|
||||
isFeatureFlagEnabled("product_categories"),
|
||||
@@ -36,47 +55,49 @@ export default (app) => {
|
||||
|
||||
route.post(
|
||||
"/",
|
||||
transformQuery(AdminPostProductCategoriesParams, {
|
||||
defaultFields: defaultProductCategoryFields,
|
||||
defaultRelations: defaultAdminProductCategoryRelations,
|
||||
isList: false,
|
||||
}),
|
||||
transformQuery(
|
||||
AdminPostProductCategoriesParams,
|
||||
retrieveTransformQueryConfig
|
||||
),
|
||||
transformBody(AdminPostProductCategoriesReq),
|
||||
middlewares.wrap(createProductCategory)
|
||||
)
|
||||
|
||||
route.get(
|
||||
"/",
|
||||
transformQuery(AdminGetProductCategoriesParams, {
|
||||
defaultFields: defaultProductCategoryFields,
|
||||
defaultRelations: defaultAdminProductCategoryRelations,
|
||||
isList: true,
|
||||
}),
|
||||
transformQuery(AdminGetProductCategoriesParams, listTransformQueryConfig),
|
||||
middlewares.wrap(listProductCategories)
|
||||
)
|
||||
|
||||
route.get(
|
||||
"/:id",
|
||||
transformQuery(AdminGetProductCategoryParams, {
|
||||
defaultFields: defaultProductCategoryFields,
|
||||
isList: false,
|
||||
}),
|
||||
transformQuery(AdminGetProductCategoryParams, retrieveTransformQueryConfig),
|
||||
middlewares.wrap(getProductCategory)
|
||||
)
|
||||
|
||||
route.post(
|
||||
"/:id",
|
||||
transformQuery(AdminPostProductCategoriesCategoryParams, {
|
||||
defaultFields: defaultProductCategoryFields,
|
||||
defaultRelations: defaultAdminProductCategoryRelations,
|
||||
isList: false,
|
||||
}),
|
||||
transformQuery(
|
||||
AdminPostProductCategoriesCategoryParams,
|
||||
retrieveTransformQueryConfig
|
||||
),
|
||||
transformBody(AdminPostProductCategoriesCategoryReq),
|
||||
middlewares.wrap(updateProductCategory)
|
||||
)
|
||||
|
||||
route.delete("/:id", middlewares.wrap(deleteProductCategory))
|
||||
|
||||
route.post(
|
||||
"/:id/products/batch",
|
||||
transformQuery(
|
||||
AdminPostProductCategoriesCategoryProductsBatchParams,
|
||||
retrieveTransformQueryConfig
|
||||
),
|
||||
transformBody(AdminPostProductCategoriesCategoryProductsBatchReq),
|
||||
validateProductsExist((req) => req.body.product_ids),
|
||||
middlewares.wrap(addProductsBatch)
|
||||
)
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
@@ -90,6 +111,11 @@ export const defaultAdminProductCategoryRelations = [
|
||||
"category_children",
|
||||
]
|
||||
|
||||
export const allowedAdminProductCategoryRelations = [
|
||||
"parent_category",
|
||||
"category_children",
|
||||
]
|
||||
|
||||
export const defaultProductCategoryFields = [
|
||||
"id",
|
||||
"name",
|
||||
|
||||
Reference in New Issue
Block a user