feat(pricing): add price rule entity (#5050)

**What**
- add price-rule entity to pricing module

blocked by #4977 

Fixes CORE-1497

Co-authored-by: Riqwan Thamir <5105988+riqwan@users.noreply.github.com>
Co-authored-by: Adrien de Peretti <25098370+adrien2p@users.noreply.github.com>
This commit is contained in:
Philip Korsholm
2023-09-26 12:59:55 +00:00
committed by GitHub
co-authored by Riqwan Thamir Adrien de Peretti
parent b3f1135fba
commit bc42b201ea
20 changed files with 1444 additions and 18 deletions
+6 -2
View File
@@ -1,10 +1,10 @@
import { ModulesSdkTypes } from "@medusajs/types"
import * as defaultRepositories from "@repositories"
import * as defaultServices from "@services"
import { LoaderOptions } from "@medusajs/modules-sdk"
import { loadCustomRepositories } from "@medusajs/utils"
import { ModulesSdkTypes } from "@medusajs/types"
import { asClass } from "awilix"
import { loadCustomRepositories } from "@medusajs/utils"
export default async ({
container,
@@ -22,6 +22,7 @@ export default async ({
moneyAmountService: asClass(defaultServices.MoneyAmountService).singleton(),
priceSetService: asClass(defaultServices.PriceSetService).singleton(),
ruleTypeService: asClass(defaultServices.RuleTypeService).singleton(),
priceRuleService: asClass(defaultServices.PriceRuleService).singleton(),
})
if (customRepositories) {
@@ -50,5 +51,8 @@ function loadDefaultRepositories({ container }) {
ruleTypeRepository: asClass(
defaultRepositories.RuleTypeRepository
).singleton(),
priceRuleRepository: asClass(
defaultRepositories.PriceRuleRepository
).singleton(),
})
}
@@ -337,6 +337,145 @@
],
"checks": [],
"foreignKeys": {}
},
{
"columns": {
"id": {
"name": "id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"price_set_id": {
"name": "price_set_id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"rule_type_id": {
"name": "rule_type_id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"is_dynamic": {
"name": "is_dynamic",
"type": "boolean",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"default": "false",
"mappedType": "boolean"
},
"value": {
"name": "value",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"priority": {
"name": "priority",
"type": "integer",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"default": "0",
"mappedType": "integer"
},
"price_set_money_amount_id": {
"name": "price_set_money_amount_id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"price_list_id": {
"name": "price_list_id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
}
},
"name": "price_rule",
"schema": "public",
"indexes": [
{
"columnNames": [
"rule_type_id"
],
"composite": false,
"keyName": "price_rule_rule_type_id_unique",
"primary": false,
"unique": true
},
{
"keyName": "price_rule_pkey",
"columnNames": [
"id"
],
"composite": false,
"primary": true,
"unique": true
}
],
"checks": [],
"foreignKeys": {
"price_rule_price_set_id_foreign": {
"constraintName": "price_rule_price_set_id_foreign",
"columnNames": [
"price_set_id"
],
"localTableName": "public.price_rule",
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.price_set",
"updateRule": "cascade"
},
"price_rule_rule_type_id_foreign": {
"constraintName": "price_rule_rule_type_id_foreign",
"columnNames": [
"rule_type_id"
],
"localTableName": "public.price_rule",
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.rule_type",
"updateRule": "cascade"
},
"price_rule_price_set_money_amount_id_foreign": {
"constraintName": "price_rule_price_set_money_amount_id_foreign",
"columnNames": [
"price_set_money_amount_id"
],
"localTableName": "public.price_rule",
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.price_set_money_amount",
"updateRule": "cascade"
}
}
}
]
}
@@ -0,0 +1,23 @@
import { Migration } from '@mikro-orm/migrations';
export class Migration20230913055746 extends Migration {
async up(): Promise<void> {
this.addSql('create table "price_rule" ("id" text not null, "price_set_id" text not null, "rule_type_id" text not null, "is_dynamic" boolean not null default false, "value" text not null, "priority" integer not null default 0, "price_set_money_amount_id" text not null, "price_list_id" text not null, constraint "price_rule_pkey" primary key ("id"));');
this.addSql('alter table "price_rule" add constraint "price_rule_price_set_id_foreign" foreign key ("price_set_id") references "price_set" ("id") on update cascade;');
this.addSql('alter table "price_rule" add constraint "price_rule_rule_type_id_foreign" foreign key ("rule_type_id") references "rule_type" ("id") on update cascade;');
this.addSql('alter table "price_rule" add constraint "price_rule_price_set_money_amount_id_foreign" foreign key ("price_set_money_amount_id") references "price_set_money_amount" ("id") on update cascade;');
}
async down(): Promise<void> {
this.addSql('alter table "price_rule" drop constraint "price_rule_price_set_id_foreign";');
this.addSql('alter table "price_rule" drop constraint "price_rule_price_set_money_amount_id_foreign";');
this.addSql('alter table "price_rule" drop constraint "price_rule_rule_type_id_foreign";');
this.addSql('drop table if exists "price_rule" cascade;');
}g
}
+1
View File
@@ -3,3 +3,4 @@ export { default as MoneyAmount } from "./money-amount"
export { default as PriceSet } from "./price-set"
export { default as PriceSetMoneyAmount } from "./price-set-money-amount"
export { default as RuleType } from "./rule-type"
export { default as PriceRule } from "./price-rule"
+69
View File
@@ -0,0 +1,69 @@
import {
BeforeCreate,
Collection,
Entity,
ManyToMany,
ManyToOne,
OneToMany,
OneToOne,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import MoneyAmount from "./money-amount"
import PriceSet from "./price-set"
import PriceSetMoneyAmount from "./price-set-money-amount"
import RuleType from "./rule-type"
import { generateEntityId } from "@medusajs/utils"
type OptionalFields = "id" | "is_dynamic" | "priority"
type OptionalRelations = "price_set" | "rule_type" | "price_set_money_amount"
@Entity()
export default class PriceRule {
[OptionalProps]: OptionalFields | OptionalRelations
@PrimaryKey({ columnType: "text" })
id!: string
@ManyToOne({
entity: () => PriceSet,
fieldName: "price_set_id",
name: "price_rule_price_set_id_unique",
})
price_set: PriceSet
@ManyToOne({
entity: () => RuleType,
fieldName: "rule_type_id",
name: "price_rule_rule_type_id_unique",
})
rule_type: RuleType
@Property({ columnType: "boolean", default: false })
is_dynamic: boolean
@Property({ columnType: "text" })
value: string
@Property({ columnType: "integer", default: 0 })
priority: number
@ManyToOne({
entity: () => PriceSetMoneyAmount,
fieldName: "price_set_money_amount_id",
name: "price_set_money_amount_id_unique",
})
price_set_money_amount: PriceSetMoneyAmount
@Property({ columnType: "text" })
price_list_id!: string
// TODO: Add price list
@BeforeCreate()
beforeCreate() {
this.id = generateEntityId(this.id, "pset")
}
}
@@ -3,3 +3,4 @@ export { CurrencyRepository } from "./currency"
export { MoneyAmountRepository } from "./money-amount"
export { PriceSetRepository } from "./price-set"
export { RuleTypeRepository } from "./rule-type"
export { PriceRuleRepository } from "./price-rule"
@@ -0,0 +1,144 @@
import {
Context,
CreatePriceRuleDTO,
DAL,
UpdatePriceRuleDTO,
} from "@medusajs/types"
import { DALUtils, MedusaError } from "@medusajs/utils"
import {
LoadStrategy,
FilterQuery as MikroFilterQuery,
FindOptions as MikroOptions,
} from "@mikro-orm/core"
import { PriceRule, PriceSet, PriceSetMoneyAmount, RuleType } from "@models"
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<PriceRule> = { where: {} },
context: Context = {}
): Promise<PriceRule[]> {
const manager = this.getActiveManager<SqlEntityManager>(context)
const findOptions_ = { ...findOptions }
findOptions_.options ??= {}
Object.assign(findOptions_.options, {
strategy: LoadStrategy.SELECT_IN,
})
return await manager.find(
PriceRule,
findOptions_.where as MikroFilterQuery<PriceRule>,
findOptions_.options as MikroOptions<PriceRule>
)
}
async findAndCount(
findOptions: DAL.FindOptions<PriceRule> = { where: {} },
context: Context = {}
): Promise<[PriceRule[], number]> {
const manager = this.getActiveManager<SqlEntityManager>(context)
const findOptions_ = { ...findOptions }
findOptions_.options ??= {}
Object.assign(findOptions_.options, {
strategy: LoadStrategy.SELECT_IN,
})
return await manager.findAndCount(
PriceRule,
findOptions_.where as MikroFilterQuery<PriceRule>,
findOptions_.options as MikroOptions<PriceRule>
)
}
async delete(ids: string[], context: Context = {}): Promise<void> {
const manager = this.getActiveManager<SqlEntityManager>(context)
await manager.nativeDelete(PriceRule, { id: { $in: ids } }, {})
}
async create(
data: CreatePriceRuleDTO[],
context: Context = {}
): Promise<PriceRule[]> {
const manager: SqlEntityManager =
this.getActiveManager<SqlEntityManager>(context)
const toCreate = await Promise.all(data.map(async (ruleData) => {
const ruleDataClone = { ...ruleData } as CreatePriceRuleDTO & {
rule_type: string
price_set: string
price_set_money_amount: string
}
ruleDataClone.rule_type = ruleData.rule_type_id
ruleDataClone.price_set = ruleData.price_set_id
ruleDataClone.price_set_money_amount = ruleData.price_set_money_amount_id
return ruleDataClone
}))
const priceRules = toCreate.map((ruleData) => {
return manager.create(PriceRule, ruleData as CreatePriceRuleDTO)
})
manager.persist(priceRules)
return priceRules
}
async update(
data: UpdatePriceRuleDTO[],
context: Context = {}
): Promise<PriceRule[]> {
const manager = this.getActiveManager<SqlEntityManager>(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
}
}
+2 -1
View File
@@ -2,4 +2,5 @@ export { default as CurrencyService } from "./currency"
export { default as MoneyAmountService } from "./money-amount"
export { default as PriceSetService } from "./price-set"
export { default as PricingModuleService } from "./pricing-module"
export { default as RuleTypeService } from "./rule-type"
export { default as RuleTypeService } from "./rule-type"
export { default as PriceRuleService } from "./price-rule"
@@ -0,0 +1,97 @@
import { Context, DAL, FindConfig, PricingTypes } from "@medusajs/types"
import {
InjectManager,
InjectTransactionManager,
MedusaContext,
ModulesSdkUtils,
doNotForceTransaction,
retrieveEntity,
shouldForceTransaction,
} from "@medusajs/utils"
import { PriceRule } from "@models"
import { PriceRuleRepository } from "@repositories"
type InjectedDependencies = {
priceRuleRepository: DAL.RepositoryService
}
export default class PriceRuleService<TEntity extends PriceRule = PriceRule> {
protected readonly priceRuleRepository_: DAL.RepositoryService
constructor({ priceRuleRepository }: InjectedDependencies) {
this.priceRuleRepository_ = priceRuleRepository
}
@InjectManager("priceRuleRepository_")
async retrieve(
priceRuleId: string,
config: FindConfig<PricingTypes.PriceRuleDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity> {
return (await retrieveEntity<PriceRule, PricingTypes.PriceRuleDTO>({
id: priceRuleId,
entityName: PriceRule.name,
repository: this.priceRuleRepository_,
config,
sharedContext,
})) as TEntity
}
@InjectManager("priceRuleRepository_")
async list(
filters: PricingTypes.FilterablePriceRuleProps = {},
config: FindConfig<PricingTypes.PriceRuleDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity[]> {
const queryConfig = ModulesSdkUtils.buildQuery<PriceRule>(filters, config)
return (await this.priceRuleRepository_.find(
queryConfig,
sharedContext
)) as TEntity[]
}
@InjectManager("priceRuleRepository_")
async listAndCount(
filters: PricingTypes.FilterablePriceRuleProps = {},
config: FindConfig<PricingTypes.PriceRuleDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<[TEntity[], number]> {
const queryConfig = ModulesSdkUtils.buildQuery<PriceRule>(filters, config)
return (await this.priceRuleRepository_.findAndCount(
queryConfig,
sharedContext
)) as [TEntity[], number]
}
@InjectTransactionManager(shouldForceTransaction, "priceRuleRepository_")
async create(
data: PricingTypes.CreatePriceRuleDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity[]> {
return (await (this.priceRuleRepository_ as PriceRuleRepository).create(
data,
sharedContext
)) as TEntity[]
}
@InjectTransactionManager(shouldForceTransaction, "priceRuleRepository_")
async update(
data: PricingTypes.UpdatePriceRuleDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity[]> {
return (await (this.priceRuleRepository_ as PriceRuleRepository).update(
data,
sharedContext
)) as TEntity[]
}
@InjectTransactionManager(doNotForceTransaction, "priceRuleRepository_")
async delete(
ids: string[],
@MedusaContext() sharedContext: Context = {}
): Promise<void> {
await this.priceRuleRepository_.delete(ids, sharedContext)
}
}
+107 -3
View File
@@ -8,13 +8,14 @@ import {
PricingFilters,
PricingTypes,
} from "@medusajs/types"
import { Currency, MoneyAmount, PriceSet, RuleType } from "@models"
import {
CurrencyService,
MoneyAmountService,
PriceSetService,
RuleTypeService,
PriceRuleService
} from "@services"
import { Currency, MoneyAmount, PriceRule, PriceSet, RuleType } from "@models"
import {
InjectManager,
@@ -29,15 +30,17 @@ type InjectedDependencies = {
baseRepository: DAL.RepositoryService
currencyService: CurrencyService<any>
moneyAmountService: MoneyAmountService<any>
ruleTypeService: RuleTypeService<any>
priceSetService: PriceSetService<any>
ruleTypeService: RuleTypeService<any>
priceRuleService: PriceRuleService<any>
}
export default class PricingModuleService<
TPriceSet extends PriceSet = PriceSet,
TMoneyAmount extends MoneyAmount = MoneyAmount,
TCurrency extends Currency = Currency,
TRuleType extends RuleType = RuleType
TRuleType extends RuleType = RuleType,
TPriceRule extends PriceRule = PriceRule,
> implements PricingTypes.IPricingModuleService
{
protected baseRepository_: DAL.RepositoryService
@@ -45,6 +48,7 @@ export default class PricingModuleService<
protected readonly moneyAmountService_: MoneyAmountService<TMoneyAmount>
protected readonly ruleTypeService_: RuleTypeService<TRuleType>
protected readonly priceSetService_: PriceSetService<TPriceSet>
protected readonly priceRuleService_: PriceRuleService<TPriceRule>
constructor(
{
@@ -53,6 +57,7 @@ export default class PricingModuleService<
currencyService,
ruleTypeService,
priceSetService,
priceRuleService,
}: InjectedDependencies,
protected readonly moduleDeclaration: InternalModuleDeclaration
) {
@@ -61,6 +66,7 @@ export default class PricingModuleService<
this.moneyAmountService_ = moneyAmountService
this.ruleTypeService_ = ruleTypeService
this.priceSetService_ = priceSetService
this.priceRuleService_ = priceRuleService
}
__joinerConfig(): ModuleJoinerConfig {
@@ -520,4 +526,102 @@ export default class PricingModuleService<
): Promise<void> {
await this.ruleTypeService_.delete(ruleTypes, sharedContext)
}
@InjectManager("baseRepository_")
async retrievePriceRule(
id: string,
config: FindConfig<PricingTypes.PriceRuleDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<PricingTypes.PriceRuleDTO> {
const priceRule = await this.priceRuleService_.retrieve(
id,
config,
sharedContext
)
return this.baseRepository_.serialize<PricingTypes.PriceRuleDTO>(priceRule, {
populate: true,
})
}
@InjectManager("baseRepository_")
async listPriceRules(
filters: PricingTypes.FilterablePriceRuleProps = {},
config: FindConfig<PricingTypes.PriceRuleDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<PricingTypes.PriceRuleDTO[]> {
const priceRules = await this.priceRuleService_.list(
filters,
config,
sharedContext
)
return this.baseRepository_.serialize<PricingTypes.PriceRuleDTO[]>(
priceRules,
{
populate: true,
}
)
}
@InjectManager("baseRepository_")
async listAndCountPriceRules(
filters: PricingTypes.FilterablePriceRuleProps = {},
config: FindConfig<PricingTypes.PriceRuleDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<[PricingTypes.PriceRuleDTO[], number]> {
const [priceRules, count] = await this.priceRuleService_.listAndCount(
filters,
config,
sharedContext
)
return [
await this.baseRepository_.serialize<PricingTypes.PriceRuleDTO[]>(
priceRules,
{
populate: true,
}
),
count,
]
}
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
async createPriceRules(
data: PricingTypes.CreatePriceRuleDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<PricingTypes.PriceRuleDTO[]> {
const priceRules = await this.priceRuleService_.create(data, sharedContext)
return this.baseRepository_.serialize<PricingTypes.PriceRuleDTO[]>(
priceRules,
{
populate: true,
}
)
}
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
async updatePriceRules(
data: PricingTypes.UpdatePriceRuleDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<PricingTypes.PriceRuleDTO[]> {
const priceRules = await this.priceRuleService_.update(data, sharedContext)
return this.baseRepository_.serialize<PricingTypes.PriceRuleDTO[]>(
priceRules,
{
populate: true,
}
)
}
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
async deletePriceRules(
priceRuleIds: string[],
@MedusaContext() sharedContext: Context = {}
): Promise<void> {
await this.priceRuleService_.delete(priceRuleIds, sharedContext)
}
}