@medusajs/pricing 0.2.0-snapshot-20240426112214 → 0.2.0-snapshot-20240429113131

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
- import { AddPricesDTO, Context, CreatePriceRuleDTO, DAL, InternalModuleDeclaration, ModuleJoinerConfig, ModulesSdkTypes, PriceSetDTO, PricingContext, PricingFilters, PricingRepositoryService, PricingTypes, RuleTypeDTO, UpsertPriceSetDTO } from "@medusajs/types";
1
+ import { AddPricesDTO, Context, CreatePriceRuleDTO, DAL, FindConfig, 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 ".";
@@ -50,6 +50,9 @@ export default class PricingModuleService<TPriceSet extends PriceSet = PriceSet,
50
50
  protected readonly priceListRuleValueService_: ModulesSdkTypes.InternalModuleService<TPriceListRuleValue>;
51
51
  constructor({ baseRepository, pricingRepository, ruleTypeService, priceSetService, priceRuleService, priceSetRuleTypeService, priceService, priceListService, priceListRuleService, priceListRuleValueService, }: InjectedDependencies, moduleDeclaration: InternalModuleDeclaration);
52
52
  __joinerConfig(): ModuleJoinerConfig;
53
+ private setupCalculatedPriceConfig_;
54
+ list(filters?: PricingTypes.FilterablePriceSetProps, config?: FindConfig<PricingTypes.PriceSetDTO>, sharedContext?: Context): Promise<PriceSetDTO[]>;
55
+ listAndCount(filters?: PricingTypes.FilterablePriceSetProps, config?: FindConfig<PricingTypes.PriceSetDTO>, sharedContext?: Context): Promise<[PriceSetDTO[], number]>;
53
56
  calculatePrices(pricingFilters: PricingFilters, pricingContext?: PricingContext, sharedContext?: Context): Promise<PricingTypes.CalculatedPriceSet[]>;
54
57
  create(data: PricingTypes.CreatePriceSetDTO, sharedContext?: Context): Promise<PriceSetDTO>;
55
58
  create(data: PricingTypes.CreatePriceSetDTO[], sharedContext?: Context): Promise<PriceSetDTO[]>;
@@ -48,13 +48,61 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
48
48
  __joinerConfig() {
49
49
  return joiner_config_1.joinerConfig;
50
50
  }
51
+ setupCalculatedPriceConfig_(filters, config) {
52
+ const fieldIdx = config.relations?.indexOf("calculated_price");
53
+ const shouldCalculatePrice = fieldIdx > -1;
54
+ if (!shouldCalculatePrice) {
55
+ return;
56
+ }
57
+ let pricingContext = filters.context ?? {};
58
+ // cleanup virtual field "calculated_price"
59
+ config.relations?.splice(fieldIdx, 1);
60
+ delete filters.context;
61
+ return pricingContext;
62
+ }
63
+ async list(filters = {}, config = {}, sharedContext = {}) {
64
+ const pricingContext = this.setupCalculatedPriceConfig_(filters, config);
65
+ const priceSets = await super.list(filters, config, sharedContext);
66
+ if (pricingContext && priceSets.length) {
67
+ const priceSetIds = [];
68
+ const priceSetMap = new Map();
69
+ for (const priceSet of priceSets) {
70
+ priceSetIds.push(priceSet.id);
71
+ priceSetMap.set(priceSet.id, priceSet);
72
+ }
73
+ const calculatedPrices = await this.calculatePrices({ id: priceSets.map((p) => p.id) }, { context: pricingContext }, sharedContext);
74
+ for (const calculatedPrice of calculatedPrices) {
75
+ const priceSet = priceSetMap.get(calculatedPrice.id);
76
+ priceSet.calculated_price = calculatedPrice;
77
+ }
78
+ }
79
+ return priceSets;
80
+ }
81
+ async listAndCount(filters = {}, config = {}, sharedContext = {}) {
82
+ const pricingContext = this.setupCalculatedPriceConfig_(filters, config);
83
+ const [priceSets, count] = await super.listAndCount(filters, config, sharedContext);
84
+ if (pricingContext && priceSets.length) {
85
+ const priceSetIds = [];
86
+ const priceSetMap = new Map();
87
+ for (const priceSet of priceSets) {
88
+ priceSetIds.push(priceSet.id);
89
+ priceSetMap.set(priceSet.id, priceSet);
90
+ }
91
+ const calculatedPrices = await this.calculatePrices({ id: priceSets.map((p) => p.id) }, { context: pricingContext }, sharedContext);
92
+ for (const calculatedPrice of calculatedPrices) {
93
+ const priceSet = priceSetMap.get(calculatedPrice.id);
94
+ priceSet.calculated_price = calculatedPrice;
95
+ }
96
+ }
97
+ return [priceSets, count];
98
+ }
51
99
  async calculatePrices(pricingFilters, pricingContext = { context: {} }, sharedContext = {}) {
52
100
  const results = await this.pricingRepository_.calculatePrices(pricingFilters, pricingContext, sharedContext);
53
101
  const pricesSetPricesMap = (0, utils_1.groupBy)(results, "price_set_id");
54
102
  const calculatedPrices = pricingFilters.id.map((priceSetId) => {
55
103
  // This is where we select prices, for now we just do a first match based on the database results
56
104
  // which is prioritized by rules_count first for exact match and then deafult_priority of the rule_type
57
- // inject custom price selection here
105
+ // TODO: inject custom price selection here
58
106
  const prices = pricesSetPricesMap.get(priceSetId) || [];
59
107
  const priceListPrice = prices.find((p) => p.price_list_id);
60
108
  const defaultPrice = prices?.find((p) => !p.price_list_id);
@@ -318,7 +366,7 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
318
366
  });
319
367
  return {
320
368
  ...rest,
321
- title: "test",
369
+ title: "",
322
370
  rules_count: numberOfRules,
323
371
  price_rules: Array.from(rulesDataMap.values()),
324
372
  };
@@ -749,6 +797,20 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
749
797
  }
750
798
  }
751
799
  exports.default = PricingModuleService;
800
+ __decorate([
801
+ (0, utils_1.InjectManager)("baseRepository_"),
802
+ __param(2, (0, utils_1.MedusaContext)()),
803
+ __metadata("design:type", Function),
804
+ __metadata("design:paramtypes", [Object, Object, Object]),
805
+ __metadata("design:returntype", Promise)
806
+ ], PricingModuleService.prototype, "list", null);
807
+ __decorate([
808
+ (0, utils_1.InjectManager)("baseRepository_"),
809
+ __param(2, (0, utils_1.MedusaContext)()),
810
+ __metadata("design:type", Function),
811
+ __metadata("design:paramtypes", [Object, Object, Object]),
812
+ __metadata("design:returntype", Promise)
813
+ ], PricingModuleService.prototype, "listAndCount", null);
752
814
  __decorate([
753
815
  (0, utils_1.InjectManager)("baseRepository_"),
754
816
  __param(2, (0, utils_1.MedusaContext)()),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@medusajs/pricing",
3
- "version": "0.2.0-snapshot-20240426112214",
3
+ "version": "0.2.0-snapshot-20240429113131",
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-20240426112214",
43
+ "medusa-test-utils": "1.1.44-snapshot-20240429113131",
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-20240426112214",
52
- "@medusajs/types": "1.12.0-snapshot-20240426112214",
53
- "@medusajs/utils": "1.12.0-snapshot-20240426112214",
51
+ "@medusajs/modules-sdk": "1.12.11-snapshot-20240429113131",
52
+ "@medusajs/types": "1.12.0-snapshot-20240429113131",
53
+ "@medusajs/utils": "1.12.0-snapshot-20240429113131",
54
54
  "@mikro-orm/core": "5.9.7",
55
55
  "@mikro-orm/migrations": "5.9.7",
56
56
  "@mikro-orm/postgresql": "5.9.7",