@medusajs/pricing 0.1.12-snapshot-20240328221015 → 0.2.0-snapshot-20240329181714

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,8 @@
1
- import { AddPricesDTO, Context, DAL, InternalModuleDeclaration, ModuleJoinerConfig, ModulesSdkTypes, PriceSetDTO, PricingContext, PricingFilters, PricingRepositoryService, PricingTypes, RuleTypeDTO } from "@medusajs/types";
1
+ import { AddPricesDTO, Context, 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 ".";
5
+ import { UpdatePriceSetInput } from "src/types/services";
5
6
  type InjectedDependencies = {
6
7
  baseRepository: DAL.RepositoryService;
7
8
  pricingRepository: PricingRepositoryService;
@@ -52,12 +53,16 @@ export default class PricingModuleService<TPriceSet extends PriceSet = PriceSet,
52
53
  calculatePrices(pricingFilters: PricingFilters, pricingContext?: PricingContext, sharedContext?: Context): Promise<PricingTypes.CalculatedPriceSet[]>;
53
54
  create(data: PricingTypes.CreatePriceSetDTO, sharedContext?: Context): Promise<PriceSetDTO>;
54
55
  create(data: PricingTypes.CreatePriceSetDTO[], sharedContext?: Context): Promise<PriceSetDTO[]>;
56
+ upsert(data: UpsertPriceSetDTO[], sharedContext?: Context): Promise<PriceSetDTO[]>;
57
+ upsert(data: UpsertPriceSetDTO, sharedContext?: Context): Promise<PriceSetDTO>;
58
+ update(id: string, data: PricingTypes.UpdatePriceSetDTO, sharedContext?: Context): Promise<PriceSetDTO>;
59
+ update(selector: PricingTypes.FilterablePriceSetProps, data: PricingTypes.UpdatePriceSetDTO, sharedContext?: Context): Promise<PriceSetDTO[]>;
60
+ protected update_(data: UpdatePriceSetInput[], sharedContext?: Context): Promise<PriceSet[]>;
55
61
  addRules(data: PricingTypes.AddRulesDTO, sharedContext?: Context): Promise<PricingTypes.PriceSetDTO>;
56
62
  addRules(data: PricingTypes.AddRulesDTO[], sharedContext?: Context): Promise<PricingTypes.PriceSetDTO[]>;
57
63
  addPrices(data: AddPricesDTO, sharedContext?: Context): Promise<PricingTypes.PriceSetDTO>;
58
64
  addPrices(data: AddPricesDTO[], sharedContext?: Context): Promise<PricingTypes.PriceSetDTO[]>;
59
65
  removeRules(data: PricingTypes.RemovePriceSetRulesDTO[], sharedContext?: Context): Promise<void>;
60
- update(data: PricingTypes.UpdatePriceSetDTO[], sharedContext?: Context): Promise<PriceSetDTO[]>;
61
66
  createPriceLists(data: PricingTypes.CreatePriceListDTO[], sharedContext?: Context): Promise<PricingTypes.PriceListDTO[]>;
62
67
  updatePriceLists(data: PricingTypes.UpdatePriceListDTO[], sharedContext?: Context): Promise<PricingTypes.PriceListDTO[]>;
63
68
  createPriceListRules(data: PricingTypes.CreatePriceListRuleDTO[], sharedContext?: Context): Promise<PricingTypes.PriceListRuleDTO[]>;
@@ -94,12 +94,51 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
94
94
  async create(data, sharedContext = {}) {
95
95
  const input = Array.isArray(data) ? data : [data];
96
96
  const priceSets = await this.create_(input, sharedContext);
97
+ // TODO: Remove the need to refetch the data here
97
98
  const dbPriceSets = await this.list({ id: priceSets.map((p) => p.id) }, { relations: ["rule_types", "prices", "price_rules"] }, sharedContext);
98
99
  // Ensure the output to be in the same order as the input
99
100
  const results = priceSets.map((priceSet) => {
100
101
  return dbPriceSets.find((p) => p.id === priceSet.id);
101
102
  });
102
- return Array.isArray(data) ? results : results[0];
103
+ return await this.baseRepository_.serialize(Array.isArray(data) ? results : results[0]);
104
+ }
105
+ async upsert(data, sharedContext = {}) {
106
+ const input = Array.isArray(data) ? data : [data];
107
+ const forUpdate = input.filter((priceSet) => !!priceSet.id);
108
+ const forCreate = input.filter((priceSet) => !priceSet.id);
109
+ const operations = [];
110
+ if (forCreate.length) {
111
+ operations.push(this.create_(forCreate, sharedContext));
112
+ }
113
+ if (forUpdate.length) {
114
+ operations.push(this.update_(forUpdate, sharedContext));
115
+ }
116
+ const result = (await (0, utils_1.promiseAll)(operations)).flat();
117
+ return await this.baseRepository_.serialize(Array.isArray(data) ? result : result[0]);
118
+ }
119
+ async update(idOrSelector, data, sharedContext = {}) {
120
+ let normalizedInput = [];
121
+ if ((0, utils_1.isString)(idOrSelector)) {
122
+ // Check if the ID exists, it will throw if not.
123
+ await this.priceSetService_.retrieve(idOrSelector, {}, sharedContext);
124
+ normalizedInput = [{ id: idOrSelector, ...data }];
125
+ }
126
+ else {
127
+ const priceSets = await this.priceSetService_.list(idOrSelector, {}, sharedContext);
128
+ normalizedInput = priceSets.map((priceSet) => ({
129
+ id: priceSet.id,
130
+ ...data,
131
+ }));
132
+ }
133
+ const updateResult = await this.update_(normalizedInput, sharedContext);
134
+ const priceSets = await this.baseRepository_.serialize(updateResult);
135
+ return (0, utils_1.isString)(idOrSelector) ? priceSets[0] : priceSets;
136
+ }
137
+ async update_(data, sharedContext = {}) {
138
+ // TODO: We are not handling rule types, rules, etc. here, add support after data models are finalized
139
+ // TODO: Since money IDs are rarely passed, this will delete all previous data and insert new entries.
140
+ // We can make the `insert` inside upsertWithReplace do an `upsert` instead to avoid this
141
+ return this.priceSetService_.upsertWithReplace(data, { relations: ["prices"] }, sharedContext);
103
142
  }
104
143
  async addRules(data, sharedContext = {}) {
105
144
  const inputs = Array.isArray(data) ? data : [data];
@@ -131,10 +170,6 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
131
170
  await this.priceSetRuleTypeService_.delete(priceSetRuleTypes.map((psrt) => psrt.id), sharedContext);
132
171
  await this.priceService_.delete(priceRules.map((pr) => pr.price.id), sharedContext);
133
172
  }
134
- async update(data, sharedContext = {}) {
135
- const priceSets = await this.priceSetService_.update(data, sharedContext);
136
- return await this.baseRepository_.serialize(priceSets);
137
- }
138
173
  async createPriceLists(data, sharedContext = {}) {
139
174
  const priceLists = await this.createPriceLists_(data, sharedContext);
140
175
  return await this.baseRepository_.serialize(priceLists);
@@ -684,28 +719,42 @@ __decorate([
684
719
  __metadata("design:type", Function),
685
720
  __metadata("design:paramtypes", [Object, Object]),
686
721
  __metadata("design:returntype", Promise)
687
- ], PricingModuleService.prototype, "addRules", null);
722
+ ], PricingModuleService.prototype, "upsert", null);
688
723
  __decorate([
689
724
  (0, utils_1.InjectManager)("baseRepository_"),
690
- __param(1, (0, utils_1.MedusaContext)()),
725
+ __param(2, (0, utils_1.MedusaContext)()),
691
726
  __metadata("design:type", Function),
692
- __metadata("design:paramtypes", [Object, Object]),
727
+ __metadata("design:paramtypes", [Object, Object, Object]),
693
728
  __metadata("design:returntype", Promise)
694
- ], PricingModuleService.prototype, "addPrices", null);
729
+ ], PricingModuleService.prototype, "update", null);
695
730
  __decorate([
696
731
  (0, utils_1.InjectTransactionManager)("baseRepository_"),
697
732
  __param(1, (0, utils_1.MedusaContext)()),
698
733
  __metadata("design:type", Function),
699
734
  __metadata("design:paramtypes", [Array, Object]),
700
735
  __metadata("design:returntype", Promise)
701
- ], PricingModuleService.prototype, "removeRules", null);
736
+ ], PricingModuleService.prototype, "update_", null);
737
+ __decorate([
738
+ (0, utils_1.InjectManager)("baseRepository_"),
739
+ __param(1, (0, utils_1.MedusaContext)()),
740
+ __metadata("design:type", Function),
741
+ __metadata("design:paramtypes", [Object, Object]),
742
+ __metadata("design:returntype", Promise)
743
+ ], PricingModuleService.prototype, "addRules", null);
744
+ __decorate([
745
+ (0, utils_1.InjectManager)("baseRepository_"),
746
+ __param(1, (0, utils_1.MedusaContext)()),
747
+ __metadata("design:type", Function),
748
+ __metadata("design:paramtypes", [Object, Object]),
749
+ __metadata("design:returntype", Promise)
750
+ ], PricingModuleService.prototype, "addPrices", null);
702
751
  __decorate([
703
752
  (0, utils_1.InjectTransactionManager)("baseRepository_"),
704
753
  __param(1, (0, utils_1.MedusaContext)()),
705
754
  __metadata("design:type", Function),
706
755
  __metadata("design:paramtypes", [Array, Object]),
707
756
  __metadata("design:returntype", Promise)
708
- ], PricingModuleService.prototype, "update", null);
757
+ ], PricingModuleService.prototype, "removeRules", null);
709
758
  __decorate([
710
759
  (0, utils_1.InjectManager)("baseRepository_"),
711
760
  __param(1, (0, utils_1.MedusaContext)()),
@@ -1 +1,2 @@
1
1
  export * from "./price-list";
2
+ export * from "./price-set";
@@ -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("./price-list"), exports);
18
+ __exportStar(require("./price-set"), exports);
@@ -0,0 +1,4 @@
1
+ import { UpdatePriceSetDTO } from "@medusajs/types";
2
+ export interface UpdatePriceSetInput extends UpdatePriceSetDTO {
3
+ id: string;
4
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@medusajs/pricing",
3
- "version": "0.1.12-snapshot-20240328221015",
3
+ "version": "0.2.0-snapshot-20240329181714",
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-20240328221015",
43
+ "medusa-test-utils": "1.1.44-snapshot-20240329181714",
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.10",
52
- "@medusajs/types": "1.12.0-snapshot-20240328221015",
53
- "@medusajs/utils": "1.12.0-snapshot-20240328221015",
51
+ "@medusajs/modules-sdk": "1.12.11-snapshot-20240329181714",
52
+ "@medusajs/types": "1.12.0-snapshot-20240329181714",
53
+ "@medusajs/utils": "1.12.0-snapshot-20240329181714",
54
54
  "@mikro-orm/core": "5.9.7",
55
55
  "@mikro-orm/migrations": "5.9.7",
56
56
  "@mikro-orm/postgresql": "5.9.7",