diff --git a/.changeset/real-rabbits-press.md b/.changeset/real-rabbits-press.md new file mode 100644 index 0000000000..2ffc10675b --- /dev/null +++ b/.changeset/real-rabbits-press.md @@ -0,0 +1,8 @@ +--- +"@medusajs/medusa": patch +"@medusajs/product": patch +"@medusajs/utils": patch +--- + +fix: Product categories repository and end points + diff --git a/package.json b/package.json index cae5fba031..1cfdf0aa8d 100644 --- a/package.json +++ b/package.json @@ -74,9 +74,9 @@ "lint:path": "eslint --ignore-path .eslintignore --ext .js,.ts,.tsx", "prettier": "prettier", "jest": "jest", - "test": "turbo run test --concurrency=50% --no-daemon --no-cache --force", + "test": "turbo run test --concurrency=50% --no-daemon --no-cache --force --filter='./packages/*' --filter='./packages/core/*' --filter='./packages/cli/*' --filter='./packages/modules/*' --filter='./packages/modules/providers/*'", "test:chunk": "./scripts/run-workspace-unit-tests-in-chunks.sh", - "test:integration:packages": "turbo run test:integration --concurrency=50% --no-daemon --no-cache --force --filter='./packages/*'", + "test:integration:packages": "turbo run test:integration --concurrency=50% --no-daemon --no-cache --force --filter='./packages/*' --filter='./packages/core/*' --filter='./packages/cli/*' --filter='./packages/modules/*' --filter='./packages/modules/providers/*'", "test:integration:api": "turbo run test:integration:chunk --concurrency=50% --no-daemon --no-cache --force --filter=integration-tests-api", "test:integration:plugins": "turbo run test:integration --concurrency=50% --no-daemon --no-cache --filter=integration-tests-plugins", "test:integration:modules": "turbo run test:integration:chunk --concurrency=50% --no-daemon --no-cache --force --filter=integration-tests-modules", diff --git a/packages/core/utils/src/dal/mikro-orm/db-error-mapper.ts b/packages/core/utils/src/dal/mikro-orm/db-error-mapper.ts index 2109d05f15..e93a68f51f 100644 --- a/packages/core/utils/src/dal/mikro-orm/db-error-mapper.ts +++ b/packages/core/utils/src/dal/mikro-orm/db-error-mapper.ts @@ -9,7 +9,6 @@ import { MedusaError, upperCaseFirst } from "../../common" export const dbErrorMapper = (err: Error) => { if (err instanceof NotFoundError) { - console.log(err) throw new MedusaError(MedusaError.Types.NOT_FOUND, err.message) } diff --git a/packages/medusa/src/api-v2/admin/product-categories/query-config.ts b/packages/medusa/src/api-v2/admin/product-categories/query-config.ts index 2af811df9b..865dcc02b4 100644 --- a/packages/medusa/src/api-v2/admin/product-categories/query-config.ts +++ b/packages/medusa/src/api-v2/admin/product-categories/query-config.ts @@ -10,8 +10,8 @@ export const defaults = [ "created_at", "updated_at", "metadata", - "parent_category", - "category_children", + "*parent_category", + "*category_children", ] export const allowed = [ diff --git a/packages/modules/product/integration-tests/__tests__/services/product-category/index.ts b/packages/modules/product/integration-tests/__tests__/services/product-category/index.ts index 9df1555c26..1b05e63758 100644 --- a/packages/modules/product/integration-tests/__tests__/services/product-category/index.ts +++ b/packages/modules/product/integration-tests/__tests__/services/product-category/index.ts @@ -132,7 +132,6 @@ moduleIntegrationTestRunner({ handle: "category-0", mpath: "category-0.", parent_category_id: null, - parent_category: null, category_children: [ expect.objectContaining({ id: "category-1", @@ -551,7 +550,6 @@ moduleIntegrationTestRunner({ handle: "category-0", mpath: "category-0.", parent_category_id: null, - parent_category: null, category_children: [ expect.objectContaining({ id: "category-1", @@ -803,7 +801,6 @@ moduleIntegrationTestRunner({ handle: "category-0", mpath: "category-0.", parent_category_id: null, - parent_category: null, category_children: [ expect.objectContaining({ id: "category-1", @@ -865,7 +862,6 @@ moduleIntegrationTestRunner({ handle: "category-0", mpath: "category-0.", parent_category_id: null, - parent_category: null, category_children: [ expect.objectContaining({ id: "category-1", diff --git a/packages/modules/product/src/repositories/product-category.ts b/packages/modules/product/src/repositories/product-category.ts index ba25e4ccfb..a51400c169 100644 --- a/packages/modules/product/src/repositories/product-category.ts +++ b/packages/modules/product/src/repositories/product-category.ts @@ -4,11 +4,11 @@ import { ProductCategoryTransformOptions, ProductTypes, } from "@medusajs/types" -import { DALUtils, MedusaError, isDefined } from "@medusajs/utils" +import { DALUtils, isDefined, MedusaError } from "@medusajs/utils" import { - LoadStrategy, FilterQuery as MikroFilterQuery, FindOptions as MikroOptions, + LoadStrategy, } from "@mikro-orm/core" import { SqlEntityManager } from "@mikro-orm/postgresql" import { ProductCategory } from "@models" @@ -51,7 +51,9 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito } const shouldExpandParent = - familyOptions.includeAncestorsTree || populate.includes("parent_category") || fields.some(field => field.startsWith('parent_category')) + familyOptions.includeAncestorsTree || + populate.includes("parent_category") || + fields.some((field) => field.startsWith("parent_category.")) if (shouldExpandParent) { populate.indexOf("parent_category") === -1 && @@ -59,7 +61,9 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito } const shouldExpandChildren = - familyOptions.includeDescendantsTree || populate.includes("category_children") || fields.some(field => field.startsWith('category_children')) + familyOptions.includeDescendantsTree || + populate.includes("category_children") || + fields.some((field) => field.startsWith("category_children.")) if (shouldExpandChildren) { populate.indexOf("category_children") === -1 && @@ -85,7 +89,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito const productCategories = await manager.find( ProductCategory, findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions + { ...findOptions_.options } as MikroOptions ) if ( @@ -116,6 +120,20 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito ): Promise { const manager = super.getActiveManager(context) + // We dont want to get the relations as we will fetch all the categories and build the tree manually + let relationIndex = + findOptions.options?.populate?.indexOf("parent_category") + const shouldPopulateParent = relationIndex !== -1 + if (shouldPopulateParent && include.ancestors) { + findOptions.options!.populate!.splice(relationIndex as number, 1) + } + + relationIndex = findOptions.options?.populate?.indexOf("category_children") + const shouldPopulateChildren = relationIndex !== -1 + if (shouldPopulateChildren && include.descendants) { + findOptions.options!.populate!.splice(relationIndex as number, 1) + } + const mpaths: any[] = [] const parentMpaths = new Set() for (const cat of productCategories) { @@ -161,7 +179,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito const categoriesById = new Map(allCategories.map((cat) => [cat.id, cat])) allCategories.forEach((cat: any) => { - if (cat.parent_category_id) { + if (cat.parent_category_id && include.ancestors) { cat.parent_category = categoriesById.get(cat.parent_category_id) } }) @@ -178,12 +196,17 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito } if (level === 0) { + if (!include.ancestors && !shouldPopulateParent) { + delete category.parent_category + } + return category } if (include.ancestors) { delete category.category_children } + if (include.descendants) { delete category.parent_category } @@ -205,7 +228,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito context: Context = {} ): Promise<[ProductCategory[], number]> { const manager = super.getActiveManager(context) - + const findOptions_ = this.buildFindOptions(findOptions, transformOptions) const [productCategories, count] = await manager.findAndCount(