feat: Categories retrieve + list API (#7009)

This commit is contained in:
Oli Juhl
2024-04-08 19:26:34 +02:00
committed by GitHub
parent db2f0ef53f
commit 6cc9a5e469
41 changed files with 1176 additions and 35 deletions
@@ -1,5 +1,6 @@
import {
DALUtils,
Searchable,
createPsqlIndexStatementHelper,
generateEntityId,
kebabCase,
@@ -18,7 +19,6 @@ import {
PrimaryKey,
Property,
} from "@mikro-orm/core"
import Product from "./product"
const categoryHandleIndexName = "IDX_category_handle_unique"
@@ -47,9 +47,11 @@ class ProductCategory {
@PrimaryKey({ columnType: "text" })
id!: string
@Searchable()
@Property({ columnType: "text", nullable: false })
name?: string
@Searchable()
@Property({ columnType: "text", default: "", nullable: false })
description?: string
@@ -124,11 +126,14 @@ class ProductCategory {
}
const { em } = args
const parentCategoryId = args.changeSet?.entity?.parent_category?.id
let parentCategory: ProductCategory | null = null
if (parentCategoryId) {
parentCategory = await em.findOne(ProductCategory, parentCategoryId)
if (this.parent_category_id) {
parentCategory = await em.findOne(
ProductCategory,
this.parent_category_id
)
}
if (parentCategory) {
@@ -4,7 +4,11 @@ import {
ProductCategoryTransformOptions,
ProductTypes,
} from "@medusajs/types"
import { DALUtils, MedusaError, isDefined } from "@medusajs/utils"
import {
DALUtils,
MedusaError,
isDefined
} from "@medusajs/utils"
import {
LoadStrategy,
FilterQuery as MikroFilterQuery,
@@ -1,5 +1,6 @@
import { Context, DAL, FindConfig, ProductTypes } from "@medusajs/types"
import {
FreeTextSearchFilterKey,
InjectManager,
InjectTransactionManager,
MedusaContext,
@@ -76,6 +77,17 @@ export default class ProductCategoryService<
delete filters.include_descendants_tree
delete filters.include_ancestors_tree
// Apply free text search filter
if (filters?.q) {
config.filters ??= {}
config.filters[FreeTextSearchFilterKey] = {
value: filters.q,
fromEntity: ProductCategory.name,
}
delete filters.q
}
const queryOptions = ModulesSdkUtils.buildQuery<ProductCategory>(
filters,
config
@@ -102,6 +114,17 @@ export default class ProductCategoryService<
delete filters.include_descendants_tree
delete filters.include_ancestors_tree
// Apply free text search filter
if (filters?.q) {
config.filters ??= {}
config.filters[FreeTextSearchFilterKey] = {
value: filters.q,
fromEntity: ProductCategory.name,
}
delete filters.q
}
const queryOptions = ModulesSdkUtils.buildQuery<ProductCategory>(
filters,
config