From b6ac768698a3b49d0162cb49e628386f3352d034 Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Wed, 10 Jan 2024 14:12:02 +0100 Subject: [PATCH] chore: abstract the modules repository (#6035) **What** Reduce the work effort to create repositories when building new modules by abstracting the most common cases into the base class default implementation returned by a factory - [x] Migrate all modules Co-authored-by: Riqwan Thamir <5105988+riqwan@users.noreply.github.com> --- .changeset/swift-mice-leave.md | 9 + .../src/repositories/auth-provider.ts | 82 +----- .../src/repositories/auth-user.ts | 76 +---- .../link-modules/src/repositories/link.ts | 71 +---- .../services/price-list-rule/index.spec.ts | 2 +- .../services/price-rule/index.spec.ts | 2 +- .../services/price-set/index.spec.ts | 2 +- .../pricing-module/price-list-rule.spec.ts | 2 +- .../pricing-module/price-rule.spec.ts | 4 +- packages/pricing/src/repositories/currency.ts | 126 +------- .../pricing/src/repositories/money-amount.ts | 126 +------- .../src/repositories/price-list-rule-value.ts | 129 +-------- .../src/repositories/price-list-rule.ts | 120 +------- .../pricing/src/repositories/price-list.ts | 111 +------ .../pricing/src/repositories/price-rule.ts | 122 +------- .../price-set-money-amount-rules.ts | 126 +------- .../repositories/price-set-money-amount.ts | 128 +-------- .../src/repositories/price-set-rule-type.ts | 126 +------- .../pricing/src/repositories/price-set.ts | 126 +------- packages/pricing/src/repositories/pricing.ts | 6 +- .../pricing/src/repositories/rule-type.ts | 133 +-------- packages/pricing/src/services/rule-type.ts | 3 + .../src/repositories/product-category.ts | 31 +- .../src/repositories/product-collection.ts | 115 +------- .../product/src/repositories/product-image.ts | 73 +---- .../src/repositories/product-option-value.ts | 38 +-- .../src/repositories/product-option.ts | 123 +------- .../product/src/repositories/product-tag.ts | 123 +------- .../product/src/repositories/product-type.ts | 122 +------- .../src/repositories/product-variant.ts | 134 +-------- packages/product/src/repositories/product.ts | 65 +---- .../promotion-module/promotion.spec.ts | 2 +- .../src/repositories/application-method.ts | 126 +------- .../src/repositories/promotion-rule-value.ts | 128 +-------- .../src/repositories/promotion-rule.ts | 127 +------- .../promotion/src/repositories/promotion.ts | 129 +-------- .../src/dal/mikro-orm/mikro-orm-repository.ts | 270 ++++++++++++------ packages/utils/src/pricing/rule-type.ts | 1 + 38 files changed, 409 insertions(+), 2830 deletions(-) create mode 100644 .changeset/swift-mice-leave.md diff --git a/.changeset/swift-mice-leave.md b/.changeset/swift-mice-leave.md new file mode 100644 index 0000000000000..45ebd1788af8c --- /dev/null +++ b/.changeset/swift-mice-leave.md @@ -0,0 +1,9 @@ +--- +"@medusajs/authentication": patch +"@medusajs/link-modules": patch +"@medusajs/pricing": patch +"@medusajs/product": patch +"@medusajs/utils": patch +--- + +chore: Attempt to abstract the modules repository diff --git a/packages/authentication/src/repositories/auth-provider.ts b/packages/authentication/src/repositories/auth-provider.ts index b3c16f1fb2b2b..a0d1d0f543af8 100644 --- a/packages/authentication/src/repositories/auth-provider.ts +++ b/packages/authentication/src/repositories/auth-provider.ts @@ -1,86 +1,16 @@ -import { Context, DAL } from "@medusajs/types" import { DALUtils } from "@medusajs/utils" -import { - FilterQuery as MikroFilterQuery, - FindOptions as MikroOptions, - LoadStrategy, -} from "@mikro-orm/core" import { AuthProvider } from "@models" import { RepositoryTypes } from "@types" import { SqlEntityManager } from "@mikro-orm/postgresql" +import { Context } from "@medusajs/types" -export class AuthProviderRepository 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 - } - - async find( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.find( - AuthProvider, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async findAndCount( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise<[AuthProvider[], number]> { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.findAndCount( - AuthProvider, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async delete(ids: string[], context: Context = {}): Promise { - const manager = this.getActiveManager(context) - await manager.nativeDelete(AuthProvider, { provider: { $in: ids } }, {}) - } - - async create( - data: RepositoryTypes.CreateAuthProviderDTO[], - context: Context = {} - ): Promise { - const manager: SqlEntityManager = - this.getActiveManager(context) - - const authProviders = data.map((authProviderData) => { - return manager.create(AuthProvider, authProviderData) - }) - - manager.persist(authProviders) - - return authProviders +export class AuthProviderRepository extends DALUtils.mikroOrmBaseRepositoryFactory< + AuthProvider, + { + create: RepositoryTypes.CreateAuthProviderDTO } - +>(AuthProvider, "provider") { async update( data: RepositoryTypes.UpdateAuthProviderDTO[], context: Context = {} diff --git a/packages/authentication/src/repositories/auth-user.ts b/packages/authentication/src/repositories/auth-user.ts index 0ca1ec0091e36..2da807aa415d8 100644 --- a/packages/authentication/src/repositories/auth-user.ts +++ b/packages/authentication/src/repositories/auth-user.ts @@ -1,77 +1,17 @@ -import { Context, DAL } from "@medusajs/types" +import { Context } from "@medusajs/types" import { DALUtils } from "@medusajs/utils" -import { - FilterQuery as MikroFilterQuery, - FindOptions as MikroOptions, - LoadStrategy, -} from "@mikro-orm/core" import { AuthUser } from "@models" import { RepositoryTypes } from "@types" import { SqlEntityManager } from "@mikro-orm/postgresql" -export class AuthUserRepository 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 - } - - async find( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.find( - AuthUser, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async findAndCount( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise<[AuthUser[], number]> { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.findAndCount( - AuthUser, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async delete(ids: string[], context: Context = {}): Promise { - const manager = this.getActiveManager(context) - await manager.nativeDelete(AuthUser, { id: { $in: ids } }, {}) - } - +export class AuthUserRepository extends DALUtils.mikroOrmBaseRepositoryFactory( + AuthUser +) { async create( data: RepositoryTypes.CreateAuthUserDTO[], context: Context = {} ): Promise { - const manager: SqlEntityManager = - this.getActiveManager(context) - const toCreate = data.map((authUser) => { const authUserClone = { ...authUser } as any @@ -80,13 +20,7 @@ export class AuthUserRepository extends DALUtils.MikroOrmBaseRepository { return authUserClone }) - const authUsers = toCreate.map((authUserData) => { - return manager.create(AuthUser, authUserData) - }) - - manager.persist(authUsers) - - return authUsers + return await super.create(toCreate, context) } async update( diff --git a/packages/link-modules/src/repositories/link.ts b/packages/link-modules/src/repositories/link.ts index 66f2f1e14bf08..ee64ccf5004b0 100644 --- a/packages/link-modules/src/repositories/link.ts +++ b/packages/link-modules/src/repositories/link.ts @@ -1,77 +1,22 @@ -import { Context, FindOptions, ModuleJoinerConfig } from "@medusajs/types" -import { - EntitySchema, - LoadStrategy, - FilterQuery as MikroFilterQuery, - FindOptions as MikroOptions, -} from "@mikro-orm/core" +import { Context, ModuleJoinerConfig } from "@medusajs/types" +import { EntitySchema } from "@mikro-orm/core" import { - MikroOrmAbstractBaseRepository, generateEntityId, + mikroOrmBaseRepositoryFactory, } from "@medusajs/utils" import { SqlEntityManager } from "@mikro-orm/postgresql" export function getLinkRepository(model: EntitySchema) { - return class LinkRepository extends MikroOrmAbstractBaseRepository { - readonly manager_: SqlEntityManager - readonly model_: EntitySchema + return class LinkRepository extends mikroOrmBaseRepositoryFactory(model) { readonly joinerConfig_: ModuleJoinerConfig - constructor({ - manager, - joinerConfig, - }: { - manager: SqlEntityManager - joinerConfig: ModuleJoinerConfig - }) { + constructor({ joinerConfig }: { joinerConfig: ModuleJoinerConfig }) { // @ts-ignore super(...arguments) - this.manager_ = manager - this.model_ = model this.joinerConfig_ = joinerConfig } - async find( - findOptions: FindOptions = { where: {} }, - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.find( - this.model_, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async findAndCount( - findOptions: FindOptions = { where: {} }, - context: Context = {} - ): Promise<[object[], number]> { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.findAndCount( - this.model_, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - async delete(data: any, context: Context = {}): Promise { const filter = {} for (const key in data) { @@ -81,7 +26,7 @@ export function getLinkRepository(model: EntitySchema) { } const manager = this.getActiveManager(context) - await manager.nativeDelete(this.model_, data, {}) + await manager.nativeDelete(model, data, {}) } async create(data: object[], context: Context = {}): Promise { @@ -93,10 +38,10 @@ export function getLinkRepository(model: EntitySchema) { this.joinerConfig_.databaseConfig?.idPrefix ?? "link" ) - return manager.create(this.model_, link) + return manager.create(model, link) }) - await manager.upsertMany(this.model_, links) + await manager.upsertMany(model, links) return links } diff --git a/packages/pricing/integration-tests/__tests__/services/price-list-rule/index.spec.ts b/packages/pricing/integration-tests/__tests__/services/price-list-rule/index.spec.ts index 14ded5bd20239..ac968117988b5 100644 --- a/packages/pricing/integration-tests/__tests__/services/price-list-rule/index.spec.ts +++ b/packages/pricing/integration-tests/__tests__/services/price-list-rule/index.spec.ts @@ -212,7 +212,7 @@ describe("PriceListRule Service", () => { } expect(error.message).toEqual( - 'PriceListRule with id(s) "does-not-exist" not found' + 'PriceListRule with id "does-not-exist" not found' ) }) }) diff --git a/packages/pricing/integration-tests/__tests__/services/price-rule/index.spec.ts b/packages/pricing/integration-tests/__tests__/services/price-rule/index.spec.ts index b67e4ac247bc7..b917fbde9f31c 100644 --- a/packages/pricing/integration-tests/__tests__/services/price-rule/index.spec.ts +++ b/packages/pricing/integration-tests/__tests__/services/price-rule/index.spec.ts @@ -283,7 +283,7 @@ describe("PriceRule Service", () => { error = e } - expect(error.message).toEqual('PriceRule with id "undefined" not found') + expect(error.message).toEqual('PriceRule with id "" not found') }) it("should create a priceRule successfully", async () => { diff --git a/packages/pricing/integration-tests/__tests__/services/price-set/index.spec.ts b/packages/pricing/integration-tests/__tests__/services/price-set/index.spec.ts index 994e3a79edfa2..29ed3ffe07501 100644 --- a/packages/pricing/integration-tests/__tests__/services/price-set/index.spec.ts +++ b/packages/pricing/integration-tests/__tests__/services/price-set/index.spec.ts @@ -370,7 +370,7 @@ describe("PriceSet Service", () => { error = e } - expect(error.message).toEqual('PriceSet with id "undefined" not found') + expect(error.message).toEqual('PriceSet with id "" not found') }) it("should create a priceSet successfully", async () => { diff --git a/packages/pricing/integration-tests/__tests__/services/pricing-module/price-list-rule.spec.ts b/packages/pricing/integration-tests/__tests__/services/pricing-module/price-list-rule.spec.ts index b530b0ec3b222..a67d5dc57d0f3 100644 --- a/packages/pricing/integration-tests/__tests__/services/pricing-module/price-list-rule.spec.ts +++ b/packages/pricing/integration-tests/__tests__/services/pricing-module/price-list-rule.spec.ts @@ -213,7 +213,7 @@ describe("PriceListRule Service", () => { } expect(error.message).toEqual( - 'PriceListRule with id(s) "does-not-exist" not found' + 'PriceListRule with id "does-not-exist" not found' ) }) }) diff --git a/packages/pricing/integration-tests/__tests__/services/pricing-module/price-rule.spec.ts b/packages/pricing/integration-tests/__tests__/services/pricing-module/price-rule.spec.ts index 09c9216d755eb..7dd4989929ce5 100644 --- a/packages/pricing/integration-tests/__tests__/services/pricing-module/price-rule.spec.ts +++ b/packages/pricing/integration-tests/__tests__/services/pricing-module/price-rule.spec.ts @@ -1,7 +1,7 @@ import { CreatePriceRuleDTO, IPricingModuleService } from "@medusajs/types" import { SqlEntityManager } from "@mikro-orm/postgresql" -import { PriceSetMoneyAmount, initialize } from "../../../../src" +import { initialize, PriceSetMoneyAmount } from "../../../../src" import { createCurrencies } from "../../../__fixtures__/currency" import { createMoneyAmounts } from "../../../__fixtures__/money-amount" import { createPriceRules } from "../../../__fixtures__/price-rule" @@ -279,7 +279,7 @@ describe("PricingModule Service - PriceRule", () => { error = e } - expect(error.message).toEqual('PriceRule with id "undefined" not found') + expect(error.message).toEqual('PriceRule with id "" not found') }) it("should create a PriceRule successfully", async () => { diff --git a/packages/pricing/src/repositories/currency.ts b/packages/pricing/src/repositories/currency.ts index bef290edaa0e4..1aa87a531980b 100644 --- a/packages/pricing/src/repositories/currency.ts +++ b/packages/pricing/src/repositories/currency.ts @@ -1,124 +1,12 @@ -import { Context, DAL } from "@medusajs/types" -import { DALUtils, MedusaError } from "@medusajs/utils" -import { - LoadStrategy, - FilterQuery as MikroFilterQuery, - FindOptions as MikroOptions, -} from "@mikro-orm/core" +import { DALUtils } from "@medusajs/utils" import { Currency } from "@models" import { RepositoryTypes } from "@types" -import { SqlEntityManager } from "@mikro-orm/postgresql" -export class CurrencyRepository 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 - } - - async find( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.find( - Currency, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async findAndCount( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise<[Currency[], number]> { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.findAndCount( - Currency, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async delete(codes: string[], context: Context = {}): Promise { - const manager = this.getActiveManager(context) - await manager.nativeDelete(Currency, { code: { $in: codes } }, {}) - } - - async create( - data: RepositoryTypes.CreateCurrencyDTO[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const currencies = data.map((currencyData) => { - return manager.create(Currency, currencyData) - }) - - manager.persist(currencies) - - return currencies - } - - async update( - data: RepositoryTypes.UpdateCurrencyDTO[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - const currencyCodes = data.map((currencyData) => currencyData.code) - const existingCurrencies = await this.find( - { - where: { - code: { - $in: currencyCodes, - }, - }, - }, - context - ) - - const existingCurrencyMap = new Map( - existingCurrencies.map<[string, Currency]>((currency) => [ - currency.code, - currency, - ]) - ) - - const currencies = data.map((currencyData) => { - const existingCurrency = existingCurrencyMap.get(currencyData.code) - - if (!existingCurrency) { - throw new MedusaError( - MedusaError.Types.NOT_FOUND, - `Currency with code "${currencyData.code}" not found` - ) - } - - return manager.assign(existingCurrency, currencyData) - }) - - manager.persist(currencies) - - return currencies +export class CurrencyRepository extends DALUtils.mikroOrmBaseRepositoryFactory< + Currency, + { + create: RepositoryTypes.CreateCurrencyDTO + update: RepositoryTypes.UpdateCurrencyDTO } -} +>(Currency, "code") {} diff --git a/packages/pricing/src/repositories/money-amount.ts b/packages/pricing/src/repositories/money-amount.ts index e2ce6afebe54b..66595fc592301 100644 --- a/packages/pricing/src/repositories/money-amount.ts +++ b/packages/pricing/src/repositories/money-amount.ts @@ -1,124 +1,12 @@ -import { Context, DAL } from "@medusajs/types" -import { DALUtils, MedusaError } from "@medusajs/utils" -import { - FilterQuery as MikroFilterQuery, - FindOptions as MikroOptions, - LoadStrategy, -} from "@mikro-orm/core" +import { DALUtils } from "@medusajs/utils" import { MoneyAmount } from "@models" import { RepositoryTypes } from "@types" -import { SqlEntityManager } from "@mikro-orm/postgresql" -export class MoneyAmountRepository 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 - } - - async find( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.find( - MoneyAmount, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async findAndCount( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise<[MoneyAmount[], number]> { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.findAndCount( - MoneyAmount, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async delete(ids: string[], context: Context = {}): Promise { - const manager = this.getActiveManager(context) - await manager.nativeDelete(MoneyAmount, { id: { $in: ids } }, {}) - } - - async create( - data: RepositoryTypes.CreateMoneyAmountDTO[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const moneyAmounts = data.map((moneyAmountData) => { - return manager.create(MoneyAmount, moneyAmountData as any) - }) - - manager.persist(moneyAmounts) - - return moneyAmounts - } - - async update( - data: RepositoryTypes.UpdateMoneyAmountDTO[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - const moneyAmountIds = data.map((moneyAmountData) => moneyAmountData.id) - const existingMoneyAmounts = await this.find( - { - where: { - id: { - $in: moneyAmountIds, - }, - }, - }, - context - ) - - const existingMoneyAmountMap = new Map( - existingMoneyAmounts.map<[string, MoneyAmount]>((moneyAmount) => [ - moneyAmount.id, - moneyAmount, - ]) - ) - - const moneyAmounts = data.map((moneyAmountData) => { - const existingMoneyAmount = existingMoneyAmountMap.get(moneyAmountData.id) - - if (!existingMoneyAmount) { - throw new MedusaError( - MedusaError.Types.NOT_FOUND, - `MoneyAmount with id "${moneyAmountData.id}" not found` - ) - } - - return manager.assign(existingMoneyAmount, moneyAmountData) - }) - - manager.persist(moneyAmounts) - - return moneyAmounts +export class MoneyAmountRepository extends DALUtils.mikroOrmBaseRepositoryFactory< + MoneyAmount, + { + create: RepositoryTypes.CreateMoneyAmountDTO + update: RepositoryTypes.UpdateMoneyAmountDTO } -} +>(MoneyAmount) {} diff --git a/packages/pricing/src/repositories/price-list-rule-value.ts b/packages/pricing/src/repositories/price-list-rule-value.ts index 76be14cb5e303..6829876056479 100644 --- a/packages/pricing/src/repositories/price-list-rule-value.ts +++ b/packages/pricing/src/repositories/price-list-rule-value.ts @@ -1,77 +1,19 @@ -import { Context, DAL } from "@medusajs/types" -import { DALUtils, MedusaError, arrayDifference } from "@medusajs/utils" -import { - LoadStrategy, - FilterQuery as MikroFilterQuery, - FindOptions as MikroOptions, -} from "@mikro-orm/core" +import { Context } from "@medusajs/types" +import { DALUtils } from "@medusajs/utils" import { PriceListRuleValue } from "@models" import { RepositoryTypes } from "@types" -import { SqlEntityManager } from "@mikro-orm/postgresql" -export class PriceListRuleValueRepository 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 - } - - async find( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.find( - PriceListRuleValue, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async findAndCount( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise<[PriceListRuleValue[], number]> { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.findAndCount( - PriceListRuleValue, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async delete(ids: string[], context: Context = {}): Promise { - const manager = this.getActiveManager(context) - - await manager.nativeDelete(PriceListRuleValue, { id: { $in: ids } }, {}) +export class PriceListRuleValueRepository extends DALUtils.mikroOrmBaseRepositoryFactory< + PriceListRuleValue, + { + update: RepositoryTypes.UpdatePriceListRuleValueDTO } - +>(PriceListRuleValue) { async create( data: RepositoryTypes.CreatePriceListRuleValueDTO[], context: Context = {} ): Promise { - const manager = this.getActiveManager(context) - const priceListRuleValues = data.map((priceRuleValueData) => { const { price_list_rule_id: priceListRuleId, ...priceRuleValue } = priceRuleValueData @@ -80,62 +22,9 @@ export class PriceListRuleValueRepository extends DALUtils.MikroOrmBaseRepositor priceRuleValue.price_list_rule = priceListRuleId } - return manager.create(PriceListRuleValue, priceRuleValue) + return priceRuleValue }) - manager.persist(priceListRuleValues) - - return priceListRuleValues - } - - async update( - data: RepositoryTypes.UpdatePriceListRuleValueDTO[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - const priceValueIds = data.map( - (priceRuleValueData) => priceRuleValueData.id - ) - const existingPriceValues = await this.find( - { - where: { - id: { - $in: priceValueIds, - }, - }, - }, - context - ) - - const dataAndExistingIdDifference = arrayDifference( - data.map((d) => d.id), - existingPriceValues.map((pv) => pv.id) - ) - - if (dataAndExistingIdDifference.length) { - throw new MedusaError( - MedusaError.Types.NOT_FOUND, - `PriceListRuleValue with id(s) "${dataAndExistingIdDifference.join( - ", " - )}" not found` - ) - } - - const existingValuesMap = new Map( - existingPriceValues.map<[string, PriceListRuleValue]>((priceValue) => [ - priceValue.id, - priceValue, - ]) - ) - - const priceValues = data.map((priceRuleValueData) => { - const existingPriceValue = existingValuesMap.get(priceRuleValueData.id)! - - return manager.assign(existingPriceValue, priceRuleValueData) - }) - - manager.persist(priceValues) - - return priceValues + return await super.create(priceListRuleValues, context) } } diff --git a/packages/pricing/src/repositories/price-list-rule.ts b/packages/pricing/src/repositories/price-list-rule.ts index 0a6cc9c0251e1..2e0664fc7858c 100644 --- a/packages/pricing/src/repositories/price-list-rule.ts +++ b/packages/pricing/src/repositories/price-list-rule.ts @@ -1,76 +1,16 @@ -import { Context, DAL } from "@medusajs/types" -import { DALUtils, MedusaError, arrayDifference } from "@medusajs/utils" -import { - LoadStrategy, - FilterQuery as MikroFilterQuery, - FindOptions as MikroOptions, -} from "@mikro-orm/core" +import { Context } from "@medusajs/types" +import { DALUtils } from "@medusajs/utils" import { PriceListRule } from "@models" import { RepositoryTypes } from "@types" -import { SqlEntityManager } from "@mikro-orm/postgresql" - -export class PriceListRuleRepository 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 - } - - async find( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.find( - PriceListRule, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async findAndCount( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise<[PriceListRule[], number]> { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.findAndCount( - PriceListRule, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async delete(ids: string[], context: Context = {}): Promise { - const manager = this.getActiveManager(context) - await manager.nativeDelete(PriceListRule, { id: { $in: ids } }, {}) - } +export class PriceListRuleRepository extends DALUtils.mikroOrmBaseRepositoryFactory( + PriceListRule +) { async create( data: RepositoryTypes.CreatePriceListRuleDTO[], context: Context = {} ): Promise { - const manager = this.getActiveManager(context) - const priceListRule = data.map((priceListRule) => { const { price_list_id: priceListId, @@ -86,60 +26,20 @@ export class PriceListRuleRepository extends DALUtils.MikroOrmBaseRepository { createData.rule_type = ruleTypeId } - return manager.create(PriceListRule, createData) + return createData }) - manager.persist(priceListRule) - - return priceListRule + return await super.create(priceListRule, context) } async update( data: RepositoryTypes.UpdatePriceListRuleDTO[], context: Context = {} ): Promise { - const manager = this.getActiveManager(context) - const priceListIds = data.map((priceListRule) => priceListRule.id) - const existingPriceListRules = await this.find( - { - where: { - id: { - $in: priceListIds, - }, - }, - }, - context - ) - - const dataAndExistingIdDifference = arrayDifference( - data.map((d) => d.id), - existingPriceListRules.map((plr) => plr.id) - ) - - if (dataAndExistingIdDifference.length) { - throw new MedusaError( - MedusaError.Types.NOT_FOUND, - `PriceListRule with id(s) "${dataAndExistingIdDifference.join( - ", " - )}" not found` - ) - } - - const existingPriceListRuleMap = new Map( - existingPriceListRules.map<[string, PriceListRule]>((priceList) => [ - priceList.id, - priceList, - ]) - ) - const priceListRules = data.map((priceListRule) => { const { price_list_id, rule_type_id, ...priceListRuleData } = priceListRule - const existingPriceListRule = existingPriceListRuleMap.get( - priceListRule.id - )! - if (price_list_id) { priceListRuleData.price_list = price_list_id } @@ -148,11 +48,9 @@ export class PriceListRuleRepository extends DALUtils.MikroOrmBaseRepository { priceListRuleData.rule_type = rule_type_id } - return manager.assign(existingPriceListRule, priceListRuleData) + return priceListRuleData }) - manager.persist(priceListRules) - - return priceListRules + return await super.update(priceListRules, context) } } diff --git a/packages/pricing/src/repositories/price-list.ts b/packages/pricing/src/repositories/price-list.ts index 2e10896e9a776..6245fbb752939 100644 --- a/packages/pricing/src/repositories/price-list.ts +++ b/packages/pricing/src/repositories/price-list.ts @@ -1,76 +1,16 @@ -import { Context, DAL } from "@medusajs/types" -import { DALUtils, GetIsoStringFromDate, MedusaError } from "@medusajs/utils" -import { - LoadStrategy, - FilterQuery as MikroFilterQuery, - FindOptions as MikroOptions, -} from "@mikro-orm/core" +import { Context } from "@medusajs/types" +import { DALUtils, GetIsoStringFromDate } from "@medusajs/utils" import { PriceList } from "@models" import { RepositoryTypes } from "@types" -import { SqlEntityManager } from "@mikro-orm/postgresql" - -export class PriceListRepository 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 - } - - async find( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.find( - PriceList, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async findAndCount( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise<[PriceList[], number]> { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.findAndCount( - PriceList, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async delete(ids: string[], context: Context = {}): Promise { - const manager = this.getActiveManager(context) - await manager.nativeDelete(PriceList, { id: { $in: ids } }, {}) - } +export class PriceListRepository extends DALUtils.mikroOrmBaseRepositoryFactory( + PriceList +) { async create( data: RepositoryTypes.CreatePriceListDTO[], context: Context = {} ): Promise { - const manager = this.getActiveManager(context) - const priceLists = data.map((priceListData: any) => { if (!!priceListData.starts_at) { priceListData.starts_at = GetIsoStringFromDate(priceListData.starts_at) @@ -80,48 +20,17 @@ export class PriceListRepository extends DALUtils.MikroOrmBaseRepository { priceListData.ends_at = GetIsoStringFromDate(priceListData.ends_at) } - return manager.create(PriceList, priceListData) + return priceListData }) - manager.persist(priceLists) - - return priceLists + return await super.create(priceLists, context) } async update( data: RepositoryTypes.UpdatePriceListDTO[], context: Context = {} ): Promise { - const manager = this.getActiveManager(context) - const priceListIds = data.map((priceListData) => priceListData.id) - const existingPriceLists = await this.find( - { - where: { - id: { - $in: priceListIds, - }, - }, - }, - context - ) - - const existingPriceListMap = new Map( - existingPriceLists.map<[string, PriceList]>((priceList) => [ - priceList.id, - priceList, - ]) - ) - const priceLists = data.map((priceListData: any) => { - const existingPriceList = existingPriceListMap.get(priceListData.id) - - if (!existingPriceList) { - throw new MedusaError( - MedusaError.Types.NOT_FOUND, - `PriceList with id "${priceListData.id}" not found` - ) - } - if (!!priceListData.starts_at) { priceListData.starts_at = GetIsoStringFromDate(priceListData.starts_at) } @@ -130,11 +39,9 @@ export class PriceListRepository extends DALUtils.MikroOrmBaseRepository { priceListData.ends_at = GetIsoStringFromDate(priceListData.ends_at) } - return manager.assign(existingPriceList, priceListData) + return priceListData }) - manager.persist(priceLists) - - return priceLists + return await super.update(priceLists, context) } } diff --git a/packages/pricing/src/repositories/price-rule.ts b/packages/pricing/src/repositories/price-rule.ts index d8399209eb465..73ffe0a9942d6 100644 --- a/packages/pricing/src/repositories/price-rule.ts +++ b/packages/pricing/src/repositories/price-rule.ts @@ -1,77 +1,19 @@ -import { Context, DAL } from "@medusajs/types" -import { DALUtils, MedusaError } from "@medusajs/utils" -import { - LoadStrategy, - FilterQuery as MikroFilterQuery, - FindOptions as MikroOptions, -} from "@mikro-orm/core" +import { Context } from "@medusajs/types" +import { DALUtils } from "@medusajs/utils" import { PriceRule } from "@models" import { RepositoryTypes } from "@types" -import { SqlEntityManager } from "@mikro-orm/postgresql" -export class PriceRuleRepository 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 - } - - async find( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.find( - PriceRule, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async findAndCount( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise<[PriceRule[], number]> { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.findAndCount( - PriceRule, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async delete(ids: string[], context: Context = {}): Promise { - const manager = this.getActiveManager(context) - await manager.nativeDelete(PriceRule, { id: { $in: ids } }, {}) +export class PriceRuleRepository extends DALUtils.mikroOrmBaseRepositoryFactory< + PriceRule, + { + update: RepositoryTypes.UpdatePriceRuleDTO } - +>(PriceRule) { async create( data: RepositoryTypes.CreatePriceRuleDTO[], context: Context = {} ): Promise { - const manager: SqlEntityManager = - this.getActiveManager(context) - const toCreate = data.map((ruleData) => { const ruleDataClone = { ...ruleData } as any @@ -83,54 +25,6 @@ export class PriceRuleRepository extends DALUtils.MikroOrmBaseRepository { return ruleDataClone }) - const priceRules = toCreate.map((ruleData) => { - return manager.create(PriceRule, ruleData) - }) - - manager.persist(priceRules) - - return priceRules - } - - async update( - data: RepositoryTypes.UpdatePriceRuleDTO[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - const priceRuleIds = data.map((priceRuleData) => priceRuleData.id) - const existingPriceRules = await this.find( - { - where: { - id: { - $in: priceRuleIds, - }, - }, - }, - context - ) - - const existingPriceRulesMap = new Map( - existingPriceRules.map<[string, PriceRule]>((priceRule) => [ - priceRule.id, - priceRule, - ]) - ) - - const priceRules = data.map((priceRuleData) => { - const existingPriceRule = existingPriceRulesMap.get(priceRuleData.id) - - if (!existingPriceRule) { - throw new MedusaError( - MedusaError.Types.NOT_FOUND, - `PriceRule with id "${priceRuleData.id}" not found` - ) - } - - return manager.assign(existingPriceRule, priceRuleData) - }) - - manager.persist(priceRules) - - return priceRules + return await super.create(toCreate, context) } } diff --git a/packages/pricing/src/repositories/price-set-money-amount-rules.ts b/packages/pricing/src/repositories/price-set-money-amount-rules.ts index 811e11a0d7abc..1cec6d2c9a789 100644 --- a/packages/pricing/src/repositories/price-set-money-amount-rules.ts +++ b/packages/pricing/src/repositories/price-set-money-amount-rules.ts @@ -1,124 +1,12 @@ -import { Context, DAL } from "@medusajs/types" -import { DALUtils, MedusaError } from "@medusajs/utils" -import { - FilterQuery as MikroFilterQuery, - FindOptions as MikroOptions, - LoadStrategy, -} from "@mikro-orm/core" +import { DALUtils } from "@medusajs/utils" import { PriceSetMoneyAmountRules } from "@models" import { RepositoryTypes } from "@types" -import { SqlEntityManager } from "@mikro-orm/postgresql" -export class PriceSetMoneyAmountRulesRepository 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 - } - - async find( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.find( - PriceSetMoneyAmountRules, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async findAndCount( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise<[PriceSetMoneyAmountRules[], number]> { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.findAndCount( - PriceSetMoneyAmountRules, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async delete(ids: string[], context: Context = {}): Promise { - const manager = this.getActiveManager(context) - await manager.nativeDelete(PriceSetMoneyAmountRules, { id: { $in: ids } }) - } - - async create( - data: RepositoryTypes.CreatePriceSetMoneyAmountRulesDTO[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const psmar = data.map((psmarData) => { - return manager.create(PriceSetMoneyAmountRules, psmarData as any) - }) - - manager.persist(psmar) - - return psmar - } - - async update( - data: RepositoryTypes.UpdatePriceSetMoneyAmountRulesDTO[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - const psmarIds = data.map((psmar) => psmar.id) - const existingRecords = await this.find( - { - where: { - id: { - $in: psmarIds, - }, - }, - }, - context - ) - - const psmarMap = new Map( - existingRecords.map<[string, PriceSetMoneyAmountRules]>((psmar) => [ - psmar.id, - psmar, - ]) - ) - - const psmar = data.map((psmarData) => { - const existingRecord = psmarMap.get(psmarData.id) - - if (!existingRecord) { - throw new MedusaError( - MedusaError.Types.NOT_FOUND, - `PriceSetMoneyAmountRules with id "${psmarData.id}" not found` - ) - } - - return manager.assign(existingRecord, psmarData as any) - }) - - manager.persist(psmar) - - return psmar +export class PriceSetMoneyAmountRulesRepository extends DALUtils.mikroOrmBaseRepositoryFactory< + PriceSetMoneyAmountRules, + { + create: RepositoryTypes.CreatePriceSetMoneyAmountRulesDTO + update: RepositoryTypes.UpdatePriceSetMoneyAmountRulesDTO } -} +>(PriceSetMoneyAmountRules) {} diff --git a/packages/pricing/src/repositories/price-set-money-amount.ts b/packages/pricing/src/repositories/price-set-money-amount.ts index f70c2701d2bdf..2c9a017d6e1b1 100644 --- a/packages/pricing/src/repositories/price-set-money-amount.ts +++ b/packages/pricing/src/repositories/price-set-money-amount.ts @@ -1,126 +1,12 @@ -import { Context, DAL } from "@medusajs/types" -import { DALUtils, MedusaError } from "@medusajs/utils" -import { - LoadStrategy, - FilterQuery as MikroFilterQuery, - FindOptions as MikroOptions, -} from "@mikro-orm/core" +import { DALUtils } from "@medusajs/utils" import { PriceSetMoneyAmount } from "@models" import { RepositoryTypes } from "@types" -import { SqlEntityManager } from "@mikro-orm/postgresql" -export class PriceSetMoneyAmountRepository 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 - } - - async find( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.find( - PriceSetMoneyAmount, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async findAndCount( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise<[PriceSetMoneyAmount[], number]> { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.findAndCount( - PriceSetMoneyAmount, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async delete(ids: string[], context: Context = {}): Promise { - const manager = this.getActiveManager(context) - await manager.nativeDelete(PriceSetMoneyAmount, { id: { $in: ids } }, {}) - } - - async create( - data: RepositoryTypes.CreatePriceSetMoneyAmountDTO[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const psma = data.map((psmaData) => { - return manager.create( - PriceSetMoneyAmount, - psmaData as unknown as PriceSetMoneyAmount - ) - }) - - manager.persist(psma) - - return psma - } - - async update( - data: RepositoryTypes.UpdatePriceSetMoneyAmountDTO[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - const ids = data.map((psmaData) => psmaData.id) - const existingPriceSetMoneyAmounts = await this.find( - { - where: { - id: { - $in: ids, - }, - }, - }, - context - ) - - const existingPSMAMap = new Map( - existingPriceSetMoneyAmounts.map<[string, PriceSetMoneyAmount]>( - (psma) => [psma.id, psma] - ) - ) - - const priceSetMoneyAmounts = data.map((psmaData) => { - const existingPSMA = existingPSMAMap.get(psmaData.id) - - if (!existingPSMA) { - throw new MedusaError( - MedusaError.Types.NOT_FOUND, - `PriceSetMoneyAmount with id "${psmaData.id}" not found` - ) - } - - return manager.assign(existingPSMA, psmaData) - }) - - manager.persist(priceSetMoneyAmounts) - - return priceSetMoneyAmounts +export class PriceSetMoneyAmountRepository extends DALUtils.mikroOrmBaseRepositoryFactory< + PriceSetMoneyAmount, + { + create: RepositoryTypes.CreatePriceSetMoneyAmountDTO + update: RepositoryTypes.UpdatePriceSetMoneyAmountDTO } -} +>(PriceSetMoneyAmount) {} diff --git a/packages/pricing/src/repositories/price-set-rule-type.ts b/packages/pricing/src/repositories/price-set-rule-type.ts index 8d189358684f3..1dea5f21ad3b6 100644 --- a/packages/pricing/src/repositories/price-set-rule-type.ts +++ b/packages/pricing/src/repositories/price-set-rule-type.ts @@ -1,124 +1,12 @@ -import { Context, DAL } from "@medusajs/types" -import { DALUtils, MedusaError } from "@medusajs/utils" -import { - LoadStrategy, - FilterQuery as MikroFilterQuery, - FindOptions as MikroOptions, -} from "@mikro-orm/core" +import { DALUtils } from "@medusajs/utils" import { PriceSetRuleType } from "@models" import { RepositoryTypes } from "@types" -import { SqlEntityManager } from "@mikro-orm/postgresql" -export class PriceSetRuleTypeRepository 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 - } - - async find( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.find( - PriceSetRuleType, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async findAndCount( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise<[PriceSetRuleType[], number]> { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.findAndCount( - PriceSetRuleType, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async delete(ids: string[], context: Context = {}): Promise { - const manager = this.getActiveManager(context) - await manager.nativeDelete(PriceSetRuleType, { id: { $in: ids } }, {}) - } - - async create( - data: RepositoryTypes.CreatePriceSetRuleTypeDTO[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const priceSets = data.map((priceSetData) => { - return manager.create(PriceSetRuleType, priceSetData) - }) - - manager.persist(priceSets) - - return priceSets - } - - async update( - data: RepositoryTypes.UpdatePriceSetRuleTypeDTO[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - const priceSetIds = data.map((priceSetData) => priceSetData.id) - const existingPriceSets = await this.find( - { - where: { - id: { - $in: priceSetIds, - }, - }, - }, - context - ) - - const existingPriceSetMap = new Map( - existingPriceSets.map<[string, PriceSetRuleType]>((priceSet) => [ - priceSet.id, - priceSet, - ]) - ) - - const priceSets = data.map((priceSetData) => { - const existingPriceSet = existingPriceSetMap.get(priceSetData.id) - - if (!existingPriceSet) { - throw new MedusaError( - MedusaError.Types.NOT_FOUND, - `PriceSetRuleType with id "${priceSetData.id}" not found` - ) - } - - return manager.assign(existingPriceSet, priceSetData) - }) - - manager.persist(priceSets) - - return priceSets +export class PriceSetRuleTypeRepository extends DALUtils.mikroOrmBaseRepositoryFactory< + PriceSetRuleType, + { + create: RepositoryTypes.CreatePriceSetRuleTypeDTO + update: RepositoryTypes.UpdatePriceSetRuleTypeDTO } -} +>(PriceSetRuleType) {} diff --git a/packages/pricing/src/repositories/price-set.ts b/packages/pricing/src/repositories/price-set.ts index eb7821ac7a4e4..03984ca0e0430 100644 --- a/packages/pricing/src/repositories/price-set.ts +++ b/packages/pricing/src/repositories/price-set.ts @@ -1,124 +1,12 @@ -import { Context, DAL } from "@medusajs/types" -import { DALUtils, MedusaError } from "@medusajs/utils" -import { - LoadStrategy, - FilterQuery as MikroFilterQuery, - FindOptions as MikroOptions, -} from "@mikro-orm/core" +import { DALUtils } from "@medusajs/utils" import { PriceSet } from "@models" import { RepositoryTypes } from "@types" -import { SqlEntityManager } from "@mikro-orm/postgresql" -export class PriceSetRepository 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 - } - - async find( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.find( - PriceSet, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async findAndCount( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise<[PriceSet[], number]> { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.findAndCount( - PriceSet, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async delete(ids: string[], context: Context = {}): Promise { - const manager = this.getActiveManager(context) - await manager.nativeDelete(PriceSet, { id: { $in: ids } }, {}) - } - - async create( - data: RepositoryTypes.CreatePriceSetDTO[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const priceSets = data.map((priceSetData) => { - return manager.create(PriceSet, priceSetData) - }) - - manager.persist(priceSets) - - return priceSets - } - - async update( - data: RepositoryTypes.UpdatePriceSetDTO[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - const priceSetIds = data.map((priceSetData) => priceSetData.id) - const existingPriceSets = await this.find( - { - where: { - id: { - $in: priceSetIds, - }, - }, - }, - context - ) - - const existingPriceSetMap = new Map( - existingPriceSets.map<[string, PriceSet]>((priceSet) => [ - priceSet.id, - priceSet, - ]) - ) - - const priceSets = data.map((priceSetData) => { - const existingPriceSet = existingPriceSetMap.get(priceSetData.id) - - if (!existingPriceSet) { - throw new MedusaError( - MedusaError.Types.NOT_FOUND, - `PriceSet with id "${priceSetData.id}" not found` - ) - } - - return manager.assign(existingPriceSet, priceSetData) - }) - - manager.persist(priceSets) - - return priceSets +export class PriceSetRepository extends DALUtils.mikroOrmBaseRepositoryFactory< + PriceSet, + { + create: RepositoryTypes.CreatePriceSetDTO + update: RepositoryTypes.UpdatePriceSetDTO } -} +>(PriceSet) {} diff --git a/packages/pricing/src/repositories/pricing.ts b/packages/pricing/src/repositories/pricing.ts index b0c011862f0b2..59fc93947d708 100644 --- a/packages/pricing/src/repositories/pricing.ts +++ b/packages/pricing/src/repositories/pricing.ts @@ -13,14 +13,10 @@ export class PricingRepository extends MikroOrmBase implements PricingRepositoryService { - protected readonly manager_: SqlEntityManager - - constructor({ manager }: { manager: SqlEntityManager }) { + constructor() { // @ts-ignore // eslint-disable-next-line prefer-rest-params super(...arguments) - - this.manager_ = manager } async calculatePrices( diff --git a/packages/pricing/src/repositories/rule-type.ts b/packages/pricing/src/repositories/rule-type.ts index beca06be72c50..1873ad675db5d 100644 --- a/packages/pricing/src/repositories/rule-type.ts +++ b/packages/pricing/src/repositories/rule-type.ts @@ -1,128 +1,11 @@ -import { Context, DAL } from "@medusajs/types" -import { DALUtils, MedusaError, validateRuleAttributes } from "@medusajs/utils" -import { - LoadStrategy, - FilterQuery as MikroFilterQuery, - FindOptions as MikroOptions, -} from "@mikro-orm/core" - -import { RepositoryTypes } from "@types" +import { DALUtils } from "@medusajs/utils" import { RuleType } from "@models" -import { SqlEntityManager } from "@mikro-orm/postgresql" - -export class RuleTypeRepository 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 - } - - async find( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.find( - RuleType, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async findAndCount( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise<[RuleType[], number]> { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.findAndCount( - RuleType, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async delete(ids: string[], context: Context = {}): Promise { - const manager = this.getActiveManager(context) - await manager.nativeDelete(RuleType, { id: { $in: ids } }, {}) - } - - async create( - data: RepositoryTypes.CreateRuleTypeDTO[], - context: Context = {} - ): Promise { - validateRuleAttributes(data.map((d) => d.rule_attribute)) - - const manager = this.getActiveManager(context) - - const ruleTypes = data.map((ruleTypeData) => { - return manager.create(RuleType, ruleTypeData) - }) - - manager.persist(ruleTypes) - - return ruleTypes - } - - async update( - data: RepositoryTypes.UpdateRuleTypeDTO[], - context: Context = {} - ): Promise { - validateRuleAttributes(data.map((d) => d.rule_attribute)) - - const manager = this.getActiveManager(context) - const ruleTypeIds = data.map((ruleType) => ruleType.id) - const existingRuleTypes = await this.find( - { - where: { - id: { - $in: ruleTypeIds, - }, - }, - }, - context - ) - - const existingRuleTypesMap = new Map( - existingRuleTypes.map<[string, RuleType]>((ruleType) => [ - ruleType.id, - ruleType, - ]) - ) - - const ruleTypes = data.map((ruleTypeData) => { - const existingRuleType = existingRuleTypesMap.get(ruleTypeData.id) - - if (!existingRuleType) { - throw new MedusaError( - MedusaError.Types.NOT_FOUND, - `RuleType with id "${ruleTypeData.id}" not found` - ) - } - - return manager.assign(existingRuleType, ruleTypeData) - }) - - manager.persist(ruleTypes) +import { RepositoryTypes } from "@types" - return ruleTypes +export class RuleTypeRepository extends DALUtils.mikroOrmBaseRepositoryFactory< + RuleType, + { + create: RepositoryTypes.CreateRuleTypeDTO + update: RepositoryTypes.UpdateRuleTypeDTO } -} +>(RuleType) {} diff --git a/packages/pricing/src/services/rule-type.ts b/packages/pricing/src/services/rule-type.ts index 21faf9d81c27e..6d1fbe674aefd 100644 --- a/packages/pricing/src/services/rule-type.ts +++ b/packages/pricing/src/services/rule-type.ts @@ -5,6 +5,7 @@ import { MedusaContext, ModulesSdkUtils, retrieveEntity, + validateRuleAttributes, } from "@medusajs/utils" import { RuleType } from "@models" import { ServiceTypes } from "@types" @@ -69,6 +70,7 @@ export default class RuleTypeService { data: ServiceTypes.CreateRuleTypeDTO[], @MedusaContext() sharedContext: Context = {} ): Promise { + validateRuleAttributes(data.map((d) => d.rule_attribute)) return (await this.ruleTypeRepository_.create( data, sharedContext @@ -80,6 +82,7 @@ export default class RuleTypeService { data: ServiceTypes.UpdateRuleTypeDTO[], @MedusaContext() sharedContext: Context = {} ): Promise { + validateRuleAttributes(data.map((d) => d.rule_attribute)) return (await this.ruleTypeRepository_.update( data, sharedContext diff --git a/packages/product/src/repositories/product-category.ts b/packages/product/src/repositories/product-category.ts index 9ec9326b90c3e..c4d66cb7a7247 100644 --- a/packages/product/src/repositories/product-category.ts +++ b/packages/product/src/repositories/product-category.ts @@ -26,22 +26,13 @@ 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 - } - +export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeRepository { async find( findOptions: DAL.FindOptions = { where: {} }, transformOptions: ProductCategoryTransformOptions = {}, context: Context = {} ): Promise { - const manager = this.getActiveManager(context) + const manager = super.getActiveManager(context) const findOptions_ = { ...findOptions } const { includeDescendantsTree } = transformOptions @@ -81,7 +72,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito findOptions: DAL.FindOptions = { where: {} }, context: Context = {} ): Promise { - const manager = this.getActiveManager(context) + const manager = super.getActiveManager(context) for (let productCategory of productCategories) { const whereOptions = { @@ -132,7 +123,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito transformOptions: ProductCategoryTransformOptions = {}, context: Context = {} ): Promise<[ProductCategory[], number]> { - const manager = this.getActiveManager(context) + const manager = super.getActiveManager(context) const findOptions_ = { ...findOptions } const { includeDescendantsTree } = transformOptions @@ -171,7 +162,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito } async delete(id: string, context: Context = {}): Promise { - const manager = this.getActiveManager(context) + const manager = super.getActiveManager(context) const productCategory = await manager.findOneOrFail( ProductCategory, { id }, @@ -197,11 +188,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito ) await this.performReordering(manager, conditions) - await (manager as SqlEntityManager).nativeDelete( - ProductCategory, - { id: id }, - {} - ) + await manager.nativeDelete(ProductCategory, { id: id }, {}) } async create( @@ -209,7 +196,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito context: Context = {} ): Promise { const categoryData = { ...data } - const manager = this.getActiveManager(context) + const manager = super.getActiveManager(context) const siblings = await manager.find(ProductCategory, { parent_category_id: categoryData?.parent_category_id || null, }) @@ -231,7 +218,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito context: Context = {} ): Promise { const categoryData = { ...data } - const manager = this.getActiveManager(context) + const manager = super.getActiveManager(context) const productCategory = await manager.findOneOrFail(ProductCategory, { id }) const conditions = this.fetchReorderConditions( @@ -403,7 +390,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito throw new Error("sibling rank is not defined") } - const rank = shouldIncrementRank ? ++sibling.rank : --sibling.rank + const rank = shouldIncrementRank ? ++sibling.rank! : --sibling.rank! manager.assign(sibling, { rank }) manager.persist(sibling) diff --git a/packages/product/src/repositories/product-collection.ts b/packages/product/src/repositories/product-collection.ts index 26ef2631de06b..9fcdb62d19366 100644 --- a/packages/product/src/repositories/product-collection.ts +++ b/packages/product/src/repositories/product-collection.ts @@ -1,11 +1,5 @@ -import { Context, DAL, ProductTypes } from "@medusajs/types" -import { DALUtils, MedusaError } from "@medusajs/utils" -import { - FilterQuery as MikroFilterQuery, - FindOptions as MikroOptions, - LoadStrategy, -} from "@mikro-orm/core" -import { SqlEntityManager } from "@mikro-orm/postgresql" +import { Context, ProductTypes } from "@medusajs/types" +import { DALUtils } from "@medusajs/utils" import { ProductCollection } from "@models" type UpdateProductCollection = ProductTypes.UpdateProductCollectionDTO & { @@ -17,71 +11,13 @@ type CreateProductCollection = ProductTypes.CreateProductCollectionDTO & { } // 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 - } - - async find( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.find( - ProductCollection, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async findAndCount( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise<[ProductCollection[], number]> { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.findAndCount( - ProductCollection, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async delete(collectionIds: string[], context: Context = {}): Promise { - const manager = this.getActiveManager(context) - await manager.nativeDelete( - ProductCollection, - { id: { $in: collectionIds } }, - {} - ) - } - +export class ProductCollectionRepository extends DALUtils.mikroOrmBaseRepositoryFactory( + ProductCollection +) { async create( data: CreateProductCollection[], context: Context = {} ): Promise { - const manager = this.getActiveManager(context) - const productCollections = data.map((collectionData) => { if (collectionData.product_ids) { collectionData.products = collectionData.product_ids @@ -89,59 +25,26 @@ export class ProductCollectionRepository extends DALUtils.MikroOrmBaseRepository delete collectionData.product_ids } - return manager.create(ProductCollection, collectionData) + return collectionData }) - manager.persist(productCollections) - - return productCollections + return await super.create(productCollections, context) } async update( data: UpdateProductCollection[], context: Context = {} ): Promise { - const manager = this.getActiveManager(context) - const collectionIds = data.map((collectionData) => collectionData.id) - const existingCollections = await this.find( - { - where: { - id: { - $in: collectionIds, - }, - }, - }, - context - ) - - const existingCollectionsMap = new Map( - existingCollections.map<[string, ProductCollection]>((collection) => [ - collection.id, - collection, - ]) - ) - const productCollections = data.map((collectionData) => { - const existingCollection = existingCollectionsMap.get(collectionData.id) - - if (!existingCollection) { - throw new MedusaError( - MedusaError.Types.NOT_FOUND, - `ProductCollection with id "${collectionData.id}" not found` - ) - } - if (collectionData.product_ids) { collectionData.products = collectionData.product_ids delete collectionData.product_ids } - return manager.assign(existingCollection, collectionData) + return collectionData }) - manager.persist(productCollections) - - return productCollections + return await super.update(productCollections, context) } } diff --git a/packages/product/src/repositories/product-image.ts b/packages/product/src/repositories/product-image.ts index 8a1a325aad28c..de12c58c04614 100644 --- a/packages/product/src/repositories/product-image.ts +++ b/packages/product/src/repositories/product-image.ts @@ -1,64 +1,12 @@ -import { - FilterQuery as MikroFilterQuery, - FindOptions as MikroOptions, - LoadStrategy, -} from "@mikro-orm/core" -import { Context, DAL } from "@medusajs/types" -import { Image, Product } from "@models" +import { Context } from "@medusajs/types" +import { Image } from "@models" import { SqlEntityManager } from "@mikro-orm/postgresql" 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 - } - - async find( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.find( - Image, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async findAndCount( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise<[Image[], number]> { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.findAndCount( - Image, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - +export class ProductImageRepository extends DALUtils.mikroOrmBaseRepositoryFactory( + Image +) { async upsert(urls: string[], context: Context = {}): Promise { const manager = this.getActiveManager(context) @@ -73,7 +21,7 @@ export class ProductImageRepository extends DALUtils.MikroOrmAbstractBaseReposit context ) - const existingImagesMap = new Map( + const existingImagesMap = new Map( existingImages.map<[string, Image]>((img) => [img.url, img]) ) @@ -97,13 +45,4 @@ export class ProductImageRepository extends DALUtils.MikroOrmAbstractBaseReposit return upsertedImgs } - - async delete(ids: string[], context: Context = {}): Promise { - const manager = this.getActiveManager(context) - await manager.nativeDelete(Product, { id: { $in: ids } }, {}) - } - - async create(data: unknown[], context: Context = {}): Promise { - throw new Error("Method not implemented.") - } } diff --git a/packages/product/src/repositories/product-option-value.ts b/packages/product/src/repositories/product-option-value.ts index f2f3e10bd8416..219764cd35f86 100644 --- a/packages/product/src/repositories/product-option-value.ts +++ b/packages/product/src/repositories/product-option-value.ts @@ -1,41 +1,16 @@ -import { Context, DAL } from "@medusajs/types" +import { Context } from "@medusajs/types" import { CreateProductOptionValueDTO, UpdateProductOptionValueDTO, } from "../types/services/product-option-value" import { DALUtils } from "@medusajs/utils" -import { FilterQuery as MikroFilterQuery } from "@mikro-orm/core/typings" -import { FindOptions as MikroOptions } from "@mikro-orm/core/drivers/IDatabaseDriver" import { ProductOptionValue } from "@models" import { SqlEntityManager } from "@mikro-orm/postgresql" -export class ProductOptionValueRepository 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 - } - - async find( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - const findOptions_ = { ...findOptions } - - findOptions_.options ??= {} - - return await manager.find( - ProductOptionValue, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - +export class ProductOptionValueRepository extends DALUtils.mikroOrmBaseRepositoryFactory( + ProductOptionValue +) { async upsert( optionValues: (UpdateProductOptionValueDTO | CreateProductOptionValueDTO)[], context: Context = {} @@ -106,9 +81,4 @@ export class ProductOptionValueRepository extends DALUtils.MikroOrmBaseRepositor return upsertedOptionValues } - - async delete(ids: string[], context: Context = {}): Promise { - const manager = this.getActiveManager(context) - await manager.nativeDelete(ProductOptionValue, { id: { $in: ids } }, {}) - } } diff --git a/packages/product/src/repositories/product-option.ts b/packages/product/src/repositories/product-option.ts index 5f7c10e8d85df..a51f9260fbeb6 100644 --- a/packages/product/src/repositories/product-option.ts +++ b/packages/product/src/repositories/product-option.ts @@ -1,74 +1,15 @@ -import { Context, DAL, ProductTypes } from "@medusajs/types" -import { DALUtils, MedusaError } from "@medusajs/utils" -import { - FilterQuery as MikroFilterQuery, - FindOptions as MikroOptions, - LoadStrategy, -} from "@mikro-orm/core" +import { Context, ProductTypes } from "@medusajs/types" +import { DALUtils } from "@medusajs/utils" import { SqlEntityManager } from "@mikro-orm/postgresql" import { Product, ProductOption } from "@models" // 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 +export class ProductOptionRepository extends DALUtils.mikroOrmBaseRepositoryFactory< + ProductOption, + { + update: ProductTypes.UpdateProductOptionDTO } - - async find( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.find( - ProductOption, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async findAndCount( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise<[ProductOption[], number]> { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.findAndCount( - ProductOption, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async delete(ids: string[], context: Context = {}): Promise { - const manager = this.getActiveManager(context) - - await (manager as SqlEntityManager).nativeDelete( - ProductOption, - { id: { $in: ids } }, - {} - ) - } - +>(ProductOption) { async create( data: ProductTypes.CreateProductOptionDTO[], context: Context = {} @@ -82,7 +23,7 @@ export class ProductOptionRepository extends DALUtils.MikroOrmAbstractBaseReposi id: { $in: productIds }, }) - const existingProductsMap = new Map( + const existingProductsMap = new Map( existingProducts.map<[string, Product]>((product) => [ product.id, product, @@ -100,54 +41,10 @@ export class ProductOptionRepository extends DALUtils.MikroOrmAbstractBaseReposi optionData.product_id = product?.id } - return manager.create(ProductOption, optionData) - }) - - manager.persist(productOptions) - - return productOptions - } - - async update( - data: ProductTypes.UpdateProductOptionDTO[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - const optionIds = data.map((optionData) => optionData.id) - const existingOptions = await this.find( - { - where: { - id: { - $in: optionIds, - }, - }, - }, - context - ) - - const existingOptionsMap = new Map( - existingOptions.map<[string, ProductOption]>((option) => [ - option.id, - option, - ]) - ) - - const productOptions = data.map((optionData) => { - const existingOption = existingOptionsMap.get(optionData.id) - - if (!existingOption) { - throw new MedusaError( - MedusaError.Types.NOT_FOUND, - `ProductOption with id "${optionData.id}" not found` - ) - } - - return manager.assign(existingOption, optionData) + return optionData }) - manager.persist(productOptions) - - return productOptions + return await super.create(productOptions, context) } async upsert( diff --git a/packages/product/src/repositories/product-tag.ts b/packages/product/src/repositories/product-tag.ts index 88628bdd8b9e7..69dfb03ea70c9 100644 --- a/packages/product/src/repositories/product-tag.ts +++ b/packages/product/src/repositories/product-tag.ts @@ -1,123 +1,20 @@ -import { - FilterQuery as MikroFilterQuery, - FindOptions as MikroOptions, - LoadStrategy, -} from "@mikro-orm/core" import { ProductTag } from "@models" import { Context, CreateProductTagDTO, - DAL, UpdateProductTagDTO, UpsertProductTagDTO, } from "@medusajs/types" import { SqlEntityManager } from "@mikro-orm/postgresql" -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 - } - - async find( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - const findOptions_ = { ...findOptions } - - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.find( - ProductTag, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async findAndCount( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise<[ProductTag[], number]> { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.findAndCount( - ProductTag, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async create( - data: CreateProductTagDTO[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const productTags = data.map((tagData) => { - return manager.create(ProductTag, tagData) - }) +import { DALUtils } from "@medusajs/utils" - manager.persist(productTags) - - return productTags +export class ProductTagRepository extends DALUtils.mikroOrmBaseRepositoryFactory< + ProductTag, + { + create: CreateProductTagDTO + update: UpdateProductTagDTO } - - async update( - data: UpdateProductTagDTO[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - const tagIds = data.map((tagData) => tagData.id) - const existingTags = await this.find( - { - where: { - id: { - $in: tagIds, - }, - }, - }, - context - ) - - const existingTagsMap = new Map( - existingTags.map<[string, ProductTag]>((tag) => [tag.id, tag]) - ) - - const productTags = data.map((tagData) => { - const existingTag = existingTagsMap.get(tagData.id) - - if (!existingTag) { - throw new MedusaError( - MedusaError.Types.NOT_FOUND, - `ProductTag with id "${tagData.id}" not found` - ) - } - - return manager.assign(existingTag, tagData) - }) - - manager.persist(productTags) - - return productTags - } - +>(ProductTag) { async upsert( tags: UpsertProductTagDTO[], context: Context = {} @@ -166,10 +63,4 @@ export class ProductTagRepository extends DALUtils.MikroOrmBaseRepository { return upsertedTags } - - 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 d2f3e2171067e..082a926bcf920 100644 --- a/packages/product/src/repositories/product-type.ts +++ b/packages/product/src/repositories/product-type.ts @@ -1,68 +1,19 @@ -import { - FilterQuery as MikroFilterQuery, - FindOptions as MikroOptions, - LoadStrategy, -} from "@mikro-orm/core" import { ProductType } from "@models" import { Context, CreateProductTypeDTO, - DAL, UpdateProductTypeDTO, } from "@medusajs/types" import { SqlEntityManager } from "@mikro-orm/postgresql" -import { DALUtils, MedusaError } from "@medusajs/utils" - -export class ProductTypeRepository extends DALUtils.MikroOrmBaseRepository { - protected readonly manager_: SqlEntityManager +import { DALUtils } from "@medusajs/utils" - constructor({ manager }: { manager: SqlEntityManager }) { - // @ts-ignore - // eslint-disable-next-line prefer-rest-params - super(...arguments) - this.manager_ = manager +export class ProductTypeRepository extends DALUtils.mikroOrmBaseRepositoryFactory< + ProductType, + { + create: CreateProductTypeDTO + update: UpdateProductTypeDTO } - - async find( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.find( - ProductType, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async findAndCount( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise<[ProductType[], number]> { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.findAndCount( - ProductType, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - +>(ProductType) { async upsert( types: CreateProductTypeDTO[], context: Context = {} @@ -112,63 +63,4 @@ export class ProductTypeRepository extends DALUtils.MikroOrmBaseRepository { return upsertedTypes } - - async delete(ids: string[], context: Context = {}): Promise { - const manager = this.getActiveManager(context) - await manager.nativeDelete(ProductType, { id: { $in: ids } }, {}) - } - - async create( - data: CreateProductTypeDTO[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const productTypes = data.map((typeData) => { - return manager.create(ProductType, typeData) - }) - - manager.persist(productTypes) - - return productTypes - } - - async update( - data: UpdateProductTypeDTO[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - const typeIds = data.map((typeData) => typeData.id) - const existingTypes = await this.find( - { - where: { - id: { - $in: typeIds, - }, - }, - }, - context - ) - - const existingTypesMap = new Map( - existingTypes.map<[string, ProductType]>((type) => [type.id, type]) - ) - - const productTypes = data.map((typeData) => { - const existingType = existingTypesMap.get(typeData.id) - - if (!existingType) { - throw new MedusaError( - MedusaError.Types.NOT_FOUND, - `ProductType with id "${typeData.id}" not found` - ) - } - - return manager.assign(existingType, typeData) - }) - - manager.persist(productTypes) - - return productTypes - } } diff --git a/packages/product/src/repositories/product-variant.ts b/packages/product/src/repositories/product-variant.ts index e525fe30aba57..3e7824e9c67b5 100644 --- a/packages/product/src/repositories/product-variant.ts +++ b/packages/product/src/repositories/product-variant.ts @@ -1,131 +1,17 @@ -import { - FilterQuery as MikroFilterQuery, - FindOptions as MikroOptions, - LoadStrategy, - RequiredEntityData, -} from "@mikro-orm/core" import { ProductVariant } from "@models" -import { Context, DAL, WithRequiredProperty } from "@medusajs/types" -import { SqlEntityManager } from "@mikro-orm/postgresql" -import { - DALUtils, - InjectTransactionManager, - MedusaContext, - MedusaError, -} from "@medusajs/utils" - +import { DALUtils } from "@medusajs/utils" +import { RequiredEntityData } from "@mikro-orm/core" +import { WithRequiredProperty } from "@medusajs/types" 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 - } - - async find( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.find( - ProductVariant, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async findAndCount( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise<[ProductVariant[], number]> { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.findAndCount( - ProductVariant, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - @InjectTransactionManager() - async delete( - ids: string[], - @MedusaContext() - { transactionManager: manager }: Context = {} - ): Promise { - await (manager as SqlEntityManager).nativeDelete( - ProductVariant, - { id: { $in: ids } }, - {} - ) - } - - async create( - data: RequiredEntityData[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - const variants = data.map((variant) => { - return (manager as SqlEntityManager).create(ProductVariant, variant) - }) - - manager.persist(variants) - - return variants - } - - async update( - data: WithRequiredProperty< +export class ProductVariantRepository extends DALUtils.mikroOrmBaseRepositoryFactory< + ProductVariant, + { + create: RequiredEntityData + update: WithRequiredProperty< ProductVariantServiceTypes.UpdateProductVariantDTO, "id" - >[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const productVariantsToUpdate = await manager.find(ProductVariant, { - id: data.map((updateData) => updateData.id), - }) - - const productVariantsToUpdateMap = new Map( - productVariantsToUpdate.map((variant) => [variant.id, variant]) - ) - - const variants = data.map((variantData) => { - const productVariant = productVariantsToUpdateMap.get(variantData.id) - - if (!productVariant) { - throw new MedusaError( - MedusaError.Types.NOT_FOUND, - `ProductVariant with id "${variantData.id}" not found` - ) - } - - return manager.assign(productVariant, variantData) - }) - - manager.persist(variants) - - return variants + > } -} +>(ProductVariant) {} diff --git a/packages/product/src/repositories/product.ts b/packages/product/src/repositories/product.ts index d60b4aa0e2bb8..855477db878d7 100644 --- a/packages/product/src/repositories/product.ts +++ b/packages/product/src/repositories/product.ts @@ -6,12 +6,6 @@ import { ProductType, } from "@models" -import { - FilterQuery as MikroFilterQuery, - FindOptions as MikroOptions, - LoadStrategy, -} from "@mikro-orm/core" - import { Context, DAL, @@ -24,29 +18,19 @@ import { DALUtils, isDefined, MedusaError, promiseAll } 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 +export class ProductRepository extends DALUtils.mikroOrmBaseRepositoryFactory< + Product, + { + create: WithRequiredProperty } - +>(Product) { async find( findOptions: DAL.FindOptions = { where: {} }, context: Context = {} ): Promise { - const manager = this.getActiveManager(context) - const findOptions_ = { ...findOptions } findOptions_.options ??= {} - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - await this.mutateNotInCategoriesConstraints(findOptions_) this.applyFreeTextSearchFilters( @@ -54,26 +38,16 @@ export class ProductRepository extends DALUtils.MikroOrmAbstractBaseRepository

, - findOptions_.options as MikroOptions - ) + return await super.find(findOptions_, context) } async findAndCount( findOptions: DAL.FindOptions = { where: {} }, context: Context = {} ): Promise<[Product[], number]> { - const manager = this.getActiveManager(context) - const findOptions_ = { ...findOptions } findOptions_.options ??= {} - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - await this.mutateNotInCategoriesConstraints(findOptions_) this.applyFreeTextSearchFilters( @@ -81,12 +55,9 @@ export class ProductRepository extends DALUtils.MikroOrmAbstractBaseRepository

, - findOptions_.options as MikroOptions - ) + return await super.findAndCount(findOptions_, context) } + /** * In order to be able to have a strict not in categories, and prevent a product * to be return in the case it also belongs to other categories, we need to @@ -127,26 +98,6 @@ export class ProductRepository extends DALUtils.MikroOrmAbstractBaseRepository

{ - const manager = this.getActiveManager(context) - await manager.nativeDelete(Product, { id: { $in: ids } }, {}) - } - - async create( - data: WithRequiredProperty[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const products = data.map((product) => { - return (manager as SqlEntityManager).create(Product, product) - }) - - manager.persist(products) - - return products - } - async update( data: WithRequiredProperty[], context: Context = {} diff --git a/packages/promotion/integration-tests/__tests__/services/promotion-module/promotion.spec.ts b/packages/promotion/integration-tests/__tests__/services/promotion-module/promotion.spec.ts index ddcff28851086..dc259dccc0eaa 100644 --- a/packages/promotion/integration-tests/__tests__/services/promotion-module/promotion.spec.ts +++ b/packages/promotion/integration-tests/__tests__/services/promotion-module/promotion.spec.ts @@ -359,7 +359,7 @@ describe("Promotion Service", () => { ]) .catch((e) => e) - expect(error.message).toContain('Promotion with id "undefined" not found') + expect(error.message).toContain('Promotion with id "" not found') }) it("should update the attributes of a promotion successfully", async () => { diff --git a/packages/promotion/src/repositories/application-method.ts b/packages/promotion/src/repositories/application-method.ts index d656a721b0935..f2d7a60517b6d 100644 --- a/packages/promotion/src/repositories/application-method.ts +++ b/packages/promotion/src/repositories/application-method.ts @@ -1,121 +1,11 @@ -import { Context, DAL } from "@medusajs/types" -import { DALUtils, MedusaError } from "@medusajs/utils" -import { - FilterQuery as MikroFilterQuery, - FindOptions as MikroOptions, -} from "@mikro-orm/core" -import { SqlEntityManager } from "@mikro-orm/postgresql" +import { DALUtils } from "@medusajs/utils" import { ApplicationMethod } from "@models" -import { - CreateApplicationMethodDTO, - UpdateApplicationMethodDTO, -} from "../types" +import { CreateApplicationMethodDTO, UpdateApplicationMethodDTO } from "@types" -export class ApplicationMethodRepository 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 - } - - async find( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - return await manager.find( - ApplicationMethod, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async findAndCount( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise<[ApplicationMethod[], number]> { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - return await manager.findAndCount( - ApplicationMethod, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async delete(ids: string[], context: Context = {}): Promise { - const manager = this.getActiveManager(context) - - await manager.nativeDelete(ApplicationMethod, { id: { $in: ids } }, {}) - } - - async create( - data: CreateApplicationMethodDTO[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const applicationMethods = data.map((applicationMethodData) => { - return manager.create(ApplicationMethod, applicationMethodData) - }) - - manager.persist(applicationMethods) - - return applicationMethods - } - - async update( - data: UpdateApplicationMethodDTO[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - const applicationMethodIds = data.map( - (applicationMethodData) => applicationMethodData.id - ) - const existingApplicationMethods = await this.find( - { - where: { - id: { - $in: applicationMethodIds, - }, - }, - }, - context - ) - - const existingApplicationMethodMap = new Map( - existingApplicationMethods.map<[string, ApplicationMethod]>( - (applicationMethod) => [applicationMethod.id, applicationMethod] - ) - ) - - const applicationMethods = data.map((applicationMethodData) => { - const existingApplicationMethod = existingApplicationMethodMap.get( - applicationMethodData.id - ) - - if (!existingApplicationMethod) { - throw new MedusaError( - MedusaError.Types.NOT_FOUND, - `ApplicationMethod with id "${applicationMethodData.id}" not found` - ) - } - - return manager.assign(existingApplicationMethod, applicationMethodData) - }) - - manager.persist(applicationMethods) - - return applicationMethods +export class ApplicationMethodRepository extends DALUtils.mikroOrmBaseRepositoryFactory< + ApplicationMethod, + { + create: CreateApplicationMethodDTO + update: UpdateApplicationMethodDTO } -} +>(ApplicationMethod) {} diff --git a/packages/promotion/src/repositories/promotion-rule-value.ts b/packages/promotion/src/repositories/promotion-rule-value.ts index beb5e9ef92c03..aa3868e5bf6cc 100644 --- a/packages/promotion/src/repositories/promotion-rule-value.ts +++ b/packages/promotion/src/repositories/promotion-rule-value.ts @@ -1,128 +1,14 @@ -import { Context, DAL } from "@medusajs/types" -import { DALUtils, MedusaError, arrayDifference } from "@medusajs/utils" -import { - FilterQuery as MikroFilterQuery, - FindOptions as MikroOptions, -} from "@mikro-orm/core" - -import { SqlEntityManager } from "@mikro-orm/postgresql" +import { DALUtils } from "@medusajs/utils" import { PromotionRuleValue } from "@models" import { CreatePromotionRuleValueDTO, UpdatePromotionRuleValueDTO, } from "@types" -export class PromotionRuleValueRepository 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 - } - - async find( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - return await manager.find( - PromotionRuleValue, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async findAndCount( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise<[PromotionRuleValue[], number]> { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - return await manager.findAndCount( - PromotionRuleValue, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async delete(ids: string[], context: Context = {}): Promise { - const manager = this.getActiveManager(context) - await manager.nativeDelete(PromotionRuleValue, { id: { $in: ids } }, {}) - } - - async create( - data: CreatePromotionRuleValueDTO[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const promotionRuleValue = data.map((promotionRuleValue) => { - return manager.create(PromotionRuleValue, promotionRuleValue) - }) - - manager.persist(promotionRuleValue) - - return promotionRuleValue - } - - async update( - data: UpdatePromotionRuleValueDTO[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - const promotionRuleValueIds = data.map( - (promotionRuleValue) => promotionRuleValue.id - ) - const existingPromotionRuleValues = await this.find( - { - where: { - id: { - $in: promotionRuleValueIds, - }, - }, - }, - context - ) - - const dataAndExistingIdDifference = arrayDifference( - data.map((d) => d.id), - existingPromotionRuleValues.map((plr) => plr.id) - ) - - if (dataAndExistingIdDifference.length) { - throw new MedusaError( - MedusaError.Types.NOT_FOUND, - `PromotionRuleValue with id(s) "${dataAndExistingIdDifference.join( - ", " - )}" not found` - ) - } - - const existingPromotionRuleValueMap = new Map( - existingPromotionRuleValues.map<[string, PromotionRuleValue]>( - (promotionRuleValue) => [promotionRuleValue.id, promotionRuleValue] - ) - ) - - const promotionRuleValue = data.map((promotionRuleValueData) => { - const existingPromotionRuleValue = existingPromotionRuleValueMap.get( - promotionRuleValueData.id - )! - - return manager.assign(existingPromotionRuleValue, promotionRuleValueData) - }) - - manager.persist(promotionRuleValue) - - return promotionRuleValue +export class PromotionRuleValueRepository extends DALUtils.mikroOrmBaseRepositoryFactory< + PromotionRuleValue, + { + create: CreatePromotionRuleValueDTO + update: UpdatePromotionRuleValueDTO } -} +>(PromotionRuleValue) {} diff --git a/packages/promotion/src/repositories/promotion-rule.ts b/packages/promotion/src/repositories/promotion-rule.ts index 784c6d0a23347..d80c92c9f06c8 100644 --- a/packages/promotion/src/repositories/promotion-rule.ts +++ b/packages/promotion/src/repositories/promotion-rule.ts @@ -1,124 +1,11 @@ -import { Context, DAL } from "@medusajs/types" -import { DALUtils, MedusaError, arrayDifference } from "@medusajs/utils" -import { - FilterQuery as MikroFilterQuery, - FindOptions as MikroOptions, -} from "@mikro-orm/core" - -import { SqlEntityManager } from "@mikro-orm/postgresql" +import { DALUtils } from "@medusajs/utils" import { PromotionRule } from "@models" import { CreatePromotionRuleDTO, UpdatePromotionRuleDTO } from "@types" -export class PromotionRuleRepository 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 - } - - async find( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - return await manager.find( - PromotionRule, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async findAndCount( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise<[PromotionRule[], number]> { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - return await manager.findAndCount( - PromotionRule, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async delete(ids: string[], context: Context = {}): Promise { - const manager = this.getActiveManager(context) - await manager.nativeDelete(PromotionRule, { id: { $in: ids } }, {}) - } - - async create( - data: CreatePromotionRuleDTO[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const promotionRule = data.map((promotionRule) => { - return manager.create(PromotionRule, promotionRule) - }) - - manager.persist(promotionRule) - - return promotionRule - } - - async update( - data: UpdatePromotionRuleDTO[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - const promotionRuleIds = data.map((promotionRule) => promotionRule.id) - const existingPromotionRules = await this.find( - { - where: { - id: { - $in: promotionRuleIds, - }, - }, - }, - context - ) - - const dataAndExistingIdDifference = arrayDifference( - data.map((d) => d.id), - existingPromotionRules.map((plr) => plr.id) - ) - - if (dataAndExistingIdDifference.length) { - throw new MedusaError( - MedusaError.Types.NOT_FOUND, - `PromotionRule with id(s) "${dataAndExistingIdDifference.join( - ", " - )}" not found` - ) - } - - const existingPromotionRuleMap = new Map( - existingPromotionRules.map<[string, PromotionRule]>((promotionRule) => [ - promotionRule.id, - promotionRule, - ]) - ) - - const promotionRule = data.map((promotionRuleData) => { - const existingPromotionRule = existingPromotionRuleMap.get( - promotionRuleData.id - )! - - return manager.assign(existingPromotionRule, promotionRuleData) - }) - - manager.persist(promotionRule) - - return promotionRule +export class PromotionRuleRepository extends DALUtils.mikroOrmBaseRepositoryFactory< + PromotionRule, + { + create: CreatePromotionRuleDTO + update: UpdatePromotionRuleDTO } -} +>(PromotionRule) {} diff --git a/packages/promotion/src/repositories/promotion.ts b/packages/promotion/src/repositories/promotion.ts index a3b3b9a1b1abc..0df0002f02a59 100644 --- a/packages/promotion/src/repositories/promotion.ts +++ b/packages/promotion/src/repositories/promotion.ts @@ -1,124 +1,11 @@ -import { Context, DAL } from "@medusajs/types" -import { DALUtils, MedusaError } from "@medusajs/utils" -import { - LoadStrategy, - FilterQuery as MikroFilterQuery, - FindOptions as MikroOptions, -} from "@mikro-orm/core" -import { SqlEntityManager } from "@mikro-orm/postgresql" +import { DALUtils } from "@medusajs/utils" import { Promotion } from "@models" -import { CreatePromotionDTO, UpdatePromotionDTO } from "../types" +import { CreatePromotionDTO, UpdatePromotionDTO } from "@types" -export class PromotionRepository 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 - } - - async find( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.find( - Promotion, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async findAndCount( - findOptions: DAL.FindOptions = { where: {} }, - context: Context = {} - ): Promise<[Promotion[], number]> { - const manager = this.getActiveManager(context) - - const findOptions_ = { ...findOptions } - findOptions_.options ??= {} - - Object.assign(findOptions_.options, { - strategy: LoadStrategy.SELECT_IN, - }) - - return await manager.findAndCount( - Promotion, - findOptions_.where as MikroFilterQuery, - findOptions_.options as MikroOptions - ) - } - - async delete(ids: string[], context: Context = {}): Promise { - const manager = this.getActiveManager(context) - - await manager.nativeDelete(Promotion, { id: { $in: ids } }, {}) - } - - async create( - data: CreatePromotionDTO[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - - const promotions = data.map((promotionData) => { - return manager.create(Promotion, promotionData) - }) - - manager.persist(promotions) - - return promotions - } - - async update( - data: UpdatePromotionDTO[], - context: Context = {} - ): Promise { - const manager = this.getActiveManager(context) - const promotionIds = data.map((promotionData) => promotionData.id) - const existingPromotions = await this.find( - { - where: { - id: { - $in: promotionIds, - }, - }, - }, - context - ) - - const existingPromotionMap = new Map( - existingPromotions.map<[string, Promotion]>((promotion) => [ - promotion.id, - promotion, - ]) - ) - - const promotions = data.map((promotionData) => { - const existingPromotion = existingPromotionMap.get(promotionData.id) - - if (!existingPromotion) { - throw new MedusaError( - MedusaError.Types.NOT_FOUND, - `Promotion with id "${promotionData.id}" not found` - ) - } - - return manager.assign(existingPromotion, promotionData) - }) - - manager.persist(promotions) - - return promotions +export class PromotionRepository extends DALUtils.mikroOrmBaseRepositoryFactory< + Promotion, + { + create: CreatePromotionDTO + Update: UpdatePromotionDTO } -} +>(Promotion) {} diff --git a/packages/utils/src/dal/mikro-orm/mikro-orm-repository.ts b/packages/utils/src/dal/mikro-orm/mikro-orm-repository.ts index 2f8fe52d329bb..df24e190beffa 100644 --- a/packages/utils/src/dal/mikro-orm/mikro-orm-repository.ts +++ b/packages/utils/src/dal/mikro-orm/mikro-orm-repository.ts @@ -1,10 +1,10 @@ import { Context, DAL, - FilterQuery, + FilterQuery as InternalFilerQuery, RepositoryTransformOptions, } from "@medusajs/types" -import { isString } from "../../common" +import { arrayDifference, isString, MedusaError } from "../../common" import { MedusaContext } from "../../decorators" import { buildQuery, InjectTransactionManager } from "../../modules-sdk" import { @@ -12,9 +12,22 @@ import { transactionWrapper, } from "../utils" import { mikroOrmSerializer, mikroOrmUpdateDeletedAtRecursively } from "./utils" +import { + EntityManager, + EntitySchema, + FilterQuery, + LoadStrategy, + RequiredEntityData, +} from "@mikro-orm/core" +import { + EntityClass, + EntityName, + FilterQuery as MikroFilterQuery, +} from "@mikro-orm/core/typings" +import { FindOptions as MikroOptions } from "@mikro-orm/core/drivers/IDatabaseDriver" export class MikroOrmBase { - protected readonly manager_: any + readonly manager_: any protected constructor({ manager }) { this.manager_ = manager @@ -53,28 +66,47 @@ export class MikroOrmBase { } } -export abstract class MikroOrmAbstractBaseRepository - extends MikroOrmBase - implements DAL.RepositoryService -{ - abstract find(options?: DAL.FindOptions, context?: Context) - - abstract findAndCount( - options?: DAL.FindOptions, - context?: Context - ): Promise<[T[], number]> +/** + * Privileged extends of the abstract classes unless most of the methods can't be implemented + * in your repository. This base repository is also used to provide a base repository + * injection if needed to be able to use the common methods without being related to an entity. + * In this case, none of the method will be implemented except the manager and transaction + * related ones. + */ - abstract create(data: unknown[], context?: Context): Promise +export class MikroOrmBaseRepository< + T extends object = object +> extends MikroOrmBase { + constructor() { + // @ts-ignore + super(...arguments) + } + create(data: unknown[], context?: Context): Promise { + throw new Error("Method not implemented.") + } update(data: unknown[], context?: Context): Promise { throw new Error("Method not implemented.") } - abstract delete(ids: string[], context?: Context): Promise + delete(ids: string[], context?: Context): Promise { + throw new Error("Method not implemented.") + } + + find(options?: DAL.FindOptions, context?: Context): Promise { + throw new Error("Method not implemented.") + } + + findAndCount( + options?: DAL.FindOptions, + context?: Context + ): Promise<[T[], number]> { + throw new Error("Method not implemented.") + } @InjectTransactionManager() async softDelete( - idsOrFilter: string[] | FilterQuery, + idsOrFilter: string[] | InternalFilerQuery, @MedusaContext() { transactionManager: manager }: Context = {} ): Promise<[T[], Record]> { @@ -91,7 +123,11 @@ export abstract class MikroOrmAbstractBaseRepository const entities = await this.find({ where: filter as any }) const date = new Date() - await mikroOrmUpdateDeletedAtRecursively(manager, entities, date) + await mikroOrmUpdateDeletedAtRecursively( + manager, + entities as any[], + date + ) const softDeletedEntitiesMap = getSoftDeletedCascadedEntitiesIdsMappedBy({ entities, @@ -102,7 +138,7 @@ export abstract class MikroOrmAbstractBaseRepository @InjectTransactionManager() async restore( - idsOrFilter: string[] | FilterQuery, + idsOrFilter: string[] | InternalFilerQuery, @MedusaContext() { transactionManager: manager }: Context = {} ): Promise<[T[], Record]> { @@ -122,7 +158,7 @@ export abstract class MikroOrmAbstractBaseRepository const entities = await this.find(query) - await mikroOrmUpdateDeletedAtRecursively(manager, entities, null) + await mikroOrmUpdateDeletedAtRecursively(manager, entities as any[], null) const softDeletedEntitiesMap = getSoftDeletedCascadedEntitiesIdsMappedBy({ entities, @@ -151,97 +187,159 @@ export abstract class MikroOrmAbstractBaseRepository } } -export abstract class MikroOrmAbstractTreeRepositoryBase - extends MikroOrmBase - implements DAL.TreeRepositoryService -{ - protected constructor({ manager }) { +export class MikroOrmBaseTreeRepository< + T extends object = object +> extends MikroOrmBase { + constructor() { // @ts-ignore super(...arguments) } - abstract find( - options?: DAL.FindOptions, + find( + options?: DAL.FindOptions, transformOptions?: RepositoryTransformOptions, context?: Context - ) + ): Promise { + throw new Error("Method not implemented.") + } - abstract findAndCount( - options?: DAL.FindOptions, + findAndCount( + options?: DAL.FindOptions, transformOptions?: RepositoryTransformOptions, context?: Context - ): Promise<[T[], number]> - - abstract create(data: unknown, context?: Context): Promise - - abstract delete(id: string, context?: Context): Promise -} - -/** - * Privileged extends of the abstract classes unless most of the methods can't be implemented - * in your repository. This base repository is also used to provide a base repository - * injection if needed to be able to use the common methods without being related to an entity. - * In this case, none of the method will be implemented except the manager and transaction - * related ones. - */ - -export class MikroOrmBaseRepository extends MikroOrmAbstractBaseRepository { - constructor({ manager }) { - // @ts-ignore - super(...arguments) - } - - create(data: unknown[], context?: Context): Promise { + ): Promise<[T[], number]> { throw new Error("Method not implemented.") } - update(data: unknown[], context?: Context): Promise { + create(data: unknown, context?: Context): Promise { throw new Error("Method not implemented.") } - delete(ids: string[], context?: Context): Promise { + delete(id: string, context?: Context): Promise { throw new Error("Method not implemented.") } +} - find(options?: DAL.FindOptions, context?: Context): Promise { - throw new Error("Method not implemented.") - } +type DtoBasedMutationMethods = "create" | "update" - findAndCount( - options?: DAL.FindOptions, - context?: Context - ): Promise<[any[], number]> { - throw new Error("Method not implemented.") +export function mikroOrmBaseRepositoryFactory< + T extends object = object, + TDTos extends { [K in DtoBasedMutationMethods]?: any } = { + [K in DtoBasedMutationMethods]?: any } -} +>( + entity: EntityClass | EntitySchema | string, + primaryKey: string = "id" +) { + class MikroOrmAbstractBaseRepository_ extends MikroOrmBaseRepository { + async create(data: TDTos["create"][], context?: Context): Promise { + const manager = this.getActiveManager(context) + + const entities = data.map((data_) => { + return manager.create( + entity as EntityName, + data_ as RequiredEntityData + ) + }) + + manager.persist(entities) + + return entities + } -export class MikroOrmBaseTreeRepository extends MikroOrmAbstractTreeRepositoryBase { - constructor({ manager }) { - // @ts-ignore - super(...arguments) - } + async update(data: TDTos["update"][], context?: Context): Promise { + const manager = this.getActiveManager(context) - find( - options?: DAL.FindOptions, - transformOptions?: RepositoryTransformOptions, - context?: Context - ): Promise { - throw new Error("Method not implemented.") - } + const primaryKeyValues: string[] = data.map((data_) => data_[primaryKey]) + const existingEntities = await this.find( + { + where: { + [primaryKey]: { + $in: primaryKeyValues, + }, + }, + } as DAL.FindOptions, + context + ) + + const missingEntities = arrayDifference( + data.map((d) => d[primaryKey]), + existingEntities.map((d: any) => d[primaryKey]) + ) + + if (missingEntities.length) { + const entityName = (entity as EntityClass).name ?? entity + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `${entityName} with ${[primaryKey]} "${missingEntities.join( + ", " + )}" not found` + ) + } + + const existingEntitiesMap = new Map( + existingEntities.map<[string, T]>((entity_: any) => [ + entity_[primaryKey], + entity_, + ]) + ) + + const entities = data.map((data_) => { + const existingEntity = existingEntitiesMap.get(data_[primaryKey])! + return manager.assign(existingEntity, data_ as RequiredEntityData) + }) + + manager.persist(entities) + + return entities + } - findAndCount( - options?: DAL.FindOptions, - transformOptions?: RepositoryTransformOptions, - context?: Context - ): Promise<[any[], number]> { - throw new Error("Method not implemented.") - } + async delete(primaryKeyValues: string[], context?: Context): Promise { + const manager = this.getActiveManager(context) - create(data: unknown, context?: Context): Promise { - throw new Error("Method not implemented.") - } + await manager.nativeDelete( + entity as EntityName, + { [primaryKey]: { $in: primaryKeyValues } } as unknown as FilterQuery + ) + } - delete(id: string, context?: Context): Promise { - throw new Error("Method not implemented.") + async find(options?: DAL.FindOptions, context?: Context): Promise { + const manager = this.getActiveManager(context) + + const findOptions_ = { ...options } + findOptions_.options ??= {} + + Object.assign(findOptions_.options, { + strategy: LoadStrategy.SELECT_IN, + }) + + return await manager.find( + entity as EntityName, + findOptions_.where as MikroFilterQuery, + findOptions_.options as MikroOptions + ) + } + + async findAndCount( + findOptions: DAL.FindOptions = { where: {} }, + context: Context = {} + ): Promise<[T[], number]> { + const manager = this.getActiveManager(context) + + const findOptions_ = { ...findOptions } + findOptions_.options ??= {} + + Object.assign(findOptions_.options, { + strategy: LoadStrategy.SELECT_IN, + }) + + return await manager.findAndCount( + entity as EntityName, + findOptions_.where as MikroFilterQuery, + findOptions_.options as MikroOptions + ) + } } + + return MikroOrmAbstractBaseRepository_ } diff --git a/packages/utils/src/pricing/rule-type.ts b/packages/utils/src/pricing/rule-type.ts index ad386e861057d..9904f8c179b9f 100644 --- a/packages/utils/src/pricing/rule-type.ts +++ b/packages/utils/src/pricing/rule-type.ts @@ -1,4 +1,5 @@ import { MedusaError } from "../common" + type RuleAttributeInput = string | undefined export const ReservedPricingRuleAttributes = [