@medusajs/pricing 0.1.13-snapshot-20240531102054 → 0.1.13-snapshot-20240607093839

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@ import { AddPricesDTO, Context, CreatePriceRuleDTO, DAL, FindConfig, InternalMod
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 ".";
5
- import { UpdatePriceSetInput } from "src/types/services";
5
+ import { ServiceTypes } from "../types";
6
6
  type InjectedDependencies = {
7
7
  baseRepository: DAL.RepositoryService;
8
8
  pricingRepository: PricingRepositoryService;
@@ -61,7 +61,7 @@ export default class PricingModuleService<TPriceSet extends PriceSet = PriceSet,
61
61
  update(id: string, data: PricingTypes.UpdatePriceSetDTO, sharedContext?: Context): Promise<PriceSetDTO>;
62
62
  update(selector: PricingTypes.FilterablePriceSetProps, data: PricingTypes.UpdatePriceSetDTO, sharedContext?: Context): Promise<PriceSetDTO[]>;
63
63
  private normalizeUpdateData;
64
- protected update_(data: UpdatePriceSetInput[], sharedContext?: Context): Promise<PriceSet[]>;
64
+ protected update_(data: ServiceTypes.UpdatePriceSetInput[], sharedContext?: Context): Promise<PriceSet[]>;
65
65
  addRules(data: PricingTypes.AddRulesDTO, sharedContext?: Context): Promise<PricingTypes.PriceSetDTO>;
66
66
  addRules(data: PricingTypes.AddRulesDTO[], sharedContext?: Context): Promise<PricingTypes.PriceSetDTO[]>;
67
67
  addPrices(data: AddPricesDTO, sharedContext?: Context): Promise<PricingTypes.PriceSetDTO>;
@@ -69,9 +69,6 @@ export default class PricingModuleService<TPriceSet extends PriceSet = PriceSet,
69
69
  removeRules(data: PricingTypes.RemovePriceSetRulesDTO[], sharedContext?: Context): Promise<void>;
70
70
  createPriceLists(data: PricingTypes.CreatePriceListDTO[], sharedContext?: Context): Promise<PricingTypes.PriceListDTO[]>;
71
71
  updatePriceLists(data: PricingTypes.UpdatePriceListDTO[], sharedContext?: Context): Promise<PricingTypes.PriceListDTO[]>;
72
- createPriceListRules(data: PricingTypes.CreatePriceListRuleDTO[], sharedContext?: Context): Promise<PricingTypes.PriceListRuleDTO[]>;
73
- createPriceListRules_(data: PricingTypes.CreatePriceListRuleDTO[], sharedContext?: Context): Promise<TPriceListRule[]>;
74
- updatePriceListRules(data: PricingTypes.UpdatePriceListRuleDTO[], sharedContext?: Context): Promise<PricingTypes.PriceListRuleDTO[]>;
75
72
  updatePriceListPrices(data: PricingTypes.UpdatePriceListPricesDTO[], sharedContext?: Context): Promise<PricingTypes.PriceDTO[]>;
76
73
  removePrices(ids: string[], sharedContext?: Context): Promise<void>;
77
74
  addPriceListPrices(data: PricingTypes.AddPriceListPricesDTO[], sharedContext?: Context): Promise<PricingTypes.PriceDTO[]>;
@@ -79,7 +76,7 @@ export default class PricingModuleService<TPriceSet extends PriceSet = PriceSet,
79
76
  removePriceListRules(data: PricingTypes.RemovePriceListRulesDTO, sharedContext?: Context): Promise<PricingTypes.PriceListDTO>;
80
77
  protected create_(data: PricingTypes.CreatePriceSetDTO[], sharedContext?: Context): Promise<TPriceSet[]>;
81
78
  protected addRules_(inputs: PricingTypes.AddRulesDTO[], sharedContext?: Context): Promise<TPriceSet[]>;
82
- protected addPrices_(input: AddPricesDTO[], sharedContext?: Context): Promise<void>;
79
+ protected addPrices_(input: AddPricesDTO[], sharedContext?: Context): Promise<TPrice[]>;
83
80
  protected createPriceLists_(data: PricingTypes.CreatePriceListDTO[], sharedContext?: Context): Promise<TPriceList[]>;
84
81
  protected updatePriceLists_(data: PricingTypes.UpdatePriceListDTO[], sharedContext?: Context): Promise<PricingTypes.PriceListDTO[]>;
85
82
  protected updatePriceListPrices_(data: PricingTypes.UpdatePriceListPricesDTO[], sharedContext?: Context): Promise<TPrice[]>;
@@ -143,7 +143,14 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
143
143
  const input = Array.isArray(data) ? data : [data];
144
144
  const priceSets = await this.create_(input, sharedContext);
145
145
  // TODO: Remove the need to refetch the data here
146
- const dbPriceSets = await this.list({ id: priceSets.map((p) => p.id) }, { relations: ["rule_types", "prices", "price_rules"] }, sharedContext);
146
+ const dbPriceSets = await this.list({ id: priceSets.map((p) => p.id) }, {
147
+ relations: [
148
+ "rule_types",
149
+ "prices",
150
+ "price_rules",
151
+ "prices.price_rules",
152
+ ],
153
+ }, sharedContext);
147
154
  // Ensure the output to be in the same order as the input
148
155
  const results = priceSets.map((priceSet) => {
149
156
  return dbPriceSets.find((p) => p.id === priceSet.id);
@@ -222,7 +229,7 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
222
229
  // We can make the `insert` inside upsertWithReplace do an `upsert` instead to avoid this
223
230
  const normalizedData = await this.normalizeUpdateData(data, sharedContext);
224
231
  const prices = normalizedData.flatMap((priceSet) => priceSet.prices || []);
225
- const upsertedPrices = await this.priceService_.upsertWithReplace(prices, {
232
+ const { entities: upsertedPrices } = await this.priceService_.upsertWithReplace(prices, {
226
233
  relations: ["price_rules"],
227
234
  }, sharedContext);
228
235
  const priceSetsToUpsert = normalizedData.map((priceSet) => {
@@ -238,7 +245,8 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
238
245
  }),
239
246
  };
240
247
  });
241
- return await this.priceSetService_.upsertWithReplace(priceSetsToUpsert, { relations: ["prices"] }, sharedContext);
248
+ const { entities: priceSets } = await this.priceSetService_.upsertWithReplace(priceSetsToUpsert, { relations: ["prices"] }, sharedContext);
249
+ return priceSets;
242
250
  }
243
251
  async addRules(data, sharedContext = {}) {
244
252
  const inputs = Array.isArray(data) ? data : [data];
@@ -278,21 +286,6 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
278
286
  const priceLists = await this.updatePriceLists_(data, sharedContext);
279
287
  return await this.baseRepository_.serialize(priceLists);
280
288
  }
281
- async createPriceListRules(data, sharedContext = {}) {
282
- const priceLists = await this.createPriceListRules_(data, sharedContext);
283
- return await this.baseRepository_.serialize(priceLists, {
284
- populate: true,
285
- });
286
- }
287
- async createPriceListRules_(data, sharedContext = {}) {
288
- return await this.priceListRuleService_.create(data, sharedContext);
289
- }
290
- async updatePriceListRules(data, sharedContext = {}) {
291
- const priceLists = await this.priceListRuleService_.update(data, sharedContext);
292
- return await this.baseRepository_.serialize(priceLists, {
293
- populate: true,
294
- });
295
- }
296
289
  async updatePriceListPrices(data, sharedContext = {}) {
297
290
  const prices = await this.updatePriceListPrices_(data, sharedContext);
298
291
  return await this.baseRepository_.serialize(prices);
@@ -378,6 +371,38 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
378
371
  });
379
372
  // Bulk create price sets
380
373
  const createdPriceSets = await this.priceSetService_.create(toCreate, sharedContext);
374
+ const eventsData = createdPriceSets.reduce((eventsData, priceSet) => {
375
+ eventsData.priceSets.push({
376
+ id: priceSet.id,
377
+ });
378
+ priceSet.prices.map((price) => {
379
+ eventsData.prices.push({
380
+ id: price.id,
381
+ });
382
+ price.price_rules.map((priceRule) => {
383
+ eventsData.priceRules.push({
384
+ id: priceRule.id,
385
+ });
386
+ });
387
+ });
388
+ return eventsData;
389
+ }, {
390
+ priceSets: [],
391
+ priceRules: [],
392
+ prices: [],
393
+ });
394
+ _utils_1.eventBuilders.createdPriceSet({
395
+ data: eventsData.priceSets,
396
+ sharedContext,
397
+ });
398
+ _utils_1.eventBuilders.createdPrice({
399
+ data: eventsData.prices,
400
+ sharedContext,
401
+ });
402
+ _utils_1.eventBuilders.createdPriceRule({
403
+ data: eventsData.priceRules,
404
+ sharedContext,
405
+ });
381
406
  if (ruleSetRuleTypeToCreateMap.size) {
382
407
  await this.priceSetRuleTypeService_.create(Array.from(ruleSetRuleTypeToCreateMap.values()), sharedContext);
383
408
  }
@@ -458,10 +483,39 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
458
483
  price_set_id: priceSetId,
459
484
  title: "test", // TODO: accept title
460
485
  rules_count: numberOfRules,
461
- priceRules,
486
+ price_rules: priceRules,
462
487
  };
463
488
  }));
464
- await this.priceService_.create(pricesToCreate, sharedContext);
489
+ const prices = await this.priceService_.create(pricesToCreate, sharedContext);
490
+ /**
491
+ * Preparing data for emitting events
492
+ */
493
+ const eventsData = prices.reduce((eventsData, price) => {
494
+ eventsData.prices.push({
495
+ id: price.id,
496
+ });
497
+ price.price_rules.map((priceRule) => {
498
+ eventsData.priceRules.push({
499
+ id: priceRule.id,
500
+ });
501
+ });
502
+ return eventsData;
503
+ }, {
504
+ priceRules: [],
505
+ prices: [],
506
+ });
507
+ /**
508
+ * Emitting events for all created entities
509
+ */
510
+ _utils_1.eventBuilders.createdPrice({
511
+ data: eventsData.prices,
512
+ sharedContext,
513
+ });
514
+ _utils_1.eventBuilders.createdPriceRule({
515
+ data: eventsData.priceRules,
516
+ sharedContext,
517
+ });
518
+ return prices;
465
519
  }
466
520
  async createPriceLists_(data, sharedContext = {}) {
467
521
  const ruleTypeAttributes = [];
@@ -515,7 +569,56 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
515
569
  prices: pricesData,
516
570
  };
517
571
  });
518
- return await this.priceListService_.create(priceListsToCreate, sharedContext);
572
+ const priceLists = await this.priceListService_.create(priceListsToCreate, sharedContext);
573
+ /**
574
+ * Preparing data for emitting events
575
+ */
576
+ const eventsData = priceLists.reduce((eventsData, priceList) => {
577
+ eventsData.priceList.push({
578
+ id: priceList.id,
579
+ });
580
+ priceList.price_list_rules.map((listRule) => {
581
+ eventsData.priceListRules.push({
582
+ id: listRule.id,
583
+ });
584
+ });
585
+ priceList.prices.map((price) => {
586
+ eventsData.prices.push({
587
+ id: price.id,
588
+ });
589
+ price.price_rules.map((priceRule) => {
590
+ eventsData.priceRules.push({
591
+ id: priceRule.id,
592
+ });
593
+ });
594
+ });
595
+ return eventsData;
596
+ }, {
597
+ priceList: [],
598
+ priceListRules: [],
599
+ priceRules: [],
600
+ prices: [],
601
+ });
602
+ /**
603
+ * Emitting events for all created entities
604
+ */
605
+ _utils_1.eventBuilders.createdPriceList({
606
+ data: eventsData.priceList,
607
+ sharedContext,
608
+ });
609
+ _utils_1.eventBuilders.createdPriceListRule({
610
+ data: eventsData.priceListRules,
611
+ sharedContext,
612
+ });
613
+ _utils_1.eventBuilders.createdPrice({
614
+ data: eventsData.prices,
615
+ sharedContext,
616
+ });
617
+ _utils_1.eventBuilders.createdPriceRule({
618
+ data: eventsData.priceRules,
619
+ sharedContext,
620
+ });
621
+ return priceLists;
519
622
  }
520
623
  async updatePriceLists_(data, sharedContext = {}) {
521
624
  const updatedPriceLists = [];
@@ -709,7 +812,30 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
709
812
  });
710
813
  pricesToCreate.push(...priceListPricesToCreate);
711
814
  }
712
- return await this.priceService_.create(pricesToCreate, sharedContext);
815
+ const createdPrices = await this.priceService_.create(pricesToCreate, sharedContext);
816
+ const eventsData = createdPrices.reduce((eventsData, price) => {
817
+ eventsData.prices.push({
818
+ id: price.id,
819
+ });
820
+ price.price_rules.map((priceRule) => {
821
+ eventsData.priceRules.push({
822
+ id: priceRule.id,
823
+ });
824
+ });
825
+ return eventsData;
826
+ }, {
827
+ priceRules: [],
828
+ prices: [],
829
+ });
830
+ _utils_1.eventBuilders.createdPrice({
831
+ data: eventsData.prices,
832
+ sharedContext,
833
+ });
834
+ _utils_1.eventBuilders.createdPriceRule({
835
+ data: eventsData.priceRules,
836
+ sharedContext,
837
+ });
838
+ return createdPrices;
713
839
  }
714
840
  async setPriceListRules_(data, sharedContext = {}) {
715
841
  // TODO: re think this method
@@ -818,6 +944,7 @@ __decorate([
818
944
  ], PricingModuleService.prototype, "calculatePrices", null);
819
945
  __decorate([
820
946
  (0, utils_1.InjectManager)("baseRepository_"),
947
+ (0, utils_1.EmitEvents)(),
821
948
  __param(1, (0, utils_1.MedusaContext)()),
822
949
  __metadata("design:type", Function),
823
950
  __metadata("design:paramtypes", [Object, Object]),
@@ -853,6 +980,7 @@ __decorate([
853
980
  ], PricingModuleService.prototype, "addRules", null);
854
981
  __decorate([
855
982
  (0, utils_1.InjectManager)("baseRepository_"),
983
+ (0, utils_1.EmitEvents)(),
856
984
  __param(1, (0, utils_1.MedusaContext)()),
857
985
  __metadata("design:type", Function),
858
986
  __metadata("design:paramtypes", [Object, Object]),
@@ -867,6 +995,7 @@ __decorate([
867
995
  ], PricingModuleService.prototype, "removeRules", null);
868
996
  __decorate([
869
997
  (0, utils_1.InjectManager)("baseRepository_"),
998
+ (0, utils_1.EmitEvents)(),
870
999
  __param(1, (0, utils_1.MedusaContext)()),
871
1000
  __metadata("design:type", Function),
872
1001
  __metadata("design:paramtypes", [Array, Object]),
@@ -879,27 +1008,6 @@ __decorate([
879
1008
  __metadata("design:paramtypes", [Array, Object]),
880
1009
  __metadata("design:returntype", Promise)
881
1010
  ], PricingModuleService.prototype, "updatePriceLists", null);
882
- __decorate([
883
- (0, utils_1.InjectManager)("baseRepository_"),
884
- __param(1, (0, utils_1.MedusaContext)()),
885
- __metadata("design:type", Function),
886
- __metadata("design:paramtypes", [Array, Object]),
887
- __metadata("design:returntype", Promise)
888
- ], PricingModuleService.prototype, "createPriceListRules", null);
889
- __decorate([
890
- (0, utils_1.InjectTransactionManager)("baseRepository_"),
891
- __param(1, (0, utils_1.MedusaContext)()),
892
- __metadata("design:type", Function),
893
- __metadata("design:paramtypes", [Array, Object]),
894
- __metadata("design:returntype", Promise)
895
- ], PricingModuleService.prototype, "createPriceListRules_", null);
896
- __decorate([
897
- (0, utils_1.InjectTransactionManager)("baseRepository_"),
898
- __param(1, (0, utils_1.MedusaContext)()),
899
- __metadata("design:type", Function),
900
- __metadata("design:paramtypes", [Array, Object]),
901
- __metadata("design:returntype", Promise)
902
- ], PricingModuleService.prototype, "updatePriceListRules", null);
903
1011
  __decorate([
904
1012
  (0, utils_1.InjectManager)("baseRepository_"),
905
1013
  __param(1, (0, utils_1.MedusaContext)()),
@@ -916,6 +1024,7 @@ __decorate([
916
1024
  ], PricingModuleService.prototype, "removePrices", null);
917
1025
  __decorate([
918
1026
  (0, utils_1.InjectManager)("baseRepository_"),
1027
+ (0, utils_1.EmitEvents)(),
919
1028
  __param(1, (0, utils_1.MedusaContext)()),
920
1029
  __metadata("design:type", Function),
921
1030
  __metadata("design:paramtypes", [Array, Object]),
@@ -0,0 +1,44 @@
1
+ export declare const eventBuilders: {
2
+ createdPriceSet: ({ data, sharedContext, }: {
3
+ data: {
4
+ id: string;
5
+ }[];
6
+ sharedContext: import("@medusajs/types").Context;
7
+ }) => void;
8
+ createdPriceSetRuleType: ({ data, sharedContext, }: {
9
+ data: {
10
+ id: string;
11
+ }[];
12
+ sharedContext: import("@medusajs/types").Context;
13
+ }) => void;
14
+ createdPrice: ({ data, sharedContext, }: {
15
+ data: {
16
+ id: string;
17
+ }[];
18
+ sharedContext: import("@medusajs/types").Context;
19
+ }) => void;
20
+ createdPriceRule: ({ data, sharedContext, }: {
21
+ data: {
22
+ id: string;
23
+ }[];
24
+ sharedContext: import("@medusajs/types").Context;
25
+ }) => void;
26
+ createdPriceList: ({ data, sharedContext, }: {
27
+ data: {
28
+ id: string;
29
+ }[];
30
+ sharedContext: import("@medusajs/types").Context;
31
+ }) => void;
32
+ createdPriceListRule: ({ data, sharedContext, }: {
33
+ data: {
34
+ id: string;
35
+ }[];
36
+ sharedContext: import("@medusajs/types").Context;
37
+ }) => void;
38
+ attachedPriceListRule: ({ data, sharedContext, }: {
39
+ data: {
40
+ id: string;
41
+ }[];
42
+ sharedContext: import("@medusajs/types").Context;
43
+ }) => void;
44
+ };
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.eventBuilders = void 0;
4
+ const utils_1 = require("@medusajs/utils");
5
+ exports.eventBuilders = {
6
+ createdPriceSet: (0, utils_1.eventBuilderFactory)({
7
+ service: utils_1.Modules.PRICING,
8
+ action: utils_1.CommonEvents.CREATED,
9
+ object: "price_set",
10
+ eventsEnum: utils_1.PricingEvents,
11
+ }),
12
+ createdPriceSetRuleType: (0, utils_1.eventBuilderFactory)({
13
+ service: utils_1.Modules.PRICING,
14
+ action: utils_1.CommonEvents.CREATED,
15
+ object: "price_set_rule_type",
16
+ eventsEnum: utils_1.PricingEvents,
17
+ }),
18
+ createdPrice: (0, utils_1.eventBuilderFactory)({
19
+ service: utils_1.Modules.PRICING,
20
+ action: utils_1.CommonEvents.CREATED,
21
+ object: "price",
22
+ eventsEnum: utils_1.PricingEvents,
23
+ }),
24
+ createdPriceRule: (0, utils_1.eventBuilderFactory)({
25
+ service: utils_1.Modules.PRICING,
26
+ action: utils_1.CommonEvents.CREATED,
27
+ object: "price_rule",
28
+ eventsEnum: utils_1.PricingEvents,
29
+ }),
30
+ createdPriceList: (0, utils_1.eventBuilderFactory)({
31
+ service: utils_1.Modules.PRICING,
32
+ action: utils_1.CommonEvents.CREATED,
33
+ object: "price_list",
34
+ eventsEnum: utils_1.PricingEvents,
35
+ }),
36
+ createdPriceListRule: (0, utils_1.eventBuilderFactory)({
37
+ service: utils_1.Modules.PRICING,
38
+ action: utils_1.CommonEvents.CREATED,
39
+ object: "price_list_rule",
40
+ eventsEnum: utils_1.PricingEvents,
41
+ }),
42
+ attachedPriceListRule: (0, utils_1.eventBuilderFactory)({
43
+ service: utils_1.Modules.PRICING,
44
+ action: utils_1.CommonEvents.ATTACHED,
45
+ object: "price_list_rule",
46
+ eventsEnum: utils_1.PricingEvents,
47
+ }),
48
+ };
@@ -1 +1,2 @@
1
1
  export * from "./validate-price-list-dates";
2
+ export * from "./events";
@@ -15,3 +15,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./validate-price-list-dates"), exports);
18
+ __exportStar(require("./events"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@medusajs/pricing",
3
- "version": "0.1.13-snapshot-20240531102054",
3
+ "version": "0.1.13-snapshot-20240607093839",
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.45-snapshot-20240531102054",
43
+ "medusa-test-utils": "1.1.45-snapshot-20240607093839",
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.13.0-snapshot-20240531102054",
52
- "@medusajs/types": "1.12.0-snapshot-20240531102054",
53
- "@medusajs/utils": "1.12.0-snapshot-20240531102054",
51
+ "@medusajs/modules-sdk": "1.13.0-snapshot-20240607093839",
52
+ "@medusajs/types": "1.12.0-snapshot-20240607093839",
53
+ "@medusajs/utils": "1.12.0-snapshot-20240607093839",
54
54
  "@mikro-orm/core": "5.9.7",
55
55
  "@mikro-orm/migrations": "5.9.7",
56
56
  "@mikro-orm/postgresql": "5.9.7",