@medusajs/pricing 0.1.2 → 0.1.3-next-20231109084604
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/repositories/pricing.js +1 -1
- package/dist/scripts/bin/run-migration-down.js +0 -0
- package/dist/scripts/bin/run-migration-up.js +0 -0
- package/dist/scripts/bin/run-seed.js +0 -0
- package/dist/scripts/migration-up.js +1 -1
- package/dist/services/currency.js +3 -4
- package/dist/services/money-amount.js +3 -4
- package/dist/services/price-rule.js +3 -3
- package/dist/services/price-set-money-amount-rules.js +3 -4
- package/dist/services/price-set-money-amount.js +7 -5
- package/dist/services/price-set-rule-type.js +3 -3
- package/dist/services/price-set.js +3 -3
- package/dist/services/pricing-module.d.ts +3 -4
- package/dist/services/pricing-module.js +113 -88
- package/dist/services/rule-type.js +3 -4
- package/package.json +4 -4
|
@@ -10,7 +10,7 @@ class PricingRepository extends utils_1.MikroOrmBase {
|
|
|
10
10
|
this.manager_ = manager;
|
|
11
11
|
}
|
|
12
12
|
async calculatePrices(pricingFilters, pricingContext = { context: {} }, sharedContext = {}) {
|
|
13
|
-
const manager = this.getActiveManager();
|
|
13
|
+
const manager = this.getActiveManager(sharedContext);
|
|
14
14
|
const knex = manager.getKnex();
|
|
15
15
|
const context = pricingContext.context || {};
|
|
16
16
|
// Quantity is used to scope money amounts based on min_quantity and max_quantity.
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -42,7 +42,7 @@ async function runMigrations({ options, logger, } = {}) {
|
|
|
42
42
|
try {
|
|
43
43
|
const migrator = orm.getMigrator();
|
|
44
44
|
const pendingMigrations = await migrator.getPendingMigrations();
|
|
45
|
-
logger.info(`Running pending migrations: ${pendingMigrations}`);
|
|
45
|
+
logger.info(`Running pending migrations: ${JSON.stringify(pendingMigrations, null, 2)}`);
|
|
46
46
|
await migrator.up({
|
|
47
47
|
migrations: pendingMigrations.map((m) => m.name),
|
|
48
48
|
});
|
|
@@ -15,7 +15,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
15
|
const types_1 = require("@medusajs/types");
|
|
16
16
|
const utils_1 = require("@medusajs/utils");
|
|
17
17
|
const _models_1 = require("../models");
|
|
18
|
-
const utils_2 = require("@medusajs/utils");
|
|
19
18
|
class CurrencyService {
|
|
20
19
|
constructor({ currencyRepository }) {
|
|
21
20
|
this.currencyRepository_ = currencyRepository;
|
|
@@ -76,21 +75,21 @@ __decorate([
|
|
|
76
75
|
__metadata("design:returntype", Promise)
|
|
77
76
|
], CurrencyService.prototype, "listAndCount", null);
|
|
78
77
|
__decorate([
|
|
79
|
-
(0, utils_1.InjectTransactionManager)(
|
|
78
|
+
(0, utils_1.InjectTransactionManager)("currencyRepository_"),
|
|
80
79
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
81
80
|
__metadata("design:type", Function),
|
|
82
81
|
__metadata("design:paramtypes", [Array, Object]),
|
|
83
82
|
__metadata("design:returntype", Promise)
|
|
84
83
|
], CurrencyService.prototype, "create", null);
|
|
85
84
|
__decorate([
|
|
86
|
-
(0, utils_1.InjectTransactionManager)(
|
|
85
|
+
(0, utils_1.InjectTransactionManager)("currencyRepository_"),
|
|
87
86
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
88
87
|
__metadata("design:type", Function),
|
|
89
88
|
__metadata("design:paramtypes", [Array, Object]),
|
|
90
89
|
__metadata("design:returntype", Promise)
|
|
91
90
|
], CurrencyService.prototype, "update", null);
|
|
92
91
|
__decorate([
|
|
93
|
-
(0, utils_1.InjectTransactionManager)(
|
|
92
|
+
(0, utils_1.InjectTransactionManager)("currencyRepository_"),
|
|
94
93
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
95
94
|
__metadata("design:type", Function),
|
|
96
95
|
__metadata("design:paramtypes", [Array, Object]),
|
|
@@ -15,7 +15,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
15
|
const types_1 = require("@medusajs/types");
|
|
16
16
|
const utils_1 = require("@medusajs/utils");
|
|
17
17
|
const _models_1 = require("../models");
|
|
18
|
-
const utils_2 = require("@medusajs/utils");
|
|
19
18
|
class MoneyAmountService {
|
|
20
19
|
constructor({ moneyAmountRepository }) {
|
|
21
20
|
this.moneyAmountRepository_ = moneyAmountRepository;
|
|
@@ -70,21 +69,21 @@ __decorate([
|
|
|
70
69
|
__metadata("design:returntype", Promise)
|
|
71
70
|
], MoneyAmountService.prototype, "listAndCount", null);
|
|
72
71
|
__decorate([
|
|
73
|
-
(0, utils_1.InjectTransactionManager)(
|
|
72
|
+
(0, utils_1.InjectTransactionManager)("moneyAmountRepository_"),
|
|
74
73
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
75
74
|
__metadata("design:type", Function),
|
|
76
75
|
__metadata("design:paramtypes", [Array, Object]),
|
|
77
76
|
__metadata("design:returntype", Promise)
|
|
78
77
|
], MoneyAmountService.prototype, "create", null);
|
|
79
78
|
__decorate([
|
|
80
|
-
(0, utils_1.InjectTransactionManager)(
|
|
79
|
+
(0, utils_1.InjectTransactionManager)("moneyAmountRepository_"),
|
|
81
80
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
82
81
|
__metadata("design:type", Function),
|
|
83
82
|
__metadata("design:paramtypes", [Array, Object]),
|
|
84
83
|
__metadata("design:returntype", Promise)
|
|
85
84
|
], MoneyAmountService.prototype, "update", null);
|
|
86
85
|
__decorate([
|
|
87
|
-
(0, utils_1.InjectTransactionManager)(
|
|
86
|
+
(0, utils_1.InjectTransactionManager)("moneyAmountRepository_"),
|
|
88
87
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
89
88
|
__metadata("design:type", Function),
|
|
90
89
|
__metadata("design:paramtypes", [Array, Object]),
|
|
@@ -69,21 +69,21 @@ __decorate([
|
|
|
69
69
|
__metadata("design:returntype", Promise)
|
|
70
70
|
], PriceRuleService.prototype, "listAndCount", null);
|
|
71
71
|
__decorate([
|
|
72
|
-
(0, utils_1.InjectTransactionManager)(
|
|
72
|
+
(0, utils_1.InjectTransactionManager)("priceRuleRepository_"),
|
|
73
73
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
74
74
|
__metadata("design:type", Function),
|
|
75
75
|
__metadata("design:paramtypes", [Array, Object]),
|
|
76
76
|
__metadata("design:returntype", Promise)
|
|
77
77
|
], PriceRuleService.prototype, "create", null);
|
|
78
78
|
__decorate([
|
|
79
|
-
(0, utils_1.InjectTransactionManager)(
|
|
79
|
+
(0, utils_1.InjectTransactionManager)("priceRuleRepository_"),
|
|
80
80
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
81
81
|
__metadata("design:type", Function),
|
|
82
82
|
__metadata("design:paramtypes", [Array, Object]),
|
|
83
83
|
__metadata("design:returntype", Promise)
|
|
84
84
|
], PriceRuleService.prototype, "update", null);
|
|
85
85
|
__decorate([
|
|
86
|
-
(0, utils_1.InjectTransactionManager)(
|
|
86
|
+
(0, utils_1.InjectTransactionManager)("priceRuleRepository_"),
|
|
87
87
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
88
88
|
__metadata("design:type", Function),
|
|
89
89
|
__metadata("design:paramtypes", [Array, Object]),
|
|
@@ -15,7 +15,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
15
|
const types_1 = require("@medusajs/types");
|
|
16
16
|
const utils_1 = require("@medusajs/utils");
|
|
17
17
|
const _models_1 = require("../models");
|
|
18
|
-
const utils_2 = require("@medusajs/utils");
|
|
19
18
|
class PriceSetMoneyAmountRulesService {
|
|
20
19
|
constructor({ priceSetMoneyAmountRulesRepository }) {
|
|
21
20
|
this.priceSetMoneyAmountRulesRepository_ =
|
|
@@ -74,21 +73,21 @@ __decorate([
|
|
|
74
73
|
__metadata("design:returntype", Promise)
|
|
75
74
|
], PriceSetMoneyAmountRulesService.prototype, "listAndCount", null);
|
|
76
75
|
__decorate([
|
|
77
|
-
(0, utils_1.InjectTransactionManager)(
|
|
76
|
+
(0, utils_1.InjectTransactionManager)("priceSetMoneyAmountRulesRepository_"),
|
|
78
77
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
79
78
|
__metadata("design:type", Function),
|
|
80
79
|
__metadata("design:paramtypes", [Array, Object]),
|
|
81
80
|
__metadata("design:returntype", Promise)
|
|
82
81
|
], PriceSetMoneyAmountRulesService.prototype, "create", null);
|
|
83
82
|
__decorate([
|
|
84
|
-
(0, utils_1.InjectTransactionManager)(
|
|
83
|
+
(0, utils_1.InjectTransactionManager)("priceSetMoneyAmountRulesRepository_"),
|
|
85
84
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
86
85
|
__metadata("design:type", Function),
|
|
87
86
|
__metadata("design:paramtypes", [Array, Object]),
|
|
88
87
|
__metadata("design:returntype", Promise)
|
|
89
88
|
], PriceSetMoneyAmountRulesService.prototype, "update", null);
|
|
90
89
|
__decorate([
|
|
91
|
-
(0, utils_1.InjectTransactionManager)(
|
|
90
|
+
(0, utils_1.InjectTransactionManager)("priceSetMoneyAmountRulesRepository_"),
|
|
92
91
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
93
92
|
__metadata("design:type", Function),
|
|
94
93
|
__metadata("design:paramtypes", [Array, Object]),
|
|
@@ -42,10 +42,12 @@ class PriceSetMoneyAmountService {
|
|
|
42
42
|
return queryOptions;
|
|
43
43
|
}
|
|
44
44
|
async create(data, sharedContext = {}) {
|
|
45
|
-
return (await this
|
|
45
|
+
return (await this
|
|
46
|
+
.priceSetMoneyAmountRepository_.create(data, sharedContext));
|
|
46
47
|
}
|
|
47
48
|
async update(data, sharedContext = {}) {
|
|
48
|
-
return (await this
|
|
49
|
+
return (await this
|
|
50
|
+
.priceSetMoneyAmountRepository_.update(data, sharedContext));
|
|
49
51
|
}
|
|
50
52
|
async delete(ids, sharedContext = {}) {
|
|
51
53
|
await this.priceSetMoneyAmountRepository_.delete(ids, sharedContext);
|
|
@@ -74,21 +76,21 @@ __decorate([
|
|
|
74
76
|
__metadata("design:returntype", Promise)
|
|
75
77
|
], PriceSetMoneyAmountService.prototype, "listAndCount", null);
|
|
76
78
|
__decorate([
|
|
77
|
-
(0, utils_1.InjectTransactionManager)(
|
|
79
|
+
(0, utils_1.InjectTransactionManager)("priceSetMoneyAmountRepository_"),
|
|
78
80
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
79
81
|
__metadata("design:type", Function),
|
|
80
82
|
__metadata("design:paramtypes", [Array, Object]),
|
|
81
83
|
__metadata("design:returntype", Promise)
|
|
82
84
|
], PriceSetMoneyAmountService.prototype, "create", null);
|
|
83
85
|
__decorate([
|
|
84
|
-
(0, utils_1.InjectTransactionManager)(
|
|
86
|
+
(0, utils_1.InjectTransactionManager)("priceSetMoneyAmountRepository_"),
|
|
85
87
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
86
88
|
__metadata("design:type", Function),
|
|
87
89
|
__metadata("design:paramtypes", [Array, Object]),
|
|
88
90
|
__metadata("design:returntype", Promise)
|
|
89
91
|
], PriceSetMoneyAmountService.prototype, "update", null);
|
|
90
92
|
__decorate([
|
|
91
|
-
(0, utils_1.InjectTransactionManager)(
|
|
93
|
+
(0, utils_1.InjectTransactionManager)("priceSetMoneyAmountRepository_"),
|
|
92
94
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
93
95
|
__metadata("design:type", Function),
|
|
94
96
|
__metadata("design:paramtypes", [Array, Object]),
|
|
@@ -74,21 +74,21 @@ __decorate([
|
|
|
74
74
|
__metadata("design:returntype", Promise)
|
|
75
75
|
], PriceSetRuleTypeService.prototype, "listAndCount", null);
|
|
76
76
|
__decorate([
|
|
77
|
-
(0, utils_1.InjectTransactionManager)(
|
|
77
|
+
(0, utils_1.InjectTransactionManager)("priceSetRuleTypeRepository_"),
|
|
78
78
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
79
79
|
__metadata("design:type", Function),
|
|
80
80
|
__metadata("design:paramtypes", [Array, Object]),
|
|
81
81
|
__metadata("design:returntype", Promise)
|
|
82
82
|
], PriceSetRuleTypeService.prototype, "create", null);
|
|
83
83
|
__decorate([
|
|
84
|
-
(0, utils_1.InjectTransactionManager)(
|
|
84
|
+
(0, utils_1.InjectTransactionManager)("priceSetRuleTypeRepository_"),
|
|
85
85
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
86
86
|
__metadata("design:type", Function),
|
|
87
87
|
__metadata("design:paramtypes", [Array, Object]),
|
|
88
88
|
__metadata("design:returntype", Promise)
|
|
89
89
|
], PriceSetRuleTypeService.prototype, "update", null);
|
|
90
90
|
__decorate([
|
|
91
|
-
(0, utils_1.InjectTransactionManager)(
|
|
91
|
+
(0, utils_1.InjectTransactionManager)("priceSetRuleTypeRepository_"),
|
|
92
92
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
93
93
|
__metadata("design:type", Function),
|
|
94
94
|
__metadata("design:paramtypes", [Array, Object]),
|
|
@@ -69,21 +69,21 @@ __decorate([
|
|
|
69
69
|
__metadata("design:returntype", Promise)
|
|
70
70
|
], PriceSetService.prototype, "listAndCount", null);
|
|
71
71
|
__decorate([
|
|
72
|
-
(0, utils_1.InjectTransactionManager)(
|
|
72
|
+
(0, utils_1.InjectTransactionManager)("priceSetRepository_"),
|
|
73
73
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
74
74
|
__metadata("design:type", Function),
|
|
75
75
|
__metadata("design:paramtypes", [Array, Object]),
|
|
76
76
|
__metadata("design:returntype", Promise)
|
|
77
77
|
], PriceSetService.prototype, "create", null);
|
|
78
78
|
__decorate([
|
|
79
|
-
(0, utils_1.InjectTransactionManager)(
|
|
79
|
+
(0, utils_1.InjectTransactionManager)("priceSetRepository_"),
|
|
80
80
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
81
81
|
__metadata("design:type", Function),
|
|
82
82
|
__metadata("design:paramtypes", [Array, Object]),
|
|
83
83
|
__metadata("design:returntype", Promise)
|
|
84
84
|
], PriceSetService.prototype, "update", null);
|
|
85
85
|
__decorate([
|
|
86
|
-
(0, utils_1.InjectTransactionManager)(
|
|
86
|
+
(0, utils_1.InjectTransactionManager)("priceSetRepository_"),
|
|
87
87
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
88
88
|
__metadata("design:type", Function),
|
|
89
89
|
__metadata("design:paramtypes", [Array, Object]),
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { Context, DAL, FindConfig, InternalModuleDeclaration, ModuleJoinerConfig, PriceSetDTO, PricingContext, PricingFilters, PricingTypes } from "@medusajs/types";
|
|
1
|
+
import { AddPricesDTO, Context, DAL, FindConfig, InternalModuleDeclaration, ModuleJoinerConfig, PriceSetDTO, PricingContext, PricingFilters, PricingTypes } from "@medusajs/types";
|
|
2
2
|
import { Currency, MoneyAmount, PriceRule, PriceSet, PriceSetMoneyAmount, PriceSetMoneyAmountRules, PriceSetRuleType, RuleType } from "../models";
|
|
3
3
|
import { CurrencyService, MoneyAmountService, PriceRuleService, PriceSetMoneyAmountRulesService, PriceSetMoneyAmountService, PriceSetRuleTypeService, PriceSetService, RuleTypeService } from ".";
|
|
4
|
-
import { AddPricesDTO } from "@medusajs/types";
|
|
5
4
|
import { PricingRepositoryService } from "../types";
|
|
6
5
|
type InjectedDependencies = {
|
|
7
6
|
baseRepository: DAL.RepositoryService;
|
|
@@ -35,13 +34,13 @@ export default class PricingModuleService<TPriceSet extends PriceSet = PriceSet,
|
|
|
35
34
|
listAndCount(filters?: PricingTypes.FilterablePriceSetProps, config?: FindConfig<PricingTypes.PriceSetDTO>, sharedContext?: Context): Promise<[PricingTypes.PriceSetDTO[], number]>;
|
|
36
35
|
create(data: PricingTypes.CreatePriceSetDTO, sharedContext?: Context): Promise<PriceSetDTO>;
|
|
37
36
|
create(data: PricingTypes.CreatePriceSetDTO[], sharedContext?: Context): Promise<PriceSetDTO[]>;
|
|
38
|
-
protected create_(data: PricingTypes.CreatePriceSetDTO[], sharedContext?: Context): Promise<
|
|
37
|
+
protected create_(data: PricingTypes.CreatePriceSetDTO[], sharedContext?: Context): Promise<TPriceSet[]>;
|
|
39
38
|
addRules(data: PricingTypes.AddRulesDTO, sharedContext?: Context): Promise<PricingTypes.PriceSetDTO>;
|
|
40
39
|
addRules(data: PricingTypes.AddRulesDTO[], sharedContext?: Context): Promise<PricingTypes.PriceSetDTO[]>;
|
|
41
40
|
protected addRules_(inputs: PricingTypes.AddRulesDTO[], sharedContext?: Context): Promise<PricingTypes.PriceSetDTO[]>;
|
|
42
41
|
addPrices(data: AddPricesDTO, sharedContext?: Context): Promise<PricingTypes.PriceSetDTO>;
|
|
43
42
|
addPrices(data: AddPricesDTO[], sharedContext?: Context): Promise<PricingTypes.PriceSetDTO[]>;
|
|
44
|
-
protected addPrices_(input: AddPricesDTO[], sharedContext?: Context): Promise<
|
|
43
|
+
protected addPrices_(input: AddPricesDTO[], sharedContext?: Context): Promise<void>;
|
|
45
44
|
removeRules(data: PricingTypes.RemovePriceSetRulesDTO[], sharedContext?: Context): Promise<void>;
|
|
46
45
|
update(data: PricingTypes.UpdatePriceSetDTO[], sharedContext?: Context): Promise<PriceSetDTO[]>;
|
|
47
46
|
delete(ids: string[], sharedContext?: Context): Promise<void>;
|
|
@@ -77,7 +77,7 @@ class PricingModuleService {
|
|
|
77
77
|
const priceSets = await this.create_(input, sharedContext);
|
|
78
78
|
const dbPriceSets = await this.list({ id: priceSets.filter((p) => !!p).map((p) => p.id) }, {
|
|
79
79
|
relations: ["rule_types", "money_amounts", "price_rules"],
|
|
80
|
-
});
|
|
80
|
+
}, sharedContext);
|
|
81
81
|
return (Array.isArray(data) ? dbPriceSets : dbPriceSets[0]);
|
|
82
82
|
}
|
|
83
83
|
async create_(data, sharedContext = {}) {
|
|
@@ -102,44 +102,61 @@ class PricingModuleService {
|
|
|
102
102
|
if (invalidMoneyAmountRule.length > 0) {
|
|
103
103
|
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Rule types don't exist for money amounts with rule attribute: ${invalidMoneyAmountRule.join(", ")}`);
|
|
104
104
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
],
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
105
|
+
// Bulk create price sets
|
|
106
|
+
const priceSetData = data.map(({ rules, prices, ...rest }) => rest);
|
|
107
|
+
const createdPriceSets = await this.priceSetService_.create(priceSetData, sharedContext);
|
|
108
|
+
// Price set rule types
|
|
109
|
+
const ruleTypeData = data.flatMap((item, index) => item.rules?.map((rule) => ({
|
|
110
|
+
rule_type: ruleTypeMap.get(rule.rule_attribute),
|
|
111
|
+
price_set: createdPriceSets[index],
|
|
112
|
+
})) || []);
|
|
113
|
+
if (ruleTypeData.length > 0) {
|
|
114
|
+
await this.priceSetRuleTypeService_.create(ruleTypeData, sharedContext);
|
|
115
|
+
}
|
|
116
|
+
// Money amounts
|
|
117
|
+
const moneyAmountData = data.flatMap((item) => item.prices || []);
|
|
118
|
+
const createdMoneyAmounts = await this.moneyAmountService_.create(moneyAmountData, sharedContext);
|
|
119
|
+
let moneyAmountIndex = 0;
|
|
120
|
+
const priceSetMoneyAmountData = [];
|
|
121
|
+
const priceRulesData = [];
|
|
122
|
+
for (const [index, item] of data.entries()) {
|
|
123
|
+
for (const ma of item.prices || []) {
|
|
124
|
+
const cleanRules = ma.rules ? (0, utils_1.removeNullish)(ma.rules) : {};
|
|
125
|
+
const numberOfRules = Object.entries(cleanRules).length;
|
|
126
|
+
const priceSetMoneyAmount = {
|
|
127
|
+
price_set: createdPriceSets[index],
|
|
128
|
+
money_amount: createdMoneyAmounts[moneyAmountIndex++],
|
|
129
|
+
title: "test",
|
|
130
|
+
number_rules: numberOfRules,
|
|
131
|
+
};
|
|
132
|
+
priceSetMoneyAmountData.push(priceSetMoneyAmount);
|
|
133
|
+
for (const [k, v] of Object.entries(cleanRules)) {
|
|
134
|
+
priceRulesData.push({
|
|
135
|
+
price_set_money_amount: null,
|
|
136
|
+
rule_type: ruleTypeMap.get(k),
|
|
137
|
+
price_set: createdPriceSets[index],
|
|
138
|
+
value: v,
|
|
139
|
+
price_list_id: "test",
|
|
140
|
+
});
|
|
138
141
|
}
|
|
139
142
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
+
}
|
|
144
|
+
// Bulk create price set money amounts
|
|
145
|
+
const createdPriceSetMoneyAmounts = await this.priceSetMoneyAmountService_.create(priceSetMoneyAmountData, sharedContext);
|
|
146
|
+
// Update price set money amount references
|
|
147
|
+
for (let i = 0, j = 0; i < priceSetMoneyAmountData.length; i++) {
|
|
148
|
+
const rulesCount = priceSetMoneyAmountData[i].number_rules;
|
|
149
|
+
for (let k = 0; k < rulesCount; k++, j++) {
|
|
150
|
+
;
|
|
151
|
+
priceRulesData[j].price_set_money_amount =
|
|
152
|
+
createdPriceSetMoneyAmounts[i];
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
// Price rules
|
|
156
|
+
if (priceRulesData.length > 0) {
|
|
157
|
+
await this.priceRuleService_.create(priceRulesData, sharedContext);
|
|
158
|
+
}
|
|
159
|
+
return createdPriceSets;
|
|
143
160
|
}
|
|
144
161
|
async addRules(data, sharedContext = {}) {
|
|
145
162
|
const inputs = Array.isArray(data) ? data : [data];
|
|
@@ -147,7 +164,7 @@ class PricingModuleService {
|
|
|
147
164
|
return (Array.isArray(data) ? priceSets : priceSets[0]);
|
|
148
165
|
}
|
|
149
166
|
async addRules_(inputs, sharedContext = {}) {
|
|
150
|
-
const priceSets = await this.priceSetService_.list({ id: inputs.map((d) => d.priceSetId) }, { relations: ["rule_types"] });
|
|
167
|
+
const priceSets = await this.priceSetService_.list({ id: inputs.map((d) => d.priceSetId) }, { relations: ["rule_types"] }, sharedContext);
|
|
151
168
|
const priceSetRuleTypeMap = new Map(priceSets.map((priceSet) => [
|
|
152
169
|
priceSet.id,
|
|
153
170
|
new Map([...priceSet.rule_types].map((rt) => [rt.rule_attribute, rt])),
|
|
@@ -195,7 +212,7 @@ class PricingModuleService {
|
|
|
195
212
|
async addPrices(data, sharedContext = {}) {
|
|
196
213
|
const input = Array.isArray(data) ? data : [data];
|
|
197
214
|
await this.addPrices_(input, sharedContext);
|
|
198
|
-
return (await this.list({ id: input.map((d) => d.priceSetId) }, { relations: ["money_amounts"] }));
|
|
215
|
+
return (await this.list({ id: input.map((d) => d.priceSetId) }, { relations: ["money_amounts"] }, sharedContext));
|
|
199
216
|
}
|
|
200
217
|
async addPrices_(input, sharedContext = {}) {
|
|
201
218
|
const priceSets = await this.list({ id: input.map((d) => d.priceSetId) }, { relations: ["rule_types"] }, sharedContext);
|
|
@@ -217,52 +234,60 @@ class PricingModuleService {
|
|
|
217
234
|
throw new utils_1.MedusaError(utils_1.MedusaError.Types.NOT_FOUND, `Rule types don't exist for: ${invalidRuleAttributes.join(", ")}`);
|
|
218
235
|
}
|
|
219
236
|
});
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
237
|
+
// Money amounts
|
|
238
|
+
const moneyAmountsBulkData = input.flatMap((entry) => entry.prices);
|
|
239
|
+
const createdMoneyAmounts = await this.moneyAmountService_.create(moneyAmountsBulkData, sharedContext);
|
|
240
|
+
// Price set money amounts
|
|
241
|
+
let maCursor = 0;
|
|
242
|
+
const priceSetMoneyAmountsBulkData = input.flatMap(({ priceSetId, prices }) => prices.map(() => {
|
|
243
|
+
const ma = createdMoneyAmounts[maCursor];
|
|
244
|
+
const numberOfRules = Object.entries(prices[maCursor]?.rules ?? {}).length;
|
|
245
|
+
maCursor++;
|
|
246
|
+
return {
|
|
247
|
+
price_set: priceSetId,
|
|
248
|
+
money_amount: ma,
|
|
249
|
+
title: "test",
|
|
250
|
+
number_rules: numberOfRules,
|
|
251
|
+
};
|
|
252
|
+
}));
|
|
253
|
+
const createdPriceSetMoneyAmounts = await this.priceSetMoneyAmountService_.create(priceSetMoneyAmountsBulkData, sharedContext);
|
|
254
|
+
// Price rules
|
|
255
|
+
let rulesCursor = 0;
|
|
256
|
+
const priceRulesBulkData = input.flatMap(({ priceSetId, prices }) => prices.flatMap((ma) => {
|
|
257
|
+
const rules = ma.rules ?? {};
|
|
258
|
+
const priceSetMoneyAmount = createdPriceSetMoneyAmounts[rulesCursor];
|
|
259
|
+
rulesCursor++;
|
|
260
|
+
return Object.entries(rules).map(([k, v]) => ({
|
|
261
|
+
price_set_money_amount: priceSetMoneyAmount,
|
|
262
|
+
rule_type: ruleTypeMap.get(priceSetId).get(k),
|
|
263
|
+
price_set: priceSetId,
|
|
264
|
+
value: v,
|
|
265
|
+
price_list_id: "test", // TODO: accept title
|
|
243
266
|
}));
|
|
267
|
+
}));
|
|
268
|
+
if (priceRulesBulkData.length > 0) {
|
|
269
|
+
await this.priceRuleService_.create(priceRulesBulkData, sharedContext);
|
|
244
270
|
}
|
|
245
|
-
return priceSets;
|
|
246
271
|
}
|
|
247
272
|
async removeRules(data, sharedContext = {}) {
|
|
248
273
|
const priceSets = await this.priceSetService_.list({
|
|
249
274
|
id: data.map((d) => d.id),
|
|
250
|
-
});
|
|
275
|
+
}, undefined, sharedContext);
|
|
251
276
|
const priceSetIds = priceSets.map((ps) => ps.id);
|
|
252
277
|
const ruleTypes = await this.ruleTypeService_.list({
|
|
253
278
|
rule_attribute: data.map((d) => d.rules || []).flat(),
|
|
254
|
-
});
|
|
279
|
+
}, undefined, sharedContext);
|
|
255
280
|
const ruleTypeIds = ruleTypes.map((rt) => rt.id);
|
|
256
281
|
const priceSetRuleTypes = await this.priceSetRuleTypeService_.list({
|
|
257
282
|
price_set_id: priceSetIds,
|
|
258
283
|
rule_type_id: ruleTypeIds,
|
|
259
|
-
});
|
|
284
|
+
}, undefined, sharedContext);
|
|
260
285
|
const priceRules = await this.priceRuleService_.list({
|
|
261
286
|
price_set_id: priceSetIds,
|
|
262
287
|
rule_type_id: ruleTypeIds,
|
|
263
288
|
}, {
|
|
264
289
|
select: ["price_set_money_amount"],
|
|
265
|
-
});
|
|
290
|
+
}, sharedContext);
|
|
266
291
|
await this.priceSetRuleTypeService_.delete(priceSetRuleTypes.map((psrt) => psrt.id), sharedContext);
|
|
267
292
|
await this.priceSetMoneyAmountService_.delete(priceRules.map((pr) => pr.price_set_money_amount.id), sharedContext);
|
|
268
293
|
}
|
|
@@ -508,7 +533,7 @@ __decorate([
|
|
|
508
533
|
__metadata("design:returntype", Promise)
|
|
509
534
|
], PricingModuleService.prototype, "create", null);
|
|
510
535
|
__decorate([
|
|
511
|
-
(0, utils_1.InjectTransactionManager)(
|
|
536
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
512
537
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
513
538
|
__metadata("design:type", Function),
|
|
514
539
|
__metadata("design:paramtypes", [Array, Object]),
|
|
@@ -522,7 +547,7 @@ __decorate([
|
|
|
522
547
|
__metadata("design:returntype", Promise)
|
|
523
548
|
], PricingModuleService.prototype, "addRules", null);
|
|
524
549
|
__decorate([
|
|
525
|
-
(0, utils_1.InjectTransactionManager)(
|
|
550
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
526
551
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
527
552
|
__metadata("design:type", Function),
|
|
528
553
|
__metadata("design:paramtypes", [Array, Object]),
|
|
@@ -536,28 +561,28 @@ __decorate([
|
|
|
536
561
|
__metadata("design:returntype", Promise)
|
|
537
562
|
], PricingModuleService.prototype, "addPrices", null);
|
|
538
563
|
__decorate([
|
|
539
|
-
(0, utils_1.InjectTransactionManager)(
|
|
564
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
540
565
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
541
566
|
__metadata("design:type", Function),
|
|
542
567
|
__metadata("design:paramtypes", [Array, Object]),
|
|
543
568
|
__metadata("design:returntype", Promise)
|
|
544
569
|
], PricingModuleService.prototype, "addPrices_", null);
|
|
545
570
|
__decorate([
|
|
546
|
-
(0, utils_1.InjectTransactionManager)(
|
|
571
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
547
572
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
548
573
|
__metadata("design:type", Function),
|
|
549
574
|
__metadata("design:paramtypes", [Array, Object]),
|
|
550
575
|
__metadata("design:returntype", Promise)
|
|
551
576
|
], PricingModuleService.prototype, "removeRules", null);
|
|
552
577
|
__decorate([
|
|
553
|
-
(0, utils_1.InjectTransactionManager)(
|
|
578
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
554
579
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
555
580
|
__metadata("design:type", Function),
|
|
556
581
|
__metadata("design:paramtypes", [Array, Object]),
|
|
557
582
|
__metadata("design:returntype", Promise)
|
|
558
583
|
], PricingModuleService.prototype, "update", null);
|
|
559
584
|
__decorate([
|
|
560
|
-
(0, utils_1.InjectTransactionManager)(
|
|
585
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
561
586
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
562
587
|
__metadata("design:type", Function),
|
|
563
588
|
__metadata("design:paramtypes", [Array, Object]),
|
|
@@ -585,21 +610,21 @@ __decorate([
|
|
|
585
610
|
__metadata("design:returntype", Promise)
|
|
586
611
|
], PricingModuleService.prototype, "listAndCountMoneyAmounts", null);
|
|
587
612
|
__decorate([
|
|
588
|
-
(0, utils_1.InjectTransactionManager)(
|
|
613
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
589
614
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
590
615
|
__metadata("design:type", Function),
|
|
591
616
|
__metadata("design:paramtypes", [Array, Object]),
|
|
592
617
|
__metadata("design:returntype", Promise)
|
|
593
618
|
], PricingModuleService.prototype, "createMoneyAmounts", null);
|
|
594
619
|
__decorate([
|
|
595
|
-
(0, utils_1.InjectTransactionManager)(
|
|
620
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
596
621
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
597
622
|
__metadata("design:type", Function),
|
|
598
623
|
__metadata("design:paramtypes", [Array, Object]),
|
|
599
624
|
__metadata("design:returntype", Promise)
|
|
600
625
|
], PricingModuleService.prototype, "updateMoneyAmounts", null);
|
|
601
626
|
__decorate([
|
|
602
|
-
(0, utils_1.InjectTransactionManager)(
|
|
627
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
603
628
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
604
629
|
__metadata("design:type", Function),
|
|
605
630
|
__metadata("design:paramtypes", [Array, Object]),
|
|
@@ -627,21 +652,21 @@ __decorate([
|
|
|
627
652
|
__metadata("design:returntype", Promise)
|
|
628
653
|
], PricingModuleService.prototype, "listAndCountCurrencies", null);
|
|
629
654
|
__decorate([
|
|
630
|
-
(0, utils_1.InjectTransactionManager)(
|
|
655
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
631
656
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
632
657
|
__metadata("design:type", Function),
|
|
633
658
|
__metadata("design:paramtypes", [Array, Object]),
|
|
634
659
|
__metadata("design:returntype", Promise)
|
|
635
660
|
], PricingModuleService.prototype, "createCurrencies", null);
|
|
636
661
|
__decorate([
|
|
637
|
-
(0, utils_1.InjectTransactionManager)(
|
|
662
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
638
663
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
639
664
|
__metadata("design:type", Function),
|
|
640
665
|
__metadata("design:paramtypes", [Array, Object]),
|
|
641
666
|
__metadata("design:returntype", Promise)
|
|
642
667
|
], PricingModuleService.prototype, "updateCurrencies", null);
|
|
643
668
|
__decorate([
|
|
644
|
-
(0, utils_1.InjectTransactionManager)(
|
|
669
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
645
670
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
646
671
|
__metadata("design:type", Function),
|
|
647
672
|
__metadata("design:paramtypes", [Array, Object]),
|
|
@@ -669,21 +694,21 @@ __decorate([
|
|
|
669
694
|
__metadata("design:returntype", Promise)
|
|
670
695
|
], PricingModuleService.prototype, "listAndCountRuleTypes", null);
|
|
671
696
|
__decorate([
|
|
672
|
-
(0, utils_1.InjectTransactionManager)(
|
|
697
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
673
698
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
674
699
|
__metadata("design:type", Function),
|
|
675
700
|
__metadata("design:paramtypes", [Array, Object]),
|
|
676
701
|
__metadata("design:returntype", Promise)
|
|
677
702
|
], PricingModuleService.prototype, "createRuleTypes", null);
|
|
678
703
|
__decorate([
|
|
679
|
-
(0, utils_1.InjectTransactionManager)(
|
|
704
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
680
705
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
681
706
|
__metadata("design:type", Function),
|
|
682
707
|
__metadata("design:paramtypes", [Array, Object]),
|
|
683
708
|
__metadata("design:returntype", Promise)
|
|
684
709
|
], PricingModuleService.prototype, "updateRuleTypes", null);
|
|
685
710
|
__decorate([
|
|
686
|
-
(0, utils_1.InjectTransactionManager)(
|
|
711
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
687
712
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
688
713
|
__metadata("design:type", Function),
|
|
689
714
|
__metadata("design:paramtypes", [Array, Object]),
|
|
@@ -725,21 +750,21 @@ __decorate([
|
|
|
725
750
|
__metadata("design:returntype", Promise)
|
|
726
751
|
], PricingModuleService.prototype, "listAndCountPriceSetMoneyAmounts", null);
|
|
727
752
|
__decorate([
|
|
728
|
-
(0, utils_1.InjectTransactionManager)(
|
|
753
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
729
754
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
730
755
|
__metadata("design:type", Function),
|
|
731
756
|
__metadata("design:paramtypes", [Array, Object]),
|
|
732
757
|
__metadata("design:returntype", Promise)
|
|
733
758
|
], PricingModuleService.prototype, "createPriceSetMoneyAmountRules", null);
|
|
734
759
|
__decorate([
|
|
735
|
-
(0, utils_1.InjectTransactionManager)(
|
|
760
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
736
761
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
737
762
|
__metadata("design:type", Function),
|
|
738
763
|
__metadata("design:paramtypes", [Array, Object]),
|
|
739
764
|
__metadata("design:returntype", Promise)
|
|
740
765
|
], PricingModuleService.prototype, "updatePriceSetMoneyAmountRules", null);
|
|
741
766
|
__decorate([
|
|
742
|
-
(0, utils_1.InjectTransactionManager)(
|
|
767
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
743
768
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
744
769
|
__metadata("design:type", Function),
|
|
745
770
|
__metadata("design:paramtypes", [Array, Object]),
|
|
@@ -767,21 +792,21 @@ __decorate([
|
|
|
767
792
|
__metadata("design:returntype", Promise)
|
|
768
793
|
], PricingModuleService.prototype, "listAndCountPriceRules", null);
|
|
769
794
|
__decorate([
|
|
770
|
-
(0, utils_1.InjectTransactionManager)(
|
|
795
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
771
796
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
772
797
|
__metadata("design:type", Function),
|
|
773
798
|
__metadata("design:paramtypes", [Array, Object]),
|
|
774
799
|
__metadata("design:returntype", Promise)
|
|
775
800
|
], PricingModuleService.prototype, "createPriceRules", null);
|
|
776
801
|
__decorate([
|
|
777
|
-
(0, utils_1.InjectTransactionManager)(
|
|
802
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
778
803
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
779
804
|
__metadata("design:type", Function),
|
|
780
805
|
__metadata("design:paramtypes", [Array, Object]),
|
|
781
806
|
__metadata("design:returntype", Promise)
|
|
782
807
|
], PricingModuleService.prototype, "updatePriceRules", null);
|
|
783
808
|
__decorate([
|
|
784
|
-
(0, utils_1.InjectTransactionManager)(
|
|
809
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
|
785
810
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
786
811
|
__metadata("design:type", Function),
|
|
787
812
|
__metadata("design:paramtypes", [Array, Object]),
|
|
@@ -15,7 +15,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
15
|
const types_1 = require("@medusajs/types");
|
|
16
16
|
const utils_1 = require("@medusajs/utils");
|
|
17
17
|
const _models_1 = require("../models");
|
|
18
|
-
const utils_2 = require("@medusajs/utils");
|
|
19
18
|
class RuleTypeService {
|
|
20
19
|
constructor({ ruleTypeRepository }) {
|
|
21
20
|
this.ruleTypeRepository_ = ruleTypeRepository;
|
|
@@ -71,21 +70,21 @@ __decorate([
|
|
|
71
70
|
__metadata("design:returntype", Promise)
|
|
72
71
|
], RuleTypeService.prototype, "listAndCount", null);
|
|
73
72
|
__decorate([
|
|
74
|
-
(0, utils_1.InjectTransactionManager)(
|
|
73
|
+
(0, utils_1.InjectTransactionManager)("ruleTypeRepository_"),
|
|
75
74
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
76
75
|
__metadata("design:type", Function),
|
|
77
76
|
__metadata("design:paramtypes", [Array, Object]),
|
|
78
77
|
__metadata("design:returntype", Promise)
|
|
79
78
|
], RuleTypeService.prototype, "create", null);
|
|
80
79
|
__decorate([
|
|
81
|
-
(0, utils_1.InjectTransactionManager)(
|
|
80
|
+
(0, utils_1.InjectTransactionManager)("ruleTypeRepository_"),
|
|
82
81
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
83
82
|
__metadata("design:type", Function),
|
|
84
83
|
__metadata("design:paramtypes", [Array, Object]),
|
|
85
84
|
__metadata("design:returntype", Promise)
|
|
86
85
|
], RuleTypeService.prototype, "update", null);
|
|
87
86
|
__decorate([
|
|
88
|
-
(0, utils_1.InjectTransactionManager)(
|
|
87
|
+
(0, utils_1.InjectTransactionManager)("ruleTypeRepository_"),
|
|
89
88
|
__param(1, (0, utils_1.MedusaContext)()),
|
|
90
89
|
__metadata("design:type", Function),
|
|
91
90
|
__metadata("design:paramtypes", [Array, Object]),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@medusajs/pricing",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3-next-20231109084604",
|
|
4
4
|
"description": "Medusa Pricing module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -47,9 +47,9 @@
|
|
|
47
47
|
"typescript": "^5.1.6"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@medusajs/modules-sdk": "
|
|
51
|
-
"@medusajs/types": "
|
|
52
|
-
"@medusajs/utils": "
|
|
50
|
+
"@medusajs/modules-sdk": "1.12.3-next-20231109084604",
|
|
51
|
+
"@medusajs/types": "1.11.6-next-20231109084604",
|
|
52
|
+
"@medusajs/utils": "1.10.5-next-20231109084604",
|
|
53
53
|
"@mikro-orm/core": "5.7.12",
|
|
54
54
|
"@mikro-orm/migrations": "5.7.12",
|
|
55
55
|
"@mikro-orm/postgresql": "5.7.12",
|