Skip to content

Commit 45df24f

Browse files
authored
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
1 parent 63fff01 commit 45df24f

File tree

3 files changed

+35
-37
lines changed

3 files changed

+35
-37
lines changed

packages/core/utils/src/dal/mikro-orm/mikro-orm-repository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class MikroOrmBase<T = any> {
5454
transactionManager,
5555
manager,
5656
}: Context = {}): TManager {
57-
return (transactionManager ?? manager ?? this.manager_) as TManager
57+
return (transactionManager ?? manager ?? this.getFreshManager()) as TManager
5858
}
5959

6060
async transaction<TManager = unknown>(

packages/modules/product/integration-tests/__tests__/product-category.ts

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -235,38 +235,38 @@ moduleIntegrationTestRunner<Service>({
235235
mpath:
236236
"electronics.computers.laptops.gaming-laptops.high-performance.4k-gaming",
237237
parent_category_id: "high-performance",
238-
parent_category: {
238+
parent_category: expect.objectContaining({
239239
id: "high-performance",
240240
parent_category_id: "gaming-laptops",
241241
handle: "high-performance-gaming-laptops",
242242
mpath:
243243
"electronics.computers.laptops.gaming-laptops.high-performance",
244-
parent_category: {
244+
parent_category: expect.objectContaining({
245245
id: "gaming-laptops",
246246
handle: "gaming-laptops",
247247
mpath: "electronics.computers.laptops.gaming-laptops",
248248
parent_category_id: "laptops",
249-
parent_category: {
249+
parent_category: expect.objectContaining({
250250
id: "laptops",
251251
parent_category_id: "computers",
252252
handle: "laptops",
253253
mpath: "electronics.computers.laptops",
254-
parent_category: {
254+
parent_category: expect.objectContaining({
255255
id: "computers",
256256
handle: "computers-&-accessories",
257257
mpath: "electronics.computers",
258258
parent_category_id: "electronics",
259-
parent_category: {
259+
parent_category: expect.objectContaining({
260260
id: "electronics",
261261
parent_category_id: null,
262262
handle: "electronics",
263263
mpath: "electronics",
264264
parent_category: null,
265-
},
266-
},
267-
},
268-
},
269-
},
265+
}),
266+
}),
267+
}),
268+
}),
269+
}),
270270
},
271271
])
272272
})
@@ -421,38 +421,38 @@ moduleIntegrationTestRunner<Service>({
421421
handle: "category-1-a",
422422
mpath: "category-0.category-1.category-1-a",
423423
parent_category_id: "category-1",
424-
parent_category: {
424+
parent_category: expect.objectContaining({
425425
id: "category-1",
426426
handle: "category-1",
427427
mpath: "category-0.category-1",
428428
parent_category_id: "category-0",
429-
parent_category: {
429+
parent_category: expect.objectContaining({
430430
id: "category-0",
431431
handle: "category-0",
432432
mpath: "category-0",
433433
parent_category_id: null,
434434
parent_category: null,
435-
},
436-
},
435+
}),
436+
}),
437437
},
438438
{
439439
id: "category-1-b",
440440
handle: "category-1-b",
441441
mpath: "category-0.category-1.category-1-b",
442442
parent_category_id: "category-1",
443-
parent_category: {
443+
parent_category: expect.objectContaining({
444444
id: "category-1",
445445
handle: "category-1",
446446
mpath: "category-0.category-1",
447447
parent_category_id: "category-0",
448-
parent_category: {
448+
parent_category: expect.objectContaining({
449449
id: "category-0",
450450
handle: "category-0",
451451
mpath: "category-0",
452452
parent_category_id: null,
453453
parent_category: null,
454-
},
455-
},
454+
}),
455+
}),
456456
},
457457
])
458458
})
@@ -469,56 +469,52 @@ moduleIntegrationTestRunner<Service>({
469469
}
470470
)
471471

472-
const serializedObject = JSON.parse(
473-
JSON.stringify(productCategoryResults)
474-
)
475-
476-
expect(serializedObject).toEqual([
472+
expect(productCategoryResults).toEqual([
477473
{
478474
id: "category-1-a",
479475
handle: "category-1-a",
480476
mpath: "category-0.category-1.category-1-a",
481477
parent_category_id: "category-1",
482-
parent_category: {
478+
parent_category: expect.objectContaining({
483479
id: "category-1",
484480
handle: "category-1",
485481
mpath: "category-0.category-1",
486482
parent_category_id: "category-0",
487-
parent_category: {
483+
parent_category: expect.objectContaining({
488484
id: "category-0",
489485
handle: "category-0",
490486
mpath: "category-0",
491487
parent_category_id: null,
492488
parent_category: null,
493-
},
494-
},
489+
}),
490+
}),
495491
category_children: [],
496492
},
497493
{
498494
id: "category-1-b",
499495
handle: "category-1-b",
500496
mpath: "category-0.category-1.category-1-b",
501497
parent_category_id: "category-1",
502-
parent_category: {
498+
parent_category: expect.objectContaining({
503499
id: "category-1",
504500
handle: "category-1",
505501
mpath: "category-0.category-1",
506502
parent_category_id: "category-0",
507-
parent_category: {
503+
parent_category: expect.objectContaining({
508504
id: "category-0",
509505
handle: "category-0",
510506
mpath: "category-0",
511507
parent_category_id: null,
512508
parent_category: null,
513-
},
514-
},
509+
}),
510+
}),
515511
category_children: [
516-
{
512+
expect.objectContaining({
517513
id: "category-1-b-1",
518514
handle: "category-1-b-1",
519515
mpath: "category-0.category-1.category-1-b.category-1-b-1",
520516
parent_category_id: "category-1-b",
521-
},
517+
}),
522518
],
523519
},
524520
])

packages/modules/product/src/repositories/product-category.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import {
66
} from "@medusajs/framework/types"
77
import { DALUtils, isDefined, MedusaError } from "@medusajs/framework/utils"
88
import {
9-
LoadStrategy,
109
FilterQuery as MikroFilterQuery,
1110
FindOptions as MikroOptions,
11+
LoadStrategy,
1212
} from "@mikro-orm/core"
1313
import { SqlEntityManager } from "@mikro-orm/postgresql"
1414
import { ProductCategory } from "@models"
@@ -93,7 +93,8 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
9393
ancestors: transformOptions.includeAncestorsTree,
9494
},
9595
productCategories,
96-
findOptions_
96+
findOptions_,
97+
context
9798
)
9899

99100
return this.sortCategoriesByRank(categoriesTree)
@@ -250,7 +251,8 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
250251
ancestors: transformOptions.includeAncestorsTree,
251252
},
252253
productCategories,
253-
findOptions_
254+
findOptions_,
255+
context
254256
)
255257

256258
return [this.sortCategoriesByRank(categoriesTree), count]

0 commit comments

Comments
 (0)