Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(product): remove decorator where it is not necessary and cleanup #4731

Merged
merged 2 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/twenty-gorillas-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/product": patch
---

chore(product): remove decorator where it is not necessary and cleanup
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {
EmitData,
EventBusTypes,
IEventBusModuleService,
Subscriber,
IEventBusModuleService
} from "@medusajs/types"

export class EventBusService implements IEventBusModuleService {
Expand All @@ -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,
Expand All @@ -30,7 +31,5 @@ export class EventBusService implements IEventBusModuleService {
return this
}

withTransaction() {

}
withTransaction() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
},
]

Expand Down Expand Up @@ -65,4 +65,3 @@ export const productCategoriesRankData = [
rank: 2,
},
]

Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
]
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ProductType,
ProductVariant,
} from "@models"

import ProductOption from "../../../src/models/product-option"

export * from "./data/create-product"
Expand Down
34 changes: 12 additions & 22 deletions packages/product/src/repositories/product-category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
}
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
})
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
27 changes: 8 additions & 19 deletions packages/product/src/repositories/product-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -133,7 +122,7 @@ export class ProductCollectionRepository extends DALUtils.MikroOrmBaseRepository
return manager.assign(existingCollection, collectionData)
})

await manager.persist(productCollections)
manager.persist(productCollections)

return productCollections
}
Expand Down
40 changes: 10 additions & 30 deletions packages/product/src/repositories/product-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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(
{
Expand Down Expand Up @@ -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.")
}
}
Loading