feat(medusa): List (service + controller) product categories #3004 (#3023)

**What:**

Introduces a store endpoint to retrieve a list of product categories

**Why:**

This is part of a greater goal of allowing products to be added to multiple categories.

**How:**

- Creates an endpoint in store routes

RESOLVES CORE-968
This commit is contained in:
Riqwan Thamir
2023-01-16 20:20:42 +01:00
committed by GitHub
parent c49747b3ad
commit 7d4b8b9cc5
10 changed files with 358 additions and 57 deletions

View File

@@ -18,6 +18,8 @@ import { extendedFindParamsMixin } from "../../../../types/common"
* - (query) parent_category_id {string} Returns categories scoped by parent
* - (query) offset=0 {integer} How many product categories to skip in the result.
* - (query) limit=100 {integer} Limit the number of product categories returned.
* - (query) expand {string} (Comma separated) Which fields should be expanded in the product category.
* - (query) fields {string} (Comma separated) Which fields should be included in the product category.
* x-codeSamples:
* - lang: Shell
* label: cURL

View File

@@ -4,11 +4,26 @@ import getProductCategory, {
StoreGetProductCategoryParams,
} from "./get-product-category"
import listProductCategories, {
StoreGetProductCategoriesParams,
} from "./list-product-categories"
const route = Router()
export default (app) => {
app.use("/product-categories", route)
route.get(
"/",
transformQuery(StoreGetProductCategoriesParams, {
defaultFields: defaultStoreProductCategoryFields,
allowedFields: allowedStoreProductCategoryFields,
defaultRelations: defaultStoreProductCategoryRelations,
isList: true,
}),
middlewares.wrap(listProductCategories)
)
route.get(
"/:id",
transformQuery(StoreGetProductCategoryParams, {
@@ -37,6 +52,7 @@ export const defaultStoreProductCategoryFields = [
"id",
"name",
"handle",
"parent_category_id",
"created_at",
"updated_at",
]
@@ -45,8 +61,10 @@ export const allowedStoreProductCategoryFields = [
"id",
"name",
"handle",
"parent_category_id",
"created_at",
"updated_at",
]
export * from "./get-product-category"
export * from "./list-product-categories"

View File

@@ -0,0 +1,102 @@
import { IsNumber, IsOptional, IsString } from "class-validator"
import { Request, Response } from "express"
import { Type, Transform } from "class-transformer"
import { ProductCategoryService } from "../../../../services"
import { extendedFindParamsMixin } from "../../../../types/common"
import { defaultStoreScope } from "."
/**
* @oas [get] /product-categories
* operationId: "GetProductCategories"
* summary: "List Product Categories"
* description: "Retrieve a list of product categories."
* x-authenticated: false
* parameters:
* - (query) q {string} Query used for searching product category names orhandles.
* - (query) parent_category_id {string} Returns categories scoped by parent
* - (query) offset=0 {integer} How many product categories to skip in the result.
* - (query) limit=100 {integer} Limit the number of product categories returned.
* x-codeSamples:
* - lang: Shell
* label: cURL
* source: |
* curl --location --request GET 'https://medusa-url.com/store/product-categories' \
* --header 'Authorization: Bearer {api_token}'
* security:
* - api_token: []
* - cookie_auth: []
* tags:
* - Product Category
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* type: object
* properties:
* product_categories:
* type: array
* items:
* $ref: "#/components/schemas/ProductCategory"
* count:
* type: integer
* description: The total number of items available
* offset:
* type: integer
* description: The number of items skipped before these items
* limit:
* type: integer
* description: The number of items per page
* "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) => {
const productCategoryService: ProductCategoryService = req.scope.resolve(
"productCategoryService"
)
const selectors = Object.assign({ ...defaultStoreScope }, req.filterableFields)
const [data, count] = await productCategoryService.listAndCount(
selectors,
req.listConfig,
defaultStoreScope
)
const { limit, offset } = req.validatedQuery
res.json({
count,
offset,
limit,
product_categories: data,
})
}
export class StoreGetProductCategoriesParams extends extendedFindParamsMixin({
limit: 100,
offset: 0,
}) {
@IsString()
@IsOptional()
q?: string
@IsString()
@IsOptional()
@Transform(({ value }) => {
return value === "null" ? null : value
})
parent_category_id?: string | null
}

View File

@@ -17,6 +17,8 @@ import {
@Entity()
@Tree("materialized-path")
export class ProductCategory extends SoftDeletableEntity {
static treeRelations = ["parent_category", "category_children"]
@Column()
name: string

View File

@@ -1,6 +1,12 @@
import { EntityRepository, TreeRepository, Brackets, ILike } from "typeorm"
import {
EntityRepository,
TreeRepository,
Brackets,
ILike,
getConnection,
} from "typeorm"
import { ProductCategory } from "../models/product-category"
import { ExtendedFindConfig, Selector } from "../types/common"
import { ExtendedFindConfig, Selector, QuerySelector } from "../types/common"
@EntityRepository(ProductCategory)
export class ProductCategoryRepository extends TreeRepository<ProductCategory> {
@@ -8,13 +14,25 @@ export class ProductCategoryRepository extends TreeRepository<ProductCategory> {
options: ExtendedFindConfig<ProductCategory, Selector<ProductCategory>> = {
where: {},
},
q: string | undefined
q: string | undefined,
treeScope: QuerySelector<ProductCategory> = {}
): Promise<[ProductCategory[], number]> {
const options_ = { ...options }
const entityName = "product_category"
const options_ = { ...options }
const relations = options_.relations || []
const selectStatements = (relationName: string): string[] => {
const modelColumns = this.manager.connection
.getMetadata(ProductCategory)
.ownColumns.map((column) => column.propertyName)
return (options_.select || modelColumns).map((column) => {
return `${relationName}.${column}`
})
}
const queryBuilder = this.createQueryBuilder(entityName)
.select()
.select(selectStatements(entityName))
.skip(options_.skip)
.take(options_.take)
@@ -33,16 +51,37 @@ export class ProductCategoryRepository extends TreeRepository<ProductCategory> {
queryBuilder.andWhere(options_.where)
const includedTreeRelations: string[] = relations.filter((rel) =>
ProductCategory.treeRelations.includes(rel)
)
includedTreeRelations.forEach((treeRelation) => {
const treeWhere = Object.entries(treeScope)
.map((entry) => `${treeRelation}.${entry[0]} = :${entry[0]}`)
.join(" AND ")
queryBuilder
.leftJoin(
`${entityName}.${treeRelation}`,
treeRelation,
treeWhere,
treeScope
)
.addSelect(selectStatements(treeRelation))
})
const nonTreeRelations: string[] = relations.filter(
(rel) => !ProductCategory.treeRelations.includes(rel)
)
nonTreeRelations.forEach((relation) => {
queryBuilder.leftJoinAndSelect(`${entityName}.${relation}`, relation)
})
if (options_.withDeleted) {
queryBuilder.withDeleted()
}
if (options_.relations?.length) {
options_.relations.forEach((rel) => {
queryBuilder.leftJoinAndSelect(`${entityName}.${rel}`, rel)
})
}
return await queryBuilder.getManyAndCount()
}
}

View File

@@ -62,7 +62,7 @@ describe("ProductCategoryService", () => {
describe("listAndCount", () => {
const productCategoryRepository = {
...MockRepository({}),
getFreeTextSearchResultsAndCount: jest.fn().mockImplementation((query, q) => {
getFreeTextSearchResultsAndCount: jest.fn().mockImplementation((query, q, treeSelector = {}) => {
if (q == "not-found") {
return Promise.resolve([[], 0])
}
@@ -87,14 +87,18 @@ describe("ProductCategoryService", () => {
expect(result.length).toEqual(1)
expect(result[0].id).toEqual(IdMap.getId(validProdCategoryId))
expect(productCategoryRepository.getFreeTextSearchResultsAndCount).toHaveBeenCalledTimes(1)
expect(productCategoryRepository.getFreeTextSearchResultsAndCount).toHaveBeenCalledWith({
order: {
created_at: "DESC",
expect(productCategoryRepository.getFreeTextSearchResultsAndCount).toHaveBeenCalledWith(
{
order: {
created_at: "DESC",
},
skip: 0,
take: 100,
where: {},
},
skip: 0,
take: 100,
where: {},
}, validProdCategoryId)
validProdCategoryId,
{}
)
})
it("returns empty array if query doesn't match database results", async () => {

View File

@@ -58,7 +58,8 @@ class ProductCategoryService extends TransactionBaseService {
skip: 0,
take: 100,
order: { created_at: "DESC" },
}
},
treeSelector: QuerySelector<ProductCategory> = {},
): Promise<[ProductCategory[], number]> {
const manager = this.transactionManager_ ?? this.manager_
const productCategoryRepo = manager.getCustomRepository(
@@ -75,7 +76,11 @@ class ProductCategoryService extends TransactionBaseService {
const query = buildQuery(selector_, config)
return await productCategoryRepo.getFreeTextSearchResultsAndCount(query, q)
return await productCategoryRepo.getFreeTextSearchResultsAndCount(
query,
q,
treeSelector
)
}
/**