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>
This commit is contained in:
1 parent
ef5024980d
commit
b6ac768698
38 files changed
+411
-2832
No files matched your search
+1
-1
@@ -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 () => {
|
||||
|
||||
@@ -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
|
||||
export class ApplicationMethodRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
ApplicationMethod,
|
||||
{
|
||||
create: CreateApplicationMethodDTO
|
||||
update: UpdateApplicationMethodDTO
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<ApplicationMethod> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<ApplicationMethod[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
return await manager.find(
|
||||
ApplicationMethod,
|
||||
findOptions_.where as MikroFilterQuery<ApplicationMethod>,
|
||||
findOptions_.options as MikroOptions<ApplicationMethod>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<ApplicationMethod> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[ApplicationMethod[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
return await manager.findAndCount(
|
||||
ApplicationMethod,
|
||||
findOptions_.where as MikroFilterQuery<ApplicationMethod>,
|
||||
findOptions_.options as MikroOptions<ApplicationMethod>
|
||||
)
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
await manager.nativeDelete(ApplicationMethod, { id: { $in: ids } }, {})
|
||||
}
|
||||
|
||||
async create(
|
||||
data: CreateApplicationMethodDTO[],
|
||||
context: Context = {}
|
||||
): Promise<ApplicationMethod[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const applicationMethods = data.map((applicationMethodData) => {
|
||||
return manager.create(ApplicationMethod, applicationMethodData)
|
||||
})
|
||||
|
||||
manager.persist(applicationMethods)
|
||||
|
||||
return applicationMethods
|
||||
}
|
||||
|
||||
async update(
|
||||
data: UpdateApplicationMethodDTO[],
|
||||
context: Context = {}
|
||||
): Promise<ApplicationMethod[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(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
|
||||
}
|
||||
}
|
||||
>(ApplicationMethod) {}
|
||||
@@ -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
|
||||
export class PromotionRuleValueRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
PromotionRuleValue,
|
||||
{
|
||||
create: CreatePromotionRuleValueDTO
|
||||
update: UpdatePromotionRuleValueDTO
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<PromotionRuleValue> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<PromotionRuleValue[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
return await manager.find(
|
||||
PromotionRuleValue,
|
||||
findOptions_.where as MikroFilterQuery<PromotionRuleValue>,
|
||||
findOptions_.options as MikroOptions<PromotionRuleValue>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<PromotionRuleValue> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[PromotionRuleValue[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
return await manager.findAndCount(
|
||||
PromotionRuleValue,
|
||||
findOptions_.where as MikroFilterQuery<PromotionRuleValue>,
|
||||
findOptions_.options as MikroOptions<PromotionRuleValue>
|
||||
)
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(PromotionRuleValue, { id: { $in: ids } }, {})
|
||||
}
|
||||
|
||||
async create(
|
||||
data: CreatePromotionRuleValueDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PromotionRuleValue[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const promotionRuleValue = data.map((promotionRuleValue) => {
|
||||
return manager.create(PromotionRuleValue, promotionRuleValue)
|
||||
})
|
||||
|
||||
manager.persist(promotionRuleValue)
|
||||
|
||||
return promotionRuleValue
|
||||
}
|
||||
|
||||
async update(
|
||||
data: UpdatePromotionRuleValueDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PromotionRuleValue[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(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
|
||||
}
|
||||
}
|
||||
>(PromotionRuleValue) {}
|
||||
@@ -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
|
||||
export class PromotionRuleRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
PromotionRule,
|
||||
{
|
||||
create: CreatePromotionRuleDTO
|
||||
update: UpdatePromotionRuleDTO
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<PromotionRule> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<PromotionRule[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
return await manager.find(
|
||||
PromotionRule,
|
||||
findOptions_.where as MikroFilterQuery<PromotionRule>,
|
||||
findOptions_.options as MikroOptions<PromotionRule>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<PromotionRule> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[PromotionRule[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
return await manager.findAndCount(
|
||||
PromotionRule,
|
||||
findOptions_.where as MikroFilterQuery<PromotionRule>,
|
||||
findOptions_.options as MikroOptions<PromotionRule>
|
||||
)
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(PromotionRule, { id: { $in: ids } }, {})
|
||||
}
|
||||
|
||||
async create(
|
||||
data: CreatePromotionRuleDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PromotionRule[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const promotionRule = data.map((promotionRule) => {
|
||||
return manager.create(PromotionRule, promotionRule)
|
||||
})
|
||||
|
||||
manager.persist(promotionRule)
|
||||
|
||||
return promotionRule
|
||||
}
|
||||
|
||||
async update(
|
||||
data: UpdatePromotionRuleDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PromotionRule[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(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
|
||||
}
|
||||
}
|
||||
>(PromotionRule) {}
|
||||
@@ -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
|
||||
export class PromotionRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
Promotion,
|
||||
{
|
||||
create: CreatePromotionDTO
|
||||
Update: UpdatePromotionDTO
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<Promotion> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<Promotion[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.find(
|
||||
Promotion,
|
||||
findOptions_.where as MikroFilterQuery<Promotion>,
|
||||
findOptions_.options as MikroOptions<Promotion>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<Promotion> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[Promotion[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.findAndCount(
|
||||
Promotion,
|
||||
findOptions_.where as MikroFilterQuery<Promotion>,
|
||||
findOptions_.options as MikroOptions<Promotion>
|
||||
)
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
await manager.nativeDelete(Promotion, { id: { $in: ids } }, {})
|
||||
}
|
||||
|
||||
async create(
|
||||
data: CreatePromotionDTO[],
|
||||
context: Context = {}
|
||||
): Promise<Promotion[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const promotions = data.map((promotionData) => {
|
||||
return manager.create(Promotion, promotionData)
|
||||
})
|
||||
|
||||
manager.persist(promotions)
|
||||
|
||||
return promotions
|
||||
}
|
||||
|
||||
async update(
|
||||
data: UpdatePromotionDTO[],
|
||||
context: Context = {}
|
||||
): Promise<Promotion[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(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
|
||||
}
|
||||
}
|
||||
>(Promotion) {}
|
||||
Reference in new issue
Block a user