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:
@@ -1,26 +1,30 @@
|
||||
import { NextFunction, Request, Response } from "express"
|
||||
import { ProductService } from "../../../services"
|
||||
import { ProductBatchSalesChannel } from "../../../types/sales-channels"
|
||||
|
||||
export function validateProductsExist(
|
||||
getProducts: (req) => ProductBatchSalesChannel[] | undefined
|
||||
type GetProductsRequiredParams = {
|
||||
id: string
|
||||
}
|
||||
|
||||
export function validateProductsExist<T extends GetProductsRequiredParams = GetProductsRequiredParams>(
|
||||
getProducts: (req) => T[]
|
||||
): (req: Request, res: Response, next: NextFunction) => Promise<void> {
|
||||
return async (req: Request, res: Response, next: NextFunction) => {
|
||||
const products = getProducts(req)
|
||||
const requestedProducts = getProducts(req)
|
||||
|
||||
if (!products?.length) {
|
||||
if (!requestedProducts?.length) {
|
||||
return next()
|
||||
}
|
||||
|
||||
const productService: ProductService = req.scope.resolve("productService")
|
||||
|
||||
const productIds = products.map((product) => product.id)
|
||||
const [existingProducts] = await productService.listAndCount({
|
||||
id: productIds,
|
||||
const productService = req.scope.resolve("productService")
|
||||
const requestedProductIds = requestedProducts.map((product) => product.id)
|
||||
const [productRecords] = await productService.listAndCount({
|
||||
id: requestedProductIds,
|
||||
})
|
||||
|
||||
const nonExistingProducts = productIds.filter(
|
||||
(scId) => existingProducts.findIndex((sc) => sc.id === scId) === -1
|
||||
const nonExistingProducts = requestedProductIds.filter(
|
||||
(requestedProductId) =>
|
||||
productRecords.findIndex(
|
||||
(productRecord) => productRecord.id === requestedProductId
|
||||
) === -1
|
||||
)
|
||||
|
||||
if (nonExistingProducts.length) {
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -20,6 +20,7 @@ import {
|
||||
@Entity()
|
||||
@Tree("materialized-path")
|
||||
export class ProductCategory extends SoftDeletableEntity {
|
||||
static productCategoryProductJoinTable = "product_category_product"
|
||||
static treeRelations = ["parent_category", "category_children"]
|
||||
|
||||
@Column()
|
||||
@@ -54,7 +55,7 @@ export class ProductCategory extends SoftDeletableEntity {
|
||||
|
||||
@ManyToMany(() => Product, { cascade: ["remove", "soft-remove"] })
|
||||
@JoinTable({
|
||||
name: "product_category_product",
|
||||
name: ProductCategory.productCategoryProductJoinTable,
|
||||
joinColumn: {
|
||||
name: "product_category_id",
|
||||
referencedColumnName: "id",
|
||||
|
||||
@@ -84,4 +84,21 @@ export class ProductCategoryRepository extends TreeRepository<ProductCategory> {
|
||||
|
||||
return await queryBuilder.getManyAndCount()
|
||||
}
|
||||
|
||||
async addProducts(
|
||||
productCategoryId: string,
|
||||
productIds: string[]
|
||||
): Promise<void> {
|
||||
await this.createQueryBuilder()
|
||||
.insert()
|
||||
.into(ProductCategory.productCategoryProductJoinTable)
|
||||
.values(
|
||||
productIds.map((id) => ({
|
||||
product_category_id: productCategoryId,
|
||||
product_id: id,
|
||||
}))
|
||||
)
|
||||
.orIgnore()
|
||||
.execute()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -288,4 +288,37 @@ describe("ProductCategoryService", () => {
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("addProducts", () => {
|
||||
const productCategoryRepository = {
|
||||
...MockRepository(),
|
||||
addProducts: jest.fn().mockImplementation((id, productIds) => {
|
||||
return Promise.resolve()
|
||||
}),
|
||||
}
|
||||
|
||||
const productCategoryService = new ProductCategoryService({
|
||||
manager: MockManager,
|
||||
productCategoryRepository,
|
||||
eventBusService,
|
||||
})
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
it("should add a list of product to a sales channel", async () => {
|
||||
const result = await productCategoryService.addProducts(
|
||||
IdMap.getId("product-category-id"),
|
||||
[IdMap.getId("product-id")]
|
||||
)
|
||||
|
||||
expect(result).toBeUndefined()
|
||||
expect(productCategoryRepository.addProducts).toHaveBeenCalledTimes(1)
|
||||
expect(productCategoryRepository.addProducts).toHaveBeenCalledWith(
|
||||
IdMap.getId("product-category-id"),
|
||||
[IdMap.getId("product-id")]
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -59,7 +59,7 @@ class ProductCategoryService extends TransactionBaseService {
|
||||
take: 100,
|
||||
order: { created_at: "DESC" },
|
||||
},
|
||||
treeSelector: QuerySelector<ProductCategory> = {},
|
||||
treeSelector: QuerySelector<ProductCategory> = {}
|
||||
): Promise<[ProductCategory[], number]> {
|
||||
const manager = this.transactionManager_ ?? this.manager_
|
||||
const productCategoryRepo = manager.getCustomRepository(
|
||||
@@ -217,6 +217,24 @@ class ProductCategoryService extends TransactionBaseService {
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a batch of product to a product category
|
||||
* @param productCategoryId - The id of the product category on which to add the products
|
||||
* @param productIds - The products ids to attach to the product category
|
||||
* @return the product category on which the products have been added
|
||||
*/
|
||||
async addProducts(
|
||||
productCategoryId: string,
|
||||
productIds: string[]
|
||||
): Promise<void> {
|
||||
return await this.atomicPhase_(async (manager) => {
|
||||
const productCategoryRepository: ProductCategoryRepository =
|
||||
manager.getCustomRepository(this.productCategoryRepo_)
|
||||
|
||||
await productCategoryRepository.addProducts(productCategoryId, productIds)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default ProductCategoryService
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { FlagRouter } from "../utils/flag-router"
|
||||
|
||||
import { isDefined, MedusaError } from "medusa-core-utils"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { EntityManager, In } from "typeorm"
|
||||
import { ProductVariantService, SearchService } from "."
|
||||
import { TransactionBaseService } from "../interfaces"
|
||||
import SalesChannelFeatureFlag from "../loaders/feature-flags/sales-channels"
|
||||
|
||||
@@ -38,3 +38,8 @@ export class AdminProductCategoriesReqBase {
|
||||
})
|
||||
parent_category_id?: string | null
|
||||
}
|
||||
|
||||
export class ProductBatchProductCategory {
|
||||
@IsString()
|
||||
id: string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user