From f8d3d5f91ac6282958911fe2f1973ad81f459747 Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Thu, 10 Aug 2023 09:57:32 +0200 Subject: [PATCH] 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 --- .changeset/twenty-gorillas-exist.md | 5 ++ .../__fixtures__/event-bus/index.ts | 9 ++-- .../product-category/data/index.ts | 5 +- .../__fixtures__/product/data/categories.ts | 6 +-- .../__fixtures__/product/index.ts | 1 + .../src/repositories/product-category.ts | 34 +++++------- .../src/repositories/product-collection.ts | 27 +++------- .../product/src/repositories/product-image.ts | 40 ++++---------- .../src/repositories/product-option.ts | 24 +++------ .../product/src/repositories/product-tag.ts | 29 +++-------- .../product/src/repositories/product-type.ts | 36 ++++--------- .../src/repositories/product-variant.ts | 14 ++--- packages/product/src/repositories/product.ts | 52 +++++++------------ .../product/src/services/product-option.ts | 26 ++++------ packages/product/src/services/product-tag.ts | 7 +-- packages/product/src/services/product-type.ts | 14 ++--- .../product/src/services/product-variant.ts | 6 ++- .../src/types/services/product-category.ts | 2 +- .../src/types/services/product-collection.ts | 2 +- 19 files changed, 118 insertions(+), 221 deletions(-) create mode 100644 .changeset/twenty-gorillas-exist.md diff --git a/.changeset/twenty-gorillas-exist.md b/.changeset/twenty-gorillas-exist.md new file mode 100644 index 0000000000..6b314cdec4 --- /dev/null +++ b/.changeset/twenty-gorillas-exist.md @@ -0,0 +1,5 @@ +--- +"@medusajs/product": patch +--- + +chore(product): remove decorator where it is not necessary and cleanup diff --git a/packages/product/integration-tests/__fixtures__/event-bus/index.ts b/packages/product/integration-tests/__fixtures__/event-bus/index.ts index ce67d6c00e..3570043f08 100644 --- a/packages/product/integration-tests/__fixtures__/event-bus/index.ts +++ b/packages/product/integration-tests/__fixtures__/event-bus/index.ts @@ -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 ): Promise + async emit(data: EventBusTypes.EmitData[]): Promise + async emit[] = string>( eventOrData: TInput, data?: T, @@ -30,7 +31,5 @@ export class EventBusService implements IEventBusModuleService { return this } - withTransaction() { - - } + withTransaction() {} } diff --git a/packages/product/integration-tests/__fixtures__/product-category/data/index.ts b/packages/product/integration-tests/__fixtures__/product-category/data/index.ts index 28f9f5b908..65231d6fcb 100644 --- a/packages/product/integration-tests/__fixtures__/product-category/data/index.ts +++ b/packages/product/integration-tests/__fixtures__/product-category/data/index.ts @@ -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, }, ] - diff --git a/packages/product/integration-tests/__fixtures__/product/data/categories.ts b/packages/product/integration-tests/__fixtures__/product/data/categories.ts index 50e78f3bf4..65bb62c5ad 100644 --- a/packages/product/integration-tests/__fixtures__/product/data/categories.ts +++ b/packages/product/integration-tests/__fixtures__/product/data/categories.ts @@ -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", }, ] diff --git a/packages/product/integration-tests/__fixtures__/product/index.ts b/packages/product/integration-tests/__fixtures__/product/index.ts index 6709b496fc..56ff57c763 100644 --- a/packages/product/integration-tests/__fixtures__/product/index.ts +++ b/packages/product/integration-tests/__fixtures__/product/index.ts @@ -8,6 +8,7 @@ import { ProductType, ProductVariant, } from "@models" + import ProductOption from "../../../src/models/product-option" export * from "./data/create-product" diff --git a/packages/product/src/repositories/product-category.ts b/packages/product/src/repositories/product-category.ts index 1295cd48c1..ae602c7d3c 100644 --- a/packages/product/src/repositories/product-category.ts +++ b/packages/product/src/repositories/product-category.ts @@ -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 { + async delete(id: string, context: Context = {}): Promise { const manager = this.getActiveManager(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 { const categoryData = { ...data } - const manager = this.getActiveManager(sharedContext) + const manager = this.getActiveManager(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 { const categoryData = { ...data } const manager = this.getActiveManager(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 diff --git a/packages/product/src/repositories/product-collection.ts b/packages/product/src/repositories/product-collection.ts index d5825f96aa..fe97e1c1af 100644 --- a/packages/product/src/repositories/product-collection.ts +++ b/packages/product/src/repositories/product-collection.ts @@ -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 { - await (manager as SqlEntityManager).nativeDelete( + async delete(collectionIds: string[], context: Context = {}): Promise { + const manager = this.getActiveManager(context) + await manager.nativeDelete( ProductCollection, { id: { $in: collectionIds } }, {} ) } - @InjectTransactionManager() async create( data: ProductTypes.CreateProductCollectionDTO[], - @MedusaContext() context: Context = {} ): Promise { const manager = this.getActiveManager(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 { const manager = this.getActiveManager(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 } diff --git a/packages/product/src/repositories/product-image.ts b/packages/product/src/repositories/product-image.ts index 8df0c2f154..8a1a325aad 100644 --- a/packages/product/src/repositories/product-image.ts +++ b/packages/product/src/repositories/product-image.ts @@ -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 { 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 { - const { transactionManager: manager } = context + async upsert(urls: string[], context: Context = {}): Promise { + const manager = this.getActiveManager(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 { - await (manager as SqlEntityManager).nativeDelete( - Product, - { id: { $in: ids } }, - {} - ) + async delete(ids: string[], context: Context = {}): Promise { + const manager = this.getActiveManager(context) + await manager.nativeDelete(Product, { id: { $in: ids } }, {}) } - @InjectTransactionManager() - async create( - data: unknown[], - @MedusaContext() - { transactionManager: manager }: Context = {} - ): Promise { + async create(data: unknown[], context: Context = {}): Promise { throw new Error("Method not implemented.") } } diff --git a/packages/product/src/repositories/product-option.ts b/packages/product/src/repositories/product-option.ts index 937d6334a8..956d65f377 100644 --- a/packages/product/src/repositories/product-option.ts +++ b/packages/product/src/repositories/product-option.ts @@ -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 { 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 { + async delete(ids: string[], context: Context = {}): Promise { const manager = this.getActiveManager(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 { const manager = this.getActiveManager(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 { const manager = this.getActiveManager(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 } diff --git a/packages/product/src/repositories/product-tag.ts b/packages/product/src/repositories/product-tag.ts index d29f185776..fb04e13d6b 100644 --- a/packages/product/src/repositories/product-tag.ts +++ b/packages/product/src/repositories/product-tag.ts @@ -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 { const manager = this.getActiveManager(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 { const manager = this.getActiveManager(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 { - const { transactionManager: manager } = context + const manager = this.getActiveManager(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 { + async delete(ids: string[], context: Context = {}): Promise { const manager = this.getActiveManager(context) await manager.nativeDelete(ProductTag, { id: { $in: ids } }, {}) diff --git a/packages/product/src/repositories/product-type.ts b/packages/product/src/repositories/product-type.ts index 132a9e021d..b23989f538 100644 --- a/packages/product/src/repositories/product-type.ts +++ b/packages/product/src/repositories/product-type.ts @@ -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 { - const { transactionManager: manager } = context + const manager = this.getActiveManager(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 { - await (manager as SqlEntityManager).nativeDelete( - ProductType, - { id: { $in: ids } }, - {} - ) + async delete(ids: string[], context: Context = {}): Promise { + const manager = this.getActiveManager(context) + await manager.nativeDelete(ProductType, { id: { $in: ids } }, {}) } - @InjectTransactionManager() async create( data: CreateProductTypeDTO[], - @MedusaContext() context: Context = {} ): Promise { const manager = this.getActiveManager(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 { const manager = this.getActiveManager(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 } diff --git a/packages/product/src/repositories/product-variant.ts b/packages/product/src/repositories/product-variant.ts index 96500be02b..e525fe30ab 100644 --- a/packages/product/src/repositories/product-variant.ts +++ b/packages/product/src/repositories/product-variant.ts @@ -16,11 +16,13 @@ import { import { ProductVariantServiceTypes } from "../types/services" +// eslint-disable-next-line max-len export class ProductVariantRepository extends DALUtils.MikroOrmAbstractBaseRepository { 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[], - @MedusaContext() - { transactionManager: manager }: Context = {} + context: Context = {} ): Promise { + const manager = this.getActiveManager(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 { - const manager = (context.transactionManager ?? - this.manager_) as SqlEntityManager + const manager = this.getActiveManager(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 } diff --git a/packages/product/src/repositories/product.ts b/packages/product/src/repositories/product.ts index 43899ecfa6..4dbc947c4a 100644 --- a/packages/product/src/repositories/product.ts +++ b/packages/product/src/repositories/product.ts @@ -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 { 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

{ - await (manager as SqlEntityManager).nativeDelete( - Product, - { id: { $in: ids } }, - {} - ) + async delete(ids: string[], context: Context = {}): Promise { + const manager = this.getActiveManager(context) + await manager.nativeDelete(Product, { id: { $in: ids } }, {}) } - @InjectTransactionManager() async create( data: WithRequiredProperty[], - @MedusaContext() - { transactionManager: manager }: Context = {} + context: Context = {} ): Promise { + const manager = this.getActiveManager(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[], - @MedusaContext() context: Context = {} + context: Context = {} ): Promise { 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(context) data.forEach((productData) => { categoryIds = categoryIds.concat( @@ -259,7 +245,7 @@ export class ProductRepository extends DALUtils.MikroOrmAbstractBaseRepository

!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

= {}, @MedusaContext() sharedContext?: Context ): Promise { - return (await retrieveEntity< - ProductOption, - ProductTypes.ProductOptionDTO - >({ + return (await retrieveEntity({ id: productOptionId, entityName: ProductOption.name, repository: this.productOptionRepository_, @@ -74,9 +66,12 @@ export default class ProductOptionService< private buildQueryForList( filters: ProductTypes.FilterableProductOptionProps = {}, - config: FindConfig = {}, + config: FindConfig = {} ) { - const queryOptions = ModulesSdkUtils.buildQuery(filters, config) + const queryOptions = ModulesSdkUtils.buildQuery( + 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 { - 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_") diff --git a/packages/product/src/services/product-tag.ts b/packages/product/src/services/product-tag.ts index 91874fd22e..6c5aa39606 100644 --- a/packages/product/src/services/product-tag.ts +++ b/packages/product/src/services/product-tag.ts @@ -38,10 +38,7 @@ export default class ProductTagService< config: FindConfig = {}, @MedusaContext() sharedContext: Context = {} ): Promise { - return (await retrieveEntity< - ProductTag, - ProductTypes.ProductTagDTO - >({ + return (await retrieveEntity({ id: productTagId, entityName: ProductTag.name, repository: this.productTagRepository_, @@ -76,7 +73,7 @@ export default class ProductTagService< private buildQueryForList( filters: ProductTypes.FilterableProductTagProps = {}, - config: FindConfig = {}, + config: FindConfig = {} ) { const queryOptions = ModulesSdkUtils.buildQuery(filters, config) diff --git a/packages/product/src/services/product-type.ts b/packages/product/src/services/product-type.ts index 995a8beaa4..bbf1182aa5 100644 --- a/packages/product/src/services/product-type.ts +++ b/packages/product/src/services/product-type.ts @@ -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 = {}, @MedusaContext() sharedContext: Context = {} ): Promise { - return (await retrieveEntity< - ProductType, - ProductTypes.ProductTypeDTO - >({ + return (await retrieveEntity({ id: productTypeId, entityName: ProductType.name, repository: this.productTypeRepository_, @@ -76,9 +73,12 @@ export default class ProductTypeService< private buildQueryForList( filters: ProductTypes.FilterableProductTypeProps = {}, - config: FindConfig = {}, + config: FindConfig = {} ) { - const queryOptions = ModulesSdkUtils.buildQuery(filters, config) + const queryOptions = ModulesSdkUtils.buildQuery( + filters, + config + ) if (filters.value) { queryOptions.where["value"] = { $ilike: filters.value } diff --git a/packages/product/src/services/product-variant.ts b/packages/product/src/services/product-variant.ts index 36146378a7..010a2d3e30 100644 --- a/packages/product/src/services/product-variant.ts +++ b/packages/product/src/services/product-variant.ts @@ -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_") diff --git a/packages/product/src/types/services/product-category.ts b/packages/product/src/types/services/product-category.ts index 81021e628e..3a2c949834 100644 --- a/packages/product/src/types/services/product-category.ts +++ b/packages/product/src/types/services/product-category.ts @@ -26,4 +26,4 @@ export interface UpdateProductCategoryDTO { rank?: number parent_category_id?: string | null metadata?: Record -} \ No newline at end of file +} diff --git a/packages/product/src/types/services/product-collection.ts b/packages/product/src/types/services/product-collection.ts index 0d337c15a4..20b46495bb 100644 --- a/packages/product/src/types/services/product-collection.ts +++ b/packages/product/src/types/services/product-collection.ts @@ -6,4 +6,4 @@ export enum ProductCollectionEvents { COLLECTION_UPDATED = "product-collection.updated", COLLECTION_CREATED = "product-collection.created", COLLECTION_DELETED = "product-collection.deleted", -} \ No newline at end of file +}