fix(product): Category repository missing ontext (#9688)

**What**
- Fix product category repository missing context passed to down level methods
- Ensure the base repository when getting the active manager returns a fresh one if possible instead of the global one in order to prevent shared entity map by mistake
This commit is contained in:
Adrien de Peretti authored and GitHub committed 2024-10-21 11:42:17 +00:00
1 parent 63fff01f2f
commit 45df24f707
3 files changed
+35 -37

No files matched your search

@@ -54,7 +54,7 @@ export class MikroOrmBase<T = any> {
transactionManager, transactionManager,
manager, manager,
}: Context = {}): TManager { }: Context = {}): TManager {
return (transactionManager ?? manager ?? this.manager_) as TManager return (transactionManager ?? manager ?? this.getFreshManager()) as TManager
} }
async transaction<TManager = unknown>( async transaction<TManager = unknown>(
@@ -235,38 +235,38 @@ moduleIntegrationTestRunner<Service>({
mpath: mpath:
"electronics.computers.laptops.gaming-laptops.high-performance.4k-gaming", "electronics.computers.laptops.gaming-laptops.high-performance.4k-gaming",
parent_category_id: "high-performance", parent_category_id: "high-performance",
parent_category: { parent_category: expect.objectContaining({
id: "high-performance", id: "high-performance",
parent_category_id: "gaming-laptops", parent_category_id: "gaming-laptops",
handle: "high-performance-gaming-laptops", handle: "high-performance-gaming-laptops",
mpath: mpath:
"electronics.computers.laptops.gaming-laptops.high-performance", "electronics.computers.laptops.gaming-laptops.high-performance",
parent_category: { parent_category: expect.objectContaining({
id: "gaming-laptops", id: "gaming-laptops",
handle: "gaming-laptops", handle: "gaming-laptops",
mpath: "electronics.computers.laptops.gaming-laptops", mpath: "electronics.computers.laptops.gaming-laptops",
parent_category_id: "laptops", parent_category_id: "laptops",
parent_category: { parent_category: expect.objectContaining({
id: "laptops", id: "laptops",
parent_category_id: "computers", parent_category_id: "computers",
handle: "laptops", handle: "laptops",
mpath: "electronics.computers.laptops", mpath: "electronics.computers.laptops",
parent_category: { parent_category: expect.objectContaining({
id: "computers", id: "computers",
handle: "computers-&-accessories", handle: "computers-&-accessories",
mpath: "electronics.computers", mpath: "electronics.computers",
parent_category_id: "electronics", parent_category_id: "electronics",
parent_category: { parent_category: expect.objectContaining({
id: "electronics", id: "electronics",
parent_category_id: null, parent_category_id: null,
handle: "electronics", handle: "electronics",
mpath: "electronics", mpath: "electronics",
parent_category: null, parent_category: null,
}, }),
}, }),
}, }),
}, }),
}, }),
}, },
]) ])
}) })
@@ -421,38 +421,38 @@ moduleIntegrationTestRunner<Service>({
handle: "category-1-a", handle: "category-1-a",
mpath: "category-0.category-1.category-1-a", mpath: "category-0.category-1.category-1-a",
parent_category_id: "category-1", parent_category_id: "category-1",
parent_category: { parent_category: expect.objectContaining({
id: "category-1", id: "category-1",
handle: "category-1", handle: "category-1",
mpath: "category-0.category-1", mpath: "category-0.category-1",
parent_category_id: "category-0", parent_category_id: "category-0",
parent_category: { parent_category: expect.objectContaining({
id: "category-0", id: "category-0",
handle: "category-0", handle: "category-0",
mpath: "category-0", mpath: "category-0",
parent_category_id: null, parent_category_id: null,
parent_category: null, parent_category: null,
}, }),
}, }),
}, },
{ {
id: "category-1-b", id: "category-1-b",
handle: "category-1-b", handle: "category-1-b",
mpath: "category-0.category-1.category-1-b", mpath: "category-0.category-1.category-1-b",
parent_category_id: "category-1", parent_category_id: "category-1",
parent_category: { parent_category: expect.objectContaining({
id: "category-1", id: "category-1",
handle: "category-1", handle: "category-1",
mpath: "category-0.category-1", mpath: "category-0.category-1",
parent_category_id: "category-0", parent_category_id: "category-0",
parent_category: { parent_category: expect.objectContaining({
id: "category-0", id: "category-0",
handle: "category-0", handle: "category-0",
mpath: "category-0", mpath: "category-0",
parent_category_id: null, parent_category_id: null,
parent_category: null, parent_category: null,
}, }),
}, }),
}, },
]) ])
}) })
@@ -469,29 +469,25 @@ moduleIntegrationTestRunner<Service>({
} }
) )
const serializedObject = JSON.parse( expect(productCategoryResults).toEqual([
JSON.stringify(productCategoryResults)
)
expect(serializedObject).toEqual([
{ {
id: "category-1-a", id: "category-1-a",
handle: "category-1-a", handle: "category-1-a",
mpath: "category-0.category-1.category-1-a", mpath: "category-0.category-1.category-1-a",
parent_category_id: "category-1", parent_category_id: "category-1",
parent_category: { parent_category: expect.objectContaining({
id: "category-1", id: "category-1",
handle: "category-1", handle: "category-1",
mpath: "category-0.category-1", mpath: "category-0.category-1",
parent_category_id: "category-0", parent_category_id: "category-0",
parent_category: { parent_category: expect.objectContaining({
id: "category-0", id: "category-0",
handle: "category-0", handle: "category-0",
mpath: "category-0", mpath: "category-0",
parent_category_id: null, parent_category_id: null,
parent_category: null, parent_category: null,
}, }),
}, }),
category_children: [], category_children: [],
}, },
{ {
@@ -499,26 +495,26 @@ moduleIntegrationTestRunner<Service>({
handle: "category-1-b", handle: "category-1-b",
mpath: "category-0.category-1.category-1-b", mpath: "category-0.category-1.category-1-b",
parent_category_id: "category-1", parent_category_id: "category-1",
parent_category: { parent_category: expect.objectContaining({
id: "category-1", id: "category-1",
handle: "category-1", handle: "category-1",
mpath: "category-0.category-1", mpath: "category-0.category-1",
parent_category_id: "category-0", parent_category_id: "category-0",
parent_category: { parent_category: expect.objectContaining({
id: "category-0", id: "category-0",
handle: "category-0", handle: "category-0",
mpath: "category-0", mpath: "category-0",
parent_category_id: null, parent_category_id: null,
parent_category: null, parent_category: null,
}, }),
}, }),
category_children: [ category_children: [
{ expect.objectContaining({
id: "category-1-b-1", id: "category-1-b-1",
handle: "category-1-b-1", handle: "category-1-b-1",
mpath: "category-0.category-1.category-1-b.category-1-b-1", mpath: "category-0.category-1.category-1-b.category-1-b-1",
parent_category_id: "category-1-b", parent_category_id: "category-1-b",
}, }),
], ],
}, },
]) ])
@@ -6,9 +6,9 @@ import {
} from "@medusajs/framework/types" } from "@medusajs/framework/types"
import { DALUtils, isDefined, MedusaError } from "@medusajs/framework/utils" import { DALUtils, isDefined, MedusaError } from "@medusajs/framework/utils"
import { import {
LoadStrategy,
FilterQuery as MikroFilterQuery, FilterQuery as MikroFilterQuery,
FindOptions as MikroOptions, FindOptions as MikroOptions,
LoadStrategy,
} from "@mikro-orm/core" } from "@mikro-orm/core"
import { SqlEntityManager } from "@mikro-orm/postgresql" import { SqlEntityManager } from "@mikro-orm/postgresql"
import { ProductCategory } from "@models" import { ProductCategory } from "@models"
@@ -93,7 +93,8 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
ancestors: transformOptions.includeAncestorsTree, ancestors: transformOptions.includeAncestorsTree,
}, },
productCategories, productCategories,
findOptions_ findOptions_,
context
) )
return this.sortCategoriesByRank(categoriesTree) return this.sortCategoriesByRank(categoriesTree)
@@ -250,7 +251,8 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
ancestors: transformOptions.includeAncestorsTree, ancestors: transformOptions.includeAncestorsTree,
}, },
productCategories, productCategories,
findOptions_ findOptions_,
context
) )
return [this.sortCategoriesByRank(categoriesTree), count] return [this.sortCategoriesByRank(categoriesTree), count]