@medusajs/pricing 0.2.0-snapshot-20240409114424 → 0.2.0-snapshot-20240423083106
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/joiner-config.d.ts +1 -0
- package/dist/joiner-config.js +13 -0
- package/dist/migrations/Migration20230929122253.js +1 -1
- package/dist/models/price.d.ts +3 -1
- package/dist/models/price.js +6 -2
- package/dist/services/pricing-module.d.ts +6 -5
- package/dist/services/pricing-module.js +75 -25
- package/package.json +5 -5
package/dist/joiner-config.d.ts
CHANGED
package/dist/joiner-config.js
CHANGED
@@ -11,6 +11,7 @@ exports.LinkableKeys = {
|
|
11
11
|
price_set_id: _models_1.PriceSet.name,
|
12
12
|
price_list_id: _models_1.PriceList.name,
|
13
13
|
price_id: _models_1.Price.name,
|
14
|
+
rule_type_id: _models_1.RuleType.name,
|
14
15
|
};
|
15
16
|
const entityLinkableKeysMap = {};
|
16
17
|
Object.entries(exports.LinkableKeys).forEach(([key, value]) => {
|
@@ -39,5 +40,17 @@ exports.joinerConfig = {
|
|
39
40
|
methodSuffix: "PriceLists",
|
40
41
|
},
|
41
42
|
},
|
43
|
+
{
|
44
|
+
name: ["price", "prices"],
|
45
|
+
args: {
|
46
|
+
methodSuffix: "Prices",
|
47
|
+
},
|
48
|
+
},
|
49
|
+
{
|
50
|
+
name: ["rule_type", "rule_types"],
|
51
|
+
args: {
|
52
|
+
methodSuffix: "RuleTypes",
|
53
|
+
},
|
54
|
+
},
|
42
55
|
],
|
43
56
|
};
|
@@ -6,7 +6,7 @@ class Migration20230929122253 extends migrations_1.Migration {
|
|
6
6
|
async up() {
|
7
7
|
this.addSql('create table if not exists "money_amount" ("id" text not null, "currency_code" text not null, "amount" numeric not null, "min_quantity" numeric null, "max_quantity" numeric null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "money_amount_pkey" primary key ("id"));');
|
8
8
|
this.addSql('create table "price_set" ("id" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "price_set_pkey" primary key ("id"));');
|
9
|
-
this.addSql('create table "price" ("id" text not null, "title" text, "price_set_id" text not null, "money_amount_id" text not null, "rules_count" integer not null default 0, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "price_pkey" primary key ("id"));');
|
9
|
+
this.addSql('create table "price" ("id" text not null, "title" text, "price_set_id" text not null, "money_amount_id" text not null, "raw_amount" jsonb not null, "rules_count" integer not null default 0, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "price_pkey" primary key ("id"));');
|
10
10
|
this.addSql('alter table "price" add constraint "price_money_amount_id_unique" unique ("money_amount_id");');
|
11
11
|
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, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "rule_type_pkey" primary key ("id"));');
|
12
12
|
this.addSql('create table "price_set_rule_type" ("id" text not null, "price_set_id" text not null, "rule_type_id" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "price_set_rule_type_pkey" primary key ("id"));');
|
package/dist/models/price.d.ts
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
import { DAL } from "@medusajs/types";
|
2
|
+
import { BigNumber } from "@medusajs/utils";
|
2
3
|
import { Collection, OptionalProps } from "@mikro-orm/core";
|
3
4
|
import PriceList from "./price-list";
|
4
5
|
import PriceRule from "./price-rule";
|
@@ -9,7 +10,8 @@ export default class Price {
|
|
9
10
|
id: string;
|
10
11
|
title: string | null;
|
11
12
|
currency_code: string;
|
12
|
-
amount: number;
|
13
|
+
amount: BigNumber | number;
|
14
|
+
raw_amount: Record<string, unknown>;
|
13
15
|
min_quantity: number | null;
|
14
16
|
max_quantity: number | null;
|
15
17
|
price_set_id: string;
|
package/dist/models/price.js
CHANGED
@@ -74,9 +74,13 @@ __decorate([
|
|
74
74
|
__metadata("design:type", String)
|
75
75
|
], Price.prototype, "currency_code", void 0);
|
76
76
|
__decorate([
|
77
|
-
(0,
|
78
|
-
__metadata("design:type",
|
77
|
+
(0, utils_1.MikroOrmBigNumberProperty)(),
|
78
|
+
__metadata("design:type", Object)
|
79
79
|
], Price.prototype, "amount", void 0);
|
80
|
+
__decorate([
|
81
|
+
(0, core_1.Property)({ columnType: "jsonb" }),
|
82
|
+
__metadata("design:type", Object)
|
83
|
+
], Price.prototype, "raw_amount", void 0);
|
80
84
|
__decorate([
|
81
85
|
(0, core_1.Property)({ columnType: "numeric", nullable: true }),
|
82
86
|
__metadata("design:type", Object)
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { AddPricesDTO, Context, DAL, InternalModuleDeclaration, ModuleJoinerConfig, ModulesSdkTypes, PriceSetDTO, PricingContext, PricingFilters, PricingRepositoryService, PricingTypes, RuleTypeDTO, UpsertPriceSetDTO } from "@medusajs/types";
|
1
|
+
import { AddPricesDTO, Context, CreatePriceRuleDTO, DAL, InternalModuleDeclaration, ModuleJoinerConfig, ModulesSdkTypes, PriceSetDTO, PricingContext, PricingFilters, PricingRepositoryService, PricingTypes, RuleTypeDTO, UpsertPriceSetDTO } from "@medusajs/types";
|
2
2
|
import { ModulesSdkUtils } from "@medusajs/utils";
|
3
3
|
import { Price, PriceList, PriceListRule, PriceListRuleValue, PriceRule, PriceSet, PriceSetRuleType, RuleType } from "../models";
|
4
4
|
import { PriceListService, RuleTypeService } from ".";
|
@@ -57,6 +57,7 @@ export default class PricingModuleService<TPriceSet extends PriceSet = PriceSet,
|
|
57
57
|
upsert(data: UpsertPriceSetDTO, sharedContext?: Context): Promise<PriceSetDTO>;
|
58
58
|
update(id: string, data: PricingTypes.UpdatePriceSetDTO, sharedContext?: Context): Promise<PriceSetDTO>;
|
59
59
|
update(selector: PricingTypes.FilterablePriceSetProps, data: PricingTypes.UpdatePriceSetDTO, sharedContext?: Context): Promise<PriceSetDTO[]>;
|
60
|
+
private normalizeUpdateData;
|
60
61
|
protected update_(data: UpdatePriceSetInput[], sharedContext?: Context): Promise<PriceSet[]>;
|
61
62
|
addRules(data: PricingTypes.AddRulesDTO, sharedContext?: Context): Promise<PricingTypes.PriceSetDTO>;
|
62
63
|
addRules(data: PricingTypes.AddRulesDTO[], sharedContext?: Context): Promise<PricingTypes.PriceSetDTO[]>;
|
@@ -68,9 +69,9 @@ export default class PricingModuleService<TPriceSet extends PriceSet = PriceSet,
|
|
68
69
|
createPriceListRules(data: PricingTypes.CreatePriceListRuleDTO[], sharedContext?: Context): Promise<PricingTypes.PriceListRuleDTO[]>;
|
69
70
|
createPriceListRules_(data: PricingTypes.CreatePriceListRuleDTO[], sharedContext?: Context): Promise<TPriceListRule[]>;
|
70
71
|
updatePriceListRules(data: PricingTypes.UpdatePriceListRuleDTO[], sharedContext?: Context): Promise<PricingTypes.PriceListRuleDTO[]>;
|
71
|
-
updatePriceListPrices(data: PricingTypes.UpdatePriceListPricesDTO[], sharedContext?: Context): Promise<PricingTypes.
|
72
|
+
updatePriceListPrices(data: PricingTypes.UpdatePriceListPricesDTO[], sharedContext?: Context): Promise<PricingTypes.PriceDTO[]>;
|
72
73
|
removePrices(ids: string[], sharedContext?: Context): Promise<void>;
|
73
|
-
addPriceListPrices(data: PricingTypes.AddPriceListPricesDTO[], sharedContext?: Context): Promise<PricingTypes.
|
74
|
+
addPriceListPrices(data: PricingTypes.AddPriceListPricesDTO[], sharedContext?: Context): Promise<PricingTypes.PriceDTO[]>;
|
74
75
|
setPriceListRules(data: PricingTypes.SetPriceListRulesDTO, sharedContext?: Context): Promise<PricingTypes.PriceListDTO>;
|
75
76
|
removePriceListRules(data: PricingTypes.RemovePriceListRulesDTO, sharedContext?: Context): Promise<PricingTypes.PriceListDTO>;
|
76
77
|
protected create_(data: PricingTypes.CreatePriceSetDTO[], sharedContext?: Context): Promise<TPriceSet[]>;
|
@@ -78,9 +79,9 @@ export default class PricingModuleService<TPriceSet extends PriceSet = PriceSet,
|
|
78
79
|
protected addPrices_(input: AddPricesDTO[], sharedContext?: Context): Promise<void>;
|
79
80
|
protected createPriceLists_(data: PricingTypes.CreatePriceListDTO[], sharedContext?: Context): Promise<TPriceList[]>;
|
80
81
|
protected updatePriceLists_(data: PricingTypes.UpdatePriceListDTO[], sharedContext?: Context): Promise<PricingTypes.PriceListDTO[]>;
|
81
|
-
protected updatePriceListPrices_(data: PricingTypes.UpdatePriceListPricesDTO[], sharedContext?: Context): Promise<
|
82
|
+
protected updatePriceListPrices_(data: PricingTypes.UpdatePriceListPricesDTO[], sharedContext?: Context): Promise<TPrice[]>;
|
82
83
|
protected removePrices_(ids: string[], sharedContext?: Context): Promise<void>;
|
83
|
-
protected addPriceListPrices_(data: PricingTypes.AddPriceListPricesDTO[], sharedContext?: Context): Promise<
|
84
|
+
protected addPriceListPrices_(data: PricingTypes.AddPriceListPricesDTO[], sharedContext?: Context): Promise<TPrice[]>;
|
84
85
|
protected setPriceListRules_(data: PricingTypes.SetPriceListRulesDTO[], sharedContext?: Context): Promise<TPriceList[]>;
|
85
86
|
protected removePriceListRules_(data: PricingTypes.RemovePriceListRulesDTO[], sharedContext?: Context): Promise<TPriceList[]>;
|
86
87
|
}
|
@@ -17,8 +17,8 @@ const utils_1 = require("@medusajs/utils");
|
|
17
17
|
const _models_1 = require("../models");
|
18
18
|
const _utils_1 = require("../utils");
|
19
19
|
const joiner_config_1 = require("../joiner-config");
|
20
|
-
const price_set_1 = require("../models/price-set");
|
21
20
|
const price_list_1 = require("../models/price-list");
|
21
|
+
const price_set_1 = require("../models/price-set");
|
22
22
|
const generateMethodForModels = [
|
23
23
|
_models_1.PriceList,
|
24
24
|
_models_1.PriceListRule,
|
@@ -134,11 +134,63 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
|
|
134
134
|
const priceSets = await this.baseRepository_.serialize(updateResult);
|
135
135
|
return (0, utils_1.isString)(idOrSelector) ? priceSets[0] : priceSets;
|
136
136
|
}
|
137
|
+
async normalizeUpdateData(data, sharedContext) {
|
138
|
+
const ruleAttributes = data
|
139
|
+
.map((d) => d.prices?.map((p) => Object.keys(p.rules ?? [])) ?? [])
|
140
|
+
.flat(Infinity)
|
141
|
+
.filter(Boolean);
|
142
|
+
const ruleTypes = await this.ruleTypeService_.list({ rule_attribute: ruleAttributes }, { take: null }, sharedContext);
|
143
|
+
const ruleTypeMap = ruleTypes.reduce((acc, curr) => {
|
144
|
+
acc.set(curr.rule_attribute, curr);
|
145
|
+
return acc;
|
146
|
+
}, new Map());
|
147
|
+
return data.map((priceSet) => {
|
148
|
+
const prices = priceSet.prices?.map((price) => {
|
149
|
+
const rules = Object.entries(price.rules ?? {}).map(([attribute, value]) => {
|
150
|
+
return {
|
151
|
+
price_set_id: priceSet.id,
|
152
|
+
rule_type_id: ruleTypeMap.get(attribute).id,
|
153
|
+
value,
|
154
|
+
};
|
155
|
+
});
|
156
|
+
const hasRulesInput = (0, utils_1.isPresent)(price.rules);
|
157
|
+
delete price.rules;
|
158
|
+
return {
|
159
|
+
...price,
|
160
|
+
price_set_id: priceSet.id,
|
161
|
+
price_rules: hasRulesInput ? rules : undefined,
|
162
|
+
rules_count: hasRulesInput ? rules.length : undefined,
|
163
|
+
};
|
164
|
+
});
|
165
|
+
return {
|
166
|
+
...priceSet,
|
167
|
+
prices,
|
168
|
+
};
|
169
|
+
});
|
170
|
+
}
|
137
171
|
async update_(data, sharedContext = {}) {
|
138
172
|
// TODO: We are not handling rule types, rules, etc. here, add support after data models are finalized
|
139
173
|
// TODO: Since money IDs are rarely passed, this will delete all previous data and insert new entries.
|
140
174
|
// We can make the `insert` inside upsertWithReplace do an `upsert` instead to avoid this
|
141
|
-
|
175
|
+
const normalizedData = await this.normalizeUpdateData(data, sharedContext);
|
176
|
+
const prices = normalizedData.flatMap((priceSet) => priceSet.prices || []);
|
177
|
+
const upsertedPrices = await this.priceService_.upsertWithReplace(prices, {
|
178
|
+
relations: ["price_rules"],
|
179
|
+
}, sharedContext);
|
180
|
+
const priceSetsToUpsert = normalizedData.map((priceSet) => {
|
181
|
+
const { prices, ...rest } = priceSet;
|
182
|
+
return {
|
183
|
+
...rest,
|
184
|
+
prices: upsertedPrices
|
185
|
+
.filter((p) => p.price_set_id === priceSet.id)
|
186
|
+
.map((price) => {
|
187
|
+
// @ts-ignore
|
188
|
+
delete price.price_rules;
|
189
|
+
return price;
|
190
|
+
}),
|
191
|
+
};
|
192
|
+
});
|
193
|
+
return await this.priceSetService_.upsertWithReplace(priceSetsToUpsert, { relations: ["prices"] }, sharedContext);
|
142
194
|
}
|
143
195
|
async addRules(data, sharedContext = {}) {
|
144
196
|
const inputs = Array.isArray(data) ? data : [data];
|
@@ -194,13 +246,15 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
|
|
194
246
|
});
|
195
247
|
}
|
196
248
|
async updatePriceListPrices(data, sharedContext = {}) {
|
197
|
-
|
249
|
+
const prices = await this.updatePriceListPrices_(data, sharedContext);
|
250
|
+
return await this.baseRepository_.serialize(prices);
|
198
251
|
}
|
199
252
|
async removePrices(ids, sharedContext = {}) {
|
200
253
|
await this.removePrices_(ids, sharedContext);
|
201
254
|
}
|
202
255
|
async addPriceListPrices(data, sharedContext = {}) {
|
203
|
-
|
256
|
+
const prices = await this.addPriceListPrices_(data, sharedContext);
|
257
|
+
return await this.baseRepository_.serialize(prices);
|
204
258
|
}
|
205
259
|
async setPriceListRules(data, sharedContext = {}) {
|
206
260
|
const [priceList] = await this.setPriceListRules_([data], sharedContext);
|
@@ -510,40 +564,37 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
|
|
510
564
|
}
|
511
565
|
const priceLists = await this.listPriceLists({ id: priceListIds }, { take: null }, sharedContext);
|
512
566
|
const priceListMap = new Map(priceLists.map((p) => [p.id, p]));
|
567
|
+
const pricesToUpdate = [];
|
568
|
+
const priceRuleIdsToDelete = [];
|
569
|
+
const priceRulesToCreate = [];
|
513
570
|
for (const { price_list_id: priceListId, prices } of data) {
|
514
571
|
const priceList = priceListMap.get(priceListId);
|
515
572
|
if (!priceList) {
|
516
573
|
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Price list with id: ${priceListId} not found`);
|
517
574
|
}
|
518
|
-
const priceRuleIdsToDelete = [];
|
519
|
-
const priceRulesToCreate = [];
|
520
|
-
const pricesToUpdate = [];
|
521
575
|
for (const priceData of prices) {
|
522
|
-
const { rules, price_set_id, ...rest } = priceData;
|
576
|
+
const { rules = {}, price_set_id, ...rest } = priceData;
|
523
577
|
const price = priceMap.get(rest.id);
|
524
578
|
const priceRules = price.price_rules;
|
525
|
-
|
526
|
-
|
527
|
-
|
579
|
+
priceRulesToCreate.push(...Object.entries(rules).map(([ruleAttribute, ruleValue]) => ({
|
580
|
+
price_set_id,
|
581
|
+
rule_type_id: ruleTypeMap.get(ruleAttribute).id,
|
582
|
+
value: ruleValue,
|
583
|
+
price_id: price.id,
|
584
|
+
})));
|
528
585
|
pricesToUpdate.push({
|
529
586
|
...rest,
|
530
587
|
rules_count: Object.keys(rules).length,
|
531
|
-
price_rules: Object.entries(rules).map(([ruleAttribute, ruleValue]) => ({
|
532
|
-
price_set_id,
|
533
|
-
rule_type_id: ruleTypeMap.get(ruleAttribute).id,
|
534
|
-
value: ruleValue,
|
535
|
-
price_id: price.id,
|
536
|
-
})),
|
537
588
|
});
|
538
589
|
priceRuleIdsToDelete.push(...priceRules.map((pr) => pr.id));
|
539
590
|
}
|
540
|
-
await (0, utils_1.promiseAll)([
|
541
|
-
this.priceRuleService_.delete(priceRuleIdsToDelete),
|
542
|
-
this.priceRuleService_.create(priceRulesToCreate),
|
543
|
-
this.priceService_.update(pricesToUpdate),
|
544
|
-
]);
|
545
591
|
}
|
546
|
-
|
592
|
+
const [_deletedPriceRule, _createdPriceRule, updatedPrices] = await (0, utils_1.promiseAll)([
|
593
|
+
this.priceRuleService_.delete(priceRuleIdsToDelete),
|
594
|
+
this.priceRuleService_.create(priceRulesToCreate),
|
595
|
+
this.priceService_.update(pricesToUpdate),
|
596
|
+
]);
|
597
|
+
return updatedPrices;
|
547
598
|
}
|
548
599
|
async removePrices_(ids, sharedContext = {}) {
|
549
600
|
await this.priceService_.delete(ids, sharedContext);
|
@@ -612,8 +663,7 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
|
|
612
663
|
});
|
613
664
|
pricesToCreate.push(...priceListPricesToCreate);
|
614
665
|
}
|
615
|
-
await this.priceService_.create(pricesToCreate, sharedContext);
|
616
|
-
return priceLists;
|
666
|
+
return await this.priceService_.create(pricesToCreate, sharedContext);
|
617
667
|
}
|
618
668
|
async setPriceListRules_(data, sharedContext = {}) {
|
619
669
|
// TODO: re think this method
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@medusajs/pricing",
|
3
|
-
"version": "0.2.0-snapshot-
|
3
|
+
"version": "0.2.0-snapshot-20240423083106",
|
4
4
|
"description": "Medusa Pricing module",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"types": "dist/index.d.ts",
|
@@ -40,7 +40,7 @@
|
|
40
40
|
"@mikro-orm/cli": "5.9.7",
|
41
41
|
"cross-env": "^5.2.1",
|
42
42
|
"jest": "^29.6.3",
|
43
|
-
"medusa-test-utils": "1.1.44-snapshot-
|
43
|
+
"medusa-test-utils": "1.1.44-snapshot-20240423083106",
|
44
44
|
"rimraf": "^3.0.2",
|
45
45
|
"ts-jest": "^29.1.1",
|
46
46
|
"ts-node": "^10.9.1",
|
@@ -48,9 +48,9 @@
|
|
48
48
|
"typescript": "^5.1.6"
|
49
49
|
},
|
50
50
|
"dependencies": {
|
51
|
-
"@medusajs/modules-sdk": "1.12.11-snapshot-
|
52
|
-
"@medusajs/types": "1.12.0-snapshot-
|
53
|
-
"@medusajs/utils": "1.12.0-snapshot-
|
51
|
+
"@medusajs/modules-sdk": "1.12.11-snapshot-20240423083106",
|
52
|
+
"@medusajs/types": "1.12.0-snapshot-20240423083106",
|
53
|
+
"@medusajs/utils": "1.12.0-snapshot-20240423083106",
|
54
54
|
"@mikro-orm/core": "5.9.7",
|
55
55
|
"@mikro-orm/migrations": "5.9.7",
|
56
56
|
"@mikro-orm/postgresql": "5.9.7",
|