chore: merge money amounts and price set money amounts (#6768)

what: 

- merges price set money amounts and money amount
This commit is contained in:
Riqwan Thamir
2024-03-21 17:10:13 +01:00
committed by GitHub
parent 1ef9c78cea
commit b792c4cab5
29 changed files with 569 additions and 1534 deletions

View File

@@ -1,11 +1,10 @@
import { Modules } from "@medusajs/modules-sdk"
import { ModuleJoinerConfig } from "@medusajs/types"
import { MapToConfig } from "@medusajs/utils"
import { MoneyAmount, PriceList, PriceSet, PriceSetMoneyAmount } from "@models"
import { PriceList, PriceSet, PriceSetMoneyAmount } from "@models"
import schema from "./schema"
export const LinkableKeys = {
money_amount_id: MoneyAmount.name,
price_set_id: PriceSet.name,
price_list_id: PriceList.name,
price_set_money_amount_id: PriceSetMoneyAmount.name,
@@ -34,13 +33,6 @@ export const joinerConfig: ModuleJoinerConfig = {
entity: "PriceSet",
},
},
{
name: ["money_amount", "money_amounts"],
args: {
methodSuffix: "MoneyAmounts",
entity: "MoneyAmount",
},
},
{
name: ["price_list", "price_lists"],
args: {

View File

@@ -1,5 +1,7 @@
{
"namespaces": ["public"],
"namespaces": [
"public"
],
"name": "public",
"tables": [
{
@@ -87,7 +89,9 @@
"indexes": [
{
"keyName": "IDX_money_amount_currency_code",
"columnNames": ["currency_code"],
"columnNames": [
"currency_code"
],
"composite": false,
"primary": false,
"unique": false,
@@ -95,7 +99,9 @@
},
{
"keyName": "IDX_money_amount_deleted_at",
"columnNames": ["deleted_at"],
"columnNames": [
"deleted_at"
],
"composite": false,
"primary": false,
"unique": false,
@@ -103,7 +109,9 @@
},
{
"keyName": "money_amount_pkey",
"columnNames": ["id"],
"columnNames": [
"id"
],
"composite": false,
"primary": true,
"unique": true
@@ -149,7 +157,10 @@
"primary": false,
"nullable": false,
"default": "'draft'",
"enumItems": ["active", "draft"],
"enumItems": [
"active",
"draft"
],
"mappedType": "enum"
},
"type": {
@@ -160,7 +171,10 @@
"primary": false,
"nullable": false,
"default": "'sale'",
"enumItems": ["sale", "override"],
"enumItems": [
"sale",
"override"
],
"mappedType": "enum"
},
"starts_at": {
@@ -231,7 +245,9 @@
"indexes": [
{
"keyName": "IDX_price_list_deleted_at",
"columnNames": ["deleted_at"],
"columnNames": [
"deleted_at"
],
"composite": false,
"primary": false,
"unique": false,
@@ -239,7 +255,9 @@
},
{
"keyName": "price_list_pkey",
"columnNames": ["id"],
"columnNames": [
"id"
],
"composite": false,
"primary": true,
"unique": true
@@ -297,7 +315,9 @@
"indexes": [
{
"keyName": "IDX_price_set_deleted_at",
"columnNames": ["deleted_at"],
"columnNames": [
"deleted_at"
],
"composite": false,
"primary": false,
"unique": false,
@@ -305,7 +325,9 @@
},
{
"keyName": "price_set_pkey",
"columnNames": ["id"],
"columnNames": [
"id"
],
"composite": false,
"primary": true,
"unique": true
@@ -334,8 +356,8 @@
"nullable": true,
"mappedType": "text"
},
"price_set_id": {
"name": "price_set_id",
"currency_code": {
"name": "currency_code",
"type": "text",
"unsigned": false,
"autoincrement": false,
@@ -343,8 +365,35 @@
"nullable": false,
"mappedType": "text"
},
"money_amount_id": {
"name": "money_amount_id",
"amount": {
"name": "amount",
"type": "numeric",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "decimal"
},
"min_quantity": {
"name": "min_quantity",
"type": "numeric",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "decimal"
},
"max_quantity": {
"name": "max_quantity",
"type": "numeric",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "decimal"
},
"price_set_id": {
"name": "price_set_id",
"type": "text",
"unsigned": false,
"autoincrement": false,
@@ -408,31 +457,30 @@
"schema": "public",
"indexes": [
{
"columnNames": ["money_amount_id"],
"keyName": "IDX_price_set_money_amount_currency_code",
"columnNames": [
"currency_code"
],
"composite": false,
"keyName": "price_set_money_amount_money_amount_id_unique",
"primary": false,
"unique": true
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_set_money_amount_currency_code\" ON \"price_set_money_amount\" (currency_code) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_price_set_money_amount_price_set_id",
"columnNames": ["price_set_id"],
"columnNames": [
"price_set_id"
],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_set_money_amount_price_set_id\" ON \"price_set_money_amount\" (price_set_id) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_price_set_money_amount_money_amount_id",
"columnNames": ["money_amount_id"],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_set_money_amount_money_amount_id\" ON \"price_set_money_amount\" (money_amount_id) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_price_set_money_amount_price_list_id",
"columnNames": ["price_list_id"],
"columnNames": [
"price_list_id"
],
"composite": false,
"primary": false,
"unique": false,
@@ -440,7 +488,9 @@
},
{
"keyName": "IDX_price_set_money_amount_deleted_at",
"columnNames": ["deleted_at"],
"columnNames": [
"deleted_at"
],
"composite": false,
"primary": false,
"unique": false,
@@ -448,7 +498,9 @@
},
{
"keyName": "price_set_money_amount_pkey",
"columnNames": ["id"],
"columnNames": [
"id"
],
"composite": false,
"primary": true,
"unique": true
@@ -458,27 +510,26 @@
"foreignKeys": {
"price_set_money_amount_price_set_id_foreign": {
"constraintName": "price_set_money_amount_price_set_id_foreign",
"columnNames": ["price_set_id"],
"columnNames": [
"price_set_id"
],
"localTableName": "public.price_set_money_amount",
"referencedColumnNames": ["id"],
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.price_set",
"deleteRule": "cascade",
"updateRule": "cascade"
},
"price_set_money_amount_money_amount_id_foreign": {
"constraintName": "price_set_money_amount_money_amount_id_foreign",
"columnNames": ["money_amount_id"],
"localTableName": "public.price_set_money_amount",
"referencedColumnNames": ["id"],
"referencedTableName": "public.money_amount",
"deleteRule": "cascade",
"updateRule": "cascade"
},
"price_set_money_amount_price_list_id_foreign": {
"constraintName": "price_set_money_amount_price_list_id_foreign",
"columnNames": ["price_list_id"],
"columnNames": [
"price_list_id"
],
"localTableName": "public.price_set_money_amount",
"referencedColumnNames": ["id"],
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.price_list",
"deleteRule": "cascade",
"updateRule": "cascade"
@@ -562,7 +613,9 @@
"indexes": [
{
"keyName": "IDX_rule_type_rule_attribute",
"columnNames": ["rule_attribute"],
"columnNames": [
"rule_attribute"
],
"composite": false,
"primary": false,
"unique": false,
@@ -570,7 +623,9 @@
},
{
"keyName": "IDX_rule_type_deleted_at",
"columnNames": ["deleted_at"],
"columnNames": [
"deleted_at"
],
"composite": false,
"primary": false,
"unique": false,
@@ -578,7 +633,9 @@
},
{
"keyName": "rule_type_pkey",
"columnNames": ["id"],
"columnNames": [
"id"
],
"composite": false,
"primary": true,
"unique": true
@@ -654,7 +711,9 @@
"indexes": [
{
"keyName": "IDX_price_set_rule_type_price_set_id",
"columnNames": ["price_set_id"],
"columnNames": [
"price_set_id"
],
"composite": false,
"primary": false,
"unique": false,
@@ -662,7 +721,9 @@
},
{
"keyName": "IDX_price_set_rule_type_rule_type_id",
"columnNames": ["rule_type_id"],
"columnNames": [
"rule_type_id"
],
"composite": false,
"primary": false,
"unique": false,
@@ -670,7 +731,9 @@
},
{
"keyName": "IDX_price_set_rule_type_deleted_at",
"columnNames": ["deleted_at"],
"columnNames": [
"deleted_at"
],
"composite": false,
"primary": false,
"unique": false,
@@ -678,7 +741,9 @@
},
{
"keyName": "price_set_rule_type_pkey",
"columnNames": ["id"],
"columnNames": [
"id"
],
"composite": false,
"primary": true,
"unique": true
@@ -688,18 +753,26 @@
"foreignKeys": {
"price_set_rule_type_price_set_id_foreign": {
"constraintName": "price_set_rule_type_price_set_id_foreign",
"columnNames": ["price_set_id"],
"columnNames": [
"price_set_id"
],
"localTableName": "public.price_set_rule_type",
"referencedColumnNames": ["id"],
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.price_set",
"deleteRule": "cascade",
"updateRule": "cascade"
},
"price_set_rule_type_rule_type_id_foreign": {
"constraintName": "price_set_rule_type_rule_type_id_foreign",
"columnNames": ["rule_type_id"],
"columnNames": [
"rule_type_id"
],
"localTableName": "public.price_set_rule_type",
"referencedColumnNames": ["id"],
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.rule_type",
"deleteRule": "cascade",
"updateRule": "cascade"
@@ -801,7 +874,9 @@
"indexes": [
{
"keyName": "IDX_price_rule_price_set_id",
"columnNames": ["price_set_id"],
"columnNames": [
"price_set_id"
],
"composite": false,
"primary": false,
"unique": false,
@@ -809,7 +884,9 @@
},
{
"keyName": "IDX_price_rule_rule_type_id",
"columnNames": ["rule_type_id"],
"columnNames": [
"rule_type_id"
],
"composite": false,
"primary": false,
"unique": false,
@@ -817,7 +894,9 @@
},
{
"keyName": "IDX_price_rule_price_set_money_amount_id_unique",
"columnNames": ["price_set_money_amount_id"],
"columnNames": [
"price_set_money_amount_id"
],
"composite": false,
"primary": false,
"unique": false,
@@ -825,7 +904,9 @@
},
{
"keyName": "IDX_price_rule_deleted_at",
"columnNames": ["deleted_at"],
"columnNames": [
"deleted_at"
],
"composite": false,
"primary": false,
"unique": false,
@@ -833,7 +914,9 @@
},
{
"keyName": "price_rule_pkey",
"columnNames": ["id"],
"columnNames": [
"id"
],
"composite": false,
"primary": true,
"unique": true
@@ -843,26 +926,38 @@
"foreignKeys": {
"price_rule_price_set_id_foreign": {
"constraintName": "price_rule_price_set_id_foreign",
"columnNames": ["price_set_id"],
"columnNames": [
"price_set_id"
],
"localTableName": "public.price_rule",
"referencedColumnNames": ["id"],
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.price_set",
"deleteRule": "cascade",
"updateRule": "cascade"
},
"price_rule_rule_type_id_foreign": {
"constraintName": "price_rule_rule_type_id_foreign",
"columnNames": ["rule_type_id"],
"columnNames": [
"rule_type_id"
],
"localTableName": "public.price_rule",
"referencedColumnNames": ["id"],
"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"],
"columnNames": [
"price_set_money_amount_id"
],
"localTableName": "public.price_rule",
"referencedColumnNames": ["id"],
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.price_set_money_amount",
"deleteRule": "cascade",
"updateRule": "cascade"
@@ -880,6 +975,15 @@
"nullable": false,
"mappedType": "text"
},
"rule_type_id": {
"name": "rule_type_id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"price_list_id": {
"name": "price_list_id",
"type": "text",
@@ -920,15 +1024,6 @@
"nullable": true,
"length": 6,
"mappedType": "datetime"
},
"rule_type_id": {
"name": "rule_type_id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
}
},
"name": "price_list_rule",
@@ -936,7 +1031,9 @@
"indexes": [
{
"keyName": "IDX_price_list_rule_rule_type_id_unique",
"columnNames": ["rule_type_id"],
"columnNames": [
"rule_type_id"
],
"composite": false,
"primary": false,
"unique": false,
@@ -944,7 +1041,9 @@
},
{
"keyName": "IDX_price_list_rule_price_list_id",
"columnNames": ["price_list_id"],
"columnNames": [
"price_list_id"
],
"composite": false,
"primary": false,
"unique": false,
@@ -952,7 +1051,9 @@
},
{
"keyName": "IDX_price_list_rule_deleted_at",
"columnNames": ["deleted_at"],
"columnNames": [
"deleted_at"
],
"composite": false,
"primary": false,
"unique": false,
@@ -960,7 +1061,9 @@
},
{
"keyName": "price_list_rule_pkey",
"columnNames": ["id"],
"columnNames": [
"id"
],
"composite": false,
"primary": true,
"unique": true
@@ -968,22 +1071,30 @@
],
"checks": [],
"foreignKeys": {
"price_list_rule_price_list_id_foreign": {
"constraintName": "price_list_rule_price_list_id_foreign",
"columnNames": ["price_list_id"],
"localTableName": "public.price_list_rule",
"referencedColumnNames": ["id"],
"referencedTableName": "public.price_list",
"deleteRule": "cascade",
"updateRule": "cascade"
},
"price_list_rule_rule_type_id_foreign": {
"constraintName": "price_list_rule_rule_type_id_foreign",
"columnNames": ["rule_type_id"],
"columnNames": [
"rule_type_id"
],
"localTableName": "public.price_list_rule",
"referencedColumnNames": ["id"],
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.rule_type",
"updateRule": "cascade"
},
"price_list_rule_price_list_id_foreign": {
"constraintName": "price_list_rule_price_list_id_foreign",
"columnNames": [
"price_list_id"
],
"localTableName": "public.price_list_rule",
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.price_list",
"deleteRule": "cascade",
"updateRule": "cascade"
}
}
},
@@ -1054,7 +1165,9 @@
"indexes": [
{
"keyName": "IDX_price_list_rule_value_price_list_rule_id",
"columnNames": ["price_list_rule_id"],
"columnNames": [
"price_list_rule_id"
],
"composite": false,
"primary": false,
"unique": false,
@@ -1062,7 +1175,9 @@
},
{
"keyName": "IDX_price_list_rule_value_deleted_at",
"columnNames": ["deleted_at"],
"columnNames": [
"deleted_at"
],
"composite": false,
"primary": false,
"unique": false,
@@ -1070,7 +1185,9 @@
},
{
"keyName": "price_list_rule_value_pkey",
"columnNames": ["id"],
"columnNames": [
"id"
],
"composite": false,
"primary": true,
"unique": true
@@ -1080,9 +1197,13 @@
"foreignKeys": {
"price_list_rule_value_price_list_rule_id_foreign": {
"constraintName": "price_list_rule_value_price_list_rule_id_foreign",
"columnNames": ["price_list_rule_id"],
"columnNames": [
"price_list_rule_id"
],
"localTableName": "public.price_list_rule_value",
"referencedColumnNames": ["id"],
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.price_list_rule",
"deleteRule": "cascade",
"updateRule": "cascade"

View File

@@ -206,5 +206,29 @@ export class Migration20230929122253 extends Migration {
this.addSql(
'alter table if exists "price_set_rule_type" add constraint "price_set_rule_type_rule_type_id_foreign" foreign key ("rule_type_id") references "rule_type" ("id") on update cascade on delete cascade;'
)
this.addSql(
'alter table if exists "price_set_money_amount" drop constraint if exists "price_set_money_amount_money_amount_id_foreign";'
)
this.addSql(
'alter table if exists "price_set_money_amount" add column if not exists "amount" numeric not null, add column "min_quantity" numeric null, add column "max_quantity" numeric null;'
)
this.addSql(
'alter table if exists "price_set_money_amount" drop constraint if exists "price_set_money_amount_money_amount_id_unique";'
)
this.addSql(
'drop index if exists "IDX_price_set_money_amount_money_amount_id";'
)
this.addSql(
'alter table if exists "price_set_money_amount" rename column "money_amount_id" to "currency_code";'
)
this.addSql(
'CREATE INDEX IF NOT EXISTS "IDX_price_set_money_amount_currency_code" ON "price_set_money_amount" (currency_code) WHERE deleted_at IS NULL;'
)
}
}

View File

@@ -1,4 +1,3 @@
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"

View File

@@ -1,95 +0,0 @@
import { DAL } from "@medusajs/types"
import {
DALUtils,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
Entity,
Filter,
OnInit,
OneToOne,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import { PriceSetMoneyAmount } from "./index"
type OptionalFields = DAL.SoftDeletableEntityDateColumns
const tableName = "money_amount"
const MoneyAmountDeletedAtIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
const MoneyAmountCurrencyCodeIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "currency_code",
where: "deleted_at IS NULL",
})
@Entity({ tableName })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
class MoneyAmount {
[OptionalProps]?: OptionalFields
@PrimaryKey({ columnType: "text" })
id!: string
@MoneyAmountCurrencyCodeIndex.MikroORMIndex()
@Property({ columnType: "text" })
currency_code: string
@Property({
columnType: "numeric",
serializer: Number,
})
amount: number
@Property({ columnType: "numeric", nullable: true })
min_quantity: number | null
@Property({ columnType: "numeric", nullable: true })
max_quantity: number | null
@OneToOne({
entity: () => PriceSetMoneyAmount,
mappedBy: (psma) => psma.money_amount,
onDelete: "cascade",
})
price_set_money_amount: PriceSetMoneyAmount
@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
@MoneyAmountDeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "ma")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "ma")
}
}
export default MoneyAmount

View File

@@ -13,12 +13,10 @@ import {
ManyToOne,
OnInit,
OneToMany,
OneToOne,
OptionalProps,
PrimaryKey,
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"
@@ -50,6 +48,12 @@ const PriceSetMoneyAmountPriceListIdIndex = createPsqlIndexStatementHelper({
where: "deleted_at IS NULL",
})
const PriceSetMoneyAmountCurrencyCodeIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "currency_code",
where: "deleted_at IS NULL",
})
@Entity({ tableName })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class PriceSetMoneyAmount {
@@ -61,6 +65,19 @@ export default class PriceSetMoneyAmount {
@Property({ columnType: "text", nullable: true })
title: string | null = null
@PriceSetMoneyAmountCurrencyCodeIndex.MikroORMIndex()
@Property({ columnType: "text" })
currency_code: string
@Property({ columnType: "numeric", serializer: Number })
amount: number
@Property({ columnType: "numeric", nullable: true })
min_quantity: number | null = null
@Property({ columnType: "numeric", nullable: true })
max_quantity: number | null = null
@PriceSetMoneyAmountPriceSetIdIndex.MikroORMIndex()
@ManyToOne(() => PriceSet, {
columnType: "text",
@@ -73,18 +90,6 @@ export default class PriceSetMoneyAmount {
@ManyToOne(() => PriceSet, { persist: false })
price_set?: PriceSet
@PriceSetMoneyAmountMoneyAmountIdIndex.MikroORMIndex()
@OneToOne(() => MoneyAmount, {
columnType: "text",
mapToPk: true,
fieldName: "money_amount_id",
onDelete: "cascade",
})
money_amount_id: string
@OneToOne(() => MoneyAmount, { persist: false })
money_amount?: MoneyAmount
@Property({ columnType: "integer", default: 0 })
rules_count: number = 0
@@ -103,10 +108,10 @@ export default class PriceSetMoneyAmount {
fieldName: "price_list_id",
onDelete: "cascade",
})
price_list_id: string
price_list_id: string | null = null
@ManyToOne(() => PriceList, { persist: false, nullable: true })
price_list?: PriceList
price_list: PriceList | null = null
@Property({
onCreate: () => new Date(),

View File

@@ -62,8 +62,11 @@ export class PricingRepository
})
.select({
id: "psma1.id",
amount: "psma1.amount",
min_quantity: "psma1.min_quantity",
max_quantity: "psma1.max_quantity",
currency_code: "psma1.currency_code",
price_set_id: "psma1.price_set_id",
money_amount_id: "psma1.money_amount_id",
rules_count: "psma1.rules_count",
price_list_id: "psma1.price_list_id",
pl_rules_count: "pl.rules_count",
@@ -149,12 +152,12 @@ export class PricingRepository
ps: "price_set",
})
.select({
id: "ma.id",
id: "psma.id",
price_set_id: "ps.id",
amount: "ma.amount",
min_quantity: "ma.min_quantity",
max_quantity: "ma.max_quantity",
currency_code: "ma.currency_code",
amount: "psma.amount",
min_quantity: "psma.min_quantity",
max_quantity: "psma.max_quantity",
currency_code: "psma.currency_code",
default_priority: "rt.default_priority",
rules_count: "psma.rules_count",
pl_rules_count: "psma.pl_rules_count",
@@ -162,11 +165,10 @@ export class PricingRepository
price_list_id: "psma.price_list_id",
})
.join(psmaSubQueryKnex.as("psma"), "psma.price_set_id", "ps.id")
.join("money_amount as ma", "ma.id", "psma.money_amount_id")
.leftJoin("price_rule as pr", "pr.price_set_money_amount_id", "psma.id")
.leftJoin("rule_type as rt", "rt.id", "pr.rule_type_id")
.whereIn("ps.id", pricingFilters.id)
.andWhere("ma.currency_code", "=", currencyCode)
.andWhere("psma.currency_code", "=", currencyCode)
.orderBy([
{ column: "psma.has_price_list", order: "asc" },
@@ -176,12 +178,12 @@ export class PricingRepository
])
if (quantity) {
priceSetQueryKnex.where("ma.min_quantity", "<=", quantity)
priceSetQueryKnex.andWhere("ma.max_quantity", ">=", quantity)
priceSetQueryKnex.where("psma.min_quantity", "<=", quantity)
priceSetQueryKnex.andWhere("psma.max_quantity", ">=", quantity)
} else {
priceSetQueryKnex.andWhere(function () {
this.andWhere("ma.min_quantity", "<=", "1").orWhereNull(
"ma.min_quantity"
this.andWhere("psma.min_quantity", "<=", "1").orWhereNull(
"psma.min_quantity"
)
})
}

View File

@@ -22,13 +22,14 @@ export async function run({
logger.info(`Loading seed data from ${path}...`)
const { moneyAmountsData, priceSetsData, priceSetMoneyAmountsData } =
await import(resolve(process.cwd(), path)).catch((e) => {
logger?.error(
`Failed to load seed data from ${path}. Please, provide a relative path and check that you export the following: priceSetsData, moneyAmountsData and priceSetMoneyAmountsData.${EOL}${e}`
)
throw e
})
const { priceSetsData, priceSetMoneyAmountsData } = await import(
resolve(process.cwd(), path)
).catch((e) => {
logger?.error(
`Failed to load seed data from ${path}. Please, provide a relative path and check that you export the following: priceSetsData and priceSetMoneyAmountsData.${EOL}${e}`
)
throw e
})
const dbData = ModulesSdkUtils.loadDatabaseConfig("pricing", options)!
const entities = Object.values(PricingModels) as unknown as EntitySchema[]
@@ -43,9 +44,8 @@ export async function run({
const manager = orm.em.fork()
try {
logger.info("Inserting price_sets & money_amounts")
logger.info("Inserting price_set & price_set_money_amount")
await createMoneyAmounts(manager as any, moneyAmountsData)
await createPriceSets(manager as any, priceSetsData)
await createPriceSetMoneyAmounts(manager as any, priceSetMoneyAmountsData)
} catch (e) {
@@ -57,19 +57,6 @@ export async function run({
await orm.close(true)
}
async function createMoneyAmounts(
manager: SqlEntityManager<PostgreSqlDriver>,
data: RequiredEntityData<PricingModels.MoneyAmount>[]
) {
const moneyAmounts = data.map((moneyAmountData) => {
return manager.create(PricingModels.MoneyAmount, moneyAmountData)
})
await manager.persistAndFlush(moneyAmounts)
return moneyAmounts
}
async function createPriceSets(
manager: SqlEntityManager<PostgreSqlDriver>,
data: RequiredEntityData<PricingModels.PriceSet>[]

View File

@@ -1,7 +1,6 @@
import {
AddPricesDTO,
Context,
CreateMoneyAmountDTO,
CreatePriceListRuleDTO,
DAL,
InternalModuleDeclaration,
@@ -28,7 +27,6 @@ import {
} from "@medusajs/utils"
import {
MoneyAmount,
PriceList,
PriceListRule,
PriceListRuleValue,
@@ -47,7 +45,6 @@ import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
type InjectedDependencies = {
baseRepository: DAL.RepositoryService
pricingRepository: PricingRepositoryService
moneyAmountService: ModulesSdkTypes.InternalModuleService<any>
priceSetService: ModulesSdkTypes.InternalModuleService<any>
ruleTypeService: RuleTypeService<any>
priceRuleService: ModulesSdkTypes.InternalModuleService<any>
@@ -59,7 +56,6 @@ type InjectedDependencies = {
}
const generateMethodForModels = [
MoneyAmount,
PriceList,
PriceListRule,
PriceListRuleValue,
@@ -71,7 +67,6 @@ const generateMethodForModels = [
export default class PricingModuleService<
TPriceSet extends PriceSet = PriceSet,
TMoneyAmount extends MoneyAmount = MoneyAmount,
TRuleType extends RuleType = RuleType,
TPriceRule extends PriceRule = PriceRule,
TPriceSetRuleType extends PriceSetRuleType = PriceSetRuleType,
@@ -84,11 +79,6 @@ export default class PricingModuleService<
InjectedDependencies,
PricingTypes.PriceSetDTO,
{
MoneyAmount: {
dto: PricingTypes.MoneyAmountDTO
create: PricingTypes.CreateMoneyAmountDTO
update: PricingTypes.UpdateMoneyAmountDTO
}
PriceSetMoneyAmount: { dto: PricingTypes.PriceSetMoneyAmountDTO }
PriceRule: {
dto: PricingTypes.PriceRuleDTO
@@ -108,7 +98,6 @@ export default class PricingModuleService<
{
protected baseRepository_: DAL.RepositoryService
protected readonly pricingRepository_: PricingRepositoryService
protected readonly moneyAmountService_: ModulesSdkTypes.InternalModuleService<TMoneyAmount>
protected readonly ruleTypeService_: RuleTypeService<TRuleType>
protected readonly priceSetService_: ModulesSdkTypes.InternalModuleService<TPriceSet>
protected readonly priceRuleService_: ModulesSdkTypes.InternalModuleService<TPriceRule>
@@ -122,7 +111,6 @@ export default class PricingModuleService<
{
baseRepository,
pricingRepository,
moneyAmountService,
ruleTypeService,
priceSetService,
priceRuleService,
@@ -139,7 +127,6 @@ export default class PricingModuleService<
this.baseRepository_ = baseRepository
this.pricingRepository_ = pricingRepository
this.moneyAmountService_ = moneyAmountService
this.ruleTypeService_ = ruleTypeService
this.priceSetService_ = priceSetService
this.ruleTypeService_ = ruleTypeService
@@ -202,7 +189,7 @@ export default class PricingModuleService<
currency_code: calculatedPrice?.currency_code || null,
calculated_price: {
money_amount_id: calculatedPrice?.id || null,
id: calculatedPrice?.id || null,
price_list_id: calculatedPrice?.price_list_id || null,
price_list_type: calculatedPrice?.price_list_type || null,
min_quantity:
@@ -212,7 +199,7 @@ export default class PricingModuleService<
},
original_price: {
money_amount_id: originalPrice?.id || null,
id: originalPrice?.id || null,
price_list_id: originalPrice?.price_list_id || null,
price_list_type: originalPrice?.price_list_type || null,
min_quantity: parseInt(originalPrice?.min_quantity || "") || null,
@@ -247,11 +234,7 @@ export default class PricingModuleService<
const dbPriceSets = await this.list(
{ id: priceSets.filter((p) => !!p).map((p) => p!.id) },
{
relations: [
"rule_types",
"price_set_money_amounts.money_amount",
"price_rules",
],
relations: ["rule_types", "price_set_money_amounts", "price_rules"],
},
sharedContext
)
@@ -333,25 +316,17 @@ export default class PricingModuleService<
)
}
// Money amounts
const moneyAmountData = data.flatMap((item) => item.prices || [])
const createdMoneyAmounts = await this.moneyAmountService_.create(
moneyAmountData,
sharedContext
)
let moneyAmountIndex = 0
const priceSetMoneyAmountData: unknown[] = []
const priceRulesData: unknown[] = []
for (const [index, item] of data.entries()) {
for (const ma of item.prices || []) {
const cleanRules = ma.rules ? removeNullish(ma.rules) : {}
for (const price of item.prices || []) {
const cleanRules = price.rules ? removeNullish(price.rules) : {}
const numberOfRules = Object.entries(cleanRules).length
const priceSetMoneyAmount = {
...price,
price_set_id: createdPriceSets[index].id,
money_amount_id: createdMoneyAmounts[moneyAmountIndex++].id,
title: "test", // TODO: accept title
rules_count: numberOfRules,
}
@@ -359,7 +334,7 @@ export default class PricingModuleService<
for (const [k, v] of Object.entries(cleanRules)) {
priceRulesData.push({
price_set_money_amount: null, // Updated later
price_set_money_amount_id: null, // Updated later
rule_type_id: ruleTypeMap.get(k).id,
price_set_id: createdPriceSets[index].id,
value: v,
@@ -583,31 +558,20 @@ export default class PricingModuleService<
}
})
// Money amounts
const moneyAmountsBulkData = input.flatMap((entry) => entry.prices)
const createdMoneyAmounts = await this.moneyAmountService_.create(
moneyAmountsBulkData as unknown as CreateMoneyAmountDTO[],
sharedContext
)
// Price set money amounts
let maCursor = 0
const priceSetMoneyAmountsBulkData: unknown[] = input.flatMap(
({ priceSetId, prices }) =>
prices.map(() => {
const ma = createdMoneyAmounts[maCursor]
const numberOfRules = Object.entries(
prices[maCursor]?.rules ?? {}
).length
maCursor++
prices.map((price) => {
const numberOfRules = Object.entries(price?.rules ?? {}).length
return {
...price,
price_set_id: priceSetId,
money_amount_id: ma.id,
title: "test", // TODO: accept title
rules_count: numberOfRules,
}
})
)
const createdPriceSetMoneyAmounts =
await this.priceSetMoneyAmountService_.create(
priceSetMoneyAmountsBulkData as ServiceTypes.CreatePriceSetMoneyAmountDTO[],
@@ -807,23 +771,18 @@ export default class PricingModuleService<
const {
price_set_id: priceSetId,
rules: priceRules = {},
...moneyAmountData
...psmaData
} = price
const [moneyAmount] = await this.moneyAmountService_.create(
[moneyAmountData],
sharedContext
)
const [priceSetMoneyAmount] =
await this.priceSetMoneyAmountService_.create(
[
{
price_set_id: priceSetId,
price_list_id: priceList.id,
money_amount_id: moneyAmount.id,
title: "test",
rules_count: Object.keys(priceRules).length,
...psmaData,
},
],
sharedContext
@@ -1020,7 +979,7 @@ export default class PricingModuleService<
): Promise<PricingTypes.PriceListDTO[]> {
const ruleTypeAttributes: string[] = []
const priceListIds: string[] = []
const moneyAmountIds: string[] = []
const psmaIds: string[] = []
const priceSetIds = data
.map((d) => d.prices.map((price) => price.price_set_id))
.flat()
@@ -1029,25 +988,22 @@ export default class PricingModuleService<
priceListIds.push(priceListData.price_list_id)
for (const price of priceListData.prices) {
moneyAmountIds.push(price.id)
psmaIds.push(price.id)
ruleTypeAttributes.push(...Object.keys(price.rules || {}))
}
}
const moneyAmounts = await this.listMoneyAmounts(
{ id: moneyAmountIds },
const psmas = await this.listPriceSetMoneyAmounts(
{ id: psmaIds },
{
take: null,
relations: [
"price_set_money_amount",
"price_set_money_amount.price_rules",
],
relations: ["price_rules"],
},
sharedContext
)
const moneyAmountMap: Map<string, PricingTypes.MoneyAmountDTO> = new Map(
moneyAmounts.map((ma) => [ma.id, ma])
const psmaMap: Map<string, PricingTypes.PriceSetMoneyAmountDTO> = new Map(
psmas.map((psma) => [psma.id, psma])
)
const ruleTypes = await this.listRuleTypes(
@@ -1131,18 +1087,15 @@ export default class PricingModuleService<
for (const price of prices) {
const { rules, price_set_id, ...priceData } = price
const moneyAmount = moneyAmountMap.get(price.id)!
const priceSetMoneyAmount = moneyAmount.price_set_money_amount!
const priceSetMoneyAmount = psmaMap.get(price.id)!
const priceRules = priceSetMoneyAmount.price_rules!
moneyAmountsToUpdate.push(priceData)
if (typeof rules === "undefined") {
continue
}
psmaToUpdate.push({
id: priceSetMoneyAmount!.id,
...priceData,
rules_count: Object.keys(rules).length,
})
@@ -1158,7 +1111,6 @@ export default class PricingModuleService<
}
await Promise.all([
this.moneyAmountService_.update(moneyAmountsToUpdate),
this.priceRuleService_.delete(priceRulesToDelete),
this.priceRuleService_.create(priceRulesToCreate),
this.priceSetMoneyAmountService_.update(psmaToUpdate),
@@ -1289,16 +1241,11 @@ export default class PricingModuleService<
const priceRules = price.rules || {}
const noOfRules = Object.keys(priceRules).length
const [moneyAmount] = await this.moneyAmountService_.create(
[price] as unknown as CreateMoneyAmountDTO[],
sharedContext
)
const [psma] = await this.priceSetMoneyAmountService_.create(
[
{
...price,
price_set_id: price.price_set_id,
money_amount_id: moneyAmount.id,
title: "test",
price_list_id: priceList.id,
rules_count: noOfRules,

View File

@@ -1,4 +1,3 @@
export * from "./money-amount"
export * from "./price-list"
export * from "./price-list-rule"
export * from "./price-list-rule-value"

View File

@@ -1,15 +0,0 @@
export interface CreateMoneyAmountDTO {
id?: string
currency_code: string
amount: number
min_quantity?: number | null
max_quantity?: number | null
}
export interface UpdateMoneyAmountDTO {
id: string
currency_code?: string
amount?: number
min_quantity?: number
max_quantity?: number
}