@medusajs/pricing 0.1.13-snapshot-20240701122250 → 0.1.13-snapshot-20240703074611
Sign up to get free protection for your applications and to get access to all the features.
@@ -43,6 +43,7 @@ export default class PricingModuleService extends PricingModuleService_base impl
|
|
43
43
|
constructor({ baseRepository, pricingRepository, priceSetService, priceRuleService, priceService, priceListService, priceListRuleService, }: InjectedDependencies, moduleDeclaration: InternalModuleDeclaration);
|
44
44
|
__joinerConfig(): ModuleJoinerConfig;
|
45
45
|
private setupCalculatedPriceConfig_;
|
46
|
+
retrievePriceSet(id: string, config?: FindConfig<PriceSetDTO> | undefined, sharedContext?: Context | undefined): Promise<PriceSetDTO>;
|
46
47
|
listPriceSets(filters?: PricingTypes.FilterablePriceSetProps, config?: FindConfig<PricingTypes.PriceSetDTO>, sharedContext?: Context): Promise<PriceSetDTO[]>;
|
47
48
|
listAndCountPriceSets(filters?: PricingTypes.FilterablePriceSetProps, config?: FindConfig<PricingTypes.PriceSetDTO>, sharedContext?: Context): Promise<[PriceSetDTO[], number]>;
|
48
49
|
calculatePrices(pricingFilters: PricingFilters, pricingContext?: PricingContext, sharedContext?: Context): Promise<PricingTypes.CalculatedPriceSet[]>;
|
@@ -73,5 +74,17 @@ export default class PricingModuleService extends PricingModuleService_base impl
|
|
73
74
|
protected setPriceListRules_(data: PricingTypes.SetPriceListRulesDTO[], sharedContext?: Context): Promise<PriceList[]>;
|
74
75
|
protected removePriceListRules_(data: PricingTypes.RemovePriceListRulesDTO[], sharedContext?: Context): Promise<PriceList[]>;
|
75
76
|
protected normalizePriceListDate(data: (ServiceTypes.UpdatePriceListDTO | ServiceTypes.CreatePriceListDTO | CreatePriceListDTO)[]): any[];
|
77
|
+
protected normalizePriceSetConfig(config: FindConfig<PricingTypes.PriceSetDTO> | undefined): {
|
78
|
+
select?: string[] | undefined;
|
79
|
+
skip?: number | null | undefined;
|
80
|
+
take?: number | null | undefined;
|
81
|
+
relations?: string[] | undefined;
|
82
|
+
order?: {
|
83
|
+
[K: string]: "ASC" | "DESC";
|
84
|
+
} | undefined;
|
85
|
+
withDeleted?: boolean | undefined;
|
86
|
+
filters?: Record<string, any> | undefined;
|
87
|
+
options: Record<string, any>;
|
88
|
+
};
|
76
89
|
}
|
77
90
|
export {};
|
@@ -53,53 +53,60 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.MedusaService(generat
|
|
53
53
|
return pricingContext;
|
54
54
|
}
|
55
55
|
// @ts-expect-error
|
56
|
+
async retrievePriceSet(id, config, sharedContext) {
|
57
|
+
const priceSet = await this.priceSetService_.retrieve(id, this.normalizePriceSetConfig(config), sharedContext);
|
58
|
+
return await this.baseRepository_.serialize(priceSet);
|
59
|
+
}
|
60
|
+
// @ts-expect-error
|
56
61
|
async listPriceSets(filters = {}, config = {}, sharedContext = {}) {
|
57
|
-
const
|
58
|
-
const
|
62
|
+
const normalizedConfig = this.normalizePriceSetConfig(config);
|
63
|
+
const pricingContext = this.setupCalculatedPriceConfig_(filters, normalizedConfig);
|
64
|
+
const priceSets = await super.listPriceSets(filters, normalizedConfig, sharedContext);
|
59
65
|
if (!pricingContext || !priceSets.length) {
|
60
66
|
return priceSets;
|
61
67
|
}
|
62
|
-
const
|
63
|
-
const
|
64
|
-
for (const priceSet of priceSets) {
|
65
|
-
priceSetIds.push(priceSet.id);
|
66
|
-
priceSetMap.set(priceSet.id, priceSet);
|
67
|
-
}
|
68
|
-
const calculatedPrices = await this.calculatePrices({ id: priceSetIds }, { context: pricingContext }, sharedContext);
|
68
|
+
const calculatedPrices = await this.calculatePrices({ id: priceSets.map((p) => p.id) }, { context: pricingContext }, sharedContext);
|
69
|
+
const calculatedPricesMap = new Map();
|
69
70
|
for (const calculatedPrice of calculatedPrices) {
|
70
|
-
|
71
|
-
|
71
|
+
calculatedPricesMap.set(calculatedPrice.id, calculatedPrice);
|
72
|
+
}
|
73
|
+
for (const priceSet of priceSets) {
|
74
|
+
const calculatedPrice = calculatedPricesMap.get(priceSet.id);
|
75
|
+
priceSet.calculated_price = calculatedPrice ?? null;
|
72
76
|
}
|
73
77
|
return priceSets;
|
74
78
|
}
|
75
79
|
// @ts-expect-error
|
76
80
|
async listAndCountPriceSets(filters = {}, config = {}, sharedContext = {}) {
|
77
|
-
const
|
78
|
-
const
|
81
|
+
const normalizedConfig = this.normalizePriceSetConfig(config);
|
82
|
+
const pricingContext = this.setupCalculatedPriceConfig_(filters, normalizedConfig);
|
83
|
+
const [priceSets, count] = await super.listAndCountPriceSets(filters, normalizedConfig, sharedContext);
|
79
84
|
if (!pricingContext || !priceSets.length) {
|
80
85
|
return [priceSets, count];
|
81
86
|
}
|
82
|
-
const
|
83
|
-
const
|
84
|
-
for (const priceSet of priceSets) {
|
85
|
-
priceSetIds.push(priceSet.id);
|
86
|
-
priceSetMap.set(priceSet.id, priceSet);
|
87
|
-
}
|
88
|
-
const calculatedPrices = await this.calculatePrices({ id: priceSetIds }, { context: pricingContext }, sharedContext);
|
87
|
+
const calculatedPrices = await this.calculatePrices({ id: priceSets.map((p) => p.id) }, { context: pricingContext }, sharedContext);
|
88
|
+
const calculatedPricesMap = new Map();
|
89
89
|
for (const calculatedPrice of calculatedPrices) {
|
90
|
-
|
91
|
-
|
90
|
+
calculatedPricesMap.set(calculatedPrice.id, calculatedPrice);
|
91
|
+
}
|
92
|
+
for (const priceSet of priceSets) {
|
93
|
+
const calculatedPrice = calculatedPricesMap.get(priceSet.id);
|
94
|
+
priceSet.calculated_price = calculatedPrice ?? null;
|
92
95
|
}
|
93
96
|
return [priceSets, count];
|
94
97
|
}
|
95
98
|
async calculatePrices(pricingFilters, pricingContext = { context: {} }, sharedContext = {}) {
|
96
99
|
const results = await this.pricingRepository_.calculatePrices(pricingFilters, pricingContext, sharedContext);
|
97
100
|
const pricesSetPricesMap = (0, utils_1.groupBy)(results, "price_set_id");
|
98
|
-
const calculatedPrices = pricingFilters.id
|
101
|
+
const calculatedPrices = pricingFilters.id
|
102
|
+
.map((priceSetId) => {
|
99
103
|
// This is where we select prices, for now we just do a first match based on the database results
|
100
104
|
// which is prioritized by rules_count first for exact match and then deafult_priority of the rule_type
|
101
105
|
// TODO: inject custom price selection here
|
102
106
|
const prices = pricesSetPricesMap.get(priceSetId) || [];
|
107
|
+
if (!prices.length) {
|
108
|
+
return null;
|
109
|
+
}
|
103
110
|
const priceListPrice = prices.find((p) => p.price_list_id);
|
104
111
|
const defaultPrice = prices?.find((p) => !p.price_list_id);
|
105
112
|
let calculatedPrice = defaultPrice;
|
@@ -132,16 +139,17 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.MedusaService(generat
|
|
132
139
|
max_quantity: parseInt(originalPrice?.max_quantity || "") || null,
|
133
140
|
},
|
134
141
|
};
|
135
|
-
})
|
142
|
+
})
|
143
|
+
.filter(Boolean);
|
136
144
|
return JSON.parse(JSON.stringify(calculatedPrices));
|
137
145
|
}
|
138
146
|
async createPriceSets(data, sharedContext = {}) {
|
139
147
|
const input = Array.isArray(data) ? data : [data];
|
140
148
|
const priceSets = await this.createPriceSets_(input, sharedContext);
|
141
149
|
// TODO: Remove the need to refetch the data here
|
142
|
-
const dbPriceSets = await this.listPriceSets({ id: priceSets.map((p) => p.id) }, {
|
150
|
+
const dbPriceSets = await this.listPriceSets({ id: priceSets.map((p) => p.id) }, this.normalizePriceSetConfig({
|
143
151
|
relations: ["prices", "prices.price_rules"],
|
144
|
-
}, sharedContext);
|
152
|
+
}), sharedContext);
|
145
153
|
// Ensure the output to be in the same order as the input
|
146
154
|
const results = priceSets.map((priceSet) => {
|
147
155
|
return dbPriceSets.find((p) => p.id === priceSet.id);
|
@@ -719,8 +727,24 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.MedusaService(generat
|
|
719
727
|
return priceListData;
|
720
728
|
});
|
721
729
|
}
|
730
|
+
normalizePriceSetConfig(config) {
|
731
|
+
return {
|
732
|
+
options: {
|
733
|
+
populateWhere: { prices: { price_list_id: null } },
|
734
|
+
},
|
735
|
+
...config,
|
736
|
+
};
|
737
|
+
}
|
722
738
|
}
|
723
739
|
exports.default = PricingModuleService;
|
740
|
+
__decorate([
|
741
|
+
(0, utils_1.InjectManager)("baseRepository_")
|
742
|
+
// @ts-expect-error
|
743
|
+
,
|
744
|
+
__metadata("design:type", Function),
|
745
|
+
__metadata("design:paramtypes", [String, Object, Object]),
|
746
|
+
__metadata("design:returntype", Promise)
|
747
|
+
], PricingModuleService.prototype, "retrievePriceSet", null);
|
724
748
|
__decorate([
|
725
749
|
(0, utils_1.InjectManager)("baseRepository_")
|
726
750
|
// @ts-expect-error
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@medusajs/pricing",
|
3
|
-
"version": "0.1.13-snapshot-
|
3
|
+
"version": "0.1.13-snapshot-20240703074611",
|
4
4
|
"description": "Medusa Pricing module",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"types": "dist/index.d.ts",
|
@@ -37,7 +37,7 @@
|
|
37
37
|
"@mikro-orm/cli": "5.9.7",
|
38
38
|
"cross-env": "^5.2.1",
|
39
39
|
"jest": "^29.7.0",
|
40
|
-
"medusa-test-utils": "1.1.45-snapshot-
|
40
|
+
"medusa-test-utils": "1.1.45-snapshot-20240703074611",
|
41
41
|
"rimraf": "^3.0.2",
|
42
42
|
"ts-jest": "^29.1.1",
|
43
43
|
"ts-node": "^10.9.1",
|
@@ -45,9 +45,9 @@
|
|
45
45
|
"typescript": "^5.1.6"
|
46
46
|
},
|
47
47
|
"dependencies": {
|
48
|
-
"@medusajs/modules-sdk": "1.13.0-snapshot-
|
49
|
-
"@medusajs/types": "1.12.0-snapshot-
|
50
|
-
"@medusajs/utils": "1.12.0-snapshot-
|
48
|
+
"@medusajs/modules-sdk": "1.13.0-snapshot-20240703074611",
|
49
|
+
"@medusajs/types": "1.12.0-snapshot-20240703074611",
|
50
|
+
"@medusajs/utils": "1.12.0-snapshot-20240703074611",
|
51
51
|
"@mikro-orm/core": "5.9.7",
|
52
52
|
"@mikro-orm/migrations": "5.9.7",
|
53
53
|
"@mikro-orm/postgresql": "5.9.7",
|