chore(product): remove decorator where it is not necessary and cleanup (#4731)
* chore(product): remove decorator where it is not necessary and cleanup * Create twenty-gorillas-exist.md
This commit is contained in:
committed by
GitHub
parent
ac866ebb51
commit
f8d3d5f91a
5
.changeset/twenty-gorillas-exist.md
Normal file
5
.changeset/twenty-gorillas-exist.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@medusajs/product": patch
|
||||
---
|
||||
|
||||
chore(product): remove decorator where it is not necessary and cleanup
|
||||
@@ -1,8 +1,7 @@
|
||||
import {
|
||||
EmitData,
|
||||
EventBusTypes,
|
||||
IEventBusModuleService,
|
||||
Subscriber,
|
||||
IEventBusModuleService
|
||||
} from "@medusajs/types"
|
||||
|
||||
export class EventBusService implements IEventBusModuleService {
|
||||
@@ -11,7 +10,9 @@ export class EventBusService implements IEventBusModuleService {
|
||||
data: T,
|
||||
options: Record<string, unknown>
|
||||
): Promise<void>
|
||||
|
||||
async emit<T>(data: EventBusTypes.EmitData<T>[]): Promise<void>
|
||||
|
||||
async emit<T, TInput extends string | EventBusTypes.EmitData<T>[] = string>(
|
||||
eventOrData: TInput,
|
||||
data?: T,
|
||||
@@ -30,7 +31,5 @@ export class EventBusService implements IEventBusModuleService {
|
||||
return this
|
||||
}
|
||||
|
||||
withTransaction() {
|
||||
|
||||
}
|
||||
withTransaction() {}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ export const productCategoriesData = [
|
||||
{
|
||||
id: "category-1",
|
||||
name: "category 1",
|
||||
parent_category_id: "category-0"
|
||||
parent_category_id: "category-0",
|
||||
},
|
||||
{
|
||||
id: "category-1-a",
|
||||
@@ -23,7 +23,7 @@ export const productCategoriesData = [
|
||||
{
|
||||
id: "category-1-b-1",
|
||||
name: "category 1 b 1",
|
||||
parent_category_id: "category-1-b"
|
||||
parent_category_id: "category-1-b",
|
||||
},
|
||||
]
|
||||
|
||||
@@ -65,4 +65,3 @@ export const productCategoriesRankData = [
|
||||
rank: 2,
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
@@ -2,16 +2,16 @@ export const categoriesData = [
|
||||
{
|
||||
id: "category-0",
|
||||
name: "category 0",
|
||||
parent_category_id: null
|
||||
parent_category_id: null,
|
||||
},
|
||||
{
|
||||
id: "category-1",
|
||||
name: "category 1",
|
||||
parent_category_id: "category-0"
|
||||
parent_category_id: "category-0",
|
||||
},
|
||||
{
|
||||
id: "category-1-a",
|
||||
name: "category 1 a",
|
||||
parent_category_id: "category-1"
|
||||
parent_category_id: "category-1",
|
||||
},
|
||||
]
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
ProductType,
|
||||
ProductVariant,
|
||||
} from "@models"
|
||||
|
||||
import ProductOption from "../../../src/models/product-option"
|
||||
|
||||
export * from "./data/create-product"
|
||||
|
||||
@@ -7,13 +7,7 @@ import { ProductCategory } from "@models"
|
||||
import { Context, DAL, ProductCategoryTransformOptions } from "@medusajs/types"
|
||||
import groupBy from "lodash/groupBy"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import {
|
||||
DALUtils,
|
||||
InjectTransactionManager,
|
||||
isDefined,
|
||||
MedusaContext,
|
||||
MedusaError,
|
||||
} from "@medusajs/utils"
|
||||
import { DALUtils, isDefined, MedusaError } from "@medusajs/utils"
|
||||
|
||||
import { ProductCategoryServiceTypes } from "../types"
|
||||
|
||||
@@ -30,11 +24,14 @@ export type ReorderConditions = {
|
||||
}
|
||||
|
||||
export const tempReorderRank = 99999
|
||||
|
||||
// eslint-disable-next-line max-len
|
||||
export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
}
|
||||
@@ -118,7 +115,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
|
||||
return category
|
||||
}
|
||||
|
||||
let children = descendantsByParentId[productCategory.id] || []
|
||||
const children = descendantsByParentId[productCategory.id] || []
|
||||
productCategory = addChildrenToCategory(productCategory, children)
|
||||
}
|
||||
|
||||
@@ -168,12 +165,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
|
||||
]
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
async delete(
|
||||
id: string,
|
||||
@MedusaContext()
|
||||
context: Context = {}
|
||||
): Promise<void> {
|
||||
async delete(id: string, context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const productCategory = await manager.findOneOrFail(
|
||||
ProductCategory,
|
||||
@@ -207,13 +199,12 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
async create(
|
||||
data: ProductCategoryServiceTypes.CreateProductCategoryDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
context: Context = {}
|
||||
): Promise<ProductCategory> {
|
||||
const categoryData = { ...data }
|
||||
const manager = this.getActiveManager<SqlEntityManager>(sharedContext)
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const siblings = await manager.find(ProductCategory, {
|
||||
parent_category_id: categoryData?.parent_category_id || null,
|
||||
})
|
||||
@@ -224,16 +215,15 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
|
||||
|
||||
const productCategory = manager.create(ProductCategory, categoryData)
|
||||
|
||||
await manager.persist(productCategory)
|
||||
manager.persist(productCategory)
|
||||
|
||||
return productCategory
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
async update(
|
||||
id: string,
|
||||
data: ProductCategoryServiceTypes.UpdateProductCategoryDTO,
|
||||
@MedusaContext() context: Context = {}
|
||||
context: Context = {}
|
||||
): Promise<ProductCategory> {
|
||||
const categoryData = { ...data }
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
@@ -267,7 +257,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
|
||||
protected fetchReorderConditions(
|
||||
productCategory: ProductCategory,
|
||||
data: ProductCategoryServiceTypes.UpdateProductCategoryDTO,
|
||||
shouldDeleteElement: boolean = false
|
||||
shouldDeleteElement = false
|
||||
): ReorderConditions {
|
||||
const originalParentId = productCategory.parent_category_id || null
|
||||
const targetParentId = data.parent_category_id
|
||||
@@ -405,7 +395,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
|
||||
}
|
||||
|
||||
if (!isDefined(sibling.rank)) {
|
||||
throw "error"
|
||||
throw new Error("sibling rank is not defined")
|
||||
}
|
||||
|
||||
const rank = shouldIncrementRank ? ++sibling.rank : --sibling.rank
|
||||
|
||||
@@ -6,18 +6,15 @@ import {
|
||||
} from "@mikro-orm/core"
|
||||
import { Context, DAL, ProductTypes } from "@medusajs/types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import {
|
||||
DALUtils,
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
MedusaError,
|
||||
} from "@medusajs/utils"
|
||||
import { DALUtils, MedusaError } from "@medusajs/utils"
|
||||
|
||||
// eslint-disable-next-line max-len
|
||||
export class ProductCollectionRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
}
|
||||
@@ -64,23 +61,17 @@ export class ProductCollectionRepository extends DALUtils.MikroOrmBaseRepository
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
async delete(
|
||||
collectionIds: string[],
|
||||
@MedusaContext()
|
||||
{ transactionManager: manager }: Context = {}
|
||||
): Promise<void> {
|
||||
await (manager as SqlEntityManager).nativeDelete(
|
||||
async delete(collectionIds: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(
|
||||
ProductCollection,
|
||||
{ id: { $in: collectionIds } },
|
||||
{}
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
async create(
|
||||
data: ProductTypes.CreateProductCollectionDTO[],
|
||||
@MedusaContext()
|
||||
context: Context = {}
|
||||
): Promise<ProductCollection[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
@@ -89,15 +80,13 @@ export class ProductCollectionRepository extends DALUtils.MikroOrmBaseRepository
|
||||
return manager.create(ProductCollection, collectionData)
|
||||
})
|
||||
|
||||
await manager.persist(productCollections)
|
||||
manager.persist(productCollections)
|
||||
|
||||
return productCollections
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
async update(
|
||||
data: ProductTypes.UpdateProductCollectionDTO[],
|
||||
@MedusaContext()
|
||||
context: Context = {}
|
||||
): Promise<ProductCollection[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
@@ -133,7 +122,7 @@ export class ProductCollectionRepository extends DALUtils.MikroOrmBaseRepository
|
||||
return manager.assign(existingCollection, collectionData)
|
||||
})
|
||||
|
||||
await manager.persist(productCollections)
|
||||
manager.persist(productCollections)
|
||||
|
||||
return productCollections
|
||||
}
|
||||
|
||||
@@ -6,17 +6,15 @@ import {
|
||||
import { Context, DAL } from "@medusajs/types"
|
||||
import { Image, Product } from "@models"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import {
|
||||
DALUtils,
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
} from "@medusajs/utils"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
|
||||
// eslint-disable-next-line max-len
|
||||
export class ProductImageRepository extends DALUtils.MikroOrmAbstractBaseRepository<Image> {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
}
|
||||
@@ -61,13 +59,8 @@ export class ProductImageRepository extends DALUtils.MikroOrmAbstractBaseReposit
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
async upsert(
|
||||
urls: string[],
|
||||
@MedusaContext()
|
||||
context: Context = {}
|
||||
): Promise<Image[]> {
|
||||
const { transactionManager: manager } = context
|
||||
async upsert(urls: string[], context: Context = {}): Promise<Image[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const existingImages = await this.find(
|
||||
{
|
||||
@@ -98,32 +91,19 @@ export class ProductImageRepository extends DALUtils.MikroOrmAbstractBaseReposit
|
||||
})
|
||||
|
||||
if (imageToCreate.length) {
|
||||
await (manager as SqlEntityManager).persist(imageToCreate)
|
||||
manager.persist(imageToCreate)
|
||||
upsertedImgs.push(...imageToCreate)
|
||||
}
|
||||
|
||||
return upsertedImgs
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
async delete(
|
||||
ids: string[],
|
||||
@MedusaContext()
|
||||
{ transactionManager: manager }: Context = {}
|
||||
): Promise<void> {
|
||||
await (manager as SqlEntityManager).nativeDelete(
|
||||
Product,
|
||||
{ id: { $in: ids } },
|
||||
{}
|
||||
)
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(Product, { id: { $in: ids } }, {})
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
async create(
|
||||
data: unknown[],
|
||||
@MedusaContext()
|
||||
{ transactionManager: manager }: Context = {}
|
||||
): Promise<Image[]> {
|
||||
async create(data: unknown[], context: Context = {}): Promise<Image[]> {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,18 +6,15 @@ import {
|
||||
import { Product, ProductOption } from "@models"
|
||||
import { Context, DAL, ProductTypes } from "@medusajs/types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import {
|
||||
DALUtils,
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
MedusaError,
|
||||
} from "@medusajs/utils"
|
||||
import { DALUtils, MedusaError } from "@medusajs/utils"
|
||||
|
||||
// eslint-disable-next-line max-len
|
||||
export class ProductOptionRepository extends DALUtils.MikroOrmAbstractBaseRepository<ProductOption> {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
}
|
||||
@@ -62,12 +59,7 @@ export class ProductOptionRepository extends DALUtils.MikroOrmAbstractBaseReposi
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
async delete(
|
||||
ids: string[],
|
||||
@MedusaContext()
|
||||
context: Context = {}
|
||||
): Promise<void> {
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
await (manager as SqlEntityManager).nativeDelete(
|
||||
@@ -77,10 +69,8 @@ export class ProductOptionRepository extends DALUtils.MikroOrmAbstractBaseReposi
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
async create(
|
||||
data: ProductTypes.CreateProductOptionDTO[],
|
||||
@MedusaContext()
|
||||
context: Context = {}
|
||||
): Promise<ProductOption[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
@@ -113,15 +103,13 @@ export class ProductOptionRepository extends DALUtils.MikroOrmAbstractBaseReposi
|
||||
return manager.create(ProductOption, optionData)
|
||||
})
|
||||
|
||||
await manager.persist(productOptions)
|
||||
manager.persist(productOptions)
|
||||
|
||||
return productOptions
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
async update(
|
||||
data: ProductTypes.UpdateProductOptionDTO[],
|
||||
@MedusaContext()
|
||||
context: Context = {}
|
||||
): Promise<ProductOption[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
@@ -157,7 +145,7 @@ export class ProductOptionRepository extends DALUtils.MikroOrmAbstractBaseReposi
|
||||
return manager.assign(existingOption, optionData)
|
||||
})
|
||||
|
||||
await manager.persist(productOptions)
|
||||
manager.persist(productOptions)
|
||||
|
||||
return productOptions
|
||||
}
|
||||
|
||||
@@ -13,18 +13,14 @@ import {
|
||||
UpsertProductTagDTO,
|
||||
} from "@medusajs/types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import {
|
||||
DALUtils,
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
MedusaError,
|
||||
} from "@medusajs/utils"
|
||||
import { DALUtils, MedusaError } from "@medusajs/utils"
|
||||
|
||||
export class ProductTagRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
}
|
||||
@@ -69,10 +65,8 @@ export class ProductTagRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
async create(
|
||||
data: CreateProductTagDTO[],
|
||||
@MedusaContext()
|
||||
context: Context = {}
|
||||
): Promise<ProductTag[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
@@ -81,15 +75,13 @@ export class ProductTagRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
return manager.create(ProductTag, tagData)
|
||||
})
|
||||
|
||||
await manager.persist(productTags)
|
||||
manager.persist(productTags)
|
||||
|
||||
return productTags
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
async update(
|
||||
data: UpdateProductTagDTO[],
|
||||
@MedusaContext()
|
||||
context: Context = {}
|
||||
): Promise<ProductTag[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
@@ -122,18 +114,16 @@ export class ProductTagRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
return manager.assign(existingTag, tagData)
|
||||
})
|
||||
|
||||
await manager.persist(productTags)
|
||||
manager.persist(productTags)
|
||||
|
||||
return productTags
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
async upsert(
|
||||
tags: UpsertProductTagDTO[],
|
||||
@MedusaContext()
|
||||
context: Context = {}
|
||||
): Promise<ProductTag[]> {
|
||||
const { transactionManager: manager } = context
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const tagsValues = tags.map((tag) => tag.value)
|
||||
const existingTags = await this.find(
|
||||
{
|
||||
@@ -169,19 +159,14 @@ export class ProductTagRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
newTags.push((manager as SqlEntityManager).create(ProductTag, tag))
|
||||
})
|
||||
|
||||
await (manager as SqlEntityManager).persist(newTags)
|
||||
manager.persist(newTags)
|
||||
upsertedTags.push(...newTags)
|
||||
}
|
||||
|
||||
return upsertedTags
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
async delete(
|
||||
ids: string[],
|
||||
@MedusaContext()
|
||||
context: Context = {}
|
||||
): Promise<void> {
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
await manager.nativeDelete(ProductTag, { id: { $in: ids } }, {})
|
||||
|
||||
@@ -12,18 +12,14 @@ import {
|
||||
UpdateProductTypeDTO,
|
||||
} from "@medusajs/types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import {
|
||||
DALUtils,
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
MedusaError,
|
||||
} from "@medusajs/utils"
|
||||
import { DALUtils, MedusaError } from "@medusajs/utils"
|
||||
|
||||
export class ProductTypeRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
}
|
||||
@@ -68,13 +64,11 @@ export class ProductTypeRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
async upsert(
|
||||
types: CreateProductTypeDTO[],
|
||||
@MedusaContext()
|
||||
context: Context = {}
|
||||
): Promise<ProductType[]> {
|
||||
const { transactionManager: manager } = context
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const typesValues = types.map((type) => type.value)
|
||||
const existingTypes = await this.find(
|
||||
@@ -111,30 +105,20 @@ export class ProductTypeRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
newTypes.push((manager as SqlEntityManager).create(ProductType, type))
|
||||
})
|
||||
|
||||
await (manager as SqlEntityManager).persist(newTypes)
|
||||
manager.persist(newTypes)
|
||||
upsertedTypes.push(...newTypes)
|
||||
}
|
||||
|
||||
return upsertedTypes
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
async delete(
|
||||
ids: string[],
|
||||
@MedusaContext()
|
||||
{ transactionManager: manager }: Context = {}
|
||||
): Promise<void> {
|
||||
await (manager as SqlEntityManager).nativeDelete(
|
||||
ProductType,
|
||||
{ id: { $in: ids } },
|
||||
{}
|
||||
)
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(ProductType, { id: { $in: ids } }, {})
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
async create(
|
||||
data: CreateProductTypeDTO[],
|
||||
@MedusaContext()
|
||||
context: Context = {}
|
||||
): Promise<ProductType[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
@@ -143,15 +127,13 @@ export class ProductTypeRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
return manager.create(ProductType, typeData)
|
||||
})
|
||||
|
||||
await manager.persist(productTypes)
|
||||
manager.persist(productTypes)
|
||||
|
||||
return productTypes
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
async update(
|
||||
data: UpdateProductTypeDTO[],
|
||||
@MedusaContext()
|
||||
context: Context = {}
|
||||
): Promise<ProductType[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
@@ -184,7 +166,7 @@ export class ProductTypeRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
return manager.assign(existingType, typeData)
|
||||
})
|
||||
|
||||
await manager.persist(productTypes)
|
||||
manager.persist(productTypes)
|
||||
|
||||
return productTypes
|
||||
}
|
||||
|
||||
@@ -16,11 +16,13 @@ import {
|
||||
|
||||
import { ProductVariantServiceTypes } from "../types/services"
|
||||
|
||||
// eslint-disable-next-line max-len
|
||||
export class ProductVariantRepository extends DALUtils.MikroOrmAbstractBaseRepository<ProductVariant> {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
}
|
||||
@@ -78,17 +80,16 @@ export class ProductVariantRepository extends DALUtils.MikroOrmAbstractBaseRepos
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
async create(
|
||||
data: RequiredEntityData<ProductVariant>[],
|
||||
@MedusaContext()
|
||||
{ transactionManager: manager }: Context = {}
|
||||
context: Context = {}
|
||||
): Promise<ProductVariant[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const variants = data.map((variant) => {
|
||||
return (manager as SqlEntityManager).create(ProductVariant, variant)
|
||||
})
|
||||
|
||||
await (manager as SqlEntityManager).persist(variants)
|
||||
manager.persist(variants)
|
||||
|
||||
return variants
|
||||
}
|
||||
@@ -100,8 +101,7 @@ export class ProductVariantRepository extends DALUtils.MikroOrmAbstractBaseRepos
|
||||
>[],
|
||||
context: Context = {}
|
||||
): Promise<ProductVariant[]> {
|
||||
const manager = (context.transactionManager ??
|
||||
this.manager_) as SqlEntityManager
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const productVariantsToUpdate = await manager.find(ProductVariant, {
|
||||
id: data.map((updateData) => updateData.id),
|
||||
@@ -124,7 +124,7 @@ export class ProductVariantRepository extends DALUtils.MikroOrmAbstractBaseRepos
|
||||
return manager.assign(productVariant, variantData)
|
||||
})
|
||||
|
||||
await manager.persist(variants)
|
||||
manager.persist(variants)
|
||||
|
||||
return variants
|
||||
}
|
||||
|
||||
@@ -19,21 +19,17 @@ import {
|
||||
WithRequiredProperty,
|
||||
} from "@medusajs/types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import {
|
||||
DALUtils,
|
||||
InjectTransactionManager,
|
||||
isDefined,
|
||||
MedusaContext,
|
||||
MedusaError,
|
||||
} from "@medusajs/utils"
|
||||
import { DALUtils, isDefined, MedusaError } from "@medusajs/utils"
|
||||
|
||||
import { ProductServiceTypes } from "../types/services"
|
||||
|
||||
// eslint-disable-next-line max-len
|
||||
export class ProductRepository extends DALUtils.MikroOrmAbstractBaseRepository<Product> {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
}
|
||||
@@ -118,46 +114,36 @@ export class ProductRepository extends DALUtils.MikroOrmAbstractBaseRepository<P
|
||||
}
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
async delete(
|
||||
ids: string[],
|
||||
@MedusaContext()
|
||||
{ transactionManager: manager }: Context = {}
|
||||
): Promise<void> {
|
||||
await (manager as SqlEntityManager).nativeDelete(
|
||||
Product,
|
||||
{ id: { $in: ids } },
|
||||
{}
|
||||
)
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(Product, { id: { $in: ids } }, {})
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
async create(
|
||||
data: WithRequiredProperty<ProductTypes.CreateProductOnlyDTO, "status">[],
|
||||
@MedusaContext()
|
||||
{ transactionManager: manager }: Context = {}
|
||||
context: Context = {}
|
||||
): Promise<Product[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const products = data.map((product) => {
|
||||
return (manager as SqlEntityManager).create(Product, product)
|
||||
})
|
||||
|
||||
await (manager as SqlEntityManager).persist(products)
|
||||
manager.persist(products)
|
||||
|
||||
return products
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
async update(
|
||||
data: WithRequiredProperty<ProductServiceTypes.UpdateProductDTO, "id">[],
|
||||
@MedusaContext() context: Context = {}
|
||||
context: Context = {}
|
||||
): Promise<Product[]> {
|
||||
let categoryIds: string[] = []
|
||||
let tagIds: string[] = []
|
||||
let collectionIds: string[] = []
|
||||
let typeIds: string[] = []
|
||||
const collectionIds: string[] = []
|
||||
const typeIds: string[] = []
|
||||
// TODO: use the getter method (getActiveManager)
|
||||
const manager = (context.transactionManager ??
|
||||
this.manager_) as SqlEntityManager
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
data.forEach((productData) => {
|
||||
categoryIds = categoryIds.concat(
|
||||
@@ -259,7 +245,7 @@ export class ProductRepository extends DALUtils.MikroOrmAbstractBaseRepository<P
|
||||
const productCategory = categoriesToAssignMap.get(categoryData.id)
|
||||
|
||||
if (productCategory) {
|
||||
await product.categories.add(productCategory)
|
||||
product.categories.add(productCategory)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -273,7 +259,7 @@ export class ProductRepository extends DALUtils.MikroOrmAbstractBaseRepository<P
|
||||
!categoryIdsToAssignSet.has(existingCategory.id)
|
||||
)
|
||||
|
||||
await product.categories.remove(categoriesToDelete)
|
||||
product.categories.remove(categoriesToDelete)
|
||||
}
|
||||
|
||||
if (isDefined(tagsData)) {
|
||||
@@ -287,7 +273,7 @@ export class ProductRepository extends DALUtils.MikroOrmAbstractBaseRepository<P
|
||||
}
|
||||
|
||||
if (productTag) {
|
||||
await product.tags.add(productTag)
|
||||
product.tags.add(productTag)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -296,7 +282,7 @@ export class ProductRepository extends DALUtils.MikroOrmAbstractBaseRepository<P
|
||||
.getItems()
|
||||
.filter((existingTag) => !tagIdsToAssignSet.has(existingTag.id))
|
||||
|
||||
await product.tags.remove(tagsToDelete)
|
||||
product.tags.remove(tagsToDelete)
|
||||
}
|
||||
|
||||
if (isDefined(collectionId)) {
|
||||
@@ -317,7 +303,7 @@ export class ProductRepository extends DALUtils.MikroOrmAbstractBaseRepository<P
|
||||
})
|
||||
)
|
||||
|
||||
await manager.persist(products)
|
||||
manager.persist(products)
|
||||
|
||||
return products
|
||||
}
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
import { ProductOption } from "@models"
|
||||
import {
|
||||
Context,
|
||||
DAL,
|
||||
FindConfig,
|
||||
ProductTypes,
|
||||
} from "@medusajs/types"
|
||||
import { Context, DAL, FindConfig, ProductTypes } from "@medusajs/types"
|
||||
import { ProductOptionRepository } from "@repositories"
|
||||
import {
|
||||
InjectTransactionManager,
|
||||
@@ -36,10 +31,7 @@ export default class ProductOptionService<
|
||||
config: FindConfig<ProductTypes.ProductOptionDTO> = {},
|
||||
@MedusaContext() sharedContext?: Context
|
||||
): Promise<TEntity> {
|
||||
return (await retrieveEntity<
|
||||
ProductOption,
|
||||
ProductTypes.ProductOptionDTO
|
||||
>({
|
||||
return (await retrieveEntity<ProductOption, ProductTypes.ProductOptionDTO>({
|
||||
id: productOptionId,
|
||||
entityName: ProductOption.name,
|
||||
repository: this.productOptionRepository_,
|
||||
@@ -74,9 +66,12 @@ export default class ProductOptionService<
|
||||
|
||||
private buildQueryForList(
|
||||
filters: ProductTypes.FilterableProductOptionProps = {},
|
||||
config: FindConfig<ProductTypes.ProductOptionDTO> = {},
|
||||
config: FindConfig<ProductTypes.ProductOptionDTO> = {}
|
||||
) {
|
||||
const queryOptions = ModulesSdkUtils.buildQuery<ProductOption>(filters, config)
|
||||
const queryOptions = ModulesSdkUtils.buildQuery<ProductOption>(
|
||||
filters,
|
||||
config
|
||||
)
|
||||
|
||||
if (filters.title) {
|
||||
queryOptions.where["title"] = { $ilike: filters.title }
|
||||
@@ -102,10 +97,9 @@ export default class ProductOptionService<
|
||||
data: ProductTypes.UpdateProductOptionDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
return (await (this.productOptionRepository_ as ProductOptionRepository).update(
|
||||
data,
|
||||
sharedContext
|
||||
)) as TEntity[]
|
||||
return (await (
|
||||
this.productOptionRepository_ as ProductOptionRepository
|
||||
).update(data, sharedContext)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(doNotForceTransaction, "productOptionRepository_")
|
||||
|
||||
@@ -38,10 +38,7 @@ export default class ProductTagService<
|
||||
config: FindConfig<ProductTypes.ProductTagDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity> {
|
||||
return (await retrieveEntity<
|
||||
ProductTag,
|
||||
ProductTypes.ProductTagDTO
|
||||
>({
|
||||
return (await retrieveEntity<ProductTag, ProductTypes.ProductTagDTO>({
|
||||
id: productTagId,
|
||||
entityName: ProductTag.name,
|
||||
repository: this.productTagRepository_,
|
||||
@@ -76,7 +73,7 @@ export default class ProductTagService<
|
||||
|
||||
private buildQueryForList(
|
||||
filters: ProductTypes.FilterableProductTagProps = {},
|
||||
config: FindConfig<ProductTypes.ProductTagDTO> = {},
|
||||
config: FindConfig<ProductTypes.ProductTagDTO> = {}
|
||||
) {
|
||||
const queryOptions = ModulesSdkUtils.buildQuery<ProductTag>(filters, config)
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import {
|
||||
UpdateProductTypeDTO,
|
||||
DAL,
|
||||
FindConfig,
|
||||
ProductTypes
|
||||
ProductTypes,
|
||||
} from "@medusajs/types"
|
||||
import { ProductTypeRepository } from "@repositories"
|
||||
import {
|
||||
@@ -38,10 +38,7 @@ export default class ProductTypeService<
|
||||
config: FindConfig<ProductTypes.ProductTypeDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity> {
|
||||
return (await retrieveEntity<
|
||||
ProductType,
|
||||
ProductTypes.ProductTypeDTO
|
||||
>({
|
||||
return (await retrieveEntity<ProductType, ProductTypes.ProductTypeDTO>({
|
||||
id: productTypeId,
|
||||
entityName: ProductType.name,
|
||||
repository: this.productTypeRepository_,
|
||||
@@ -76,9 +73,12 @@ export default class ProductTypeService<
|
||||
|
||||
private buildQueryForList(
|
||||
filters: ProductTypes.FilterableProductTypeProps = {},
|
||||
config: FindConfig<ProductTypes.ProductTypeDTO> = {},
|
||||
config: FindConfig<ProductTypes.ProductTypeDTO> = {}
|
||||
) {
|
||||
const queryOptions = ModulesSdkUtils.buildQuery<ProductType>(filters, config)
|
||||
const queryOptions = ModulesSdkUtils.buildQuery<ProductType>(
|
||||
filters,
|
||||
config
|
||||
)
|
||||
|
||||
if (filters.value) {
|
||||
queryOptions.where["value"] = { $ilike: filters.value }
|
||||
|
||||
@@ -138,9 +138,11 @@ export default class ProductVariantService<
|
||||
const variantsData = [...data]
|
||||
variantsData.forEach((variant) => Object.assign(variant, { product }))
|
||||
|
||||
return await (this.productVariantRepository_ as ProductVariantRepository).update(variantsData, {
|
||||
return (await (
|
||||
this.productVariantRepository_ as ProductVariantRepository
|
||||
).update(variantsData, {
|
||||
transactionManager: sharedContext.transactionManager,
|
||||
}) as TEntity[]
|
||||
})) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(doNotForceTransaction, "productVariantRepository_")
|
||||
|
||||
@@ -26,4 +26,4 @@ export interface UpdateProductCategoryDTO {
|
||||
rank?: number
|
||||
parent_category_id?: string | null
|
||||
metadata?: Record<string, unknown>
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,4 +6,4 @@ export enum ProductCollectionEvents {
|
||||
COLLECTION_UPDATED = "product-collection.updated",
|
||||
COLLECTION_CREATED = "product-collection.created",
|
||||
COLLECTION_DELETED = "product-collection.deleted",
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user