feat(pricing,types): price list API + price calculations with price lists (#5498)

**what:**

**PriceList Service APIs:**

- createPriceList
- updatePriceList
- addPriceListPrices
- removePriceListRules
- setPriceListRules
- deletePriceList
- listPriceLists
- listAndCountPriceLists

**Price Calculations**

- Returns prices with price list prices
- Returns a new shape with calculated and original prices


Co-authored-by: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com>
This commit is contained in:
Riqwan Thamir
2023-11-15 21:24:29 +01:00
committed by GitHub
parent 2947f57db1
commit 1772e80ed1
58 changed files with 5745 additions and 1112 deletions
+3
View File
@@ -1,5 +1,8 @@
export { default as Currency } from "./currency"
export { default as MoneyAmount } from "./money-amount"
export { default as PriceList } from "./price-list"
export { default as PriceListRule } from "./price-list-rule"
export { default as PriceListRuleValue } from "./price-list-rule-value"
export { default as PriceRule } from "./price-rule"
export { default as PriceSet } from "./price-set"
export { default as PriceSetMoneyAmount } from "./price-set-money-amount"
+36 -1
View File
@@ -3,17 +3,27 @@ import {
BeforeCreate,
Collection,
Entity,
Index,
ManyToMany,
ManyToOne,
OneToOne,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import Currency from "./currency"
import { PriceSetMoneyAmount } from "./index"
import PriceSet from "./price-set"
@Entity()
class MoneyAmount {
[OptionalProps]?:
| "created_at"
| "updated_at"
| "deleted_at"
| "price_set_money_amount"
@PrimaryKey({ columnType: "text" })
id!: string
@@ -26,6 +36,12 @@ class MoneyAmount {
})
price_sets = new Collection<PriceSet>(this)
@OneToOne({
entity: () => PriceSetMoneyAmount,
mappedBy: (psma) => psma.money_amount,
})
price_set_money_amount: PriceSetMoneyAmount
@ManyToOne(() => Currency, {
nullable: true,
index: "IDX_money_amount_currency_code",
@@ -33,7 +49,7 @@ class MoneyAmount {
})
currency?: Currency
@Property({ columnType: "numeric", nullable: true })
@Property({ columnType: "numeric", nullable: true, serializer: Number })
amount?: number
@Property({ columnType: "numeric", nullable: true })
@@ -42,6 +58,25 @@ class MoneyAmount {
@Property({ columnType: "numeric", nullable: true })
max_quantity?: number | null
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@Index({ name: "IDX_money_amount_deleted_at" })
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "ma")
@@ -0,0 +1,31 @@
import {
BeforeCreate,
Entity,
ManyToOne,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import PriceListRule from "./price-list-rule"
import { generateEntityId } from "@medusajs/utils"
@Entity()
export default class PriceListRuleValue {
@PrimaryKey({ columnType: "text" })
id!: string
@ManyToOne(() => PriceListRule, {
onDelete: "cascade",
fieldName: 'price_list_rule_id',
index: "IDX_price_list_rule_price_list_rule_value_id",
})
price_list_rule: PriceListRule
@Property({ columnType: "text" })
value: string
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "plrv")
}
}
@@ -0,0 +1,57 @@
import {
BeforeCreate,
Cascade,
Collection,
Entity,
ManyToOne,
OneToMany,
OptionalProps,
PrimaryKey,
Unique,
} from "@mikro-orm/core"
import { generateEntityId } from "@medusajs/utils"
import PriceList from "./price-list"
import PriceListRuleValue from "./price-list-rule-value"
import RuleType from "./rule-type"
type OptionalFields = "id"
type OptionalRelations = "rule_type" | "price_list"
@Entity()
@Unique({
name: "IDX_price_list_rule_rule_type_id_price_list_id_unique",
properties: ["price_list", "rule_type"],
})
export default class PriceListRule {
[OptionalProps]: OptionalFields | OptionalRelations
@PrimaryKey({ columnType: "text" })
id!: string
@ManyToOne({
entity: () => RuleType,
fieldName: "rule_type_id",
name: "price_rule_rule_type_id_unique",
index: "IDX_price_list_rule_rule_type_id",
})
rule_type: RuleType
@OneToMany(() => PriceListRuleValue, (plrv) => plrv.price_list_rule, {
cascade: [Cascade.REMOVE],
})
price_list_rule_values = new Collection<PriceListRuleValue>(this)
@ManyToOne({
entity: () => PriceList,
fieldName: "price_list_id",
name: "price_rule_price_list_id",
index: "IDX_price_list_rule_price_list_id",
})
price_list: PriceList
@BeforeCreate()
beforeCreate() {
this.id = generateEntityId(this.id, "plrule")
}
}
+110
View File
@@ -0,0 +1,110 @@
import { generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
Collection,
Entity,
Enum,
Index,
ManyToMany,
OneToMany,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import { PriceListStatus, PriceListType } from "@medusajs/utils"
import PriceListRule from "./price-list-rule"
import PriceSetMoneyAmount from "./price-set-money-amount"
import RuleType from "./rule-type"
type OptionalFields =
| "status"
| "type"
| "number_rules"
| "starts_at"
| "ends_at"
| "created_at"
| "updated_at"
| "deleted_at"
type OptionalRelations =
| "price_set_money_amounts"
| "rule_types"
| "price_list_rules"
@Entity()
export default class PriceList {
[OptionalProps]: OptionalFields | OptionalRelations
@PrimaryKey({ columnType: "text" })
id!: string
@Property({ columnType: "text" })
title: string
@Property({ columnType: "text" })
description: string
@Enum({ items: () => PriceListStatus, default: PriceListStatus.DRAFT })
status?: PriceListStatus
@Enum({ items: () => PriceListType, default: PriceListType.SALE })
type?: PriceListType
@Property({
columnType: "timestamptz",
nullable: true,
})
starts_at?: Date | null
@Property({
columnType: "timestamptz",
nullable: true,
})
ends_at?: Date | null
@OneToMany(() => PriceSetMoneyAmount, (psma) => psma.price_list, {
cascade: [Cascade.REMOVE],
})
price_set_money_amounts = new Collection<PriceSetMoneyAmount>(this)
@OneToMany(() => PriceListRule, (pr) => pr.price_list, {
cascade: [Cascade.REMOVE],
})
price_list_rules = new Collection<PriceListRule>(this)
@ManyToMany({
entity: () => RuleType,
pivotEntity: () => PriceListRule,
cascade: [Cascade.REMOVE],
})
rule_types = new Collection<RuleType>(this)
@Property({ columnType: "integer", default: 0 })
number_rules?: number
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at?: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at?: Date
@Index({ name: "IDX_price_list_deleted_at" })
@Property({ columnType: "timestamptz", nullable: true })
deleted_at?: Date
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "plist")
}
}
@@ -57,11 +57,6 @@ export default class PriceRule {
})
price_set_money_amount: PriceSetMoneyAmount
@Property({ columnType: "text" })
price_list_id!: string
// TODO: Add price list
@BeforeCreate()
beforeCreate() {
this.id = generateEntityId(this.id, "prule")
@@ -5,12 +5,14 @@ import {
Entity,
ManyToOne,
OneToMany,
OneToOne,
PrimaryKey,
PrimaryKeyType,
Property,
} from "@mikro-orm/core"
import MoneyAmount from "./money-amount"
import PriceList from "./price-list"
import PriceRule from "./price-rule"
import PriceSet from "./price-set"
import PriceSetMoneyAmountRules from "./price-set-money-amount-rules"
@@ -29,7 +31,7 @@ export default class PriceSetMoneyAmount {
})
price_set?: PriceSet
@ManyToOne(() => MoneyAmount, {
@OneToOne(() => MoneyAmount, {
onDelete: "cascade",
index: "IDX_price_set_money_amount_money_amount_id",
})
@@ -50,6 +52,13 @@ export default class PriceSetMoneyAmount {
})
price_set_money_amount_rules = new Collection<PriceSetMoneyAmountRules>(this)
@ManyToOne(() => PriceList, {
index: "IDX_price_rule_price_list_id",
onDelete: "cascade",
nullable: true,
})
price_list?: PriceList
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "psma")
+1 -1
View File
@@ -18,7 +18,7 @@ import RuleType from "./rule-type"
@Entity()
export default class PriceSet {
[OptionalProps]?: "price_set_money_amounts" | "rule_types"
[OptionalProps]?: "price_set_money_amounts" | "rule_types" | "money_amounts"
@PrimaryKey({ columnType: "text" })
id!: string