feat(pricing, types, utils): Exact match based on context + fallback on rule priority if not (#5214)

* initial

* initial service

* update pricing module service

* add integration test for rule-type

* update pricing-module integration tests

* update pricing service interface

* feat(pricing): PriceSets as entry point to pricing module

* chore: add price set money amount

* chore: add price set money amount

* chore: change name of test

* chore: added changeset

* chore: use filterable props from money amount in price sets

* chore: update migrations

* test update integration test

* fix weird behavior

* Update packages/pricing/integration-tests/__fixtures__/rule-type/index.ts

Co-authored-by: Riqwan Thamir <rmthamir@gmail.com>

* Apply suggestions from code review

Co-authored-by: Riqwan Thamir <rmthamir@gmail.com>

* move rule-type to common

* chore: reset migration

* chore: remove incorrect conflicts

* chore: address review

* chore: remove ghost price list

* Apply suggestions from code review

Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>

* update id prefix

* use persist not persistAndflush

* rename key_value to rule_attribute

* more renaming

* feat(types,pricing): add price set money amount rules to pricing module

* chore: cleanup + add test cases for relationship update

* chore: revert package json

* chore: cleanup

* initial

* update pricing module service

* update pricing-module integration tests

* update pricing service interface

* chore: update migrations

* fix weird behavior

* Apply suggestions from code review

Co-authored-by: Riqwan Thamir <rmthamir@gmail.com>

* move rule-type to common

* chore: delete duplicate migration files

* fix(link-modules): Fix link module initialization (#4990)

**What**
Add a new configuration on the relationship to specify if the relation is consumed from an internal service (from medusa core). In that case do not check if the service is part of the loaded modules

* initial price rule

* rebase develop

* save here

* final changes to create

* update price rule integration test

* add module integraiton tests for price rules

* fix merge

* redo wierd order change

* pr cleanup

* pr cleanup

* pr cleanup

* update pr

* sort out migrations

* [wip]

* wip

* chore: temporarily emulate mikroorm internals

* currency code hard filtering

* before creating subqueries

* chore: wip

* chore: wip

* chore: add exact match multiple contexts

* chore: add one more test

* chore: add query that works with exact match

* chore: qb the thingy

* chore: add some comments

* chore: removed extra filter

* chore: added some more comments + prettify

* chore: test with carlos

* chore: add fallbacks and exact match tests

* chore: cleanup

* feat(types,pricing): add price set money amount rules to pricing module (#5065)

* initial

* initial service

* update pricing module service

* add integration test for rule-type

* update pricing-module integration tests

* update pricing service interface

* feat(pricing): PriceSets as entry point to pricing module

* chore: add price set money amount

* chore: add price set money amount

* chore: change name of test

* chore: added changeset

* chore: use filterable props from money amount in price sets

* chore: update migrations

* test update integration test

* fix weird behavior

* Update packages/pricing/integration-tests/__fixtures__/rule-type/index.ts

Co-authored-by: Riqwan Thamir <rmthamir@gmail.com>

* Apply suggestions from code review

Co-authored-by: Riqwan Thamir <rmthamir@gmail.com>

* move rule-type to common

* chore: reset migration

* chore: remove incorrect conflicts

* chore: address review

* chore: remove ghost price list

* Apply suggestions from code review

Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>

* update id prefix

* use persist not persistAndflush

* rename key_value to rule_attribute

* more renaming

* feat(types,pricing): add price set money amount rules to pricing module

* chore: cleanup + add test cases for relationship update

* chore: revert package json

* chore: cleanup

---------

Co-authored-by: Philip Korsholm <philip.korsholm@hotmail.com>
Co-authored-by: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com>
Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>

* chore: minor cleanup

* chore: added money amount scoping

* chore: added review comments

* chore: update changset and undo test scoping

* chore: introduce group by util + no queries on empty context

* Feat/pricing module methods (#5218)

chore: add removePrices to pricing module

* Revert "Feat/pricing module methods (#5218)" (#5236)

This reverts commit 95c8aaa66423d290a35b6e736e5b187e12d44a36.

* chore: review changes

* chore: update schema

* chore: reset migration

---------

Co-authored-by: Philip Korsholm <philip.korsholm@hotmail.com>
Co-authored-by: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com>
Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
Co-authored-by: Adrien de Peretti <adrien.deperetti@gmail.com>
This commit is contained in:
Riqwan Thamir
2023-09-29 13:23:41 +02:00
committed by GitHub
parent dc298acefc
commit 1e7db5a5cb
49 changed files with 2414 additions and 553 deletions

View File

@@ -3,8 +3,8 @@ import * as defaultServices from "@services"
import { LoaderOptions } from "@medusajs/modules-sdk"
import { ModulesSdkTypes } from "@medusajs/types"
import { asClass } from "awilix"
import { loadCustomRepositories } from "@medusajs/utils"
import { asClass } from "awilix"
export default async ({
container,
@@ -22,6 +22,9 @@ export default async ({
moneyAmountService: asClass(defaultServices.MoneyAmountService).singleton(),
priceSetService: asClass(defaultServices.PriceSetService).singleton(),
ruleTypeService: asClass(defaultServices.RuleTypeService).singleton(),
priceSetMoneyAmountRulesService: asClass(
defaultServices.PriceSetMoneyAmountRulesService
).singleton(),
priceRuleService: asClass(defaultServices.PriceRuleService).singleton(),
})
@@ -51,6 +54,9 @@ function loadDefaultRepositories({ container }) {
ruleTypeRepository: asClass(
defaultRepositories.RuleTypeRepository
).singleton(),
priceSetMoneyAmountRulesRepository: asClass(
defaultRepositories.PriceSetMoneyAmountRulesRepository
).singleton(),
priceRuleRepository: asClass(
defaultRepositories.PriceRuleRepository
).singleton(),

View File

@@ -1,150 +0,0 @@
{
"namespaces": [
"public"
],
"name": "public",
"tables": [
{
"columns": {
"code": {
"name": "code",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"symbol": {
"name": "symbol",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"symbol_native": {
"name": "symbol_native",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"name": {
"name": "name",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
}
},
"name": "currency",
"schema": "public",
"indexes": [
{
"keyName": "currency_pkey",
"columnNames": [
"code"
],
"composite": false,
"primary": true,
"unique": true
}
],
"checks": [],
"foreignKeys": {}
},
{
"columns": {
"id": {
"name": "id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"currency_code": {
"name": "currency_code",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "text"
},
"amount": {
"name": "amount",
"type": "numeric",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"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"
}
},
"name": "money_amount",
"schema": "public",
"indexes": [
{
"columnNames": [
"currency_code"
],
"composite": false,
"keyName": "IDX_money_amount_currency_code",
"primary": false,
"unique": false
},
{
"keyName": "money_amount_pkey",
"columnNames": [
"id"
],
"composite": false,
"primary": true,
"unique": true
}
],
"checks": [],
"foreignKeys": {
"money_amount_currency_code_foreign": {
"constraintName": "money_amount_currency_code_foreign",
"columnNames": [
"currency_code"
],
"localTableName": "public.money_amount",
"referencedColumnNames": [
"code"
],
"referencedTableName": "public.currency",
"deleteRule": "set null",
"updateRule": "cascade"
}
}
}
]
}

View File

@@ -211,6 +211,16 @@
"primary": false,
"nullable": false,
"mappedType": "text"
},
"number_rules": {
"name": "number_rules",
"type": "integer",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"default": "0",
"mappedType": "integer"
}
},
"name": "price_set_money_amount",
@@ -338,6 +348,104 @@
"checks": [],
"foreignKeys": {}
},
{
"columns": {
"id": {
"name": "id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"price_set_money_amount_id": {
"name": "price_set_money_amount_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"
},
"value": {
"name": "value",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
}
},
"name": "price_set_money_amount_rules",
"schema": "public",
"indexes": [
{
"columnNames": [
"price_set_money_amount_id"
],
"composite": false,
"keyName": "IDX_price_set_money_amount_rules_price_set_money_amount_id",
"primary": false,
"unique": false
},
{
"columnNames": [
"rule_type_id"
],
"composite": false,
"keyName": "IDX_price_set_money_amount_rules_rule_type_id",
"primary": false,
"unique": false
},
{
"keyName": "price_set_money_amount_rules_pkey",
"columnNames": [
"id"
],
"composite": false,
"primary": true,
"unique": true
}
],
"checks": [],
"foreignKeys": {
"price_set_money_amount_rules_price_set_money_amount_id_foreign": {
"constraintName": "price_set_money_amount_rules_price_set_money_amount_id_foreign",
"columnNames": [
"price_set_money_amount_id"
],
"localTableName": "public.price_set_money_amount_rules",
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.price_set_money_amount",
"updateRule": "cascade"
},
"price_set_money_amount_rules_rule_type_id_foreign": {
"constraintName": "price_set_money_amount_rules_rule_type_id_foreign",
"columnNames": [
"rule_type_id"
],
"localTableName": "public.price_set_money_amount_rules",
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.rule_type",
"updateRule": "cascade"
}
}
},
{
"columns": {
"id": {
@@ -418,14 +526,32 @@
"name": "price_rule",
"schema": "public",
"indexes": [
{
"columnNames": [
"price_set_id"
],
"composite": false,
"keyName": "IDX_price_rule_price_set_id",
"primary": false,
"unique": false
},
{
"columnNames": [
"rule_type_id"
],
"composite": false,
"keyName": "price_rule_rule_type_id_unique",
"keyName": "IDX_price_rule_rule_type_id",
"primary": false,
"unique": true
"unique": false
},
{
"columnNames": [
"price_set_money_amount_id"
],
"composite": false,
"keyName": "IDX_price_rule_price_set_money_amount_id",
"primary": false,
"unique": false
},
{
"keyName": "price_rule_pkey",
@@ -449,6 +575,7 @@
"id"
],
"referencedTableName": "public.price_set",
"deleteRule": "cascade",
"updateRule": "cascade"
},
"price_rule_rule_type_id_foreign": {

View File

@@ -1,23 +0,0 @@
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
}

View File

@@ -1,52 +0,0 @@
import { Migration } from "@mikro-orm/migrations"
export class Migration20230913123118 extends Migration {
async up(): Promise<void> {
this.addSql(
'create table "currency" ("code" text not null, "symbol" text not null, "symbol_native" text not null, "name" text not null, constraint "currency_pkey" primary key ("code"));'
)
this.addSql(
'create table "money_amount" ("id" text not null, "currency_code" text null, "amount" numeric null, "min_quantity" numeric null, "max_quantity" numeric null, constraint "money_amount_pkey" primary key ("id"));'
)
this.addSql(
'create index "IDX_money_amount_currency_code" on "money_amount" ("currency_code");'
)
this.addSql(
'create table "price_set" ("id" text not null, constraint "price_set_pkey" primary key ("id"));'
)
this.addSql(
'create table "price_set_money_amount" ("id" text not null, "title" text not null, "price_set_id" text not null, "money_amount_id" text not null, constraint "price_set_money_amount_pkey" primary key ("id"));'
)
this.addSql(
'create index "IDX_price_set_money_amount_price_set_id" on "price_set_money_amount" ("price_set_id");'
)
this.addSql(
'create index "IDX_price_set_money_amount_money_amount_id" on "price_set_money_amount" ("money_amount_id");'
)
this.addSql(
'create table "rule_type" ("id" text not null, "name" text not null, "rule_attribute" text not null, "default_priority" integer not null default 0, constraint "rule_type_pkey" primary key ("id"));'
)
this.addSql(
'create index "IDX_rule_type_rule_attribute" on "rule_type" ("rule_attribute");'
)
this.addSql(
'alter table "money_amount" add constraint "money_amount_currency_code_foreign" foreign key ("currency_code") references "currency" ("code") on update cascade on delete set null;'
)
this.addSql(
'alter table "price_set_money_amount" add constraint "price_set_money_amount_price_set_id_foreign" foreign key ("price_set_id") references "price_set" ("id") on update cascade on delete cascade;'
)
this.addSql(
'alter table "price_set_money_amount" add constraint "price_set_money_amount_money_amount_id_foreign" foreign key ("money_amount_id") references "money_amount" ("id") on update cascade;'
)
}
}

View File

@@ -0,0 +1,88 @@
import { Migration } from "@mikro-orm/migrations"
export class Migration20230928154931 extends Migration {
async up(): Promise<void> {
this.addSql(
'create table "currency" ("code" text not null, "symbol" text not null, "symbol_native" text not null, "name" text not null, constraint "currency_pkey" primary key ("code"));'
)
this.addSql(
'create table "money_amount" ("id" text not null, "currency_code" text null, "amount" numeric null, "min_quantity" numeric null, "max_quantity" numeric null, constraint "money_amount_pkey" primary key ("id"));'
)
this.addSql(
'create index "IDX_money_amount_currency_code" on "money_amount" ("currency_code");'
)
this.addSql(
'create table "price_set" ("id" text not null, constraint "price_set_pkey" primary key ("id"));'
)
this.addSql(
'create table "price_set_money_amount" ("id" text not null, "title" text not null, "price_set_id" text not null, "money_amount_id" text not null, "number_rules" integer not null default 0, constraint "price_set_money_amount_pkey" primary key ("id"));'
)
this.addSql(
'create index "IDX_price_set_money_amount_price_set_id" on "price_set_money_amount" ("price_set_id");'
)
this.addSql(
'create index "IDX_price_set_money_amount_money_amount_id" on "price_set_money_amount" ("money_amount_id");'
)
this.addSql(
'create table "rule_type" ("id" text not null, "name" text not null, "rule_attribute" text not null, "default_priority" integer not null default 0, constraint "rule_type_pkey" primary key ("id"));'
)
this.addSql(
'create index "IDX_rule_type_rule_attribute" on "rule_type" ("rule_attribute");'
)
this.addSql(
'create table "price_set_money_amount_rules" ("id" text not null, "price_set_money_amount_id" text not null, "rule_type_id" text not null, "value" text not null, constraint "price_set_money_amount_rules_pkey" primary key ("id"));'
)
this.addSql(
'create index "IDX_price_set_money_amount_rules_price_set_money_amount_id" on "price_set_money_amount_rules" ("price_set_money_amount_id");'
)
this.addSql(
'create index "IDX_price_set_money_amount_rules_rule_type_id" on "price_set_money_amount_rules" ("rule_type_id");'
)
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(
'create index "IDX_price_rule_price_set_id" on "price_rule" ("price_set_id");'
)
this.addSql(
'create index "IDX_price_rule_rule_type_id" on "price_rule" ("rule_type_id");'
)
this.addSql(
'create index "IDX_price_rule_price_set_money_amount_id" on "price_rule" ("price_set_money_amount_id");'
)
this.addSql(
'alter table "money_amount" add constraint "money_amount_currency_code_foreign" foreign key ("currency_code") references "currency" ("code") on update cascade on delete set null;'
)
this.addSql(
'alter table "price_set_money_amount" add constraint "price_set_money_amount_price_set_id_foreign" foreign key ("price_set_id") references "price_set" ("id") on update cascade on delete cascade;'
)
this.addSql(
'alter table "price_set_money_amount" add constraint "price_set_money_amount_money_amount_id_foreign" foreign key ("money_amount_id") references "money_amount" ("id") on update cascade;'
)
this.addSql(
'alter table "price_set_money_amount_rules" add constraint "price_set_money_amount_rules_price_set_money_amount_id_foreign" foreign key ("price_set_money_amount_id") references "price_set_money_amount" ("id") on update cascade;'
)
this.addSql(
'alter table "price_set_money_amount_rules" add constraint "price_set_money_amount_rules_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_id_foreign" foreign key ("price_set_id") references "price_set" ("id") on update cascade on delete 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;'
)
}
}

View File

@@ -1,6 +1,7 @@
export { default as Currency } from "./currency"
export { default as MoneyAmount } from "./money-amount"
export { default as PriceRule } from "./price-rule"
export { default as PriceSet } from "./price-set"
export { default as PriceSetMoneyAmount } from "./price-set-money-amount"
export { default as PriceSetMoneyAmountRules } from "./price-set-money-amount-rules"
export { default as RuleType } from "./rule-type"
export { default as PriceRule } from "./price-rule"

View File

@@ -1,21 +1,16 @@
import {
BeforeCreate,
Collection,
Entity,
ManyToMany,
ManyToOne,
OneToMany,
OneToOne,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import MoneyAmount from "./money-amount"
import { generateEntityId } from "@medusajs/utils"
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"
@@ -31,6 +26,8 @@ export default class PriceRule {
entity: () => PriceSet,
fieldName: "price_set_id",
name: "price_rule_price_set_id_unique",
onDelete: "cascade",
index: "IDX_price_rule_price_set_id",
})
price_set: PriceSet
@@ -38,6 +35,7 @@ export default class PriceRule {
entity: () => RuleType,
fieldName: "rule_type_id",
name: "price_rule_rule_type_id_unique",
index: "IDX_price_rule_rule_type_id",
})
rule_type: RuleType
@@ -54,6 +52,7 @@ export default class PriceRule {
entity: () => PriceSetMoneyAmount,
fieldName: "price_set_money_amount_id",
name: "price_set_money_amount_id_unique",
index: "IDX_price_rule_price_set_money_amount_id",
})
price_set_money_amount: PriceSetMoneyAmount

View File

@@ -0,0 +1,35 @@
import { generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Entity,
ManyToOne,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import PriceSetMoneyAmount from "./price-set-money-amount"
import RuleType from "./rule-type"
@Entity()
export default class PriceSetMoneyAmountRules {
@PrimaryKey({ columnType: "text" })
id!: string
@ManyToOne(() => PriceSetMoneyAmount, {
index: "IDX_price_set_money_amount_rules_price_set_money_amount_id",
})
price_set_money_amount?: PriceSetMoneyAmount | string
@ManyToOne(() => RuleType, {
index: "IDX_price_set_money_amount_rules_rule_type_id",
})
rule_type?: RuleType | string
@Property({ columnType: "text" })
value: string
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "psmar")
}
}

View File

@@ -1,15 +1,19 @@
import { generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Collection,
Entity,
ManyToOne,
OneToMany,
PrimaryKey,
PrimaryKeyType,
Property,
} from "@mikro-orm/core"
import MoneyAmount from "./money-amount"
import PriceRule from "./price-rule"
import PriceSet from "./price-set"
import PriceSetMoneyAmountRules from "./price-set-money-amount-rules"
@Entity()
export default class PriceSetMoneyAmount {
@@ -30,6 +34,21 @@ export default class PriceSetMoneyAmount {
})
money_amount?: MoneyAmount
@Property({ columnType: "integer", default: 0 })
number_rules?: number
@OneToMany({
entity: () => PriceRule,
mappedBy: (pr) => pr.price_set_money_amount,
})
price_rules = new Collection<PriceRule>(this)
@OneToMany({
entity: () => PriceSetMoneyAmountRules,
mappedBy: (psmar) => psmar.price_set_money_amount,
})
price_set_money_amount_rules = new Collection<PriceSetMoneyAmountRules>(this)
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "psma")

View File

@@ -1,20 +1,36 @@
import { generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
Collection,
Entity,
ManyToMany,
OneToMany,
OptionalProps,
PrimaryKey,
} from "@mikro-orm/core"
import MoneyAmount from "./money-amount"
import PriceRule from "./price-rule"
import PriceSetMoneyAmount from "./price-set-money-amount"
@Entity()
export default class PriceSet {
[OptionalProps]?: "price_set_money_amounts"
@PrimaryKey({ columnType: "text" })
id!: string
@OneToMany(() => PriceSetMoneyAmount, (psma) => psma.price_set, {
cascade: [Cascade.REMOVE],
})
price_set_money_amounts = new Collection<PriceSetMoneyAmount>(this)
@OneToMany(() => PriceRule, (pr) => pr.price_set, {
cascade: [Cascade.REMOVE],
})
price_rules = new Collection<PriceRule>(this)
@ManyToMany({
entity: () => MoneyAmount,
pivotEntity: () => PriceSetMoneyAmount,

View File

@@ -1,6 +1,7 @@
export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/utils"
export { CurrencyRepository } from "./currency"
export { MoneyAmountRepository } from "./money-amount"
export { PriceSetRepository } from "./price-set"
export { RuleTypeRepository } from "./rule-type"
export { PriceRuleRepository } from "./price-rule"
export { PriceSetRepository } from "./price-set"
export { PriceSetMoneyAmountRulesRepository } from "./price-set-money-amount-rules"
export { RuleTypeRepository } from "./rule-type"

View File

@@ -10,7 +10,7 @@ import {
FilterQuery as MikroFilterQuery,
FindOptions as MikroOptions,
} from "@mikro-orm/core"
import { PriceRule, PriceSet, PriceSetMoneyAmount, RuleType } from "@models"
import { PriceRule } from "@models"
import { SqlEntityManager } from "@mikro-orm/postgresql"
@@ -76,20 +76,19 @@ export class PriceRuleRepository extends DALUtils.MikroOrmBaseRepository {
const manager: SqlEntityManager =
this.getActiveManager<SqlEntityManager>(context)
const toCreate = await Promise.all(data.map(async (ruleData) => {
const toCreate = data.map((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
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)

View File

@@ -0,0 +1,127 @@
import {
Context,
CreatePriceSetMoneyAmountRulesDTO,
DAL,
UpdatePriceSetMoneyAmountRulesDTO,
} 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 { PriceSetMoneyAmountRules } from "@models"
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<PriceSetMoneyAmountRules> = { where: {} },
context: Context = {}
): Promise<PriceSetMoneyAmountRules[]> {
const manager = this.getActiveManager<SqlEntityManager>(context)
const findOptions_ = { ...findOptions }
findOptions_.options ??= {}
Object.assign(findOptions_.options, {
strategy: LoadStrategy.SELECT_IN,
})
return await manager.find(
PriceSetMoneyAmountRules,
findOptions_.where as MikroFilterQuery<PriceSetMoneyAmountRules>,
findOptions_.options as MikroOptions<PriceSetMoneyAmountRules>
)
}
async findAndCount(
findOptions: DAL.FindOptions<PriceSetMoneyAmountRules> = { where: {} },
context: Context = {}
): Promise<[PriceSetMoneyAmountRules[], number]> {
const manager = this.getActiveManager<SqlEntityManager>(context)
const findOptions_ = { ...findOptions }
findOptions_.options ??= {}
Object.assign(findOptions_.options, {
strategy: LoadStrategy.SELECT_IN,
})
return await manager.findAndCount(
PriceSetMoneyAmountRules,
findOptions_.where as MikroFilterQuery<PriceSetMoneyAmountRules>,
findOptions_.options as MikroOptions<PriceSetMoneyAmountRules>
)
}
async delete(ids: string[], context: Context = {}): Promise<void> {
const manager = this.getActiveManager<SqlEntityManager>(context)
await manager.nativeDelete(PriceSetMoneyAmountRules, { id: { $in: ids } })
}
async create(
data: CreatePriceSetMoneyAmountRulesDTO[],
context: Context = {}
): Promise<PriceSetMoneyAmountRules[]> {
const manager = this.getActiveManager<SqlEntityManager>(context)
const psmar = data.map((psmarData) => {
return manager.create(PriceSetMoneyAmountRules, psmarData)
})
manager.persistAndFlush(psmar)
return psmar
}
async update(
data: UpdatePriceSetMoneyAmountRulesDTO[],
context: Context = {}
): Promise<PriceSetMoneyAmountRules[]> {
const manager = this.getActiveManager<SqlEntityManager>(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)
})
manager.persist(psmar)
return psmar
}
}

View File

@@ -1,6 +1,7 @@
export { default as CurrencyService } from "./currency"
export { default as MoneyAmountService } from "./money-amount"
export { default as PriceRuleService } from "./price-rule"
export { default as PriceSetService } from "./price-set"
export { default as PriceSetMoneyAmountRulesService } from "./price-set-money-amount-rules"
export { default as PricingModuleService } from "./pricing-module"
export { default as RuleTypeService } from "./rule-type"
export { default as PriceRuleService } from "./price-rule"

View File

@@ -45,8 +45,13 @@ export default class MoneyAmountService<
config: FindConfig<PricingTypes.MoneyAmountDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity[]> {
const queryOptions = ModulesSdkUtils.buildQuery<MoneyAmount>(
filters,
config
)
return (await this.moneyAmountRepository_.find(
this.buildQueryForList(filters, config),
queryOptions,
sharedContext
)) as TEntity[]
}
@@ -57,26 +62,15 @@ export default class MoneyAmountService<
config: FindConfig<PricingTypes.MoneyAmountDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<[TEntity[], number]> {
return (await this.moneyAmountRepository_.findAndCount(
this.buildQueryForList(filters, config),
sharedContext
)) as [TEntity[], number]
}
private buildQueryForList(
filters: PricingTypes.FilterableMoneyAmountProps = {},
config: FindConfig<PricingTypes.MoneyAmountDTO> = {}
) {
const queryOptions = ModulesSdkUtils.buildQuery<MoneyAmount>(
filters,
config
)
if (filters.id) {
queryOptions.where["id"] = { $in: filters.id }
}
return queryOptions
return (await this.moneyAmountRepository_.findAndCount(
queryOptions,
sharedContext
)) as [TEntity[], number]
}
@InjectTransactionManager(shouldForceTransaction, "moneyAmountRepository_")

View File

@@ -58,7 +58,7 @@ export default class PriceRuleService<TEntity extends PriceRule = PriceRule> {
@MedusaContext() sharedContext: Context = {}
): Promise<[TEntity[], number]> {
const queryConfig = ModulesSdkUtils.buildQuery<PriceRule>(filters, config)
return (await this.priceRuleRepository_.findAndCount(
queryConfig,
sharedContext

View File

@@ -0,0 +1,120 @@
import { Context, DAL, FindConfig, PricingTypes } from "@medusajs/types"
import {
InjectManager,
InjectTransactionManager,
MedusaContext,
ModulesSdkUtils,
retrieveEntity,
} from "@medusajs/utils"
import { PriceSetMoneyAmountRules } from "@models"
import { doNotForceTransaction, shouldForceTransaction } from "@medusajs/utils"
type InjectedDependencies = {
priceSetMoneyAmountRulesRepository: DAL.RepositoryService
}
export default class PriceSetMoneyAmountRulesService<
TEntity extends PriceSetMoneyAmountRules = PriceSetMoneyAmountRules
> {
protected readonly priceSetMoneyAmountRulesRepository_: DAL.RepositoryService
constructor({ priceSetMoneyAmountRulesRepository }: InjectedDependencies) {
this.priceSetMoneyAmountRulesRepository_ =
priceSetMoneyAmountRulesRepository
}
@InjectManager("priceSetMoneyAmountRulesRepository_")
async retrieve(
priceSetMoneyAmountRulesId: string,
config: FindConfig<PricingTypes.PriceSetMoneyAmountRulesDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity> {
return (await retrieveEntity<
PriceSetMoneyAmountRules,
PricingTypes.PriceSetMoneyAmountRulesDTO
>({
id: priceSetMoneyAmountRulesId,
identifierColumn: "id",
entityName: PriceSetMoneyAmountRules.name,
repository: this.priceSetMoneyAmountRulesRepository_,
config,
sharedContext,
})) as TEntity
}
@InjectManager("priceSetMoneyAmountRulesRepository_")
async list(
filters: PricingTypes.FilterablePriceSetMoneyAmountRulesProps = {},
config: FindConfig<PricingTypes.PriceSetMoneyAmountRulesDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity[]> {
return (await this.priceSetMoneyAmountRulesRepository_.find(
this.buildQueryForList(filters, config),
sharedContext
)) as TEntity[]
}
@InjectManager("priceSetMoneyAmountRulesRepository_")
async listAndCount(
filters: PricingTypes.FilterablePriceSetMoneyAmountRulesProps = {},
config: FindConfig<PricingTypes.PriceSetMoneyAmountRulesDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<[TEntity[], number]> {
return (await this.priceSetMoneyAmountRulesRepository_.findAndCount(
this.buildQueryForList(filters, config),
sharedContext
)) as [TEntity[], number]
}
private buildQueryForList(
filters: PricingTypes.FilterablePriceSetMoneyAmountRulesProps = {},
config: FindConfig<PricingTypes.PriceSetMoneyAmountRulesDTO> = {}
) {
const queryOptions = ModulesSdkUtils.buildQuery<PriceSetMoneyAmountRules>(
filters,
config
)
return queryOptions
}
@InjectTransactionManager(
shouldForceTransaction,
"priceSetMoneyAmountRulesRepository_"
)
async create(
data: PricingTypes.CreatePriceSetMoneyAmountRulesDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity[]> {
return (await this.priceSetMoneyAmountRulesRepository_.create(
data,
sharedContext
)) as TEntity[]
}
@InjectTransactionManager(
shouldForceTransaction,
"priceSetMoneyAmountRulesRepository_"
)
async update(
data: PricingTypes.UpdatePriceSetMoneyAmountRulesDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity[]> {
return (await this.priceSetMoneyAmountRulesRepository_.update(
data,
sharedContext
)) as TEntity[]
}
@InjectTransactionManager(
doNotForceTransaction,
"priceSetMoneyAmountRulesRepository_"
)
async delete(
ids: string[],
@MedusaContext() sharedContext: Context = {}
): Promise<void> {
await this.priceSetMoneyAmountRulesRepository_.delete(ids, sharedContext)
}
}

View File

@@ -43,8 +43,10 @@ export default class PriceSetService<TEntity extends PriceSet = PriceSet> {
config: FindConfig<PricingTypes.PriceSetDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity[]> {
const queryOptions = ModulesSdkUtils.buildQuery<PriceSet>(filters, config)
return (await this.priceSetRepository_.find(
this.buildQueryForList(filters, config),
queryOptions,
sharedContext
)) as TEntity[]
}
@@ -55,23 +57,12 @@ export default class PriceSetService<TEntity extends PriceSet = PriceSet> {
config: FindConfig<PricingTypes.PriceSetDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<[TEntity[], number]> {
return (await this.priceSetRepository_.findAndCount(
this.buildQueryForList(filters, config),
sharedContext
)) as [TEntity[], number]
}
private buildQueryForList(
filters: PricingTypes.FilterablePriceSetProps = {},
config: FindConfig<PricingTypes.PriceSetDTO> = {}
) {
const queryOptions = ModulesSdkUtils.buildQuery<PriceSet>(filters, config)
if (filters.id) {
queryOptions.where.id = { $in: filters.id }
}
return queryOptions
return (await this.priceSetRepository_.findAndCount(
queryOptions,
sharedContext
)) as [TEntity[], number]
}
@InjectTransactionManager(shouldForceTransaction, "priceSetRepository_")

View File

@@ -8,19 +8,30 @@ import {
PricingFilters,
PricingTypes,
} from "@medusajs/types"
import {
Currency,
MoneyAmount,
PriceRule,
PriceSet,
PriceSetMoneyAmountRules,
RuleType,
} from "@models"
import {
CurrencyService,
MoneyAmountService,
PriceRuleService,
PriceSetMoneyAmountRulesService,
PriceSetService,
RuleTypeService,
PriceRuleService
} from "@services"
import { Currency, MoneyAmount, PriceRule, PriceSet, RuleType } from "@models"
import { EntityManager } from "@mikro-orm/postgresql"
import {
InjectManager,
InjectTransactionManager,
MedusaContext,
groupBy,
shouldForceTransaction,
} from "@medusajs/utils"
@@ -31,6 +42,7 @@ type InjectedDependencies = {
currencyService: CurrencyService<any>
moneyAmountService: MoneyAmountService<any>
priceSetService: PriceSetService<any>
priceSetMoneyAmountRulesService: PriceSetMoneyAmountRulesService<any>
ruleTypeService: RuleTypeService<any>
priceRuleService: PriceRuleService<any>
}
@@ -40,7 +52,8 @@ export default class PricingModuleService<
TMoneyAmount extends MoneyAmount = MoneyAmount,
TCurrency extends Currency = Currency,
TRuleType extends RuleType = RuleType,
TPriceRule extends PriceRule = PriceRule,
TPriceSetMoneyAmountRules extends PriceSetMoneyAmountRules = PriceSetMoneyAmountRules,
TPriceRule extends PriceRule = PriceRule
> implements PricingTypes.IPricingModuleService
{
protected baseRepository_: DAL.RepositoryService
@@ -48,6 +61,7 @@ export default class PricingModuleService<
protected readonly moneyAmountService_: MoneyAmountService<TMoneyAmount>
protected readonly ruleTypeService_: RuleTypeService<TRuleType>
protected readonly priceSetService_: PriceSetService<TPriceSet>
protected readonly priceSetMoneyAmountRulesService_: PriceSetMoneyAmountRulesService<TPriceSetMoneyAmountRules>
protected readonly priceRuleService_: PriceRuleService<TPriceRule>
constructor(
@@ -57,6 +71,7 @@ export default class PricingModuleService<
currencyService,
ruleTypeService,
priceSetService,
priceSetMoneyAmountRulesService,
priceRuleService,
}: InjectedDependencies,
protected readonly moduleDeclaration: InternalModuleDeclaration
@@ -66,6 +81,8 @@ export default class PricingModuleService<
this.moneyAmountService_ = moneyAmountService
this.ruleTypeService_ = ruleTypeService
this.priceSetService_ = priceSetService
this.priceSetMoneyAmountRulesService_ = priceSetMoneyAmountRulesService
this.ruleTypeService_ = ruleTypeService
this.priceRuleService_ = priceRuleService
}
@@ -79,46 +96,91 @@ export default class PricingModuleService<
pricingContext: PricingContext = { context: {} },
@MedusaContext() sharedContext: Context = {}
): Promise<PricingTypes.CalculatedPriceSetDTO> {
const manager = sharedContext.manager as EntityManager
const knex = manager.getKnex()
// Keeping this whole logic raw in here for now as they will undergo
// some changes, will abstract them out once we have a final version
const context = pricingContext.context || {}
const priceSetFilters: PricingTypes.FilterablePriceSetProps = {
id: pricingFilters.id,
// Quantity is used to scope money amounts based on min_quantity and max_quantity.
// We should potentially think of reserved words in pricingContext that can't be used in rules
// or have a separate pricing options that accept things like quantity, price_list_id and other
// pricing module features
const quantity = context.quantity
delete context.quantity
// Gets all the price set money amounts where rules match for each of the contexts
// that the price set is configured for
const psmaSubQueryKnex = knex({
psma: "price_set_money_amount",
})
.select({
id: "psma.id",
price_set_id: "psma.price_set_id",
money_amount_id: "psma.money_amount_id",
number_rules: "psma.number_rules",
})
.join("price_rule as pr", "pr.price_set_money_amount_id", "psma.id")
.join("rule_type as rt", "rt.id", "pr.rule_type_id")
.orderBy("number_rules", "desc")
for (const [key, value] of Object.entries(context)) {
psmaSubQueryKnex.orWhere({
"rt.rule_attribute": key,
"pr.value": value,
})
}
const priceSets = await this.list(
priceSetFilters,
{
select: [
"id",
"money_amounts.id",
"money_amounts.currency_code",
"money_amounts.amount",
"money_amounts.min_quantity",
"money_amounts.max_quantity",
],
relations: ["money_amounts"],
},
sharedContext
)
psmaSubQueryKnex
.groupBy("psma.id")
.having(knex.raw("count(DISTINCT rt.rule_attribute) = psma.number_rules"))
const calculatedPrices = priceSets.map(
(priceSet): PricingTypes.CalculatedPriceSetDTO => {
// TODO: This will change with the rules engine selection,
// making a DB query directly instead
// This should look for a default price when no rules apply
// When no price is set, return null values for all cases
const selectedMoneyAmount = priceSet.money_amounts?.find(
(ma) =>
context.currency_code && ma.currency_code === context.currency_code
)
const priceSetQueryKnex = knex({
ps: "price_set",
})
.select({
id: "ps.id",
amount: "ma.amount",
min_quantity: "ma.min_quantity",
max_quantity: "ma.max_quantity",
currency_code: "ma.currency_code",
default_priority: "rt.default_priority",
number_rules: "psma.number_rules",
})
.join(psmaSubQueryKnex.as("psma"), "psma.price_set_id", "ps.id")
.join("money_amount as ma", "ma.id", "psma.money_amount_id")
.join("price_rule as pr", "pr.price_set_money_amount_id", "psma.id")
.join("rule_type as rt", "rt.id", "pr.rule_type_id")
.whereIn("ps.id", pricingFilters.id)
.orderBy([
{ column: "number_rules", order: "desc" },
{ column: "default_priority", order: "desc" },
])
if (quantity) {
priceSetQueryKnex.where("ma.min_quantity", "<=", quantity)
priceSetQueryKnex.andWhere("ma.max_quantity", ">=", quantity)
}
const isContextPresent = Object.entries(context).length
// Only if the context is present do we need to query the database.
const queryBuilderResults = isContextPresent ? await priceSetQueryKnex : []
const pricesSetPricesMap = groupBy(queryBuilderResults, "id")
const calculatedPrices = pricingFilters.id.map(
(priceSetId: string): PricingTypes.CalculatedPriceSetDTO => {
// This is where we select prices, for now we just do a first match based on the database results
// which is prioritized by number_rules first for exact match and then deafult_priority of the rule_type
// inject custom price selection here
const price = pricesSetPricesMap.get(priceSetId)?.[0]
return {
id: priceSet.id,
amount: selectedMoneyAmount?.amount || null,
currency_code: selectedMoneyAmount?.currency_code || null,
min_quantity: selectedMoneyAmount?.min_quantity || null,
max_quantity: selectedMoneyAmount?.max_quantity || null,
id: priceSetId,
amount: price?.amount || null,
currency_code: price?.currency_code || null,
min_quantity: price?.min_quantity || null,
max_quantity: price?.max_quantity || null,
}
}
)
@@ -527,6 +589,110 @@ export default class PricingModuleService<
await this.ruleTypeService_.delete(ruleTypes, sharedContext)
}
@InjectManager("baseRepository_")
async retrievePriceSetMoneyAmountRules(
id: string,
config: FindConfig<PricingTypes.PriceSetMoneyAmountRulesDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<PricingTypes.PriceSetMoneyAmountRulesDTO> {
const record = await this.priceSetMoneyAmountRulesService_.retrieve(
id,
config,
sharedContext
)
return this.baseRepository_.serialize<PricingTypes.PriceSetMoneyAmountRulesDTO>(
record,
{
populate: true,
}
)
}
@InjectManager("baseRepository_")
async listPriceSetMoneyAmountRules(
filters: PricingTypes.FilterablePriceSetMoneyAmountRulesProps = {},
config: FindConfig<PricingTypes.PriceSetMoneyAmountRulesDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<PricingTypes.PriceSetMoneyAmountRulesDTO[]> {
const records = await this.priceSetMoneyAmountRulesService_.list(
filters,
config,
sharedContext
)
return this.baseRepository_.serialize<
PricingTypes.PriceSetMoneyAmountRulesDTO[]
>(records, {
populate: true,
})
}
@InjectManager("baseRepository_")
async listAndCountPriceSetMoneyAmountRules(
filters: PricingTypes.FilterablePriceSetMoneyAmountRulesProps = {},
config: FindConfig<PricingTypes.PriceSetMoneyAmountRulesDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<[PricingTypes.PriceSetMoneyAmountRulesDTO[], number]> {
const [records, count] =
await this.priceSetMoneyAmountRulesService_.listAndCount(
filters,
config,
sharedContext
)
return [
await this.baseRepository_.serialize<
PricingTypes.PriceSetMoneyAmountRulesDTO[]
>(records, {
populate: true,
}),
count,
]
}
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
async createPriceSetMoneyAmountRules(
data: PricingTypes.CreatePriceSetMoneyAmountRulesDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<PricingTypes.PriceSetMoneyAmountRulesDTO[]> {
const records = await this.priceSetMoneyAmountRulesService_.create(
data,
sharedContext
)
return this.baseRepository_.serialize<
PricingTypes.PriceSetMoneyAmountRulesDTO[]
>(records, {
populate: true,
})
}
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
async updatePriceSetMoneyAmountRules(
data: PricingTypes.UpdatePriceSetMoneyAmountRulesDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<PricingTypes.PriceSetMoneyAmountRulesDTO[]> {
const records = await this.priceSetMoneyAmountRulesService_.update(
data,
sharedContext
)
return this.baseRepository_.serialize<
PricingTypes.PriceSetMoneyAmountRulesDTO[]
>(records, {
populate: true,
})
}
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
async deletePriceSetMoneyAmountRules(
ids: string[],
@MedusaContext() sharedContext: Context = {}
): Promise<void> {
await this.priceSetMoneyAmountRulesService_.delete(ids, sharedContext)
}
@InjectManager("baseRepository_")
async retrievePriceRule(
id: string,
@@ -539,9 +705,12 @@ export default class PricingModuleService<
sharedContext
)
return this.baseRepository_.serialize<PricingTypes.PriceRuleDTO>(priceRule, {
populate: true,
})
return this.baseRepository_.serialize<PricingTypes.PriceRuleDTO>(
priceRule,
{
populate: true,
}
)
}
@InjectManager("baseRepository_")

View File

@@ -43,8 +43,10 @@ export default class RuleTypeService<TEntity extends RuleType = RuleType> {
config: FindConfig<PricingTypes.RuleTypeDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity[]> {
const queryOptions = ModulesSdkUtils.buildQuery<RuleType>(filters, config)
return (await this.ruleTypeRepository_.find(
this.buildQueryForList(filters, config),
queryOptions,
sharedContext
)) as TEntity[]
}
@@ -55,23 +57,12 @@ export default class RuleTypeService<TEntity extends RuleType = RuleType> {
config: FindConfig<PricingTypes.RuleTypeDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<[TEntity[], number]> {
return (await this.ruleTypeRepository_.findAndCount(
this.buildQueryForList(filters, config),
sharedContext
)) as [TEntity[], number]
}
private buildQueryForList(
filters: PricingTypes.FilterableRuleTypeProps = {},
config: FindConfig<PricingTypes.RuleTypeDTO> = {}
) {
const queryOptions = ModulesSdkUtils.buildQuery<RuleType>(filters, config)
if (filters.id) {
queryOptions.where["id"] = { $in: filters.id }
}
return queryOptions
return (await this.ruleTypeRepository_.findAndCount(
queryOptions,
sharedContext
)) as [TEntity[], number]
}
@InjectTransactionManager(shouldForceTransaction, "ruleTypeRepository_")