fix: Medusa service base method transaction (#7758)
**What** Remove transaction decorator from the base medusa service method, the transaction will always be coming from the shared context. It fixes the issue that when you consume a base method directly it will return a proper tuple from the DB instead of the one from the entity map cc @VariableVic **NOTE** This pr also fix some categories issues in the product module which was preventing the tests from working. if @sradevski you could have a look later, in the mean time we can still merge it FIXES CORE-2342
This commit is contained in:
@@ -274,9 +274,17 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
|
||||
|
||||
const categories = await Promise.all(
|
||||
ids.map(async (id) => {
|
||||
const productCategory = await manager.findOneOrFail(ProductCategory, {
|
||||
const productCategory = await manager.findOne(ProductCategory, {
|
||||
id,
|
||||
})
|
||||
|
||||
if (!productCategory) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`ProductCategory with id: ${id} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
manager.assign(productCategory, { deleted_at: new Date() })
|
||||
return productCategory
|
||||
})
|
||||
@@ -310,7 +318,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
|
||||
|
||||
await Promise.all(
|
||||
ids.map(async (id) => {
|
||||
const productCategory = await manager.findOneOrFail(
|
||||
const productCategory = await manager.findOne(
|
||||
ProductCategory,
|
||||
{ id },
|
||||
{
|
||||
@@ -318,6 +326,13 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
|
||||
}
|
||||
)
|
||||
|
||||
if (!productCategory) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`ProductCategory with id: ${id} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
if (productCategory.category_children.length > 0) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_ALLOWED,
|
||||
@@ -389,10 +404,17 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
|
||||
const categories = await Promise.all(
|
||||
data.map(async (entry, i) => {
|
||||
const categoryData: Partial<ProductCategory> = { ...entry }
|
||||
const productCategory = await manager.findOneOrFail(ProductCategory, {
|
||||
const productCategory = await manager.findOne(ProductCategory, {
|
||||
id: categoryData.id,
|
||||
})
|
||||
|
||||
if (!productCategory) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`ProductCategory with id: ${categoryData.id} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
// If the parent or rank are not changed, no need to reorder anything.
|
||||
if (
|
||||
!isDefined(categoryData.parent_category_id) &&
|
||||
|
||||
@@ -3,10 +3,10 @@ import {
|
||||
FreeTextSearchFilterKey,
|
||||
InjectManager,
|
||||
InjectTransactionManager,
|
||||
isDefined,
|
||||
MedusaContext,
|
||||
MedusaError,
|
||||
ModulesSdkUtils,
|
||||
isDefined,
|
||||
} from "@medusajs/utils"
|
||||
import { ProductCategory } from "@models"
|
||||
import { ProductCategoryRepository } from "@repositories"
|
||||
@@ -169,6 +169,7 @@ export default class ProductCategoryService<
|
||||
await this.productCategoryRepository_.delete(ids, sharedContext)
|
||||
}
|
||||
|
||||
@InjectTransactionManager("productCategoryRepository_")
|
||||
async softDelete(
|
||||
ids: string[],
|
||||
@MedusaContext() sharedContext?: Context
|
||||
@@ -178,6 +179,7 @@ export default class ProductCategoryService<
|
||||
).softDelete(ids, sharedContext)) as any
|
||||
}
|
||||
|
||||
@InjectTransactionManager("productCategoryRepository_")
|
||||
async restore(
|
||||
ids: string[],
|
||||
@MedusaContext() sharedContext?: Context
|
||||
|
||||
Reference in New Issue
Block a user